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"]