Update plan

This commit is contained in:
cgonzalesmo 2022-08-22 15:50:45 -05:00
parent 81fea5524b
commit b76118049f
5 changed files with 192 additions and 23 deletions

View File

@ -15,10 +15,177 @@
use App\Models\ProblemasOportunidades; use App\Models\ProblemasOportunidades;
use App\Models\Recursos; use App\Models\Recursos;
use App\Models\Responsables; use App\Models\Responsables;
use App\Models\Estandar;
class PlanController extends Controller{ class PlanController extends Controller{
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
$estandar = Estandar::find($request->estandar_id);
if(isset($estandar)){
$plan->estandars()->associate($estandar);
}
/*-------------------------------Fuentes------------------------------*/
$fuentes = $request->fuentes;
//Eliminar fuentes que no esten en el Request
$existingsIds = collect($fuentes)->pluck('fuente_id')->filter();
$plan->fuentes()->whereNotIn('id', $existingsIds)->delete();
//Actualizar fuentes de estandar
if(isset($fuentes)){
foreach($fuentes as $fuente){
$plan->fuentes()->updateOrCreate(
[
"id" => $fuente['fuente_id']
],
[
"descripcion" => $fuente['descripcion'],
"id_plan"=> $plan->id
]);
}
}
/*----------------------------Problemas-------------------------------*/
$problemas = $request->problemas;
//Eliminar problemas que no esten en el Request
$existingsIds = collect($problemas)->pluck('problema_id')->filter();
$plan->problemasOportunidade()->whereNotIn('id', $existingsIds)->delete();
//Actualizar problemas de estandar
if(isset($problemas)){
foreach($problemas as $problema){
$plan->problemasOportunidade()->updateOrCreate(
[
"id" => $problema['problema_id']
],
[
"descripcion" => $problema['descripcion'],
"id_plan"=> $plan->id
]);
}
}
/*--------------------------------Causas-------------------------------*/
$causas = $request->causas;
//Eliminar causas que no esten en el Request
$existingsIds = collect($causas)->pluck('causa_id')->filter();
$plan->causasRaices()->whereNotIn('id', $existingsIds)->delete();
//Actualizar causas de estandar
if(isset($causas)){
foreach($causas as $causa){
$plan->causasRaices()->updateOrCreate(
[
"id" => $causa['causa_id']
],
[
"descripcion" => $causa['descripcion'],
"id_plan"=> $plan->id
]);
}
}
/*------------------------------Acciones-------------------------------*/
$acciones = $request->acciones;
//Eliminar acciones que no esten en el Request
$existingsIds = collect($acciones)->pluck('accion_id')->filter();
$plan->accionesMejoras()->whereNotIn('id', $existingsIds)->delete();
//Actualizar acciones de estandar
if(isset($acciones)){
foreach($acciones as $accion){
$plan->accionesMejoras()->updateOrCreate(
[
"id" => $accion['accion_id']
],
[
"descripcion" => $accion['descripcion'],
"id_plan"=> $plan->id
]);
}
}
/*------------------------------Recursos-------------------------------*/
$recursos = $request->recursos;
//Eliminar recursos que no esten en el Request
$existingsIds = collect($recursos)->pluck('meta_id')->filter();
$plan->recursos()->whereNotIn('id', $existingsIds)->delete();
//Actualizar recursos de estandar
if(isset($recursos)){
foreach($recursos as $recurso){
$plan->recursos()->updateOrCreate(
[
"id" => $recurso['recurso_id']
],
[
"descripcion" => $recurso['descripcion'],
"id_plan"=> $plan->id
]);
}
}
/*--------------------------------Metas-------------------------------*/
$metas = $request->metas;
//Eliminar metas que no esten en el Request
$existingsIds = collect($metas)->pluck('meta_id')->filter();
$plan->metas()->whereNotIn('id', $existingsIds)->delete();
//Actualizar metas de estandar
if(isset($metas)){
foreach($metas as $meta){
$plan->metas()->updateOrCreate(
[
"id" => $meta['meta_id']
],
[
"descripcion" => $meta['descripcion'],
"id_plan"=> $plan->id
]);
}
}
/*---------------------------Responsables-------------------------------*/
$responsables = $request->responsables;
//Eliminar responsables que no esten en el Request
$existingsIds = collect($responsables)->pluck('responsable_id')->filter();
$plan->responsables()->whereNotIn('id', $existingsIds)->delete();
//Actualizar responsables de estandar
if(isset($responsables)){
foreach($responsables as $responsable){
$plan->responsables()->updateOrCreate(
[
"id" => $responsable['responsable_id']
],
[
"nombre" => $responsable['nombre'],
"id_plan"=> $plan->id
]);
}
}
/*--------------------------Observaciones-------------------------------*/
$observaciones = $request->observaciones;
//Eliminar observaciones que no esten en el Request
$existingsIds = collect($observaciones)->pluck('observacion_id')->filter();
$plan->observaciones()->whereNotIn('id', $existingsIds)->delete();
//Actualizar observaciones de estandar
if(isset($observaciones)){
foreach($observaciones as $observacion){
$plan->observaciones()->updateOrCreate(
[
"id" => $observacion['observacion_id']
],
[
"descripcion" => $observacion['descripcion'],
"id_plan"=> $plan->id
]);
}
}
return response()->json($plan, 200);
}
// Arreglar el formato de IDs // Arreglar el formato de IDs
public function createPlan(Request $request){ public function createPlan(Request $request){
$request->validate([ $request->validate([
@ -204,7 +371,6 @@ public function updatePlan(Request $request){
} }
} }
public function deletePlan($id){ public function deletePlan($id){
$id_user = auth()->user()->id; $id_user = auth()->user()->id;
if(plan::where(["id"=>$id,"id_user"=>$id_user])->exists()){ if(plan::where(["id"=>$id,"id_user"=>$id_user])->exists()){
@ -222,7 +388,6 @@ public function deletePlan($id){
],404); ],404);
} }
} }
//faltas completar //faltas completar
public function showPlan($id){ public function showPlan($id){
if(plan::where("id",$id)->exists()){ if(plan::where("id",$id)->exists()){

View File

@ -64,5 +64,6 @@ class Kernel extends HttpKernel
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class, 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
//'cors' => \App\Http\Middleware\Cors::class,
]; ];
} }

View File

@ -20,7 +20,7 @@ public function users(){
return $this->belongsTo(User::class,'id_user'); return $this->belongsTo(User::class,'id_user');
} }
public function plans(){ public function plans(){
return $this->hasMany(plan::class,'id'); return $this->hasMany(plan::class,'id_estandar');
} }
} }

