REFACTOR: Mejorar la lógica de filtrado en ExcelController y optimizar la creación de tags en PackageController
This commit is contained in:
parent
74dedc32df
commit
c56e3b1435
@ -456,11 +456,6 @@ public function excelGeneral(Request $request)
|
||||
'cancellationReason'
|
||||
])
|
||||
->whereIn('action_type', ['inscripcion', 'sustitucion', 'cancelacion'])
|
||||
->when($moduleId, function ($query) use ($moduleId) {
|
||||
$query->whereHas('vehicle.records', function ($q) use ($moduleId) {
|
||||
$q->where('module_id', $moduleId);
|
||||
});
|
||||
})
|
||||
->where(function ($query) use ($fechaInicio, $fechaFin) {
|
||||
$query->where(function ($q) use ($fechaInicio, $fechaFin) {
|
||||
$q->whereIn('action_type', ['inscripcion', 'sustitucion'])
|
||||
@ -470,23 +465,31 @@ public function excelGeneral(Request $request)
|
||||
$q->where('action_type', 'cancelacion')
|
||||
->whereBetween('cancellation_at', [$fechaInicio, $fechaFin]);
|
||||
});
|
||||
})->get();
|
||||
})
|
||||
// DESPUÉS: Aplicar filtro de módulo
|
||||
->when($moduleId, function ($query) use ($moduleId) {
|
||||
$query->whereHas('vehicle.records', function ($q) use ($moduleId) {
|
||||
$q->where('module_id', $moduleId);
|
||||
});
|
||||
})
|
||||
->get();
|
||||
|
||||
$logsT = TagCancellationLog::with([
|
||||
'tag.module',
|
||||
'tag.vehicle',
|
||||
'cancellationReason'
|
||||
])->when($moduleId, function ($query) use ($moduleId) {
|
||||
$query->whereHas('tag', function ($q) use ($moduleId) {
|
||||
$q->where('module_id', $moduleId);
|
||||
});
|
||||
})
|
||||
])
|
||||
->whereBetween('cancellation_at', [$fechaInicio, $fechaFin])
|
||||
->when($moduleId, function ($query) use ($moduleId) {
|
||||
$query->whereHas('tag', function ($q) use ($moduleId) {
|
||||
$q->where('module_id', $moduleId);
|
||||
});
|
||||
})
|
||||
->get();
|
||||
|
||||
if ($logs->isEmpty() && $logsT->isEmpty()) {
|
||||
return ApiResponse::NOT_FOUND->response([
|
||||
'message' => 'No se encontraron constancias canceladas en el periodo especificado',
|
||||
'message' => 'No se encontraron registros en el periodo especificado',
|
||||
'fecha_inicio' => $fechaInicio->format('Y-m-d'),
|
||||
'fecha_fin' => $fechaFin->format('Y-m-d'),
|
||||
'module_id' => $moduleId,
|
||||
@ -665,10 +668,10 @@ private function prepareExcelDataGeneral($logs)
|
||||
$data = [];
|
||||
$no = 1;
|
||||
|
||||
foreach($logs as $log){
|
||||
foreach ($logs as $log) {
|
||||
$isVehicleTagLog = isset($log->action_type);
|
||||
|
||||
if($isVehicleTagLog){
|
||||
if ($isVehicleTagLog) {
|
||||
$vehicle = $log->vehicle;
|
||||
$tag = $log->tag;
|
||||
$actionType = strtoupper($log->action_type);
|
||||
@ -676,10 +679,10 @@ private function prepareExcelDataGeneral($logs)
|
||||
$moduleName = $vehicle->records->first()?->module?->name ?? 'SIN MODULO ASIGNADO';
|
||||
|
||||
$fecha = $log->action_type == 'cancelacion'
|
||||
? $log->cancellation_at?->format('d/m/Y')
|
||||
: $log->created_at->format('d/m/Y');
|
||||
? $log->cancellation_at?->format('d/m/Y')
|
||||
: $log->created_at->format('d/m/Y');
|
||||
|
||||
$motivo = match($log->action_type){
|
||||
$motivo = match ($log->action_type) {
|
||||
'inscripcion' => 'INSCRIPCIÓN',
|
||||
'sustitucion' => 'SUSTITUCIÓN DE CONSTANCIA',
|
||||
'cancelacion' => $log->cancellationReason?->name ?? 'N/A',
|
||||
|
||||
@ -6,10 +6,12 @@
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Repuve\PackageStoreRequest;
|
||||
use App\Http\Requests\Repuve\PackageUpdateRequest;
|
||||
use App\Models\CatalogTagStatus;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Database\QueryException;
|
||||
use App\Models\Package;
|
||||
use App\Models\Tag;
|
||||
|
||||
class PackageController extends Controller
|
||||
{
|
||||
@ -50,13 +52,29 @@ public function store(PackageStoreRequest $request)
|
||||
'ending_page' => $request->ending_page,
|
||||
]);
|
||||
|
||||
// Obtener el status "available" para los tags
|
||||
$statusAvailable = CatalogTagStatus::where('code', 'available')->first();
|
||||
|
||||
if (!$statusAvailable) {
|
||||
throw new \Exception('No se encontró el status "available" para los tags');
|
||||
}
|
||||
|
||||
// Crear los tags según el rango de páginas
|
||||
for ($page = $request->starting_page; $page <= $request->ending_page; $page++) {
|
||||
Tag::create([
|
||||
'folio' => $page,
|
||||
'tag_number' => null,
|
||||
'package_id' => $package->id,
|
||||
'status_id' => $statusAvailable->id,
|
||||
]);
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
|
||||
return ApiResponse::CREATED->response([
|
||||
'message' => 'Paquete registrado exitosamente',
|
||||
'package' => $package,
|
||||
'expected_tags' => $package->ending_page - $package->starting_page + 1,
|
||||
'actual_tags' => $package->tags()->count(),
|
||||
'message' => 'Paquete registrado exitosamente con sus tags',
|
||||
'package' => $package->load('tags'),
|
||||
'tags_created' => $package->tags()->count(),
|
||||
]);
|
||||
} catch (QueryException $e) {
|
||||
DB::rollBack();
|
||||
|
||||
@ -44,12 +44,14 @@ public function tagSubstitution(Request $request)
|
||||
$request->validate([
|
||||
'folio' => 'required|string|exists:records,folio',
|
||||
'new_tag_folio' => 'required|string|exists:tags,folio',
|
||||
'new_tag_folio' => 'required|string',
|
||||
'cancellation_reason_id' => 'nullable|exists:catalog_cancellation_reasons,id',
|
||||
'cancellation_observations' => 'nullable|string|max:500',
|
||||
]);
|
||||
|
||||
$folio = $request->input('folio');
|
||||
$newTagFolio = $request->input('new_tag_folio');
|
||||
$newTagNumber = $request->input('new_tag_number');
|
||||
$cancellationReasonId = $request->input('cancellation_reason_id');
|
||||
$cancellationObservations = $request->input('cancellation_observations');
|
||||
|
||||
@ -106,6 +108,25 @@ public function tagSubstitution(Request $request)
|
||||
]);
|
||||
}
|
||||
|
||||
if (!$newTag->tag_number) {
|
||||
$existingTag = Tag::where('tag_number', $newTagNumber)->first();
|
||||
if ($existingTag && $existingTag->id !== $newTag->id) {
|
||||
return ApiResponse::BAD_REQUEST->response([
|
||||
'message' => 'El tag_number ya está asignado a otro folio.',
|
||||
'tag_number' => $newTagNumber,
|
||||
'folio_existente' => $existingTag->folio,
|
||||
]);
|
||||
}
|
||||
} elseif ($newTag->tag_number !== $newTagNumber) {
|
||||
// Si el tag ya tiene un tag_number diferente
|
||||
return ApiResponse::BAD_REQUEST->response([
|
||||
'message' => 'El folio del nuevo TAG ya tiene un tag_number diferente asignado.',
|
||||
'new_tag_folio' => $newTagFolio,
|
||||
'tag_number_actual' => $newTag->tag_number,
|
||||
'tag_number_enviado' => $newTagNumber,
|
||||
]);
|
||||
}
|
||||
|
||||
// Verificar robo del vehículo
|
||||
$isStolen = $this->checkIfStolen($vehicle->niv);
|
||||
|
||||
@ -125,6 +146,8 @@ public function tagSubstitution(Request $request)
|
||||
VehicleTagLog::create([
|
||||
'vehicle_id' => $vehicle->id,
|
||||
'tag_id' => $oldTag->id,
|
||||
'new_tag_folio' => $newTagFolio,
|
||||
'new_tag_number' => $newTagNumber,
|
||||
'action_type' => 'sustitucion',
|
||||
'cancellation_reason_id' => $cancellationReasonId,
|
||||
'cancellation_observations' => $cancellationObservations,
|
||||
@ -138,6 +161,11 @@ public function tagSubstitution(Request $request)
|
||||
'folio' => $newTagFolio
|
||||
]);
|
||||
|
||||
if (!$newTag->tag_number) {
|
||||
$newTag->tag_number = $newTagNumber;
|
||||
$newTag->save();
|
||||
}
|
||||
|
||||
// Asignar el nuevo TAG al vehículo (usa el nuevo folio)
|
||||
$newTag->markAsAssigned($vehicle->id, $newTagFolio);
|
||||
|
||||
@ -145,6 +173,7 @@ public function tagSubstitution(Request $request)
|
||||
VehicleTagLog::create([
|
||||
'vehicle_id' => $vehicle->id,
|
||||
'tag_id' => $newTag->id,
|
||||
'old_tag_folio' => $oldTag->folio,
|
||||
'action_type' => 'sustitucion',
|
||||
'performed_by' => Auth::id(),
|
||||
]);
|
||||
@ -170,7 +199,7 @@ public function tagSubstitution(Request $request)
|
||||
],
|
||||
'new_tag' => [
|
||||
'folio' => $newTag->folio,
|
||||
'tag_number' => $newTag->tag_number,
|
||||
'tag_number' => $newTagNumber,
|
||||
'status' => 'assigned',
|
||||
],
|
||||
'performed_by' => Auth::user()->name,
|
||||
@ -479,7 +508,7 @@ public function vehicleUpdate(VehicleUpdateRequest $request)
|
||||
}
|
||||
}
|
||||
|
||||
private function checkIfStolen(string $niv): bool
|
||||
private function checkIfStolen(string $niv)
|
||||
{
|
||||
return $this->repuveService->verificarRobo($niv);
|
||||
}
|
||||
|
||||
@ -15,7 +15,7 @@ public function rules(): array
|
||||
{
|
||||
return [
|
||||
'lot' => ['required', 'string'],
|
||||
'box_number' => ['required', 'string'],
|
||||
'box_number' => ['required', 'integer'],
|
||||
'starting_page' => ['required', 'integer', 'min:1'],
|
||||
'ending_page' => ['required', 'integer', 'min:1', 'gte:starting_page'],
|
||||
];
|
||||
|
||||
@ -169,7 +169,12 @@ public function verificarRobo(?string $niv = null, ?string $placa = null): array
|
||||
try {
|
||||
$url = $this->baseUrl . $this->roboEndpoint;
|
||||
|
||||
$arg2 = ($niv ?? '') . '|' . ($placa ?? '') . str_repeat('|', 7);
|
||||
$campo1 = $niv ?? '';
|
||||
$campo2 = '';
|
||||
$campo3 = $placa ?? '';
|
||||
$campo4_8 = str_repeat('|', 5);
|
||||
|
||||
$arg2 = $campo1 . '|' . $campo2 . '|' . $campo3 . '|' . $campo4_8;
|
||||
|
||||
$soapBody = <<<XML
|
||||
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdl="http://consultaRpv.org/wsdl">
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user