Skip to content

Commit 34f50d9

Browse files
Merge pull request #937 from cordada/task/sc-17311--lib-cl-sii-python-error-en-parseo-de-rcv
rcv: Refactor grouping logic for RCV parsing
2 parents 7e0486f + 9898051 commit 34f50d9

File tree

3 files changed

+586
-127
lines changed

3 files changed

+586
-127
lines changed

src/cl_sii/rcv/parse_csv.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,20 +1590,22 @@ def _parse_rcv_csv_file(
15901590
expected_field_names=expected_input_field_names,
15911591
)
15921592

1593-
# Group rows by folio and handle "Otros Impuestos" logic
1594-
folio_groups: MutableMapping[str, Any] = {}
1593+
# Group rows by 'folio + tipo docto + rut' to handle "Otros Impuestos" logic
1594+
entry_key_groups: MutableMapping[str, Any] = {}
15951595

1596-
# Otros Impuestos field names
1596+
# Field names differ slightly between RCV Venta and RCV Compra
15971597
if isinstance(input_csv_row_schema, RcvVentaCsvRowSchema):
15981598
codigo_otro_impuesto_key = "Codigo Otro Imp."
15991599
valor_otro_impuesto_key = "Valor Otro Imp."
16001600
tasa_otro_impuesto_key = "Tasa Otro Imp."
1601+
rut_key = "Rut cliente"
16011602
else:
16021603
codigo_otro_impuesto_key = "Codigo Otro Impuesto"
16031604
valor_otro_impuesto_key = "Valor Otro Impuesto"
16041605
tasa_otro_impuesto_key = "Tasa Otro Impuesto"
1606+
rut_key = "Rut Proveedor"
16051607

1606-
# First pass: collect all rows and group by folio
1608+
# First pass: collect all rows and group by folio + tipo docto + rut
16071609
for row_ix, row_data in enumerate(csv_reader, start=1):
16081610
if max_n_rows is not None and row_ix > max_n_rows + n_rows_offset:
16091611
raise rows_processing.MaxRowsExceeded(f"Exceeded 'max_n_rows' limit: {max_n_rows}.")
@@ -1615,13 +1617,17 @@ def _parse_rcv_csv_file(
16151617
row_data.pop(_field_name, None)
16161618

16171619
folio = str(row_data.get('Folio')) if row_data.get('Folio') is not None else ''
1620+
tipo_docto = row_data.get('Tipo Doc')
1621+
rut = row_data.get(rut_key)
16181622

1619-
# If both fields are None, it's an "otros impuestos" row
1623+
# Concatenate folio, tipo_docto, and rut to create unique entry key
1624+
entry_key = f"{folio}_{tipo_docto}_{rut}"
1625+
1626+
# If Nro, Fecha Recepcion, and Monto Total are all None or empty,
1627+
# it's an "otros impuestos" row
16201628
is_main = not (
1621-
(
1622-
row_data.get("Nro") in (None, "")
1623-
and row_data.get("Fecha Recepcion") in (None, "")
1624-
)
1629+
row_data.get("Nro") in (None, "")
1630+
and row_data.get("Fecha Recepcion") in (None, "")
16251631
and (row_data.get("Monto Total") in (None, ""))
16261632
)
16271633
otros_impuestos_data = {
@@ -1642,17 +1648,21 @@ def _parse_rcv_csv_file(
16421648
),
16431649
}
16441650

1645-
if folio not in folio_groups:
1646-
folio_groups[folio] = {
1651+
if entry_key not in entry_key_groups:
1652+
entry_key_groups[entry_key] = {
16471653
'row': (row_ix, row_data),
16481654
'otros_impuestos': [otros_impuestos_data],
16491655
}
1650-
if not is_main and folio in folio_groups:
1656+
elif is_main:
1657+
# Update main row if this is a main entry
1658+
entry_key_groups[entry_key]['row'] = (row_ix, row_data)
1659+
entry_key_groups[entry_key]['otros_impuestos'].insert(0, otros_impuestos_data)
1660+
elif not is_main:
16511661
if any(otros_impuestos_data.values()):
1652-
folio_groups[folio]['otros_impuestos'].append(otros_impuestos_data)
1662+
entry_key_groups[entry_key]['otros_impuestos'].append(otros_impuestos_data)
16531663

16541664
# Second pass: yield grouped rows
1655-
for folio, group in folio_groups.items():
1665+
for folio, group in entry_key_groups.items():
16561666
if group['row']:
16571667
row_ix, row_data = group['row']
16581668
row_data['Otros Impuestos'] = group['otros_impuestos']

src/tests/test_data/sii-rcv/RCV-venta-extra-empty-impuestos-rows.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ Nro;Tipo Doc;Tipo Venta;Rut cliente;Razon Social;Folio;Fecha Docto;Fecha Recepci
33
;33;Del Giro;54213736-3;CHILE SPA;6541;01/09/2025;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;271;701395;18;;;;;;
44
2;33;Del Giro;42509414-9;COMERCIAL SPA;9874;01/09/2025;01/09/2025 09:53:17;;;0;8879040;1687018;13136156;0;0;0;0;0;-;0;0;0;0;0;;;;0;;0;2;0;0;0;;;;12354;;24;2570098;31.5
55
3;33;Del Giro;68840666-8;TEXAS SPA;3210;01/09/2025;01/09/2025 10:58:51;08/09/2025 14:15:23;;0;20522880;3899347;30471437;0;0;0;0;0;-;0;0;0;0;0;;;;0;;0;2;0;0;0;;;;12354;;24;6049210;31.5;
6+
4;34;Del Giro;68840666-8;TEXAS SPA;3210;01/09/2025;01/09/2025 10:58:51;08/09/2025 14:15:23;;0;9999;3899347;30471437;0;0;0;0;0;-;0;0;0;0;0;;;;0;;0;2;0;0;0;;;;12354;;24;6049210;31.5;
7+
5;33;Del Giro;54213736-3;THE COMPANY SPA;3210;01/09/2025;01/09/2025 10:58:51;08/09/2025 14:15:23;;0;9999;3899347;30471437;0;0;0;0;0;-;0;0;0;0;0;;;;0;;0;2;0;0;0;;;;12354;;24;6049210;31.5;
8+
;33;Del Giro;54213736-3;THE COMPANY SPA;3210;01/09/2025;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;271;701395;18;;;;;;

0 commit comments

Comments
 (0)