fix: cancelación de constancia pdf dañado
This commit is contained in:
parent
1b2522c4f0
commit
d3bd94e158
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Http\Requests\Repuve\CancelConstanciaRequest;
|
use App\Http\Requests\Repuve\CancelConstanciaRequest;
|
||||||
use App\Models\CatalogCancellationReason;
|
use Illuminate\Validation\ValidationException;
|
||||||
use App\Models\Record;
|
use App\Models\Record;
|
||||||
use App\Models\Tag;
|
use App\Models\Tag;
|
||||||
use App\Models\TagCancellationLog;
|
use App\Models\TagCancellationLog;
|
||||||
@ -287,9 +287,18 @@ public function cancelarTagNoAsignado(Request $request)
|
|||||||
'cancelled_by' => Auth::id(),
|
'cancelled_by' => Auth::id(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Cargar las relaciones necesarias
|
// Cargar las relaciones necesarias ANTES de usarlas
|
||||||
$cancellationLog->load(['cancellationReason', 'cancelledBy']);
|
$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
|
// Actualizar el módulo del tag si se proporciona
|
||||||
if ($request->filled('module_id')) {
|
if ($request->filled('module_id')) {
|
||||||
$tag->module_id = $request->module_id;
|
$tag->module_id = $request->module_id;
|
||||||
@ -301,31 +310,31 @@ public function cancelarTagNoAsignado(Request $request)
|
|||||||
|
|
||||||
DB::commit();
|
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 {
|
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', [
|
$pdf = Pdf::loadView('pdfs.tag', [
|
||||||
'cancellation' => $cancellationData,
|
'cancellation' => $cancellationData,
|
||||||
])
|
])
|
||||||
@ -338,32 +347,23 @@ public function cancelarTagNoAsignado(Request $request)
|
|||||||
|
|
||||||
return $pdf->stream('constancia_cancelada_' . ($tag->tag_number ?? $tag->folio) . '.pdf');
|
return $pdf->stream('constancia_cancelada_' . ($tag->tag_number ?? $tag->folio) . '.pdf');
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
// Si falla la generación del PDF, devolver respuesta JSON
|
// Retornar error como JSON
|
||||||
Log::error('Error al generar PDF de tag cancelado: ' . $e->getMessage());
|
return ApiResponse::INTERNAL_ERROR->response([
|
||||||
|
'message' => 'Tag cancelado pero error al generar PDF',
|
||||||
return ApiResponse::OK->response([
|
'error' => $e->getMessage(),
|
||||||
'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(),
|
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} catch (ValidationException $e) {
|
||||||
|
// Errores de validación
|
||||||
|
return ApiResponse::BAD_REQUEST->response([
|
||||||
|
'message' => 'Error de validación',
|
||||||
|
'errors' => $e->errors(),
|
||||||
|
]);
|
||||||
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
DB::rollBack();
|
DB::rollBack();
|
||||||
|
return ApiResponse::INTERNAL_ERROR->response([
|
||||||
return ApiResponse::BAD_REQUEST->response([
|
|
||||||
'message' => 'Error al cancelar el tag',
|
'message' => 'Error al cancelar el tag',
|
||||||
'error' => $e->getMessage(),
|
'error' => $e->getMessage(),
|
||||||
]);
|
]);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user