ADD: Se mejoró la búsqueda de registros en InscriptionController y se añadieron nuevos filtros. Se actualizó la generación de PDF en RecordController para incluir datos del vehículo y propietario. Se modificó la ruta para generar el formulario PDF.

This commit is contained in:
Juan Felipe Zapata Moreno 2025-12-04 13:50:46 -06:00
parent 70f3679ba4
commit 8e2db75ad2
5 changed files with 186 additions and 59 deletions

View File

@ -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);
}
}

View File

@ -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)

View File

@ -15,6 +15,10 @@ class Package extends Model
'description',
];
protected $attributes = [
'total_boxes' => 0,
];
protected function casts(): array
{
return [

View File

@ -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'
);
}
}

View File

@ -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