Actualizar backend/services.py

This commit is contained in:
2026-10-09 16:44:31 -05:00
parent 296226dd25
commit 9d6fd29ebd

View File

@@ -226,6 +226,19 @@ def facturacion(ano="TODOS", mes="TODOS", cuota="TODOS"):
try: return float(v) if v is not None else 0.0 try: return float(v) if v is not None else 0.0
except Exception: return 0.0 except Exception: return 0.0
def _fstr(fch):
"""Fecha a 'dd/mm/yyyy'."""
if fch is None: return ""
try: return fch.strftime("%d/%m/%Y")
except Exception:
s = str(fch)
return f"{s[8:10]}/{s[5:7]}/{s[0:4]}" if len(s) >= 10 else s
def _etiq_comp(comp, fch, deudor):
"""Etiqueta del comprobante: '12/08/2026 - B00100... - JUAN CARLOS'."""
partes = [p for p in (_fstr(fch), str(comp or "").strip(), str(deudor or "").strip()) if p]
return " - ".join(partes) or "(SIN COMPROBANTE)"
# ── Índice de facturas por num_comprobante: programa, matrícula, min cuota ── # ── Índice de facturas por num_comprobante: programa, matrícula, min cuota ──
# (para enlazar NC -> factura). Guardamos el primer programa/matrícula visto # (para enlazar NC -> factura). Guardamos el primer programa/matrícula visto
# y el MIN(num_cuota) del comprobante (num_cuota_NC del Power BI). # y el MIN(num_cuota) del comprobante (num_cuota_NC del Power BI).
@@ -239,6 +252,8 @@ def facturacion(ano="TODOS", mes="TODOS", cuota="TODOS"):
comp_rep[nc] = comp_rep.get(nc, 0) + 1 comp_rep[nc] = comp_rep.get(nc, 0) + 1
g = fact_idx.setdefault(nc, {"prog": f.get("dsc_det_programa") or "", g = fact_idx.setdefault(nc, {"prog": f.get("dsc_det_programa") or "",
"glosa": f.get("dsc_glosa") or "", "glosa": f.get("dsc_glosa") or "",
"deudor": f.get("dsc_deudor") or "",
"fch": f.get("fch_emision"),
"matricula": str(f.get("num_matricula") or "").strip(), "matricula": str(f.get("num_matricula") or "").strip(),
"min_cuota": None}) "min_cuota": None})
cu = f.get("num_cuota") cu = f.get("num_cuota")
@@ -291,7 +306,8 @@ def facturacion(ano="TODOS", mes="TODOS", cuota="TODOS"):
base = _num(f.get("imp_subtotal")) base = _num(f.get("imp_subtotal"))
if str(f.get("cod_moneda") or "").upper() != "SOL": if str(f.get("cod_moneda") or "").upper() != "SOL":
base *= _num(f.get("imp_tc")) base *= _num(f.get("imp_tc"))
acc[(sede, tipo, pnom, comp or "(SIN COMPROBANTE)")]["ing"] += base etiq = _etiq_comp(comp, f.get("fch_emision"), f.get("dsc_deudor"))
acc[(sede, tipo, pnom, etiq)]["ing"] += base
# ── NOTAS DE CRÉDITO ── # ── NOTAS DE CRÉDITO ──
for n in notas: for n in notas:
@@ -318,7 +334,9 @@ def facturacion(ano="TODOS", mes="TODOS", cuota="TODOS"):
base = _num(n.get("imp_subtotal")) base = _num(n.get("imp_subtotal"))
if str(n.get("cod_moneda") or "").upper() != "SOL": if str(n.get("cod_moneda") or "").upper() != "SOL":
base *= _num(n.get("imp_tc")) base *= _num(n.get("imp_tc"))
acc[(sede, tipo, pnom, comp_bf or "(SIN COMPROBANTE)")]["nc"] += base # Etiqueta con fecha/deudor de la factura enlazada (misma línea que el ingreso)
etiq = _etiq_comp(comp_bf, fi.get("fch"), fi.get("deudor"))
acc[(sede, tipo, pnom, etiq)]["nc"] += base
# ── armar jerarquía Sede -> Tipo -> Programa -> Comprobante ── # ── armar jerarquía Sede -> Tipo -> Programa -> Comprobante ──
def _n0(): return {"ing": 0.0, "nc": 0.0} def _n0(): return {"ing": 0.0, "nc": 0.0}