|
|
@@ -2738,6 +2738,164 @@ class PreventiveMaintenanceController extends Controller
|
|
|
return $this->responseController->makeResponse(false, 'EXITO.');
|
|
|
}
|
|
|
|
|
|
+ // Visitas técnicas no programadas (Preventivas)
|
|
|
+ public function startPreventiveVisit(Request $request)
|
|
|
+ {
|
|
|
+ DB::enableQueryLog();
|
|
|
+
|
|
|
+ $validator = Validator::make($request->all(), [
|
|
|
+ 'id_user' => 'required|string',
|
|
|
+ 'linea' => 'required|integer',
|
|
|
+ 'id_visit' => 'required|string'
|
|
|
+ ]);
|
|
|
+
|
|
|
+ if ($validator->fails()) {
|
|
|
+ return $this->responseController->makeResponse(
|
|
|
+ true,
|
|
|
+ "Se encontraron uno o más errores.",
|
|
|
+ $this->responseController->makeErrors(
|
|
|
+ $validator->errors()->messages()
|
|
|
+ ),
|
|
|
+ 401
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ $form = $request->all();
|
|
|
+ $idUser = $this->encryptionController->decrypt($form['id_user']);
|
|
|
+ if (!$idUser) {
|
|
|
+ return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
|
|
|
+ }
|
|
|
+
|
|
|
+ $usr = DB::table('S002V01TUSUA')->where([
|
|
|
+ ['USUA_NULI', '=', $form['linea']],
|
|
|
+ ['USUA_IDUS', '=', $idUser]
|
|
|
+ ])->first();
|
|
|
+
|
|
|
+ if (is_null($usr)) {
|
|
|
+ return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
|
|
|
+ }
|
|
|
+
|
|
|
+ $idVisit = $this->encryptionController->decrypt($form['id_visit']);
|
|
|
+ if (!$idVisit) {
|
|
|
+ return $this->responseController->makeResponse(true, 'El ID de la visita no fue encriptado correctamente.', [], 400);
|
|
|
+ }
|
|
|
+
|
|
|
+ $visit = DB::table('S002V01TRVTN')->where([
|
|
|
+ ['RVTN_NULI', '=', $form['linea']],
|
|
|
+ ['RVTN_IDVI', '=', $idVisit]
|
|
|
+ ])->first();
|
|
|
+
|
|
|
+ if (is_null($visit)) {
|
|
|
+ return $this->responseController->makeResponse(true, 'La visita solicitada no existe.', [], 404);
|
|
|
+ }
|
|
|
+
|
|
|
+ $visitStates = [
|
|
|
+ 'VA' => 'Validada',
|
|
|
+ 'EP' => 'En progreso',
|
|
|
+ 'CP' => 'Cerrada pendiente',
|
|
|
+ 'CE' => 'Cerrada',
|
|
|
+ '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 está $statusName.", [], 401);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($visit->RVTN_USRE != $idUser) {
|
|
|
+ return $this->responseController->makeResponse(true, 'El usuario que solicitó la acción no tiene los permisos necesarios.', [], 401);
|
|
|
+ }
|
|
|
+
|
|
|
+ $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);
|
|
|
+ }
|
|
|
+
|
|
|
+ $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'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $now = $this->functionsController->now();
|
|
|
+ $nowStr = $now->toDateTimeString();
|
|
|
+
|
|
|
+ $statusHistoryArr[] = [
|
|
|
+ 'USUARIO' => $idUser,
|
|
|
+ 'ESTADO' => 'EP',
|
|
|
+ 'FECHA' => $nowStr
|
|
|
+ ];
|
|
|
+
|
|
|
+ $statusHistoryStr = json_encode($statusHistoryArr);
|
|
|
+
|
|
|
+ DB::table('S002V01TRVTN')->where([
|
|
|
+ ['RVTN_IDVI', '=', $idVisit],
|
|
|
+ ['RVTN_NULI', '=', $form['linea']]
|
|
|
+ ])->update([
|
|
|
+ 'RVTN_ESTA' => 'EP',
|
|
|
+ 'RVTN_HIES' => $statusHistoryStr,
|
|
|
+ 'RVTN_USMO' => $idUser,
|
|
|
+ 'RVTN_FEMO' => $nowStr,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $this->notificationsController->emitNotification(
|
|
|
+ 'S002V01M10GMPR',
|
|
|
+ "Visita de mantenimiento preventivo #$idVisit",
|
|
|
+ "Inicio de la ejecución de la visita de mantenimiento preventivo #$idVisit.",
|
|
|
+ [[
|
|
|
+ 'BOTON' => 'Ver detalles',
|
|
|
+ 'FUNCION' => 'openPreventiveWorkOrderDetails',
|
|
|
+ 'PARAMETROS' => json_encode([$this->encryptionController->encrypt($idVisit)])
|
|
|
+ ], [
|
|
|
+ 'BOTON' => 'Ir al módulo',
|
|
|
+ 'FUNCION' => 'openModule',
|
|
|
+ 'PARAMETROS' => json_encode([$this->encryptionController->encrypt('GMPR/AOTR/RVTP')])
|
|
|
+ ]],
|
|
|
+ $audience,
|
|
|
+ $idUser,
|
|
|
+ $form['linea'],
|
|
|
+ $this->getSocketClient(),
|
|
|
+ $idVisit,
|
|
|
+ 'Preventivo'
|
|
|
+ );
|
|
|
+
|
|
|
+ $actions = DB::getQueryLog();
|
|
|
+ $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
|
|
|
+
|
|
|
+ $idac = $this->functionsController->registerActivity(
|
|
|
+ $form['linea'],
|
|
|
+ 'S002V01M10GMPR',
|
|
|
+ 'S002V01F11RVTP',
|
|
|
+ 'S002V01P01REVI',
|
|
|
+ 'Actualización',
|
|
|
+ "El usuario $name (" . $usr->USUA_IDUS . ") inició la visita preventiva #$idVisit.",
|
|
|
+ $idUser,
|
|
|
+ $nowStr,
|
|
|
+ 'S002V01S02AOTR'
|
|
|
+ );
|
|
|
+
|
|
|
+ $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
|
|
|
+ return $this->responseController->makeResponse(false, 'EXITO.');
|
|
|
+ }
|
|
|
+
|
|
|
public function updateOrderStatus(Request $request)
|
|
|
{
|
|
|
DB::enableQueryLog();
|
|
|
@@ -5469,6 +5627,7 @@ class PreventiveMaintenanceController extends Controller
|
|
|
'EP' => 'En progreso', // Automática
|
|
|
'CP' => 'Cerrado pendiente', // Automática
|
|
|
'CE' => 'Cerrado', // Automática
|
|
|
+ 'PE' => 'Pendiente de validación', // Automática
|
|
|
'P' => 'Pendiente', // Manual
|
|
|
'C' => 'Cancelado', // Manual
|
|
|
'R' => 'Rechazado', // Manual
|