Forráskód Böngészése

envio de atrbutos boolean para cuando no hay herramientas y/o refacciones asignadas a la orden

EmilianoOrtiz 20 órája
szülő
commit
d107b971dc

+ 121 - 86
sistema-mantenimiento-back/app/Http/Controllers/CorrectiveMaintenanceController.php

@@ -356,7 +356,7 @@ class CorrectiveMaintenanceController extends Controller
                 ['MEST_CODE', '=', $stationCode],
                 ['MEST_NULI', '=', $form['linea']]
             ])->first();
-            
+
             if (!is_null($station) && !is_null($station->MEST_FNCO)) {
                 $iconId = $this->encryptionController->encrypt($station->MEST_FNCO);
             }
@@ -375,7 +375,7 @@ class CorrectiveMaintenanceController extends Controller
             'FUNCION' => 'openModule',
             'PARAMETROS' => json_encode([$this->encryptionController->encrypt('GMCO/ORTR/GEOP')])
         ]];
-        
+
         if (!is_null($iconId)) {
             $actions[] = ['ICON_ID' => $iconId];
         }
@@ -697,7 +697,7 @@ class CorrectiveMaintenanceController extends Controller
         }
 
         $order = DB::table('S002V01TOTCO')
-            ->select(['OTCO_HIES', 'OTCO_RHUT', 'OTCO_ANCO'])
+            ->select(['OTCO_HIES', 'OTCO_RHUT', 'OTCO_ANCO', 'OTCO_ESOR'])
             ->where([
                 ['OTCO_IDOT', '=', $idOrder],
                 ['OTCO_NULI', '=', $line]
@@ -707,9 +707,29 @@ class CorrectiveMaintenanceController extends Controller
             return $this->responseController->makeResponse(true, 'La orden no está registrada.', [], 404);
         }
 
+        if ($order->OTCO_ESOR !== 'CP') {
+            return $this->responseController->makeResponse(true, 'Solo se puede consultar análisis de costos en órdenes con estado "Cerrado pendiente".', [], 400);
+        }
+
         $historial = json_decode($order->OTCO_HIES, true);
         $recursos = json_decode($order->OTCO_RHUT, true);
 
+        // Verificar si tiene herramientas y refacciones reales
+        $tieneHerramientas = false;
+        $tieneRefacciones = false;
+
+        if (!is_null($recursos) && !empty($recursos)) {
+            foreach ($recursos as $recurso) {
+                if ($recurso['ID'] !== 'SH' && $recurso['REQ'] !== '-') {
+                    if ($recurso['TYPE'] == 'T') {
+                        $tieneHerramientas = true;
+                    } else {
+                        $tieneRefacciones = true;
+                    }
+                }
+            }
+        }
+
         // Buscar usuarios participantes en estado VA
         $usuariosParticipantes = [];
         foreach ($historial as $estado) {
@@ -742,60 +762,66 @@ class CorrectiveMaintenanceController extends Controller
         // Procesar recursos
         $herramientas = [];
         $refacciones = [];
+        $subtotalHerramientasArray = [];
+        $subtotalRefaccionesArray = [];
         $subtotalesHerramientas = [];
         $subtotalesRefacciones = [];
 
-        foreach ($recursos as $recurso) {
-            if ($recurso['ID'] != 'SH') {
-                $stock = DB::table('S002V01TINST')
-                    ->select(['INST_IDIS', 'INST_MODE', 'INAR_COMO', 'INAR_PREC', 'CAMO_DESC'])
-                    ->join('S002V01TINAR', 'INST_ARTI', '=', 'INAR_IDIN')
-                    ->join('S002V01TCAMO', 'INAR_COMO', '=', 'CAMO_COMO')
-                    ->where([
-                        ['INST_IDIS', '=', $recurso['ID']],
-                        ['INST_NULI', '=', $line]
-                    ])->first();
-
-                if ($stock) {
-                    $subtotal = $stock->INAR_PREC * $recurso['REQ'];
-                    $moneda = $stock->INAR_COMO;
-
-                    $item = [
-                        'ID' => $recurso['ID'],
-                        'NOMBRE' => $stock->INST_MODE,
-                        'CANTIDAD' => $recurso['REQ'],
-                        'PRECIO_UNITARIO' => $stock->INAR_PREC,
-                        'SUBTOTAL' => $subtotal,
-                        'MONEDA_SIMBOLO' => $moneda,
-                        'MONEDA_NOMBRE' => $stock->CAMO_DESC
-                    ];
-
-                    if ($recurso['TYPE'] == 'T') {
-                        $herramientas[] = $item;
-                        if (!isset($subtotalesHerramientas[$moneda])) {
-                            $subtotalesHerramientas[$moneda] = 0;
-                        }
-                        $subtotalesHerramientas[$moneda] += $subtotal;
-                    } else {
-                        $refacciones[] = $item;
-                        if (!isset($subtotalesRefacciones[$moneda])) {
-                            $subtotalesRefacciones[$moneda] = 0;
+        if (!is_null($recursos) && !empty($recursos)) {
+            foreach ($recursos as $recurso) {
+                if ($recurso['ID'] != 'SH' && $recurso['REQ'] !== '-') {
+                    $stock = DB::table('S002V01TINST')
+                        ->select(['INST_IDIS', 'INST_MODE', 'INAR_COMO', 'INAR_PREC', 'CAMO_DESC'])
+                        ->join('S002V01TINAR', 'INST_ARTI', '=', 'INAR_IDIN')
+                        ->join('S002V01TCAMO', 'INAR_COMO', '=', 'CAMO_COMO')
+                        ->where([
+                            ['INST_IDIS', '=', $recurso['ID']],
+                            ['INST_NULI', '=', $line]
+                        ])->first();
+
+                    if ($stock) {
+                        $subtotal = $stock->INAR_PREC * $recurso['REQ'];
+                        $moneda = $stock->INAR_COMO;
+
+                        $item = [
+                            'ID' => $recurso['ID'],
+                            'NOMBRE' => $stock->INST_MODE,
+                            'CANTIDAD' => $recurso['REQ'],
+                            'PRECIO_UNITARIO' => $stock->INAR_PREC,
+                            'SUBTOTAL' => $subtotal,
+                            'MONEDA_SIMBOLO' => $moneda,
+                            'MONEDA_NOMBRE' => $stock->CAMO_DESC
+                        ];
+
+                        if ($recurso['TYPE'] == 'T') {
+                            $herramientas[] = $item;
+                            if (!isset($subtotalesHerramientas[$moneda])) {
+                                $subtotalesHerramientas[$moneda] = 0;
+                            }
+                            $subtotalesHerramientas[$moneda] += $subtotal;
+                        } else {
+                            $refacciones[] = $item;
+                            if (!isset($subtotalesRefacciones[$moneda])) {
+                                $subtotalesRefacciones[$moneda] = 0;
+                            }
+                            $subtotalesRefacciones[$moneda] += $subtotal;
                         }
-                        $subtotalesRefacciones[$moneda] += $subtotal;
                     }
                 }
             }
         }
 
-        // Convertir subtotales a arrays
-        $subtotalHerramientasArray = [];
-        foreach ($subtotalesHerramientas as $simbolo => $subtotal) {
-            $subtotalHerramientasArray[] = ['SIMBOLO' => $simbolo, 'SUBTOTAL' => $subtotal];
+        // Convertir subtotales a arrays solo si hay recursos
+        if ($tieneHerramientas) {
+            foreach ($subtotalesHerramientas as $simbolo => $subtotal) {
+                $subtotalHerramientasArray[] = ['SIMBOLO' => $simbolo, 'SUBTOTAL' => $subtotal];
+            }
         }
 
-        $subtotalRefaccionesArray = [];
-        foreach ($subtotalesRefacciones as $simbolo => $subtotal) {
-            $subtotalRefaccionesArray[] = ['SIMBOLO' => $simbolo, 'SUBTOTAL' => $subtotal];
+        if ($tieneRefacciones) {
+            foreach ($subtotalesRefacciones as $simbolo => $subtotal) {
+                $subtotalRefaccionesArray[] = ['SIMBOLO' => $simbolo, 'SUBTOTAL' => $subtotal];
+            }
         }
 
         $now = $this->functionsController->now();
@@ -825,15 +851,25 @@ class CorrectiveMaintenanceController extends Controller
             $analisisPrevio = json_decode($order->OTCO_ANCO, true);
         }
 
-        return $this->responseController->makeResponse(false, 'EXITO.', [
+        $response = [
             'USUARIOS' => $usuariosParticipantes,
-            'HERRAMIENTAS' => $herramientas,
-            'REFACCIONES' => $refacciones,
-            'SUBTOTAL_HERRAMIENTAS' => $subtotalHerramientasArray,
-            'SUBTOTAL_REFACCIONES' => $subtotalRefaccionesArray,
+            'TIENE_HERRAMIENTAS' => $tieneHerramientas,
+            'TIENE_REFACCIONES' => $tieneRefacciones,
             'TIENE_ANALISIS_PREVIO' => $tieneAnalisisPrevio,
             'ANALISIS_PREVIO' => $analisisPrevio
-        ]);
+        ];
+
+        if ($tieneHerramientas) {
+            $response['HERRAMIENTAS'] = $herramientas;
+            $response['SUBTOTAL_HERRAMIENTAS'] = $subtotalHerramientasArray;
+        }
+
+        if ($tieneRefacciones) {
+            $response['REFACCIONES'] = $refacciones;
+            $response['SUBTOTAL_REFACCIONES'] = $subtotalRefaccionesArray;
+        }
+
+        return $this->responseController->makeResponse(false, 'EXITO.', $response);
     }
 
     public function saveCostAnalysis(Request $request)
@@ -846,11 +882,11 @@ class CorrectiveMaintenanceController extends Controller
             'id_order' => 'required|string',
             'COMMENTS' => 'string',
             'USUARIOS' => 'required|array',
-            'HERRAMIENTAS' => 'required|array',
-            'REFACCIONES' => 'required|array',
+            'HERRAMIENTAS' => 'array',
+            'REFACCIONES' => 'array',
             'SUBTOTAL_USUARIOS' => 'required|array',
-            'SUBTOTAL_HERRAMIENTAS' => 'required|array',
-            'SUBTOTAL_REFACCIONES' => 'required|array',
+            'SUBTOTAL_HERRAMIENTAS' => 'array',
+            'SUBTOTAL_REFACCIONES' => 'array',
             'TOTAL' => 'required|array'
         ]);
 
@@ -905,11 +941,11 @@ class CorrectiveMaintenanceController extends Controller
             'USUARIO' => $idUser,
             'COMENTARIOS' => $form['COMMENTS'] ?? '',
             'USUARIOS' => $form['USUARIOS'],
-            'HERRAMIENTAS' => $form['HERRAMIENTAS'],
-            'REFACCIONES' => $form['REFACCIONES'],
+            'HERRAMIENTAS' => $form['HERRAMIENTAS'] ?? [],
+            'REFACCIONES' => $form['REFACCIONES'] ?? [],
             'SUBTOTAL_USUARIOS' => $form['SUBTOTAL_USUARIOS'],
-            'SUBTOTAL_HERRAMIENTAS' => $form['SUBTOTAL_HERRAMIENTAS'],
-            'SUBTOTAL_REFACCIONES' => $form['SUBTOTAL_REFACCIONES'],
+            'SUBTOTAL_HERRAMIENTAS' => $form['SUBTOTAL_HERRAMIENTAS'] ?? [],
+            'SUBTOTAL_REFACCIONES' => $form['SUBTOTAL_REFACCIONES'] ?? [],
             'TOTAL' => $form['TOTAL']
         ];
 
@@ -1845,7 +1881,7 @@ class CorrectiveMaintenanceController extends Controller
     }
 
 
-        public function updateWorkOrderStatus(Request $request)
+    public function updateWorkOrderStatus(Request $request)
     {
         DB::enableQueryLog();
         $validator = Validator::make($request->all(), [
@@ -1949,9 +1985,8 @@ class CorrectiveMaintenanceController extends Controller
         $ftin = null;
         $dtin = null;
         //if(($status == 'CE' || $status == 'CP') && is_null($order->OTCO_FTIN)){
-        if ($status == 'CP' && is_null($order->OTCO_FTIN)) {
 
-        if($status == 'CP' && is_null($order->OTCO_FTIN)){
+        if ($status == 'CP' && is_null($order->OTCO_FTIN)) {
             $ftin = $nowStr;
             $startDate = new Carbon($order->OTCO_FIFA);
 
@@ -1964,8 +1999,8 @@ class CorrectiveMaintenanceController extends Controller
         }
 
         $statusHistoryArr = json_decode($order->OTCO_HIES, true);
-        if($status == 'CE'){
-            $validatedStatusFilt = array_filter($statusHistoryArr, function($v, $k) {
+        if ($status == 'CE') {
+            $validatedStatusFilt = array_filter($statusHistoryArr, function ($v, $k) {
                 return $v['ESTADO'] == 'VA';
             }, ARRAY_FILTER_USE_BOTH);
 
@@ -1973,20 +2008,20 @@ class CorrectiveMaintenanceController extends Controller
             $attendance = $validatedStatus['ATENCION'];
             $enabledUsersToClose = [];
 
-            foreach($attendance as $item){
-                if($item['RESPUESTA'] == 'A') $enabledUsersToClose[] = $item['ID'];
+            foreach ($attendance as $item) {
+                if ($item['RESPUESTA'] == 'A') $enabledUsersToClose[] = $item['ID'];
             }
 
-        if (!in_array($idUser, $enabledUsersToClose)) {
-            return $this->responseController->makeResponse(true, 'El usuario que solicitó la acción no tiene los permisos necesarios.', [], 401);
-        }
-            if(!in_array($idUser, $enabledUsersToClose)){
+            if (!in_array($idUser, $enabledUsersToClose)) {
+                return $this->responseController->makeResponse(true, 'El usuario que solicitó la acción no tiene los permisos necesarios.', [], 401);
+            }
+            if (!in_array($idUser, $enabledUsersToClose)) {
                 return $this->responseController->makeResponse(true, 'El usuario que solicitó la acción no tiene los permisos necesarios.', [], 401);
             }
 
             $closeUserName = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
             $audience = [$order->OTCO_IDUR];
-                
+
             $paramsStr = file_get_contents('C:\inetpub\wwwroot\sam\storage\app\files\system-params.json');
             $paramsArr = json_decode($paramsStr, true);
             $orderPriorities = $paramsArr['order_priorities'];
@@ -1996,7 +2031,7 @@ class CorrectiveMaintenanceController extends Controller
             }, ARRAY_FILTER_USE_BOTH);
 
             $notificationColor = "rgb(35, 95, 172)";
-            if(count($priorityFilt) > 0){
+            if (count($priorityFilt) > 0) {
                 $priority = end($priorityFilt);
                 $notificationColor = $priority['color'];
             }
@@ -2015,26 +2050,26 @@ class CorrectiveMaintenanceController extends Controller
                     'PARAMETROS' => json_encode([$this->encryptionController->encrypt('GMCO/ORTR/GEOP')])
                 ]],
                 $audience,
-                $idUser, 
+                $idUser,
                 $form['linea'],
                 $this->getSocketClient(),
                 $idOrder,
                 'Correctivo',
                 $notificationColor
             );
-        }else if($status == 'CA'){
-            $pendingStatusFilt = array_filter($statusHistoryArr, function($v, $k) {
+        } else if ($status == 'CA') {
+            $pendingStatusFilt = array_filter($statusHistoryArr, function ($v, $k) {
                 return $v['ESTADO'] == 'PE';
             }, ARRAY_FILTER_USE_BOTH);
 
             $pendingStatus = end($pendingStatusFilt);
-            if($pendingStatus['USUARIO'] != $idUser){
+            if ($pendingStatus['USUARIO'] != $idUser) {
                 return $this->responseController->makeResponse(true, 'El usuario que solicitó la acción no tiene los permisos necesarios.', [], 401);
             }
 
             $cancelUserName = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
             $audience = [$order->OTCO_IDUR];
-                
+
             $paramsStr = file_get_contents('C:\inetpub\wwwroot\sam\storage\app\files\system-params.json');
             $paramsArr = json_decode($paramsStr, true);
             $orderPriorities = $paramsArr['order_priorities'];
@@ -2044,7 +2079,7 @@ class CorrectiveMaintenanceController extends Controller
             }, ARRAY_FILTER_USE_BOTH);
 
             $notificationColor = "rgb(35, 95, 172)";
-            if(count($priorityFilt) > 0){
+            if (count($priorityFilt) > 0) {
                 $priority = end($priorityFilt);
                 $notificationColor = $priority['color'];
             }
@@ -2063,26 +2098,26 @@ class CorrectiveMaintenanceController extends Controller
                     'PARAMETROS' => json_encode([$this->encryptionController->encrypt('GMCO/ORTR/GEOP')])
                 ]],
                 $audience,
-                $idUser, 
+                $idUser,
                 $form['linea'],
                 $this->getSocketClient(),
                 $idOrder,
                 'Correctivo',
                 $notificationColor
             );
-        }else if($status == 'RE'){
-            if($order->OTCO_IDUR != $idUser){
+        } else if ($status == 'RE') {
+            if ($order->OTCO_IDUR != $idUser) {
                 return $this->responseController->makeResponse(true, 'El usuario que solicitó la acción no tiene los permisos necesarios.', [], 401);
             }
 
-            $pendingStatusFilt = array_filter($statusHistoryArr, function($v, $k) {
+            $pendingStatusFilt = array_filter($statusHistoryArr, function ($v, $k) {
                 return $v['ESTADO'] == 'PE';
             }, ARRAY_FILTER_USE_BOTH);
 
             $pendingStatus = end($pendingStatusFilt);
             $declineUserName = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
             $audience = [$pendingStatus['USUARIO']];
-                
+
             $paramsStr = file_get_contents('C:\inetpub\wwwroot\sam\storage\app\files\system-params.json');
             $paramsArr = json_decode($paramsStr, true);
             $orderPriorities = $paramsArr['order_priorities'];
@@ -2092,7 +2127,7 @@ class CorrectiveMaintenanceController extends Controller
             }, ARRAY_FILTER_USE_BOTH);
 
             $notificationColor = "rgb(35, 95, 172)";
-            if(count($priorityFilt) > 0){
+            if (count($priorityFilt) > 0) {
                 $priority = end($priorityFilt);
                 $notificationColor = $priority['color'];
             }
@@ -2111,7 +2146,7 @@ class CorrectiveMaintenanceController extends Controller
                     'PARAMETROS' => json_encode([$this->encryptionController->encrypt('GMCO/ORTR/GEOP')])
                 ]],
                 $audience,
-                $idUser, 
+                $idUser,
                 $form['linea'],
                 $this->getSocketClient(),
                 $idOrder,