View File

@ -31,28 +31,28 @@ public function estandars(){
return $this->belongsTo(Estandar::class,'id_estandar'); return $this->belongsTo(Estandar::class,'id_estandar');
} }
public function fuentes(){ public function fuentes(){
return $this->hasMany(Fuentes::class,'id'); return $this->hasMany(Fuentes::class,'id_plan');
} }
public function metas(){ public function metas(){
return $this->hasMany(Metas::class,'id'); return $this->hasMany(Metas::class,'id_plan');
} }
public function recursos(){ public function recursos(){
return $this->hasMany(Recursos::class,'id'); return $this->hasMany(Recursos::class,'id_plan');
} }
public function observaciones(){ public function observaciones(){
return $this->hasMany(Observaciones::class,'id'); return $this->hasMany(Observaciones::class,'id_plan');
} }
public function problemasOportunidade(){ public function problemasOportunidade(){
return $this->hasMany(ProblemasOportunidades::class,'id'); return $this->hasMany(ProblemasOportunidades::class,'id_plan');
} }
public function accionesMejoras(){ public function accionesMejoras(){
return $this->hasMany(AccionesMejoras::class,'id'); return $this->hasMany(AccionesMejoras::class,'id_plan');
} }
public function causasRaices(){ public function causasRaices(){
return $this->hasMany(causasRaices::class,'id'); return $this->hasMany(causasRaices::class,'id_plan');
} }
public function responsablesplanesmejora(){ public function responsables(){
return $this->hasMany(ResponsablesPlanesMejora::class,'id'); return $this->hasMany(Responsables::class,'id_plan');
} }
// //

View File

@ -36,6 +36,8 @@
//Estandares valores //Estandares valores
Route::get('estandares', [EstandarController::class,'listEstandarValores']); Route::get('estandares', [EstandarController::class,'listEstandarValores']);
Route::middleware("auth:sanctum")->group (function(){ Route::middleware("auth:sanctum")->group (function(){
//rutas auth //rutas auth
Route::get('user-profile', [UserController::class,'userProfile']); Route::get('user-profile', [UserController::class,'userProfile']);
@ -53,7 +55,8 @@
Route::get('plan',[PlanController::class,'listPlan']); Route::get('plan',[PlanController::class,'listPlan']);
Route::get('plan/{id}',[PlanController::class,'showPlan']); Route::get('plan/{id}',[PlanController::class,'showPlan']);
Route::delete('plan/{id}',[PlanController::class,'deletePlan']); Route::delete('plan/{id}',[PlanController::class,'deletePlan']);
Route::put('plan',[PlanController::class,'updatePlan']); Route::put('plan/{id}',[PlanController::class,'update']);
//Route::put('plan',[PlanController::class,'updatePlan']);
//rutas metas //rutas metas
Route::post('meta',[MetasController::class,'create']); Route::post('meta',[MetasController::class,'create']);