Refactor invoice processing logic to remove legacy returned quantity fields and enhance data handling

- Updated invoice processing functions to eliminate legacy fields related to returned quantities, now relying on new balance movement and discharge records.
- Adjusted various components and services to reflect changes in quantity handling, including updates to the frontend for displaying used quantities instead of returned ones.
- Improved the logic for invoice total updates and validation processes to ensure consistency with the new data structure.

These changes aim to streamline invoice management and improve data integrity across the application.
This commit is contained in:
2026-03-20 11:42:18 -05:00
parent a2ad6d782d
commit 54c137c156
26 changed files with 880 additions and 101 deletions

View File

@@ -228,6 +228,7 @@ BASE_SELECT = """
COALESCE(cl.material_key,'') AS "C42",
CONCAT(ped.year,'-',ped.customs_office,'-',ped.license,'-',ped.pedimento_number) AS "C43",
COALESCE(ptc.payment_date_code, 'P') AS "C44",
COALESCE(lm.last_movement_type, '') AS "C_last_movement_type",
COALESCE(ilc.octave_fraction,'') AS "C45",
'' AS "C47",
COALESCE(ped.pedimento_code,'') AS "C48",
@@ -256,19 +257,63 @@ BASE_JOINS = """
LEFT JOIN (
SELECT DISTINCT ON (import_item_line_id)
import_item_line_id,
quantity AS qty_impo,
value_me AS val_me_impo,
value_mn AS val_mn_impo
movement_type AS last_movement_type
FROM a24.balance_movement
WHERE movement_type = 'entry' AND tenant_id = :tenant_id
WHERE tenant_id = :tenant_id
ORDER BY import_item_line_id, id DESC
) lm ON lm.import_item_line_id = il.id
LEFT JOIN (
SELECT
import_item_line_id,
SUM(
CASE
WHEN movement_type = 'entry' THEN quantity
WHEN movement_type = 'entry_void' THEN -1 * quantity
ELSE 0
END
) AS qty_impo,
SUM(
CASE
WHEN movement_type = 'entry' THEN COALESCE(value_me, 0)
WHEN movement_type = 'entry_void' THEN -1 * COALESCE(value_me, 0)
ELSE 0
END
) AS val_me_impo,
SUM(
CASE
WHEN movement_type = 'entry' THEN COALESCE(value_mn, 0)
WHEN movement_type = 'entry_void' THEN -1 * COALESCE(value_mn, 0)
ELSE 0
END
) AS val_mn_impo
FROM a24.balance_movement
WHERE tenant_id = :tenant_id
GROUP BY import_item_line_id
) ent ON ent.import_item_line_id = il.id
LEFT JOIN (
SELECT
import_item_line_id,
SUM(CASE WHEN movement_type IN ('consumption', 'waste', 'scrap', 'destruction') THEN quantity ELSE 0 END) as qty_used,
SUM(CASE WHEN movement_type IN ('consumption', 'waste', 'scrap', 'destruction') THEN value_me ELSE 0 END) as val_me_used,
SUM(CASE WHEN movement_type IN ('consumption', 'waste', 'scrap', 'destruction') THEN value_mn ELSE 0 END) as val_mn_used,
SUM(
CASE
WHEN movement_type IN ('consumption', 'waste', 'scrap', 'destruction') THEN quantity
WHEN movement_type = 'return' THEN -1 * quantity
ELSE 0
END
) as qty_used,
SUM(
CASE
WHEN movement_type IN ('consumption', 'waste', 'scrap', 'destruction') THEN COALESCE(value_me, 0)
WHEN movement_type = 'return' THEN -1 * COALESCE(value_me, 0)
ELSE 0
END
) as val_me_used,
SUM(
CASE
WHEN movement_type IN ('consumption', 'waste', 'scrap', 'destruction') THEN COALESCE(value_mn, 0)
WHEN movement_type = 'return' THEN -1 * COALESCE(value_mn, 0)
ELSE 0
END
) as val_mn_used,
SUM(CASE WHEN movement_type IN ('consumption', 'waste', 'scrap', 'destruction', 'neg_adj', 'transfer_out', 'expiration', 'regime_chg_out', 'entry_void')
THEN -1 * quantity ELSE quantity END) as qty_balance
FROM a24.balance_movement
@@ -462,6 +507,10 @@ def _build_row(
cant_used = _d(row.get("C18"))
cant_saldo = _d(row.get("C_balance"))
# Mostrar saldo 0, excepto lotes anulados (último movimiento ENTRY_VOID).
if str(row.get("C_last_movement_type") or "").lower() == "entry_void":
return None
if filters.omit_low_balance and cant_saldo <= Decimal(0):
return None