diff --git a/app/Http/Controllers/Repuve/CancellationController.php b/app/Http/Controllers/Repuve/CancellationController.php index dd4862f..e0be38e 100644 --- a/app/Http/Controllers/Repuve/CancellationController.php +++ b/app/Http/Controllers/Repuve/CancellationController.php @@ -4,7 +4,7 @@ use App\Http\Controllers\Controller; use App\Http\Requests\Repuve\CancelConstanciaRequest; -use App\Models\CatalogCancellationReason; +use Illuminate\Validation\ValidationException; use App\Models\Record; use App\Models\Tag; use App\Models\TagCancellationLog; @@ -287,9 +287,18 @@ public function cancelarTagNoAsignado(Request $request) 'cancelled_by' => Auth::id(), ]); - // Cargar las relaciones necesarias + // Cargar las relaciones necesarias ANTES de usarlas $cancellationLog->load(['cancellationReason', 'cancelledBy']); + // Validar que la relación se cargó correctamente + if (!$cancellationLog->cancellationReason) { + DB::rollBack(); + return ApiResponse::INTERNAL_ERROR->response([ + 'message' => 'Error al cargar el motivo de cancelación.', + 'error' => 'La relación cancellationReason no se pudo cargar correctamente.', + ]); + } + // Actualizar el módulo del tag si se proporciona if ($request->filled('module_id')) { $tag->module_id = $request->module_id; @@ -301,31 +310,31 @@ public function cancelarTagNoAsignado(Request $request) DB::commit(); + // Recargar el tag con sus relaciones actualizadas + $tag->load(['status', 'cancellationLogs.cancellationReason', 'cancellationLogs.cancelledBy', 'module']); + + // Obtener datos de cancelación del último log + $lastCancellation = $tag->cancellationLogs() + ->with(['cancellationReason', 'cancelledBy']) + ->latest() + ->first(); + + // Preparar datos para el PDF + $cancellationData = [ + 'fecha' => $lastCancellation ? $lastCancellation->cancellation_at->format('d/m/Y') : now()->format('d/m/Y'), + 'folio' => $tag->folio ?? '', + 'tag_number' => $tag->tag_number ?? 'N/A', + 'motivo' => $lastCancellation && $lastCancellation->cancellationReason + ? $lastCancellation->cancellationReason->name + : 'No especificado', + 'operador' => $lastCancellation && $lastCancellation->cancelledBy + ? $lastCancellation->cancelledBy->name + : 'Sistema', + 'modulo' => $tag->module ? $tag->module->name : 'No especificado', + 'ubicacion' => $tag->module ? $tag->module->address : 'No especificado', + ]; + try { - // Recargar el tag con sus relaciones actualizadas - $tag->load(['status', 'cancellationLogs.cancellationReason', 'cancellationLogs.cancelledBy', 'module']); - - // Obtener datos de cancelación del último log - $lastCancellation = $tag->cancellationLogs() - ->with(['cancellationReason', 'cancelledBy']) - ->latest() - ->first(); - - // Preparar datos para el PDF - $cancellationData = [ - 'fecha' => $lastCancellation ? $lastCancellation->cancellation_at->format('d/m/Y') : now()->format('d/m/Y'), - 'folio' => $tag->folio ?? '', - 'tag_number' => $tag->tag_number ?? 'N/A', - 'motivo' => $lastCancellation && $lastCancellation->cancellationReason - ? $lastCancellation->cancellationReason->name - : 'No especificado', - 'operador' => $lastCancellation && $lastCancellation->cancelledBy - ? $lastCancellation->cancelledBy->name - : 'Sistema', - 'modulo' => $tag->module ? $tag->module->name : 'No especificado', - 'ubicacion' => $tag->module ? $tag->module->address : 'No especificado', - ]; - $pdf = Pdf::loadView('pdfs.tag', [ 'cancellation' => $cancellationData, ]) @@ -338,32 +347,23 @@ public function cancelarTagNoAsignado(Request $request) return $pdf->stream('constancia_cancelada_' . ($tag->tag_number ?? $tag->folio) . '.pdf'); } catch (\Exception $e) { - // Si falla la generación del PDF, devolver respuesta JSON - Log::error('Error al generar PDF de tag cancelado: ' . $e->getMessage()); - - return ApiResponse::OK->response([ - 'message' => 'Tag cancelado exitosamente (Error al generar PDF)', - 'tag' => [ - 'id' => $tag->id, - 'tag_number' => $tag->tag_number, - 'folio' => $tag->folio, - 'previous_status' => 'Disponible', - 'new_status' => 'Dañado', - ], - 'cancellation' => [ - 'id' => $cancellationLog->id, - 'reason' => $cancellationLog->cancellationReason->name, - 'observations' => $cancellationLog->cancellation_observations, - 'cancelled_at' => $cancellationLog->cancellation_at->toDateTimeString(), - 'cancelled_by' => Auth::user()->name, - ], - 'pdf_error' => $e->getMessage(), + // Retornar error como JSON + return ApiResponse::INTERNAL_ERROR->response([ + 'message' => 'Tag cancelado pero error al generar PDF', + 'error' => $e->getMessage(), ]); } + + } catch (ValidationException $e) { + // Errores de validación + return ApiResponse::BAD_REQUEST->response([ + 'message' => 'Error de validación', + 'errors' => $e->errors(), + ]); + } catch (\Exception $e) { DB::rollBack(); - - return ApiResponse::BAD_REQUEST->response([ + return ApiResponse::INTERNAL_ERROR->response([ 'message' => 'Error al cancelar el tag', 'error' => $e->getMessage(), ]);