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

484 lines
19 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;
//plan::where(["id_user" => $id_user, "id" => $id])->exists()
2022-10-20 07:34:46 +00:00
class PlanController extends Controller
{
public function update(Request $request, $id)
{
$id_user = auth()->user();
if ($id_user->isCreadorPlan($id) or $id_user->isAdmin()) {
2022-10-20 07:34:46 +00:00
//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
/*$estandar = Estandar::find($request->id_estandar);
2022-10-03 19:14:36 +00:00
if(isset($estandar)){
$plan->estandars()->associate($estandar);
}*/
2022-10-20 07:34:46 +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-------------------------------*/
$causas = $request->causas_raices;
//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);
} else {
2022-10-03 19:14:36 +00:00
return response([
"status" => 0,
"message" => "!No se encontro el plan o no esta autorizado",
2022-10-20 07:34:46 +00:00
], 404);
2022-10-03 19:14:36 +00:00
}
2022-10-20 07:34:46 +00:00
}
2022-10-20 07:34:46 +00:00
// Arreglar el formato de IDs
public function createPlan(Request $request)
{
2022-07-27 05:42:33 +00:00
$request->validate([
2022-10-20 07:34:46 +00:00
"id_estandar" => "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) {
2022-08-25 07:15:43 +00:00
return $query->where('id_estandar', $request->id_estandar);
}),
],
2022-10-20 07:34:46 +00:00
"fuentes" => "present",
"fuentes.*.value" => "required",
"problemas_oportunidades" => "present",
"problemas_oportunidades.*.value" => "required",
"causas_raices" => "present",
"causas_raices.*.value" => "required",
"oportunidad_plan" => "present|max:255",
"acciones_mejoras" => "present",
"acciones_mejoras.*.value" => "required",
"semestre_ejecucion" => "present|max:8", //aaaa-A/B/C/AB
"duracion" => "present|integer",
"recursos" => "present",
"recursos.*.value" => "required",
"metas" => "present",
"metas.*.value" => "required",
"responsables" => "present",
"responsables.*.value" => "required",
"observaciones" => "present",
"observaciones.*.value" => "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;
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;
2022-10-20 07:34:46 +00:00
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
}
2022-10-20 07:34:46 +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
}
2022-10-20 07:34:46 +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
}
2022-10-20 07:34:46 +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
}
2022-10-20 07:34:46 +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
}
2022-10-20 07:34:46 +00:00
foreach ($request->metas as $meta) {
2022-07-27 05:42:33 +00:00
$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();
}
2022-10-20 07:34:46 +00:00
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-10-20 07:34:46 +00:00
foreach ($request->responsables as $responsable) {
2022-08-17 03:18:49 +00:00
$responsable_aux = new Responsables();
2022-10-20 07:34:46 +00:00
$responsable_aux->nombre = $responsable["value"];
$responsable_aux->id_plan = $id_plan;
$responsable_aux->save();
2022-08-09 05:04:46 +00:00
}
2022-07-27 05:42:33 +00:00
return response([
"status" => 1,
"message" => "!Plan de mejora creado exitosamente",
]);
}
2022-10-22 05:10:31 +00:00
public function assignPlan(Request $request)
{
$id_user = auth()->user();
if ($id_user->isAdmin()) {
$request->validate([
'id_estandar' => 'required|integer|exists:estandars,id',
'id_user' => 'required|integer|exists:users,id',
'codigo' => [
'required',
Rule::unique('plans', 'codigo')->where(function ($query) use ($request) {
return $query->where('id_estandar', $request->id_estandar);
}),
],
]);
$plan = new plan();
$plan->id_user = $request->id_user;
$plan->id_estandar = $request->id_estandar;
$plan->codigo = $request->codigo;
$plan->save();
return response([
"status" => 1,
"message" => "!Plan de mejora asignado exitosamente",
], 200);
} else {
return response([
"status" => 0,
"message" => "No tiene permisos para realizar esta acción",
], 403);
}
}
2022-08-09 05:04:46 +00:00
//confirmar los datos nesesarios
2022-10-20 07:34:46 +00:00
public function listPlan()
{
2022-08-17 03:18:49 +00:00
$id_user = auth()->user()->id;
2022-10-20 07:34:46 +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')
->join('estandars', 'plans.id_estandar', '=', 'estandars.id')
->join('users', 'plans.id_user', '=', 'users.id')
->orderBy('plans.id', 'asc')
->get();
2022-08-17 03:18:49 +00:00
2022-10-20 07:34:46 +00:00
foreach ($planAll as $plan) {
$plan->esCreador = ($plan->id_user == $id_user) ? true : false;
2022-08-05 06:25:59 +00:00
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-10-20 07:34:46 +00:00
public function updatePlan(Request $request)
{
2022-08-17 03:18:49 +00:00
$request->validate([
2022-10-20 07:34:46 +00:00
"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",
2022-08-17 03:18:49 +00:00
]);
$id = $request->id;
$id_user = auth()->user();
if ($id_user->isCreadorPlan($id) or $id_user->isAdmin()) {
2022-07-27 05:42:33 +00:00
$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,
]);
2022-10-20 07:34:46 +00:00
} else {
2022-07-27 05:42:33 +00:00
return response([
"status" => 0,
"message" => "!No se encontro el plan o no esta autorizado",
2022-10-20 07:34:46 +00:00
], 404);
2022-07-27 05:42:33 +00:00
}
}
2022-08-05 06:25:59 +00:00
2022-10-20 07:34:46 +00:00
public function deletePlan($id)
{
$id_user = auth()->user();
if ($id_user->isCreadorPlan($id) or $id_user->isAdmin()) {
2022-10-20 07:34:46 +00:00
$plan = plan::where(["id" => $id, "id_user" => $id_user])->first();
$plan->delete();
return response([
"status" => 1,
"message" => "!Plan de mejora eliminado",
]);
} else {
2022-07-27 05:42:33 +00:00
return response([
"status" => 0,
"message" => "!No se encontro el plan de mejora o no esta autorizado",
2022-10-20 07:34:46 +00:00
], 404);
2022-07-27 05:42:33 +00:00
}
}
2022-08-01 01:21:58 +00:00
//faltas completar
2022-10-20 07:34:46 +00:00
public function showPlan($id)
{
2022-10-22 06:30:51 +00:00
2022-10-20 07:34:46 +00:00
if (plan::where("id", $id)->exists()) {
2022-07-27 05:42:33 +00:00
$plan = plan::find($id);
2022-10-20 07:34:46 +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']);
$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,
]);
2022-10-20 07:34:46 +00:00
} else {
return response([
"status" => 0,
"message" => "!No se encontro el plan de mejora",
], 404);
2022-07-27 05:42:33 +00:00
}
2022-10-20 07:34:46 +00:00
}
public function listPlanUser()
{
$id_user = auth()->user()->id;
$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')
->join('estandars', 'plans.id_estandar', '=', 'estandars.id')
->join('users', 'plans.id_user', '=', 'users.id')
->where("plans.id_user", $id_user)
->orderBy('plans.id', 'asc')
->get();
if ($planAll->count() > 0) {
return response([
"status" => 1,
"message" => "!Lista de planes de mejora",
"data" => $planAll,
]);
} else {
return response([
"status" => 0,
"message" => "!No tienes planes de mejora",
"data" => [],
], 404);
2022-07-27 05:42:33 +00:00
}
}
}