El estado de cuenta usa la hoja membretada completa (logo, dirección, marca de agua) extraída en runtime de Plantilla_WK2.docm — hereda automáticamente cualquier cambio de branding de la plantilla. Título solo 'ESTADO DE CUENTA' (el logo ya da la marca), tablas en el navy del logotipo, márgenes ajustados a las esquinas del membrete. Si la plantilla no está, cae al encabezado de texto sin tronar. 140 tests. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
153 lines
6.4 KiB
Python
153 lines
6.4 KiB
Python
"""
|
||
Estado de cuenta PDF: saldos vigentes por subcuenta + últimos X
|
||
movimientos de la bitácora. Validamos el contenido extrayendo el texto
|
||
con pdfplumber.
|
||
"""
|
||
import io
|
||
import os
|
||
|
||
os.environ.setdefault("OPENAI_API_KEY", "test-key-no-usada")
|
||
|
||
import pdfplumber
|
||
|
||
from src.services import estado_cuenta
|
||
|
||
RENGLONES = [
|
||
{"tipo": "renta", "cliente": "GRUPO MEENT", "vehiculo": "HILUX",
|
||
"capital": 20000.0, "moratorio": 312.40, "abonado": 6000.0,
|
||
"saldo": 14312.40, "fecha_alta": "2026-06-01",
|
||
"periodos": [
|
||
{"periodo": "2026-07", "mensualidad": 10000.0,
|
||
"fecha_vencimiento": "2026-07-05"},
|
||
{"periodo": "2026-08", "mensualidad": 10000.0,
|
||
"fecha_vencimiento": "2026-08-05"},
|
||
]},
|
||
{"tipo": "vr", "cliente": "GRUPO MEENT", "vehiculo": "EXCAVADORA",
|
||
"capital": 171572.70, "moratorio": 201.76, "abonado": 0.0,
|
||
"saldo": 171774.46, "fecha_vencimiento": "2026-08-10",
|
||
"fecha_alta": "2026-08-01"},
|
||
]
|
||
|
||
MOVIMIENTOS = [
|
||
{"fecha": "2026-08-01", "cliente": "GRUPO MEENT", "vehiculo": "HILUX",
|
||
"tipo": "renta", "movimiento": "abono", "monto": 6000.0,
|
||
"nota": "transferencia BBVA", "capital": 20000.0, "abonado": 6000.0,
|
||
"fecha_alta": "2026-06-01", "saldo_despues": 14312.40},
|
||
# Sin saldo_despues (movimiento previo al campo): se aproxima con
|
||
# capital − abonado del snapshot.
|
||
{"fecha": "2026-07-15", "cliente": "GRUPO MEENT", "vehiculo": "HILUX",
|
||
"tipo": "renta", "movimiento": "saldo_periodo", "monto": 10000.0,
|
||
"nota": "mes 2026-06", "capital": 30000.0, "abonado": 12500.0,
|
||
"fecha_alta": "2026-06-01"},
|
||
]
|
||
|
||
|
||
def _texto(pdf_bytes: bytes) -> str:
|
||
with pdfplumber.open(io.BytesIO(pdf_bytes)) as pdf:
|
||
return "\n".join(p.extract_text() or "" for p in pdf.pages)
|
||
|
||
|
||
def test_pdf_contiene_saldos_y_movimientos():
|
||
pdf = estado_cuenta.generar_pdf(
|
||
cliente="GRUPO MEENT", renglones=RENGLONES,
|
||
movimientos=MOVIMIENTOS, visibles=10,
|
||
)
|
||
assert pdf.startswith(b"%PDF")
|
||
txt = _texto(pdf)
|
||
assert "ESTADO DE CUENTA" in txt
|
||
assert "GRUPO MEENT" in txt
|
||
# Saldos vigentes: subcuentas, montos y total.
|
||
assert "HILUX" in txt and "EXCAVADORA" in txt
|
||
assert "Renta mensual" in txt and "Valor residual" in txt
|
||
assert "$14,312.40" in txt and "$171,774.46" in txt
|
||
assert "$186,086.86" in txt # TOTAL = suma de saldos
|
||
# Vencimiento de cada obligación: renta = periodo más antiguo pendiente;
|
||
# vr = su fecha_vencimiento.
|
||
assert "Vencimiento" in txt
|
||
assert "05/07/2026" in txt and "10/08/2026" in txt
|
||
# Movimientos: fecha, etiqueta, nota y snapshot de la subcuenta
|
||
# (originación, capital, abonado).
|
||
assert "MOVIMIENTOS" in txt and "01/08/2026" in txt
|
||
assert "Abono" in txt and "Mes saldado" in txt
|
||
assert "transferencia BBVA" in txt
|
||
assert "$6,000.00" in txt
|
||
assert "(01/06/2026)" in txt # originación en la subcuenta
|
||
assert "Originación" not in txt # ya no es columna aparte
|
||
assert "$20,000.00" in txt # capital de la subcuenta
|
||
# Saldo previo (con mora) y saldo después: la fila cuadra
|
||
# previo − monto = después. Primero: 20,312.40 − 6,000 = 14,312.40.
|
||
# Viejo sin campo: aproximado (30,000 − 12,500 = 17,500 después;
|
||
# previo = 17,500 + 10,000 = 27,500).
|
||
assert "Saldo previo" in txt and "Saldo después" in txt
|
||
assert "Capital" in txt # sigue en SALDOS VIGENTES
|
||
assert "$20,312.40" in txt and "$14,312.40" in txt
|
||
assert "$27,500.00" in txt and "$17,500.00" in txt
|
||
|
||
|
||
def test_pdf_sin_renglones_ni_movs_avisa():
|
||
# Cliente saldado por completo: sin subcuentas activas ni bitácora aún.
|
||
pdf = estado_cuenta.generar_pdf(
|
||
cliente="ACME", renglones=[], movimientos=[], visibles=10,
|
||
)
|
||
txt = _texto(pdf)
|
||
assert "Sin saldos vigentes" in txt
|
||
assert "Sin movimientos registrados" in txt
|
||
|
||
|
||
def test_pdf_solo_movimientos_de_cliente_saldado():
|
||
# El renglón se borró al saldarse, pero la bitácora sobrevive: el
|
||
# estado de cuenta sigue mostrando el historial. Sin snapshot ni
|
||
# renglón activo, las columnas de contexto salen como '—'.
|
||
movs = [{"fecha": "2026-08-01", "cliente": "ACME", "vehiculo": "GT3",
|
||
"tipo": "vr", "movimiento": "saldado", "monto": 500000.0,
|
||
"nota": "pago total"}]
|
||
pdf = estado_cuenta.generar_pdf(
|
||
cliente="ACME", renglones=[], movimientos=movs, visibles=5,
|
||
)
|
||
txt = _texto(pdf)
|
||
assert "Sin saldos vigentes" in txt
|
||
assert "Saldado" in txt and "$500,000.00" in txt
|
||
assert "(últimos 5)" in txt
|
||
assert "—" in txt
|
||
|
||
|
||
def test_pdf_con_membrete_y_sin_plantilla(monkeypatch, tmp_path):
|
||
# Con la plantilla presente (repo) el PDF lleva el membrete W+k y el
|
||
# título es solo 'ESTADO DE CUENTA' (el logo ya da la marca).
|
||
pdf = estado_cuenta.generar_pdf(
|
||
cliente="ACME", renglones=RENGLONES, movimientos=[], visibles=10,
|
||
)
|
||
txt = _texto(pdf)
|
||
assert "ESTADO DE CUENTA" in txt
|
||
assert "WK FINANCING SOLUTIONS" not in txt # lo pone el logo, no texto
|
||
assert len(pdf) > 100_000 # trae el jpeg del membrete
|
||
|
||
# Sin plantilla (deploy sin el .docm): NO truena — cae al encabezado
|
||
# de texto de siempre.
|
||
monkeypatch.setattr(estado_cuenta, "TEMPLATE_PATH",
|
||
tmp_path / "no-existe.docm")
|
||
monkeypatch.setattr(estado_cuenta, "_membrete_cache", None)
|
||
monkeypatch.setattr(estado_cuenta, "_membrete_probado", False)
|
||
pdf2 = estado_cuenta.generar_pdf(
|
||
cliente="ACME", renglones=RENGLONES, movimientos=[], visibles=10,
|
||
)
|
||
txt2 = _texto(pdf2)
|
||
assert "WK FINANCING SOLUTIONS" in txt2 # encabezado de texto
|
||
# monkeypatch restaura TEMPLATE_PATH y el cache al salir del test.
|
||
|
||
|
||
def test_pdf_movimiento_viejo_toma_contexto_del_renglon_activo():
|
||
# Movimientos registrados ANTES del snapshot (sin capital/abonado/
|
||
# fecha_alta) heredan el contexto del renglón activo que coincide.
|
||
movs = [{"fecha": "2026-08-03", "cliente": "GRUPO MEENT",
|
||
"vehiculo": "EXCAVADORA", "tipo": "vr",
|
||
"movimiento": "abono", "monto": 25.0, "nota": ""}]
|
||
pdf = estado_cuenta.generar_pdf(
|
||
cliente="GRUPO MEENT", renglones=RENGLONES, movimientos=movs,
|
||
visibles=10,
|
||
)
|
||
txt = _texto(pdf)
|
||
# Toma capital y fecha_alta del renglón EXCAVADORA·vr activo.
|
||
assert "$171,572.70" in txt
|
||
assert "01/08/2026" in txt
|