diff --git a/backend/api/v1/modules/crm/quotes/routes.py b/backend/api/v1/modules/crm/quotes/routes.py index a5b38d5..7693f01 100644 --- a/backend/api/v1/modules/crm/quotes/routes.py +++ b/backend/api/v1/modules/crm/quotes/routes.py @@ -1,4 +1,4 @@ -from fastapi import APIRouter, Depends, File, Query, UploadFile, status +from fastapi import APIRouter, Depends, File, Query, Response, UploadFile, status from sqlalchemy.orm import Session from core.database import get_core_db @@ -152,16 +152,23 @@ def reject_quote( return service.reject_quote(db, quote_id, current_user["tenant_id"], company_id) -@router.get("/quotes/{quote_id}/pdf-url") -def quote_pdf_url( +@router.get("/quotes/{quote_id}/pdf") +def quote_pdf( quote_id: int, company_id: int = Query(..., description="Company ID"), current_user: dict = Depends(get_current_user), db: Session = Depends(get_core_db), ): - """Genera el PDF de la cotización (formato maestro + marca) y devuelve una URL.""" - url = pdf_service.get_pdf_url(db, quote_id, current_user["tenant_id"], company_id) - return {"url": url} + """Devuelve el PDF de la cotización directamente (vía backend, sin exponer MinIO).""" + tenant_id = current_user["tenant_id"] + quote = service.get_quote(db, quote_id, tenant_id, company_id) + pdf_bytes = pdf_service.build_pdf_bytes(db, quote, tenant_id, company_id) + ref = (quote.reference or f"cot-{quote.id}").replace("/", "-") + return Response( + content=pdf_bytes, + media_type="application/pdf", + headers={"Content-Disposition": f'inline; filename="cotizacion-{ref}.pdf"'}, + ) @router.post("/quotes/{quote_id}/send-email") diff --git a/frontend/src/lib/api/crm/commercial.ts b/frontend/src/lib/api/crm/commercial.ts index 37bd255..e190fde 100644 --- a/frontend/src/lib/api/crm/commercial.ts +++ b/frontend/src/lib/api/crm/commercial.ts @@ -158,7 +158,7 @@ export const quotesAPI = { clone: (id: number, companyId: number) => unwrap(api.post(`/v1/crm/quotes/${id}/clone?${qp(companyId)}`, {})), remove: (id: number, companyId: number) => unwrap(api.delete(`/v1/crm/quotes/${id}?${qp(companyId)}`)), items: (quoteId: number, companyId: number) => unwrap(api.get(`/v1/crm/quotes/${quoteId}/items?${qp(companyId)}`)), - pdfUrl: (id: number, companyId: number) => unwrap<{ url: string }>(api.get(`/v1/crm/quotes/${id}/pdf-url?${qp(companyId)}`)), + pdfBlob: (id: number, companyId: number) => (api as any).getBlob(`/v1/crm/quotes/${id}/pdf?${qp(companyId)}`) as Promise, sendEmail: (id: number, companyId: number, body: { to?: string | null; subject?: string | null; message?: string | null }) => unwrap<{ sent_to: string; reference: string }>(api.post(`/v1/crm/quotes/${id}/send-email?${qp(companyId)}`, body)) }; diff --git a/frontend/src/routes/dashboard/crm/cotizaciones/[id]/+page.svelte b/frontend/src/routes/dashboard/crm/cotizaciones/[id]/+page.svelte index 585d5c6..6573670 100644 --- a/frontend/src/routes/dashboard/crm/cotizaciones/[id]/+page.svelte +++ b/frontend/src/routes/dashboard/crm/cotizaciones/[id]/+page.svelte @@ -146,8 +146,10 @@ if (!companyId || !quote) return; busy = true; try { - const { url } = await quotesAPI.pdfUrl(quote.id, companyId); - if (url) window.open(url, '_blank', 'noopener'); + const blob = await quotesAPI.pdfBlob(quote.id, companyId); + const url = URL.createObjectURL(blob); + window.open(url, '_blank', 'noopener'); + setTimeout(() => URL.revokeObjectURL(url), 60000); } catch (e) { toast.error(e instanceof Error ? e.message : 'No se pudo generar el PDF'); } finally {