feat(cobranza): estado de cuenta PDF por cliente (Fase 4)
Botón '📄 Estado de cuenta' en cada cliente de la vista Cuentas abre GET /api/estado-cuenta.pdf?cliente=... — PDF ReportLab con dos secciones: SALDOS VIGENTES (subcuentas activas: capital, mora, abonado, saldo, total) y MOVIMIENTOS (últimos X de la bitácora, X = movimientos_visibles de la config o ?limite=N). Funciona también para clientes ya saldados (sin renglones, solo historial). Generador en src/services/estado_cuenta.py, tests validan el contenido extrayendo texto con pdfplumber. 136 tests en verde. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
8c35461761
commit
1649e2f732
4 files changed
+351
-6
No files matched your search
+52
-2
@@ -14,10 +14,17 @@ from datetime import date, datetime
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Query
|
||||
from fastapi.responses import HTMLResponse
|
||||
from fastapi.responses import HTMLResponse, Response
|
||||
from pydantic import BaseModel
|
||||
|
||||
from src.services import cobranza_acumulada, pagos, recordatorios, sheets, whatsapp
|
||||
from src.services import (
|
||||
cobranza_acumulada,
|
||||
estado_cuenta,
|
||||
pagos,
|
||||
recordatorios,
|
||||
sheets,
|
||||
whatsapp,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -505,6 +512,49 @@ async def movimientos(
|
||||
return {"movimientos": movs, "visibles": visibles}
|
||||
|
||||
|
||||
@router.get("/api/estado-cuenta.pdf")
|
||||
async def estado_cuenta_pdf(
|
||||
token: str = Query(...),
|
||||
cliente: str = Query(...),
|
||||
limite: int | None = Query(default=None, ge=1, le=cobranza_acumulada.MOV_MAX),
|
||||
):
|
||||
"""Estado de cuenta PDF de un cliente: saldos vigentes por subcuenta +
|
||||
últimos X movimientos de la bitácora (X = config `movimientos_visibles`
|
||||
o el `limite` pedido)."""
|
||||
numero = await _numero_token(token)
|
||||
objetivo = cobranza_acumulada._norm(cliente)
|
||||
renglones = [
|
||||
r for r in await cobranza_acumulada.listar(numero)
|
||||
if cobranza_acumulada._norm(r.get("cliente") or "") == objetivo
|
||||
]
|
||||
cfg = await cobranza_acumulada.obtener_config_global()
|
||||
visibles = (
|
||||
limite or cfg.get("movimientos_visibles")
|
||||
or cobranza_acumulada.MOV_DEFAULT_VISIBLES
|
||||
)
|
||||
movs = await cobranza_acumulada.obtener_movimientos(
|
||||
numero, cliente=cliente, limite=visibles,
|
||||
)
|
||||
if not renglones and not movs:
|
||||
raise HTTPException(status_code=404, detail="Sin datos de ese cliente")
|
||||
nombre = renglones[0].get("cliente") if renglones else (
|
||||
movs[0].get("cliente") or cliente
|
||||
)
|
||||
pdf = estado_cuenta.generar_pdf(
|
||||
cliente=nombre, renglones=renglones, movimientos=movs,
|
||||
visibles=visibles,
|
||||
)
|
||||
slug = re.sub(r"[^A-Za-z0-9]+", "-", cliente).strip("-").lower() or "cliente"
|
||||
return Response(
|
||||
content=pdf,
|
||||
media_type="application/pdf",
|
||||
headers={
|
||||
"Content-Disposition":
|
||||
f'inline; filename="estado-cuenta-{slug}.pdf"',
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@router.put("/api/renglon")
|
||||
async def editar(body: RenglonEditBody, token: str = Query(...)):
|
||||
numero = await _numero_token(token)
|
||||
|
||||
Reference in new issue
Block a user