unsa-pis-be/app/Http/Controllers/Api/PlanController.php

246 lines
8.9 KiB
PHP
Raw Normal View History

2022-07-27 05:42:33 +00:00
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
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
class PlanController extends Controller{
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([
"estandar_id"=> "required|integer",
"nombre"=>"present|max:255",
/* "codigo"=> "required|unique_with:plans,id_estandar|max:11", */
'codigo' => [
'required',
Rule::unique('plans', 'codigo')->where(function ($query) use ($request) {
return $query->where('id_estandar', $request->estandar_id);
}),
],
"fuentes"=>"present",
"fuentes.*.descripcion"=> "required",
"problemas_oportunidades"=>"present",
"problemas_oportunidades.*.descripcion"=> "required",
"causas_raices"=>"present",
"causas_raices.*.descripcion"=> "required",
"oportunidad_plan"=>"present|max:255",
"acciones_mejoras"=>"present",
"acciones_mejoras.*.descripcion"=> "required",
"semestre_ejecucion"=>"present|max:8", //aaaa-A/B/C/AB
"duracion"=> "present|integer",
"recursos"=>"present",
"recursos.*.descripcion"=> "required",
"metas"=>"present",
"metas.*.descripcion"=> "required",
"responsables"=>"present",
"responsables.*.nombre"=> "required",
"observaciones"=>"present",
"observaciones.*.descripcion"=> "required",
"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;
$plan->id_estandar = $request->estandar_id; //actualizar a estandar_id
$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();
$fuente_aux->descripcion = $fuente["descripcion"];
$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();
$problema_oportunidad_aux->descripcion = $problema["descripcion"];
$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();
$causa_raiz_aux->descripcion = $causa["descripcion"];
$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();
$accion_mejora_aux->descripcion = $accion["descripcion"];
$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();
$recurso_aux->descripcion = $recurso["descripcion"];
$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();
$meta_aux->descripcion = $meta["descripcion"];
$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();
$observacion_aux->descripcion = $observacion["descripcion"];
$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-09 05:04:46 +00:00
$responsable_aux ->nombre = $responsable["nombre"];
$responsable_aux ->id_plan = $id_plan;
$responsable_aux ->save();
}
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')
->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-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-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-02 04:44:21 +00:00
$plan->fuentes = Fuentes::where("id_plan",$id)->get();
$plan->problemas_oportunidades = ProblemasOportunidades::where("id_plan",$id)->get();
$plan->causas_raices = CausasRaices::where("id_plan",$id)->get();
$plan->acciones_mejoras = AccionesMejoras::where("id_plan",$id)->get();
$plan->recursos = Recursos::where("id_plan",$id)->get();
$plan->metas = Metas::where("id_plan",$id)->get();
$plan->observaciones = Observaciones::where("id_plan",$id)->get();
$plan->evidencias_planes_mejoras = Evidencias::where("id_plan",$id)->get();
$plan->responsables = Responsables::where("id_plan",$id)->get();
$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);
}
}
}