Merge pull request 'feature/saldos_temporales_nueva_tabla' (#230) from feature/saldos_temporales_nueva_tabla into development

Reviewed-on: ADUANASOFT/anexo76#230
This commit is contained in:
2026-03-19 16:21:18 +00:00

View File

@@ -202,13 +202,13 @@ BASE_SELECT = """
REPLACE(REPLACE(COALESCE(ild.description_spanish,''),CHR(10),''),CHR(13),' ') AS "C14",
REPLACE(REPLACE(COALESCE(ild.description_english,''),CHR(10),''),CHR(13),' ') AS "C15",
COALESCE(ilc.origin_country,'') AS "C16",
COALESCE(ilq.quantity, 0) AS "C17",
COALESCE(ilq.quantity_returned, 0) AS "C18",
COALESCE(bal.qty_impo, 0) AS "C17",
COALESCE(bal.qty_used, 0) AS "C18",
COALESCE(uom.code,'') AS "C19",
COALESCE(ilf.value_mxn, 0) AS "C20",
COALESCE(ilf.value_returned_mxn, 0) AS "C21",
COALESCE(ilf.value_usd, 0) AS "C22",
COALESCE(ilf.value_returned_usd, 0) AS "C23",
COALESCE(bal.val_mn_impo, 0) AS "C20",
COALESCE(bal.val_mn_used, 0) AS "C21",
COALESCE(bal.val_me_impo, 0) AS "C22",
COALESCE(bal.val_me_used, 0) AS "C23",
COALESCE(ilq.net_weight, 0) AS "C24",
COALESCE(ilc.fraction,'') AS "C26",
COALESCE(ilc.fraction_type,'') AS "C27",
@@ -220,7 +220,7 @@ BASE_SELECT = """
il.id AS "C34",
il.line_number AS "C35",
COALESCE(p.part_number,'') AS "C36",
COALESCE(ilq.quantity_returned_temp, 0) AS "C37",
0 AS "C37",
COALESCE(il.location,'') AS "C38",
'' AS "C39",
COALESCE(icm.edocument,'') AS "C40",
@@ -234,7 +234,8 @@ BASE_SELECT = """
COALESCE(ilc.rate,'') AS "C49",
COALESCE(il.iv32_type_key,'') AS "C50",
COALESCE(il.guide_number,'') AS "C_embarque",
COALESCE(c_proj.name, '') AS "C_proyecto"
COALESCE(c_proj.name, '') AS "C_proyecto",
COALESCE(bal.qty_balance, 0) AS "C_balance"
"""
BASE_JOINS = """
@@ -252,6 +253,21 @@ BASE_JOINS = """
LEFT JOIN a76.item_line_descriptions ild ON ild.item_line_id = il.id
LEFT JOIN a76.parts p ON p.id = il.part_number_id
LEFT JOIN a76.units_of_measure uom ON uom.id = il.unit_of_measure
LEFT JOIN (
SELECT
import_item_line_id,
SUM(CASE WHEN movement_type = 'entry' THEN quantity ELSE 0 END) as qty_impo,
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', 'neg_adj', 'transfer_out', 'expiration', 'regime_chg_out', 'entry_void')
THEN -1 * quantity ELSE quantity END) as qty_balance,
SUM(CASE WHEN movement_type = 'entry' THEN value_me ELSE 0 END) as val_me_impo,
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 = 'entry' THEN value_mn ELSE 0 END) as val_mn_impo,
SUM(CASE WHEN movement_type IN ('consumption', 'waste', 'scrap', 'destruction') THEN value_mn ELSE 0 END) as val_mn_used
FROM a24.balance_movement
WHERE tenant_id = :tenant_id
GROUP BY import_item_line_id
) bal ON bal.import_item_line_id = il.id
"""
# ---------------------------------------------------------------------------
@@ -287,6 +303,7 @@ def _query_ped(filters: SaldosFilter) -> tuple:
FROM a76.item_lines il
{BASE_JOINS}
WHERE ih.tenant_id = :tenant_id
AND ih.operation_type = 'imp'
{company_filter}
{date_filter}
{level_filter}
@@ -310,6 +327,7 @@ def _query_fpp(filters: SaldosFilter) -> tuple:
FROM a76.item_lines il
{BASE_JOINS}
WHERE ih.tenant_id = :tenant_id
AND ih.operation_type = 'imp'
{company_filter}
{date_filter}
{level_filter}
@@ -332,6 +350,7 @@ def _query_ffa(filters: SaldosFilter) -> tuple:
FROM a76.item_lines il
{BASE_JOINS}
WHERE ih.tenant_id = :tenant_id
AND ih.operation_type = 'imp'
{company_filter}
{date_filter}
{level_filter}
@@ -362,6 +381,7 @@ def _query_par(filters: SaldosFilter) -> tuple:
FROM a76.item_lines il
{BASE_JOINS}
WHERE ih.tenant_id = :tenant_id
AND ih.operation_type = 'imp'
{company_filter}
{id_filter}
{date_filter}
@@ -393,6 +413,7 @@ def _query_cla(filters: SaldosFilter) -> tuple:
FROM a76.item_lines il
{BASE_JOINS}
WHERE ih.tenant_id = :tenant_id
AND ih.operation_type = 'imp'
{company_filter}
{id_filter}
{date_filter}
@@ -431,15 +452,15 @@ def _build_row(
# CANTIDADES
cant_orig = _d(row.get("C17"))
cant_ret = _d(row.get("C18")) + _d(row.get("C37"))
cant_saldo = cant_orig - cant_ret
cant_used = _d(row.get("C18"))
cant_saldo = _d(row.get("C_balance"))
if filters.omit_low_balance and cant_saldo <= Decimal(0):
return None
# PESO
peso_neto = _d(row.get("C30"))
peso_usado = (cant_ret * peso_neto / cant_orig) if cant_orig != 0 else Decimal(0)
peso_usado = (cant_used * peso_neto / cant_orig) if cant_orig != 0 else Decimal(0)
peso_saldo = peso_neto - peso_usado
# TIPO DE CAMBIO:
@@ -479,11 +500,11 @@ def _build_row(
if cant_orig != 0:
if use_mn:
if use_fp and fecha_pago:
valor_usado = cant_ret * _d(row.get("C22")) * tc / cant_orig
valor_usado = cant_used * _d(row.get("C22")) * tc / cant_orig
else:
valor_usado = cant_ret * _d(row.get("C20")) / cant_orig
valor_usado = cant_used * _d(row.get("C20")) / cant_orig
else:
valor_usado = cant_ret * _d(row.get("C22")) / cant_orig
valor_usado = cant_used * _d(row.get("C22")) / cant_orig
else:
valor_usado = Decimal(0)
@@ -566,7 +587,7 @@ def _build_row(
"UM": str(row.get("C19") or ""),
"PesoNeto": _fmt_num(peso_neto),
"ValorOriginal": _fmt_num(valor_orig),
"CantidadUsada": _fmt_num(cant_ret),
"CantidadUsada": _fmt_num(cant_used),
"PesoUsado": _fmt_num(peso_usado),
"ValorUsado": _fmt_num(valor_usado),
"CantidadSaldo": _fmt_num(cant_saldo),