diff --git a/app/Http/Controllers/Repuve/ExcelController.php b/app/Http/Controllers/Repuve/ExcelController.php index 5a63c01..8583c12 100644 --- a/app/Http/Controllers/Repuve/ExcelController.php +++ b/app/Http/Controllers/Repuve/ExcelController.php @@ -22,6 +22,7 @@ use PhpOffice\PhpSpreadsheet\Style\Fill; use PhpOffice\PhpSpreadsheet\Worksheet\Drawing; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Log; use Illuminate\Routing\Controllers\HasMiddleware; @@ -381,9 +382,23 @@ public function constanciasSustituidas(Request $request) ->orderBy('created_at', 'asc') ->first(); - // Consultar REPUVE Nacional para obtener el folio_CI que se sustituyó - $repuveData = $this->repuveService->consultarVehiculo($log->vehicle->niv, $log->vehicle->placa); - $folioCi = $repuveData['folio_CI'] ?? ''; + // Usar folio_anterior guardado; si no existe, consultar RepuveService + $folioAnt = $log->folio_anterior; + if ($folioAnt === null) { + Log::info('[constanciasSustituidas] Registro histórico sin folio_anterior, consultando RepuveService', [ + 'log_id' => $log->id, + 'vehicle_id'=> $log->vehicle_id, + 'niv' => $log->vehicle->niv, + 'placa' => $log->vehicle->placa, + ]); + $repuveData = $this->repuveService->consultarVehiculo($log->vehicle->niv, $log->vehicle->placa); + $folioAnt = $repuveData['folio_CI'] ?? ''; + Log::info('[constanciasSustituidas] Respuesta RepuveService', [ + 'log_id' => $log->id, + 'folio_CI' => $folioAnt, + 'repuve_has_error' => $repuveData['has_error'] ?? false, + ]); + } return [ 'niv' => $log->vehicle->niv ?? '', @@ -391,7 +406,7 @@ public function constanciasSustituidas(Request $request) 'marca' => $log->vehicle->marca ?? '', 'placa' => $log->vehicle->placa ?? '', 'modelo' => $log->vehicle->modelo ?? '', - 'folio_ant' => $folioCi, + 'folio_ant' => $folioAnt, 'folio_act' => $newTagLog?->tag?->folio ?? '', 'chip' => substr($newTagLog?->tag?->rfid ?? $newTagLog?->tag?->tag_number ?? '', 0, 24), 'fecha' => $log->cancellation_at?->format('d/m/Y') ?? $log->created_at->format('d/m/Y'), diff --git a/app/Http/Controllers/Repuve/UpdateController.php b/app/Http/Controllers/Repuve/UpdateController.php index 62b6f11..9098ba0 100644 --- a/app/Http/Controllers/Repuve/UpdateController.php +++ b/app/Http/Controllers/Repuve/UpdateController.php @@ -150,6 +150,10 @@ public function tagSubstitution(Request $request) ]); } + // Obtener folio_CI de REPUVE antes de cancelar (folio anterior) + $repuveData = $this->repuveService->consultarVehiculo($vehicle->niv, $vehicle->placa); + $folioAnterior = $repuveData['folio_CI'] ?? null; + DB::beginTransaction(); // Cancelar el TAG anterior @@ -159,6 +163,7 @@ public function tagSubstitution(Request $request) VehicleTagLog::create([ 'vehicle_id' => $vehicle->id, 'tag_id' => $oldTag->id, + 'folio_anterior' => $folioAnterior, 'new_tag_folio' => $newTagFolio, 'new_tag_number' => $newTagNumber, 'action_type' => 'sustitucion', diff --git a/app/Models/VehicleTagLog.php b/app/Models/VehicleTagLog.php index c02096e..720aaea 100644 --- a/app/Models/VehicleTagLog.php +++ b/app/Models/VehicleTagLog.php @@ -14,6 +14,7 @@ class VehicleTagLog extends Model protected $fillable = [ 'vehicle_id', 'tag_id', + 'folio_anterior', 'action_type', 'cancellation_reason_id', 'cancellation_observations', diff --git a/database/migrations/2026_03_25_094012_add_folio_anterior_to_vehicle_tags_logs_table.php b/database/migrations/2026_03_25_094012_add_folio_anterior_to_vehicle_tags_logs_table.php new file mode 100644 index 0000000..c43febe --- /dev/null +++ b/database/migrations/2026_03_25_094012_add_folio_anterior_to_vehicle_tags_logs_table.php @@ -0,0 +1,28 @@ +string('folio_anterior')->nullable()->after('tag_id'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('vehicle_tags_logs', function (Blueprint $table) { + $table->dropColumn('folio_anterior'); + }); + } +};