feat: inscripción de vehículo con datos de repuve nacional y estatal

This commit is contained in:
Juan Felipe Zapata Moreno 2026-02-14 12:32:19 -06:00
parent 557fe6858c
commit 2093ff7538
2 changed files with 82 additions and 52 deletions

View File

@ -88,18 +88,31 @@ public function vehicleInscription(VehicleStoreRequest $request)
]); ]);
} }
// Obtener datos del servicio ESTATAL
$datosCompletosRaw = $this->padronEstatalService->getVehiculoByPlaca($placa); $datosCompletosRaw = $this->padronEstatalService->getVehiculoByPlaca($placa);
// Obtener datos de API Estatal por placa // Extraer datos del servicio estatal
$vehicleData = $this->padronEstatalService->extraerDatosVehiculo($datosCompletosRaw); $vehicleDataEstatal = $this->padronEstatalService->extraerDatosVehiculo($datosCompletosRaw);
$ownerData = $this->padronEstatalService->extraerDatosPropietario($datosCompletosRaw); $ownerData = $this->padronEstatalService->extraerDatosPropietario($datosCompletosRaw);
// Obtener NIV de los datos del vehículo para verificar robo // Obtener NIV para consultar REPUVE Nacional
$niv = $vehicleData['niv']; $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); $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) { if ($roboResult['is_robado'] ?? false) {
DB::rollBack(); DB::rollBack();
return ApiResponse::FORBIDDEN->response([ 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([ $vehicle = Vehicle::create([
'placa' => $vehicleData['placa'], // Datos de Estatal
'niv' => $vehicleData['niv'], 'placa' => $vehicleDataEstatal['placa'],
'marca' => $vehicleData['marca'], 'numero_motor' => $vehicleDataEstatal['numero_motor'],
'linea' => $vehicleData['linea'],
'sublinea' => $vehicleData['sublinea'], // Datos de REPUVE NACIONAL
'modelo' => $vehicleData['modelo'], 'niv' => $repuveNacionalData['niv'],
'color' => $vehicleData['color'], 'marca' => $repuveNacionalData['marca'],
'numero_motor' => $vehicleData['numero_motor'], 'linea' => $repuveNacionalData['linea'],
'clase_veh' => $vehicleData['clase_veh'], 'modelo' => $repuveNacionalData['modelo'],
'tipo_servicio' => $vehicleData['tipo_servicio'],
'rfv' => $vehicleData['rfv'], // Otros datos - de estatal
'ofcexpedicion' => $vehicleData['ofcexpedicion'], 'sublinea' => $vehicleDataEstatal['sublinea'],
'fechaexpedicion' => $vehicleData['fechaexpedicion'], 'color' => $repuveNacionalData['color'] ?? $vehicleDataEstatal['color'],
'tipo_veh' => $vehicleData['tipo_veh'], 'clase_veh' => $vehicleDataEstatal['clase_veh'],
'numptas' => $vehicleData['numptas'], 'tipo_servicio' => $vehicleDataEstatal['tipo_servicio'],
'observac' => $vehicleData['observac'], 'rfv' => $vehicleDataEstatal['rfv'],
'cve_vehi' => $vehicleData['cve_vehi'], 'ofcexpedicion' => $vehicleDataEstatal['ofcexpedicion'],
'nrpv' => $vehicleData['nrpv'], 'fechaexpedicion' => $vehicleDataEstatal['fechaexpedicion'],
'tipo_mov' => $vehicleData['tipo_mov'], '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, '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.', 'end_date.after_or_equal' => 'La fecha de fin debe ser posterior o igual a la fecha de inicio.',
]); ]);
$records = Record::with([ $records = Record::forUserModule(Auth::user())
// Vehículo y propietario ->with([
'vehicle', // Vehículo y propietario
'vehicle.owner', 'vehicle',
'vehicle.owner',
// Tag con Package // Tag con Package
'vehicle.tag:id,vehicle_id,folio,tag_number,status_id,package_id', 'vehicle.tag:id,vehicle_id,folio,tag_number,status_id,package_id',
'vehicle.tag.status:id,code,name', 'vehicle.tag.status:id,code,name',
'vehicle.tag.package:id,lot,box_number', 'vehicle.tag.package:id,lot,box_number',
// Archivos // Archivos
'files:id,record_id,name_id,path,md5', 'files:id,record_id,name_id,path,md5',
'files.catalogName:id,name', 'files.catalogName:id,name',
// Operador y módulo // Operador y módulo
'user:id,name,username,module_id', 'user:id,name,username,module_id',
'module:id,name', 'module:id,name',
// Error si existe // Error si existe
'error:id,code,description', 'error:id,code,description',
// Log de acciones // Log de acciones
'vehicle.vehicleTagLogs' => function ($q) { 'vehicle.vehicleTagLogs' => function ($q) {
$q->with([ $q->with([
'tag:id,folio,tag_number,status_id,module_id,package_id', 'tag:id,folio,tag_number,status_id,module_id,package_id',
'tag.status:id,code,name', 'tag.status:id,code,name',
'tag.module:id,name', 'tag.module:id,name',
'tag.package:id,lot,box_number' 'tag.package:id,lot,box_number'
])->orderBy('created_at', 'DESC'); ])->orderBy('created_at', 'DESC');
}, },
])->orderBy('id', 'ASC'); ])->orderBy('id', 'ASC');
if ($request->filled('folio')) { if ($request->filled('folio')) {
$records->whereHas('vehicle.tag', function ($q) use ($request) { $records->whereHas('vehicle.tag', function ($q) use ($request) {

View File

@ -60,4 +60,15 @@ public function vehicleTagLog()
'id' '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);
}
} }