From 2093ff7538d582d48ec73c4d5360f7c31d0dae57 Mon Sep 17 00:00:00 2001 From: Juan Felipe Zapata Moreno Date: Sat, 14 Feb 2026 12:32:19 -0600 Subject: [PATCH] =?UTF-8?q?feat:=20inscripci=C3=B3n=20de=20veh=C3=ADculo?= =?UTF-8?q?=20con=20datos=20de=20repuve=20nacional=20y=20estatal?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Repuve/InscriptionController.php | 123 ++++++++++-------- app/Models/Record.php | 11 ++ 2 files changed, 82 insertions(+), 52 deletions(-) diff --git a/app/Http/Controllers/Repuve/InscriptionController.php b/app/Http/Controllers/Repuve/InscriptionController.php index 35dbd0d..396d19e 100644 --- a/app/Http/Controllers/Repuve/InscriptionController.php +++ b/app/Http/Controllers/Repuve/InscriptionController.php @@ -88,18 +88,31 @@ public function vehicleInscription(VehicleStoreRequest $request) ]); } + // Obtener datos del servicio ESTATAL $datosCompletosRaw = $this->padronEstatalService->getVehiculoByPlaca($placa); - // Obtener datos de API Estatal por placa - $vehicleData = $this->padronEstatalService->extraerDatosVehiculo($datosCompletosRaw); + // Extraer datos del servicio estatal + $vehicleDataEstatal = $this->padronEstatalService->extraerDatosVehiculo($datosCompletosRaw); $ownerData = $this->padronEstatalService->extraerDatosPropietario($datosCompletosRaw); - // Obtener NIV de los datos del vehículo para verificar robo - $niv = $vehicleData['niv']; + // Obtener NIV para consultar REPUVE Nacional + $niv = $vehicleDataEstatal['niv']; - // Verificar robo (API Repuve Nacional) + // Consultar REPUVE Nacional para obtener datos oficiales del vehículo + $repuveNacionalData = $this->repuveService->consultarVehiculo($niv, $placa); + + // Verificar si hubo error en la consulta a REPUVE Nacional + if ($repuveNacionalData['has_error'] ?? false) { + DB::rollBack(); + return ApiResponse::INTERNAL_ERROR->response([ + 'message' => 'Error al consultar REPUVE Nacional.', + 'error' => $repuveNacionalData['error_message'] ?? 'Error desconocido', + ]); + } + + // Verificar robo $roboResult = $this->checkIfStolen($niv, $placa); - // Solo bloquear si explícitamente está marcado como robado + // Solo bloquear si está marcado como robado if ($roboResult['is_robado'] ?? false) { DB::rollBack(); return ApiResponse::FORBIDDEN->response([ @@ -132,27 +145,32 @@ public function vehicleInscription(VehicleStoreRequest $request) ] ); - // Crear vehículo + // Crear vehículo combinando datos de REPUVE Nacional y estatal $vehicle = Vehicle::create([ - 'placa' => $vehicleData['placa'], - 'niv' => $vehicleData['niv'], - 'marca' => $vehicleData['marca'], - 'linea' => $vehicleData['linea'], - 'sublinea' => $vehicleData['sublinea'], - 'modelo' => $vehicleData['modelo'], - 'color' => $vehicleData['color'], - 'numero_motor' => $vehicleData['numero_motor'], - 'clase_veh' => $vehicleData['clase_veh'], - 'tipo_servicio' => $vehicleData['tipo_servicio'], - 'rfv' => $vehicleData['rfv'], - 'ofcexpedicion' => $vehicleData['ofcexpedicion'], - 'fechaexpedicion' => $vehicleData['fechaexpedicion'], - 'tipo_veh' => $vehicleData['tipo_veh'], - 'numptas' => $vehicleData['numptas'], - 'observac' => $vehicleData['observac'], - 'cve_vehi' => $vehicleData['cve_vehi'], - 'nrpv' => $vehicleData['nrpv'], - 'tipo_mov' => $vehicleData['tipo_mov'], + // Datos de Estatal + 'placa' => $vehicleDataEstatal['placa'], + 'numero_motor' => $vehicleDataEstatal['numero_motor'], + + // Datos de REPUVE NACIONAL + 'niv' => $repuveNacionalData['niv'], + 'marca' => $repuveNacionalData['marca'], + 'linea' => $repuveNacionalData['linea'], + 'modelo' => $repuveNacionalData['modelo'], + + // Otros datos - de estatal + 'sublinea' => $vehicleDataEstatal['sublinea'], + 'color' => $repuveNacionalData['color'] ?? $vehicleDataEstatal['color'], + 'clase_veh' => $vehicleDataEstatal['clase_veh'], + 'tipo_servicio' => $vehicleDataEstatal['tipo_servicio'], + 'rfv' => $vehicleDataEstatal['rfv'], + 'ofcexpedicion' => $vehicleDataEstatal['ofcexpedicion'], + 'fechaexpedicion' => $vehicleDataEstatal['fechaexpedicion'], + 'tipo_veh' => $vehicleDataEstatal['tipo_veh'], + 'numptas' => $vehicleDataEstatal['numptas'], + 'observac' => $vehicleDataEstatal['observac'], + 'cve_vehi' => $vehicleDataEstatal['cve_vehi'], + 'nrpv' => $vehicleDataEstatal['nrpv'], + 'tipo_mov' => $vehicleDataEstatal['tipo_mov'], 'owner_id' => $owner->id, ]); @@ -302,37 +320,38 @@ public function searchRecord(Request $request) 'end_date.after_or_equal' => 'La fecha de fin debe ser posterior o igual a la fecha de inicio.', ]); - $records = Record::with([ - // Vehículo y propietario - 'vehicle', - 'vehicle.owner', + $records = Record::forUserModule(Auth::user()) + ->with([ + // Vehículo y propietario + 'vehicle', + 'vehicle.owner', - // Tag con Package - 'vehicle.tag:id,vehicle_id,folio,tag_number,status_id,package_id', - 'vehicle.tag.status:id,code,name', - 'vehicle.tag.package:id,lot,box_number', + // Tag con Package + 'vehicle.tag:id,vehicle_id,folio,tag_number,status_id,package_id', + 'vehicle.tag.status:id,code,name', + 'vehicle.tag.package:id,lot,box_number', - // Archivos - 'files:id,record_id,name_id,path,md5', - 'files.catalogName:id,name', + // Archivos + 'files:id,record_id,name_id,path,md5', + 'files.catalogName:id,name', - // Operador y módulo - 'user:id,name,username,module_id', - 'module:id,name', + // Operador y módulo + 'user:id,name,username,module_id', + 'module:id,name', - // Error si existe - 'error:id,code,description', + // Error si existe + 'error:id,code,description', - // Log de acciones - 'vehicle.vehicleTagLogs' => function ($q) { - $q->with([ - 'tag:id,folio,tag_number,status_id,module_id,package_id', - 'tag.status:id,code,name', - 'tag.module:id,name', - 'tag.package:id,lot,box_number' - ])->orderBy('created_at', 'DESC'); - }, - ])->orderBy('id', 'ASC'); + // Log de acciones + 'vehicle.vehicleTagLogs' => function ($q) { + $q->with([ + 'tag:id,folio,tag_number,status_id,module_id,package_id', + 'tag.status:id,code,name', + 'tag.module:id,name', + 'tag.package:id,lot,box_number' + ])->orderBy('created_at', 'DESC'); + }, + ])->orderBy('id', 'ASC'); if ($request->filled('folio')) { $records->whereHas('vehicle.tag', function ($q) use ($request) { diff --git a/app/Models/Record.php b/app/Models/Record.php index b0972e1..2ebd38c 100644 --- a/app/Models/Record.php +++ b/app/Models/Record.php @@ -60,4 +60,15 @@ public function vehicleTagLog() 'id' ); } + + /** + * Filtra registros por módulo del usuario + */ + public function scopeForUserModule($query, $user) + { + if ($user->hasRole('admin')) { + return $query; + } + return $query->where('module_id', $user->module_id); + } }