소스 검색

modificacion de validacion para enviar un mensaje amigable cuando la visita tiene estado VA pero no tiene operarios asignados

EmilianoOrtiz 6 일 전
부모
커밋
b66767f03d
1개의 변경된 파일46개의 추가작업 그리고 15개의 파일을 삭제
  1. 46 15
      sistema-mantenimiento-back/app/Http/Controllers/PreventiveMaintenanceController.php

+ 46 - 15
sistema-mantenimiento-back/app/Http/Controllers/PreventiveMaintenanceController.php

@@ -2633,7 +2633,6 @@ class PreventiveMaintenanceController extends Controller
     // Visitas técnicas no programadas (Preventivas)
     public function updateVisitStatus(Request $request)
     {
-        Log::info($request->all());
         DB::enableQueryLog();
 
         // RVTN_ESTA enum: 'VA','EP','CP','CE','P','C','R','A','F'
@@ -2644,7 +2643,7 @@ class PreventiveMaintenanceController extends Controller
             'id_user' => 'required|string',
             'linea' => 'required|integer',
             'id_visit' => 'required|string',
-            'comments' => 'required|string|min:35',
+            'comments' => 'required|string|min:15',
             'status' => 'required|string|in:VA,EP,CP,CE,P,C,R,A,F'
         ]);
 
@@ -2919,7 +2918,7 @@ class PreventiveMaintenanceController extends Controller
             'id_user' => 'required|string',
             'linea' => 'required|integer',
             'id_visit' => 'required|string',
-            'comment' => 'required|string|min:35'
+            'comment' => 'required|string|min:15'
         ]);
 
         if ($validator->fails()) {
@@ -3320,27 +3319,30 @@ class PreventiveMaintenanceController extends Controller
 
         $statusHistoryArr = json_decode($visit->RVTN_HIES, true);
         if (!is_array($statusHistoryArr)) {
-            return $this->responseController->makeResponse(true, 'No se encontró un historial de estados válido para la visita.', [], 500);
+            $statusHistoryArr = [];
         }
 
+        // Buscar objeto VA en el historial
+        // Si no existe pero RVTN_ESTA == 'VA', la visita está validada pero aún no tiene operarios asignados
         $validatedHistoryFilt = array_filter($statusHistoryArr, function ($item) {
             return array_key_exists('ESTADO', $item) && $item['ESTADO'] == 'VA';
         });
 
-        if (empty($validatedHistoryFilt)) {
-            return $this->responseController->makeResponse(true, 'La visita no cuenta con un estado validado para iniciar.', [], 401);
-        }
-
-        $validatedStatus = end($validatedHistoryFilt);
         $audience = [];
 
-        if (array_key_exists('ATENCION', $validatedStatus) && is_array($validatedStatus['ATENCION'])) {
-            foreach ($validatedStatus['ATENCION'] as $item) {
-                if (($item['RESPUESTA'] ?? '') === 'A') {
-                    $audience[] = $item['ID'];
+        // Si existe el objeto VA en el historial, obtener los operarios aceptados
+        if (!empty($validatedHistoryFilt)) {
+            $validatedStatus = end($validatedHistoryFilt);
+            if (array_key_exists('ATENCION', $validatedStatus) && is_array($validatedStatus['ATENCION'])) {
+                foreach ($validatedStatus['ATENCION'] as $item) {
+                    if (($item['RESPUESTA'] ?? '') === 'A') {
+                        $audience[] = $item['ID'];
+                    }
                 }
             }
         }
+        // Si no existe el objeto VA en el historial pero RVTN_ESTA == 'VA', 
+        // la audiencia quedará vacía (no hay operarios asignados aún)
 
         $now = $this->functionsController->now();
         $nowStr = $now->toDateTimeString();
@@ -5971,13 +5973,42 @@ class PreventiveMaintenanceController extends Controller
             return $this->responseController->makeResponse(true, 'La visita relacionada no está registrada.', [], 404);
         }
 
+        // Validar que la visita esté en estado VA (validado)
+        // Puede estar en estado VA en RVTN_ESTA sin tener aún el objeto VA en RVTN_HIES
+        // (cuando se crea automáticamente pero aún no se han asignado operarios)
+        $visitStates = [
+            'VA' => 'Validada',
+            'EP' => 'En progreso',
+            'CP' => 'Cerrada pendiente',
+            'CE' => 'Cerrada',
+            'PE' => 'Pendiente de validación',
+            'P' => 'Pendiente',
+            'C' => 'Cancelada',
+            'R' => 'Rechazada',
+            'A' => 'Aprobada',
+            'F' => 'Finalizada',
+        ];
+
+        if ($visit->RVTN_ESTA !== 'VA') {
+            $statusKey = $visit->RVTN_ESTA;
+            $statusName = array_key_exists($statusKey, $visitStates) ? $visitStates[$statusKey] : $statusKey;
+            return $this->responseController->makeResponse(true, "La visita relacionada no está en el estado de validación (estado actual: $statusName)", [], 404);
+        }
+
         $statusHistory = json_decode($visit->RVTN_HIES, true);
+        if (!is_array($statusHistory)) {
+            $statusHistory = [];
+        }
+
         $validatedHistoryFilt = array_filter($statusHistory, function ($v, $k) {
-            return $v['ESTADO'] == 'VA';
+            return isset($v['ESTADO']) && $v['ESTADO'] == 'VA';
         }, ARRAY_FILTER_USE_BOTH);
 
+        // Si no hay objeto VA en el historial pero el estado es VA, significa que aún no se han asignado operarios
         if (empty($validatedHistoryFilt)) {
-            return $this->responseController->makeResponse(true, 'La visita relacionada no está en el estado de validación', [], 404);
+            $statusKey = $visit->RVTN_ESTA;
+            $statusName = array_key_exists($statusKey, $visitStates) ? $visitStates[$statusKey] : $statusKey;
+            return $this->responseController->makeResponse(true, "La visita tiene estado $statusName pero aún no ha asignado operarios.", [], 404);
         }
 
         $validatedHistory = end($validatedHistoryFilt);