From 8e2db75ad24439226b03c8a7023f61d87d1c671d Mon Sep 17 00:00:00 2001 From: Juan Felipe Zapata Moreno Date: Thu, 4 Dec 2025 13:50:46 -0600 Subject: [PATCH] =?UTF-8?q?ADD:=20Se=20mejor=C3=B3=20la=20b=C3=BAsqueda=20?= =?UTF-8?q?de=20registros=20en=20InscriptionController=20y=20se=20a=C3=B1a?= =?UTF-8?q?dieron=20nuevos=20filtros.=20Se=20actualiz=C3=B3=20la=20generac?= =?UTF-8?q?i=C3=B3n=20de=20PDF=20en=20RecordController=20para=20incluir=20?= =?UTF-8?q?datos=20del=20veh=C3=ADculo=20y=20propietario.=20Se=20modific?= =?UTF-8?q?=C3=B3=20la=20ruta=20para=20generar=20el=20formulario=20PDF.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Repuve/InscriptionController.php | 135 ++++++++++++++---- .../Controllers/Repuve/RecordController.php | 92 +++++++----- app/Models/Package.php | 4 + app/Models/Record.php | 12 ++ routes/api.php | 2 +- 5 files changed, 186 insertions(+), 59 deletions(-) diff --git a/app/Http/Controllers/Repuve/InscriptionController.php b/app/Http/Controllers/Repuve/InscriptionController.php index c3a047d..c1f67c3 100644 --- a/app/Http/Controllers/Repuve/InscriptionController.php +++ b/app/Http/Controllers/Repuve/InscriptionController.php @@ -322,56 +322,141 @@ public function searchRecord(Request $request) 'folio' => 'nullable|string', 'placa' => 'nullable|string', 'vin' => 'nullable|string', + 'module_id' => 'nullable|integer|exists:modules,id', + 'action_type' => 'nullable|string|in:inscripcion,actualizacion,sustitucion,cancelacion', + 'status' => 'nullable|string', ], [ 'required_without_all' => 'Debe proporcionar al menos uno de los siguientes: folio, placa o vin.' ]); $records = Record::with([ + // Vehículo y propietario 'vehicle', 'vehicle.owner', - 'vehicle.tag:id,vehicle_id,folio,tag_number,status_id', + + // Tag con Box y Package + 'vehicle.tag:id,vehicle_id,folio,tag_number,status_id,box_id', 'vehicle.tag.status:id,code,name', + 'vehicle.tag.box:id,box_number,package_id', + 'vehicle.tag.box.package:id,lot', + + // Archivos 'files:id,record_id,name_id,path,md5', 'files.catalogName:id,name', - 'user:id,name,email', - 'error:id,code,description' + + // Operador y módulo + 'user:id,name,email,module_id', + 'module:id,name', + + // Error si existe + 'error:id,code,description', + + // Log de acciones + 'vehicle.vehicleTagLogs:id,vehicle_id,tag_id,action_type,performed_by,created_at' ])->orderBy('id', 'ASC'); if ($request->filled('folio')) { $records->whereHas('vehicle.tag', function ($q) use ($request) { $q->where('folio', 'LIKE', '%' . $request->input('folio') . '%'); }); - } elseif ($request->filled('placa')) { + } + if ($request->filled('placa')) { $records->whereHas('vehicle', function ($q) use ($request) { $q->where('placa', 'LIKE', '%' . $request->input('placa') . '%'); }); - } elseif ($request->filled('vin')) { + } + if ($request->filled('vin')) { $records->whereHas('vehicle', function ($q) use ($request) { $q->where('niv', 'LIKE', '%' . $request->input('vin') . '%'); }); } + if ($request->filled('module_id')) { + $records->where('module_id', $request->input('module_id')); + } + if ($request->filled('action_type')) { + $records->whereHas('vehicle.vehicleTagLogs', function ($q) use ($request) { + $q->where('action_type', $request->input('action_type')); + }); + } + if ($request->filled('status')) { + $records->whereHas('vehicle.tag.status', function ($q) use ($request) { + $q->where('code', $request->input('status')); + }); + } + + $paginatedRecords = $records->paginate(config('app.pagination')); + + $paginatedRecords->getCollection()->transform(function ($record) { + // Obtener el último log de acción + $latestLog = $record->vehicle->vehicleTagLogs->sortByDesc('created_at')->first(); + + return [ + 'id' => $record->id, + 'folio' => $record->folio, + 'created_at' => $record->created_at, + + // MÓDULO + 'module' => $record->module ? [ + 'id' => $record->module->id, + 'name' => $record->module->name, + ] : null, + + // OPERADOR + 'operator' => $record->user ? [ + 'id' => $record->user->id, + 'name' => $record->user->name, + 'email' => $record->user->email, + ] : null, + + // TIPO DE TRÁMITE + 'action_type' => $latestLog?->action_type ?? 'inscripcion', + 'action_date' => $latestLog?->created_at ?? $record->created_at, + + // Vehículo + 'vehicle' => $record->vehicle->id, + + // Propietario + 'owner' => $record->vehicle->owner, + + // Tag y Caja + 'tag' => $record->vehicle->tag ? [ + 'id' => $record->vehicle->tag->id, + 'folio' => $record->vehicle->tag->folio, + 'tag_number' => $record->vehicle->tag->tag_number, + 'status' => $record->vehicle->tag->status?->name, + + // Caja + 'box' => $record->vehicle->tag->box ? [ + 'id' => $record->vehicle->tag->box->id, + 'box_number' => $record->vehicle->tag->box->box_number, + 'package' => $record->vehicle->tag->box->package ? [ + 'id' => $record->vehicle->tag->box->package->id, + 'lot' => $record->vehicle->tag->box->package->lot, + ] : null, + ] : null, + ] : null, + + // Archivos + 'files' => $record->files->map(function ($file) { + return [ + 'id' => $file->id, + 'name' => $file->catalogName?->name, + 'path' => $file->path, + 'url' => $file->url, + ]; + }), + + // Error + 'error' => $record->error ? [ + 'id' => $record->error->id, + 'code' => $record->error->code, + 'description' => $record->error->description, + ] : null, + ]; + }); return ApiResponse::OK->response([ - 'records' => $records->select([ - 'id', - 'folio', - 'vehicle_id', - 'user_id', - 'error_id', - 'created_at' - ])->paginate(config('app.pagination')) + 'records' => $paginatedRecords ]); } - - private function getVehicleByPlaca(string $placa): array - { - $datos = $this->padronEstatalService->getVehiculoByPlaca($placa); - return $this->padronEstatalService->extraerDatosVehiculo($datos); - } - - private function getOwnerByPlaca(string $placa): array - { - $datos = $this->padronEstatalService->getVehiculoByPlaca($placa); - return $this->padronEstatalService->extraerDatosPropietario($datos); - } } diff --git a/app/Http/Controllers/Repuve/RecordController.php b/app/Http/Controllers/Repuve/RecordController.php index 00e63be..80575f9 100644 --- a/app/Http/Controllers/Repuve/RecordController.php +++ b/app/Http/Controllers/Repuve/RecordController.php @@ -181,44 +181,70 @@ public function generatePdfImages($id) } } - public function generatePdfForm(Request $request) + public function generatePdfForm($id) { - $request->validate([ - 'marca' => 'required|string', - 'linea' => 'required|string', - 'modelo' => 'required|string', - 'niv' => 'required|string|max:17', - 'numero_motor' => 'required|string', - 'placa' => 'required|string|max:7', - 'folio' => 'required|string', - 'telefono' => 'nullable|string', - ]); + try { + $record = Record::with([ + 'vehicle', + 'vehicle.owner', + 'vehicle.tag', + ])->findOrFail($id); - $now = Carbon::now()->locale('es_MX'); + if (!$record->vehicle) { + return ApiResponse::NOT_FOUND->response([ + 'message' => 'El registro no tiene un vehículo asociado.', + 'record_id' => $id, + ]); + } - $data = [ - 'marca' => $request->input('marca'), - 'linea' => $request->input('linea'), - 'modelo' => $request->input('modelo'), - 'niv' => $request->input('niv'), - 'numero_motor' => $request->input('numero_motor'), - 'placa' => $request->input('placa'), - 'folio' => $request->input('folio'), - 'telefono' => $request->input('telefono', ''), - 'fecha' => $now->format('d'), - 'mes' => ucfirst($now->translatedFormat('F')), - 'anio' => $now->format('Y'), - ]; + $vehicle = $record->vehicle; + $owner = $vehicle->owner; + $tag = $vehicle->tag; - $pdf = Pdf::loadView('pdfs.form', $data) - ->setPaper('a4', 'portrait') - ->setOptions([ - 'defaultFont' => 'sans-serif', - 'isHtml5ParserEnabled' => true, - 'isRemoteEnabled' => true, + $now = Carbon::now()->locale('es_MX'); + + $data = [ + // Datos del vehículo + 'marca' => strtoupper($vehicle->marca ?? ''), + 'linea' => strtoupper($vehicle->linea ?? ''), + 'modelo' => $vehicle->modelo ?? '', + 'niv' => strtoupper($vehicle->niv ?? ''), + 'numero_motor' => strtoupper($vehicle->numero_motor ?? ''), + 'placa' => strtoupper($vehicle->placa ?? ''), + 'folio' => $tag?->folio ?? $record->folio ?? '', + + // Datos del propietario + 'telefono' => $owner?->telefono ?? '', + + // Fecha actual + 'fecha' => $now->format('d'), + 'mes' => ucfirst($now->translatedFormat('F')), + 'anio' => $now->format('Y'), + + 'record_id' => $record->id, + 'owner_name' => $owner?->full_name ?? '', + ]; + + $pdf = Pdf::loadView('pdfs.form', $data) + ->setPaper('a4', 'portrait') + ->setOptions([ + 'defaultFont' => 'sans-serif', + 'isHtml5ParserEnabled' => true, + 'isRemoteEnabled' => true, + ]); + + return $pdf->stream('solicitud-sustitucion-' . time() . '.pdf'); + } catch (\Exception $e) { + return ApiResponse::NOT_FOUND->response([ + 'message' => 'No se encontró el registro del expediente proporcionado.', + 'record_id' => $id, ]); - - return $pdf->stream('solicitud-sustitucion-' . time() . '.pdf'); + } catch (\Exception $e) { + return ApiResponse::INTERNAL_ERROR->response([ + 'message' => 'Error al generar el PDF del formulario', + 'error' => $e->getMessage(), + ]); + } } public function pdfCancelledTag(Tag $tag) diff --git a/app/Models/Package.php b/app/Models/Package.php index ca62276..f592bb1 100644 --- a/app/Models/Package.php +++ b/app/Models/Package.php @@ -15,6 +15,10 @@ class Package extends Model 'description', ]; + protected $attributes = [ + 'total_boxes' => 0, + ]; + protected function casts(): array { return [ diff --git a/app/Models/Record.php b/app/Models/Record.php index 416e981..b0972e1 100644 --- a/app/Models/Record.php +++ b/app/Models/Record.php @@ -48,4 +48,16 @@ public function module() { return $this->belongsTo(Module::class); } + + public function vehicleTagLog() + { + return $this->hasManyThrough( + VehicleTagLog::class, + Vehicle::class, + 'id', + 'vehicle_id', + 'vehicle_id', + 'id' + ); + } } diff --git a/routes/api.php b/routes/api.php index edbbb26..e24db63 100644 --- a/routes/api.php +++ b/routes/api.php @@ -42,7 +42,7 @@ Route::get('expediente/{id}/pdfConstancia', [RecordController::class, 'generatePdfConstancia']); Route::get('expediente/{id}/pdfImagenes', [RecordController::class, 'generatePdfImages']); Route::get('tags/{tag}/pdfTag-cancelado', [RecordController::class, 'pdfCancelledTag']); - Route::post('expediente/pdfFormulario', [RecordController::class, 'generatePdfForm']); + Route::get('expediente/{id}/pdfFormulario', [RecordController::class, 'generatePdfForm']); Route::get('RecordErrors', [RecordController::class, 'errors']); //Rutas de Actualización