2022-07-27 05:42:33 +00:00
< ? php
namespace App\Http\Controllers\Api ;
use App\Http\Controllers\Controller ;
2022-08-18 06:41:10 +00:00
use Illuminate\Validation\Rule ;
2022-07-27 05:42:33 +00:00
use Illuminate\Http\Request ;
use App\Models\plan ;
use App\Models\AccionesMejoras ;
use App\Models\CausasRaices ;
use App\Models\Evidencias ;
use App\Models\Fuentes ;
use App\Models\Metas ;
use App\Models\Observaciones ;
use App\Models\ProblemasOportunidades ;
use App\Models\Recursos ;
use App\Models\Responsables ;
2022-08-22 20:50:45 +00:00
2022-08-24 02:24:20 +00:00
class PlanController extends Controller {
2022-08-25 07:15:08 +00:00
public function update ( Request $request , $id ){
//Actualizamos los atributos propios
$plan = plan :: find ( $id );
$plan -> update ([
//"codigo" => $request->codigo,
" nombre " => $request -> nombre ,
" oportunidad_plan " => $request -> oportunidad_plan ,
" semestre_ejecucion " => $request -> semestre_ejecucion ,
" duracion " => $request -> duracion ,
" estado " => $request -> estado ,
" evaluacion_eficacia " => $request -> evaluacion_eficacia ,
" avance " => $request -> avance ,
]);
//Actualizar estandar
2022-08-25 08:31:47 +00:00
/* $estandar = Estandar :: find ( $request -> id_estandar );
2022-08-25 07:15:08 +00:00
if ( isset ( $estandar )){
$plan -> estandars () -> associate ( $estandar );
2022-08-25 08:31:47 +00:00
} */
2022-08-25 07:15:08 +00:00
/*-------------------------------Fuentes------------------------------*/
$fuentes = $request -> fuentes ;
//Eliminar fuentes que no esten en el Request
$existingsIds = collect ( $fuentes ) -> pluck ( 'id' ) -> filter ();
$plan -> fuentes () -> whereNotIn ( 'id' , $existingsIds ) -> delete ();
//Actualizar fuentes de estandar
if ( isset ( $fuentes )){
foreach ( $fuentes as $fuente ){
$plan -> fuentes () -> updateOrCreate (
[
" id " => $fuente [ 'id' ]
],
[
" descripcion " => $fuente [ 'value' ],
" id_plan " => $plan -> id
]);
}
}
/*----------------------------Problemas-------------------------------*/
$problemas = $request -> problemas ;
//Eliminar problemas que no esten en el Request
$existingsIds = collect ( $problemas ) -> pluck ( 'id' ) -> filter ();
$plan -> problemasOportunidade () -> whereNotIn ( 'id' , $existingsIds ) -> delete ();
//Actualizar problemas de estandar
if ( isset ( $problemas )){
foreach ( $problemas as $problema ){
$plan -> problemasOportunidade () -> updateOrCreate (
[
" id " => $problema [ 'id' ]
],
[
" descripcion " => $problema [ 'value' ],
" id_plan " => $plan -> id
]);
}
}
/*--------------------------------Causas-------------------------------*/
2022-08-25 10:09:42 +00:00
$causas = $request -> causas_raices ;
2022-08-25 07:15:08 +00:00
//Eliminar causas que no esten en el Request
$existingsIds = collect ( $causas ) -> pluck ( 'id' ) -> filter ();
$plan -> causasRaices () -> whereNotIn ( 'id' , $existingsIds ) -> delete ();
//Actualizar causas de estandar
if ( isset ( $causas )){
foreach ( $causas as $causa ){
$plan -> causasRaices () -> updateOrCreate (
[
" id " => $causa [ 'id' ]
],
[
" descripcion " => $causa [ 'value' ],
" id_plan " => $plan -> id
]);
}
}
/*------------------------------Acciones-------------------------------*/
$acciones = $request -> acciones ;
//Eliminar acciones que no esten en el Request
$existingsIds = collect ( $acciones ) -> pluck ( 'id' ) -> filter ();
$plan -> accionesMejoras () -> whereNotIn ( 'id' , $existingsIds ) -> delete ();
//Actualizar acciones de estandar
if ( isset ( $acciones )){
foreach ( $acciones as $accion ){
$plan -> accionesMejoras () -> updateOrCreate (
[
" id " => $accion [ 'id' ]
],
[
" descripcion " => $accion [ 'value' ],
" id_plan " => $plan -> id
]);
}
}
/*------------------------------Recursos-------------------------------*/
$recursos = $request -> recursos ;
//Eliminar recursos que no esten en el Request
$existingsIds = collect ( $recursos ) -> pluck ( 'id' ) -> filter ();
$plan -> recursos () -> whereNotIn ( 'id' , $existingsIds ) -> delete ();
//Actualizar recursos de estandar
if ( isset ( $recursos )){
foreach ( $recursos as $recurso ){
$plan -> recursos () -> updateOrCreate (
[
" id " => $recurso [ 'id' ]
],
[
" descripcion " => $recurso [ 'value' ],
" id_plan " => $plan -> id
]);
}
}
/*--------------------------------Metas-------------------------------*/
$metas = $request -> metas ;
//Eliminar metas que no esten en el Request
$existingsIds = collect ( $metas ) -> pluck ( 'id' ) -> filter ();
$plan -> metas () -> whereNotIn ( 'id' , $existingsIds ) -> delete ();
//Actualizar metas de estandar
if ( isset ( $metas )){
foreach ( $metas as $meta ){
$plan -> metas () -> updateOrCreate (
[
" id " => $meta [ 'id' ]
],
[
" descripcion " => $meta [ 'value' ],
" id_plan " => $plan -> id
]);
}
}
/*---------------------------Responsables-------------------------------*/
$responsables = $request -> responsables ;
//Eliminar responsables que no esten en el Request
$existingsIds = collect ( $responsables ) -> pluck ( 'id' ) -> filter ();
$plan -> responsables () -> whereNotIn ( 'id' , $existingsIds ) -> delete ();
//Actualizar responsables de estandar
if ( isset ( $responsables )){
foreach ( $responsables as $responsable ){
$plan -> responsables () -> updateOrCreate (
[
" id " => $responsable [ 'id' ]
],
[
" nombre " => $responsable [ 'value' ],
" id_plan " => $plan -> id
]);
}
}
/*--------------------------Observaciones-------------------------------*/
$observaciones = $request -> observaciones ;
//Eliminar observaciones que no esten en el Request
$existingsIds = collect ( $observaciones ) -> pluck ( 'id' ) -> filter ();
$plan -> observaciones () -> whereNotIn ( 'id' , $existingsIds ) -> delete ();
//Actualizar observaciones de estandar
if ( isset ( $observaciones )){
foreach ( $observaciones as $observacion ){
$plan -> observaciones () -> updateOrCreate (
[
" id " => $observacion [ 'id' ]
],
[
" descripcion " => $observacion [ 'value' ],
" id_plan " => $plan -> id
]);
}
}
return response () -> json ( $plan , 200 );
}
2022-08-22 20:50:45 +00:00
2022-07-27 05:42:33 +00:00
// Arreglar el formato de IDs
public function createPlan ( Request $request ){
$request -> validate ([
2022-08-25 07:15:43 +00:00
" id_estandar " => " required|integer " ,
2022-08-18 06:41:10 +00:00
" nombre " => " present|max:255 " ,
2022-08-25 07:15:08 +00:00
/* "codigo"=> "required|unique_with:plans,id_estandar|max:11", */
2022-08-18 06:41:10 +00:00
'codigo' => [
2022-08-25 07:15:08 +00:00
'required' ,
2022-08-18 06:41:10 +00:00
Rule :: unique ( 'plans' , 'codigo' ) -> where ( function ( $query ) use ( $request ) {
2022-08-25 07:15:43 +00:00
return $query -> where ( 'id_estandar' , $request -> id_estandar );
2022-08-18 06:41:10 +00:00
}),
],
" fuentes " => " present " ,
2022-08-25 09:33:19 +00:00
" fuentes.*.value " => " required " ,
2022-08-18 06:41:10 +00:00
" problemas_oportunidades " => " present " ,
2022-08-25 09:33:19 +00:00
" problemas_oportunidades.*.value " => " required " ,
2022-08-18 06:41:10 +00:00
" causas_raices " => " present " ,
2022-08-25 09:33:19 +00:00
" causas_raices.*.value " => " required " ,
2022-08-18 06:41:10 +00:00
" oportunidad_plan " => " present|max:255 " ,
" acciones_mejoras " => " present " ,
2022-08-25 09:33:19 +00:00
" acciones_mejoras.*.value " => " required " ,
2022-08-18 06:41:10 +00:00
" semestre_ejecucion " => " present|max:8 " , //aaaa-A/B/C/AB
" duracion " => " present|integer " ,
" recursos " => " present " ,
2022-08-25 09:33:19 +00:00
" recursos.*.value " => " required " ,
2022-08-18 06:41:10 +00:00
" metas " => " present " ,
2022-08-25 09:33:19 +00:00
" metas.*.value " => " required " ,
2022-08-18 06:41:10 +00:00
" responsables " => " present " ,
2022-08-25 09:33:19 +00:00
" responsables.*.value " => " required " ,
2022-08-18 06:41:10 +00:00
" observaciones " => " present " ,
2022-08-25 09:33:19 +00:00
" observaciones.*.value " => " required " ,
2022-08-18 06:41:10 +00:00
" estado " => " present|max:30 " ,
" evaluacion_eficacia " => " present|boolean " ,
" avance " => " present|integer "
2022-07-27 05:42:33 +00:00
]);
$id_user = auth () -> user () -> id ;
$plan = new plan ();
2022-08-17 03:18:49 +00:00
2022-07-27 05:42:33 +00:00
$plan -> id_user = $id_user ;
2022-08-25 07:15:43 +00:00
$plan -> id_estandar = $request -> id_estandar ; //actualizar a id_estandar
2022-07-27 05:42:33 +00:00
$plan -> nombre = $request -> nombre ;
$plan -> codigo = $request -> codigo ;
2022-08-09 05:04:46 +00:00
2022-07-27 05:42:33 +00:00
$plan -> oportunidad_plan = $request -> oportunidad_plan ;
$plan -> semestre_ejecucion = $request -> semestre_ejecucion ;
$plan -> duracion = $request -> duracion ;
$plan -> estado = $request -> estado ;
$plan -> evaluacion_eficacia = $request -> evaluacion_eficacia ;
$plan -> avance = $request -> avance ;
$plan -> save ();
2022-08-17 03:18:49 +00:00
2022-07-27 05:42:33 +00:00
$id_plan = $plan -> id ;
foreach ( $request -> fuentes as $fuente ){
2022-08-09 05:04:46 +00:00
$fuente_aux = new Fuentes ();
2022-08-25 07:15:43 +00:00
$fuente_aux -> descripcion = $fuente [ " value " ];
2022-08-09 05:04:46 +00:00
$fuente_aux -> id_plan = $id_plan ;
$fuente_aux -> save ();
2022-07-27 05:42:33 +00:00
}
foreach ( $request -> problemas_oportunidades as $problema ){
2022-08-09 05:04:46 +00:00
$problema_oportunidad_aux = new ProblemasOportunidades ();
2022-08-25 07:15:43 +00:00
$problema_oportunidad_aux -> descripcion = $problema [ " value " ];
2022-08-09 05:04:46 +00:00
$problema_oportunidad_aux -> id_plan = $id_plan ;
$problema_oportunidad_aux -> save ();
2022-07-27 05:42:33 +00:00
}
foreach ( $request -> causas_raices as $causa ){
2022-08-09 05:04:46 +00:00
$causa_raiz_aux = new CausasRaices ();
2022-08-25 07:15:43 +00:00
$causa_raiz_aux -> descripcion = $causa [ " value " ];
2022-08-09 05:04:46 +00:00
$causa_raiz_aux -> id_plan = $id_plan ;
$causa_raiz_aux -> save ();
2022-07-27 05:42:33 +00:00
}
foreach ( $request -> acciones_mejoras as $accion ){
2022-08-09 05:04:46 +00:00
$accion_mejora_aux = new AccionesMejoras ();
2022-08-25 07:15:43 +00:00
$accion_mejora_aux -> descripcion = $accion [ " value " ];
2022-08-09 05:04:46 +00:00
$accion_mejora_aux -> id_plan = $id_plan ;
$accion_mejora_aux -> save ();
2022-07-27 05:42:33 +00:00
}
foreach ( $request -> recursos as $recurso ){
2022-08-09 05:04:46 +00:00
$recurso_aux = new Recursos ();
2022-08-25 07:15:43 +00:00
$recurso_aux -> descripcion = $recurso [ " value " ];
2022-08-09 05:04:46 +00:00
$recurso_aux -> id_plan = $id_plan ;
$recurso_aux -> save ();
2022-07-27 05:42:33 +00:00
}
foreach ( $request -> metas as $meta ){
$meta_aux = new Metas ();
2022-08-25 07:15:43 +00:00
$meta_aux -> descripcion = $meta [ " value " ];
2022-07-27 05:42:33 +00:00
$meta_aux -> id_plan = $id_plan ;
$meta_aux -> save ();
}
foreach ( $request -> observaciones as $observacion ){
2022-08-09 05:04:46 +00:00
$observacion_aux = new Observaciones ();
2022-08-25 07:15:43 +00:00
$observacion_aux -> descripcion = $observacion [ " value " ];
2022-08-09 05:04:46 +00:00
$observacion_aux -> id_plan = $id_plan ;
$observacion_aux -> save ();
2022-07-27 05:42:33 +00:00
}
2022-08-09 05:04:46 +00:00
foreach ( $request -> responsables as $responsable ){
2022-08-17 03:18:49 +00:00
$responsable_aux = new Responsables ();
2022-08-25 07:15:43 +00:00
$responsable_aux -> nombre = $responsable [ " value " ];
2022-08-09 05:04:46 +00:00
$responsable_aux -> id_plan = $id_plan ;
$responsable_aux -> save ();
}
2022-08-25 07:15:08 +00:00
2022-07-27 05:42:33 +00:00
return response ([
" status " => 1 ,
" message " => " !Plan de mejora creado exitosamente " ,
]);
}
2022-08-09 05:04:46 +00:00
2022-07-27 05:42:33 +00:00
public function listPlan (){
2022-08-17 03:18:49 +00:00
$id_user = auth () -> user () -> id ;
2022-08-05 06:25:59 +00:00
$planAll = plan :: select ( 'plans.id' , 'plans.nombre' , 'plans.codigo' , 'plans.avance' , 'plans.estado' , 'plans.id_user' , 'estandars.name as estandar_name' , 'users.name as user_name' )
2022-07-27 05:42:33 +00:00
-> join ( 'estandars' , 'plans.id_estandar' , '=' , 'estandars.id' )
-> join ( 'users' , 'plans.id_user' , '=' , 'users.id' )
2022-08-13 02:27:34 +00:00
-> orderBy ( 'plans.id' , 'asc' )
2022-07-27 05:42:33 +00:00
-> get ();
2022-08-17 03:18:49 +00:00
foreach ( $planAll as $plan ){
2022-08-05 06:25:59 +00:00
$plan -> esCreador = ( $plan -> id_user == $id_user ) ? true : false ;
unset ( $plan -> id_user );
}
2022-07-27 05:42:33 +00:00
return response ([
" status " => 1 ,
" message " => " !Lista de planes de mejora " ,
" data " => $planAll ,
]);
}
2022-08-17 03:18:49 +00:00
public function updatePlan ( Request $request ){
$request -> validate ([
" id " => " required|integer " ,
" nombre " => " required|max:255 " ,
" oportunidad_plan " => " required|max:255 " ,
" semestre_ejecucion " => " required|max:8 " ,
" duracion " => " required|integer " ,
" estado " => " required|max:30 " ,
" evaluacion_eficacia " => " required|boolean " ,
" avance " => " required|integer " ,
]);
$id = $request -> id ;
2022-07-27 05:42:33 +00:00
$id_user = auth () -> user () -> id ;
if ( plan :: where ([ " id_user " => $id_user , " id " => $id ]) -> exists ()){
$plan = plan :: find ( $id );
2022-08-05 06:25:59 +00:00
$plan -> nombre = $request -> nombre ;
$plan -> oportunidad_plan = $request -> oportunidad_plan ;
$plan -> semestre_ejecucion = $request -> semestre_ejecucion ;
$plan -> duracion = $request -> duracion ;
$plan -> estado = $request -> estado ;
$plan -> evaluacion_eficacia = $request -> evaluacion_eficacia ;
$plan -> avance = $request -> avance ;
2022-07-27 05:42:33 +00:00
$plan -> save ();
return response ([
" status " => 1 ,
" message " => " !Plan de mejora actualizado " ,
" data " => $plan ,
]);
}
else {
return response ([
" status " => 0 ,
" message " => " !No se encontro el plan o no esta autorizado " ,
], 404 );
}
}
2022-08-05 06:25:59 +00:00
2022-08-24 02:24:20 +00:00
2022-07-27 05:42:33 +00:00
public function deletePlan ( $id ){
$id_user = auth () -> user () -> id ;
if ( plan :: where ([ " id " => $id , " id_user " => $id_user ]) -> exists ()){
$plan = plan :: where ([ " id " => $id , " id_user " => $id_user ]) -> first ();
$plan -> delete ();
return response ([
" status " => 1 ,
" message " => " !Plan de mejora eliminado " ,
]);
}
else {
return response ([
" status " => 0 ,
" message " => " !No se encontro el plan de mejora o no esta autorizado " ,
], 404 );
}
}
2022-08-24 02:24:20 +00:00
2022-08-01 01:21:58 +00:00
//faltas completar
2022-07-27 05:42:33 +00:00
public function showPlan ( $id ){
if ( plan :: where ( " id " , $id ) -> exists ()){
$plan = plan :: find ( $id );
2022-08-25 10:09:42 +00:00
$plan -> fuentes = Fuentes :: where ( " id_plan " , $id ) -> get ([ 'id' , 'descripcion as value' ]);
$plan -> problemas_oportunidades = ProblemasOportunidades :: where ( " id_plan " , $id ) -> get ([ 'id' , 'descripcion as value' ]);
$plan -> causas_raices = CausasRaices :: where ( " id_plan " , $id ) -> get ([ 'id' , 'descripcion as value' ]);
$plan -> acciones_mejoras = AccionesMejoras :: where ( " id_plan " , $id ) -> get ([ 'id' , 'descripcion as value' ]);
$plan -> recursos = Recursos :: where ( " id_plan " , $id ) -> get ([ 'id' , 'descripcion as value' ]);
$plan -> metas = Metas :: where ( " id_plan " , $id ) -> get ([ 'id' , 'descripcion as value' ]);
$plan -> observaciones = Observaciones :: where ( " id_plan " , $id ) -> get ([ 'id' , 'descripcion as value' ]);
$plan -> responsables = Responsables :: where ( " id_plan " , $id ) -> get ([ 'id' , 'nombre as value' ]);
2022-08-24 02:24:20 +00:00
$plan -> evidencias = Evidencias :: where ( " id_plan " , $id ) -> get ();
2022-07-27 05:42:33 +00:00
return response ([
" status " => 1 ,
2022-08-02 04:44:21 +00:00
" message " => " !Plan de mejora encontrado " ,
2022-07-27 05:42:33 +00:00
" data " => $plan ,
]);
}
else {
return response ([
" status " => 0 ,
" message " => " !No se encontro el plan de mejora " ,
], 404 );
}
}
}