|
|
|
|
@ -11,18 +11,12 @@ use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
|
|
|
|
class ReglaAreaProcesoController extends Controller
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Mostrar cursos de un area_proceso con reglas existentes
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function areasProcesos()
|
|
|
|
|
ion areasProcesos()
|
|
|
|
|
{
|
|
|
|
|
$areasProcesos = DB::table('area_proceso as ap')
|
|
|
|
|
->leftJoin('reglas_area_proceso as r', 'ap.id', '=', 'r.area_proceso_id')
|
|
|
|
|
->leftJoin('area_curso as ac', 'ap.area_id', '=', 'ac.area_id') // pivot area_curso
|
|
|
|
|
->leftJoin('cursos as c', 'ac.curso_id', '=', 'c.id') // unimos los cursos reales
|
|
|
|
|
->leftJoin('area_curso as ac', 'ap.area_id', '=', 'ac.area_id')
|
|
|
|
|
->leftJoin('cursos as c', 'ac.curso_id', '=', 'c.id')
|
|
|
|
|
->join('areas as a', 'ap.area_id', '=', 'a.id')
|
|
|
|
|
->join('procesos as p', 'ap.proceso_id', '=', 'p.id')
|
|
|
|
|
->select(
|
|
|
|
|
@ -48,7 +42,7 @@ class ReglaAreaProcesoController extends Controller
|
|
|
|
|
|
|
|
|
|
public function index($areaProcesoId)
|
|
|
|
|
{
|
|
|
|
|
// Obtener el area_proceso y su proceso
|
|
|
|
|
|
|
|
|
|
$areaProceso = DB::table('area_proceso as ap')
|
|
|
|
|
->join('areas as a', 'a.id', '=', 'ap.area_id')
|
|
|
|
|
->join('procesos as p', 'p.id', '=', 'ap.proceso_id')
|
|
|
|
|
@ -61,19 +55,16 @@ public function index($areaProcesoId)
|
|
|
|
|
return response()->json(['error' => 'AreaProceso no encontrado'], 404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Traer todos los cursos del área (pivot area_curso)
|
|
|
|
|
$cursos = DB::table('area_curso as ac')
|
|
|
|
|
->join('cursos as c', 'c.id', '=', 'ac.curso_id')
|
|
|
|
|
->where('ac.area_id', $areaProceso->area_id)
|
|
|
|
|
->select('c.id as curso_id', 'c.nombre as nombre')
|
|
|
|
|
->get();
|
|
|
|
|
|
|
|
|
|
// Traer reglas existentes para este area_proceso
|
|
|
|
|
$reglasExistentes = DB::table('reglas_area_proceso')
|
|
|
|
|
->where('area_proceso_id', $areaProcesoId)
|
|
|
|
|
->get();
|
|
|
|
|
|
|
|
|
|
// Mapear cursos con reglas si existen
|
|
|
|
|
$reglas = $cursos->map(function ($curso) use ($reglasExistentes) {
|
|
|
|
|
$regla = $reglasExistentes->firstWhere('curso_id', $curso->curso_id);
|
|
|
|
|
return [
|
|
|
|
|
@ -109,7 +100,6 @@ public function store(Request $request, $areaProcesoId)
|
|
|
|
|
'ponderacion' => 'nullable|numeric|min:0|max:100',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// 🔹 Cantidad total de preguntas del proceso (vía pivot)
|
|
|
|
|
$areaProceso = DB::table('area_proceso as ap')
|
|
|
|
|
->join('procesos as p', 'ap.proceso_id', '=', 'p.id')
|
|
|
|
|
->where('ap.id', $areaProcesoId)
|
|
|
|
|
@ -124,7 +114,6 @@ public function store(Request $request, $areaProcesoId)
|
|
|
|
|
|
|
|
|
|
$totalPreguntasProceso = $areaProceso->cantidad_pregunta;
|
|
|
|
|
|
|
|
|
|
// 🔹 Total ya asignado (excluyendo este curso si ya existe)
|
|
|
|
|
$totalAsignado = DB::table('reglas_area_proceso')
|
|
|
|
|
->where('area_proceso_id', $areaProcesoId)
|
|
|
|
|
->where('curso_id', '!=', $request->curso_id)
|
|
|
|
|
@ -139,7 +128,6 @@ public function store(Request $request, $areaProcesoId)
|
|
|
|
|
], 422);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 🔹 Insertar o actualizar UNA regla
|
|
|
|
|
DB::table('reglas_area_proceso')->updateOrInsert(
|
|
|
|
|
[
|
|
|
|
|
'area_proceso_id' => $areaProcesoId,
|
|
|
|
|
@ -177,7 +165,6 @@ public function update(Request $request, $reglaId)
|
|
|
|
|
'ponderacion' => 'nullable|numeric|min:0|max:100',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// 🔹 Obtener la regla actual
|
|
|
|
|
$regla = DB::table('reglas_area_proceso')
|
|
|
|
|
->where('id', $reglaId)
|
|
|
|
|
->first();
|
|
|
|
|
@ -188,7 +175,6 @@ public function update(Request $request, $reglaId)
|
|
|
|
|
], 404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 🔹 Obtener cantidad total de preguntas del proceso (vía pivot)
|
|
|
|
|
$areaProceso = DB::table('area_proceso as ap')
|
|
|
|
|
->join('procesos as p', 'ap.proceso_id', '=', 'p.id')
|
|
|
|
|
->where('ap.id', $regla->area_proceso_id)
|
|
|
|
|
@ -203,7 +189,6 @@ public function update(Request $request, $reglaId)
|
|
|
|
|
|
|
|
|
|
$totalPreguntasProceso = $areaProceso->cantidad_pregunta;
|
|
|
|
|
|
|
|
|
|
// 🔹 Total asignado EXCLUYENDO esta regla
|
|
|
|
|
$totalAsignado = DB::table('reglas_area_proceso')
|
|
|
|
|
->where('area_proceso_id', $regla->area_proceso_id)
|
|
|
|
|
->where('id', '!=', $reglaId)
|
|
|
|
|
@ -218,7 +203,6 @@ public function update(Request $request, $reglaId)
|
|
|
|
|
], 422);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 🔹 Actualizar la regla
|
|
|
|
|
DB::table('reglas_area_proceso')
|
|
|
|
|
->where('id', $reglaId)
|
|
|
|
|
->update([
|
|
|
|
|
@ -241,9 +225,7 @@ public function update(Request $request, $reglaId)
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Eliminar una regla
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public function destroy($reglaId)
|
|
|
|
|
{
|
|
|
|
|
$regla = ReglaAreaProceso::findOrFail($reglaId);
|
|
|
|
|
@ -272,7 +254,6 @@ public function storeMultiple(Request $request, $areaProcesoId)
|
|
|
|
|
'reglas.*.ponderacion' => 'nullable|numeric|min:0|max:100',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// Obtener la cantidad total de preguntas del proceso a través del pivot
|
|
|
|
|
$areaProceso = DB::table('area_proceso as ap')
|
|
|
|
|
->join('procesos as p', 'ap.proceso_id', '=', 'p.id')
|
|
|
|
|
->where('ap.id', $areaProcesoId)
|
|
|
|
|
@ -281,7 +262,6 @@ public function storeMultiple(Request $request, $areaProcesoId)
|
|
|
|
|
|
|
|
|
|
$totalPreguntasProceso = $areaProceso->cantidad_pregunta ?? 0;
|
|
|
|
|
|
|
|
|
|
// Validar total de preguntas asignadas
|
|
|
|
|
$totalNuevo = collect($request->reglas)->sum('cantidad_preguntas');
|
|
|
|
|
if ($totalNuevo > $totalPreguntasProceso) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
@ -289,10 +269,8 @@ public function storeMultiple(Request $request, $areaProcesoId)
|
|
|
|
|
], 422);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Eliminar reglas existentes directamente en la tabla pivot
|
|
|
|
|
DB::table('reglas_area_proceso')->where('area_proceso_id', $areaProcesoId)->delete();
|
|
|
|
|
|
|
|
|
|
// Insertar las nuevas reglas
|
|
|
|
|
$insertData = collect($request->reglas)->map(function ($r) use ($areaProcesoId) {
|
|
|
|
|
return [
|
|
|
|
|
'area_proceso_id' => $areaProcesoId,
|
|
|
|
|
|