From 6db44f502fcccbc12b4029f3368ed08d2157f33e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorem=20Ju=C3=A1rez?= Date: Mon, 29 Jun 2026 12:21:48 -0600 Subject: [PATCH] fix(cobranza): ordena VR y extraordinarias por fecha de vencimiento MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Las secciones de Valor Residual y Rentas Extraordinarias del reporte se agregaban en orden de fila del Sheet. Ahora se ordenan por su fecha (fecha_vr / fecha_extra) y alfabético por cliente en empate. Aplica a todos los días. Solo ordenamiento; montos, totales y filas intactos. tests/test_reporte_orden.py: verifica el orden por fecha de VR y extras. Co-Authored-By: Claude Opus 4.8 (1M context) --- src/services/sheets.py | 4 ++++ tests/test_reporte_orden.py | 42 +++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/services/sheets.py b/src/services/sheets.py index dae3e01..e35047f 100644 --- a/src/services/sheets.py +++ b/src/services/sheets.py @@ -993,6 +993,10 @@ def generar_reporte_cobros( ), }) + # Orden cronológico por fecha de vencimiento (y alfabético en empate). + vr_items.sort(key=lambda v: (v.get("fecha_vr") or date.max, v.get("cliente", ""))) + extras.sort(key=lambda e: (e.get("fecha_extra") or date.max, e.get("cliente", ""))) + return { "fecha": hoy, "es_lunes": es_lunes, diff --git a/tests/test_reporte_orden.py b/tests/test_reporte_orden.py index 7c7571d..683e3dc 100644 --- a/tests/test_reporte_orden.py +++ b/tests/test_reporte_orden.py @@ -54,3 +54,45 @@ def test_lunes_ordena_cronologico(mock_get_client): ("ALPHA", 29), # lunes, alfabético dentro del día ("ZETA", 29), ] + + +def _fila_vr(cliente, fecha_ddmmaaaa, total="100000"): + f = [""] * 23 + f[1] = cliente + f[2] = "VEHICULO" + f[11] = total # _IDX_VR_TOTAL + f[13] = "" # plazo vacío → monto_a_cobrar = total + f[18] = f"({fecha_ddmmaaaa})" # _IDX_FECHAS_VR + return f + + +def _fila_extra(cliente, fecha_ddmmaaaa, monto="$5,000"): + f = [""] * 23 + f[1] = cliente + f[2] = "VEHICULO" + f[16] = monto # _IDX_NOTAS (de donde se extrae el monto) + f[19] = f"({fecha_ddmmaaaa})" # _IDX_FECHAS_EXTRA + return f + + +@patch.object(sheets, "_IDX_TASA_MORATORIA_CACHE", None) +@patch.object(sheets, "_gc", None) +@patch.object(sheets, "_get_client") +def test_vr_y_extras_ordenan_por_fecha(mock_get_client): + rows = [ + HEADER, + _fila_vr("C-VR-30", "30/06/2026"), + _fila_vr("C-VR-15", "15/06/2026"), + _fila_vr("C-VR-17", "17/06/2026"), + _fila_extra("C-EX-22", "22/06/2026"), + _fila_extra("C-EX-05", "05/06/2026"), + ] + mock_get_client.return_value = _mock_gc(rows) + + reporte = sheets.generar_reporte_cobros(hoy=date(2026, 6, 29)) + + vr_fechas = [v["fecha"] for v in reporte["vr"]] + assert vr_fechas == ["15/06/2026", "17/06/2026", "30/06/2026"] + + ex_fechas = [e["fecha"] for e in reporte["extras"]] + assert ex_fechas == ["05/06/2026", "22/06/2026"]