Compare commits

..

No commits in common. "ef315b147901918cf87610d4e0a1c2ca1ff00865" and "a7868ba6c893b5ae58c21cd02adbaecb87f3da3a" have entirely different histories.

4 changed files with 19 additions and 20 deletions

View File

@ -135,9 +135,9 @@ def normalize_details_invoice_fields(fd: Dict[str, Any]) -> Dict[str, Any]:
Normaliza campos de detalle de factura desde el registro de origen `fd`.
Escalas:
- quantity_value: escala 2 (cents)
- unit_amount_value: escala 4 (cents4)
- unit_value: escala 4 (cents4)
- discount_percentage_value: escala 2 (10 -> 1000)
- total_amount_value: escala 4 (cents4)
- total_value: escala 4 (cents4)
- iva_amount: escala 2 (cents), calculado con redondeo a 2 decimales
-
"""
@ -157,7 +157,7 @@ def normalize_details_invoice_fields(fd: Dict[str, Any]) -> Dict[str, Any]:
# --- cantidad y precio unitario ---
quantity_value = None if cantidad is None else cents(
cantidad) # escala 2
unit_amount_value = None if importe_unidad is None else cents4(
unit_value = None if importe_unidad is None else cents4(
importe_unidad) # escala 4
# --- descuento de linea % (escala 2) ---
@ -182,17 +182,16 @@ def normalize_details_invoice_fields(fd: Dict[str, Any]) -> Dict[str, Any]:
total_discount_amount_value = discount_amount_value + global_discount_amount_value
# --- total de línea (escala 4) ---
total_amount_value = cents4(total_det)
total_value = cents4(total_det)
if total_amount_value > 0:
if total_value > 0:
# DEBE SER LO MISMO LO CALCULADO QUE LO QUE NOS VIENE POR BD DE FACTUGES
logger.info("total con dto linea calculado: %s - total que llega: %s",
subtotal_amount_with_dto, total_amount_value)
logger.info("total con dto linea calculado: %s - total que llega: %s", subtotal_amount_with_dto, total_value)
# la base imponible sobre la que calcular impuestos es el neto menos el dto de linea y dto global
base = unscale_to_decimal(taxable_amount_value, 4)
if total_amount_value > 0:
if total_value > 0:
logger.info("base imponible calculada: %s - subtotal: %s - descuentodto: %s - descuentoglobal: %s",
base, subtotal_amount_value, discount_amount_value, global_discount_amount_value)
@ -220,13 +219,13 @@ def normalize_details_invoice_fields(fd: Dict[str, Any]) -> Dict[str, Any]:
taxes_amount_value = iva_amount_c4 + rec_amount_c4
total_amount_value = taxable_amount_value + taxes_amount_value
total_value = taxable_amount_value + taxes_amount_value
return {
'position': int(fd.get("POSICION") or 0),
'description': rtf_a_texto_plano(str(concepto_rtf or "")),
'quantity_value': quantity_value,
'unit_amount_value': unit_amount_value,
'unit_value': unit_value,
'subtotal_amount_value': subtotal_amount_value,
'disc_pct': disc_pct,
'discount_percentage_value': discount_percentage_value,
@ -235,7 +234,7 @@ def normalize_details_invoice_fields(fd: Dict[str, Any]) -> Dict[str, Any]:
'global_discount_amount_value': global_discount_amount_value,
'total_discount_amount_value': total_discount_amount_value,
'taxable_amount_value': taxable_amount_value,
'total_amount_value': total_amount_value,
'total_value': total_value,
'iva_code': iva_code,
'iva_percentage_value': iva_percentage_value,
'iva_amount_value': iva_amount_c4,

View File

@ -376,7 +376,7 @@ def insert_item_and_taxes(cur, invoice_id: str, fields: Dict[str, Any]) -> None:
fields.get("position"),
fields.get("description"),
fields.get("quantity_value"),
fields.get("unit_amount_value"),
fields.get("unit_value"),
fields.get("subtotal_amount_value"),
fields.get("discount_percentage_value"),
fields.get("discount_amount_value"),
@ -384,7 +384,7 @@ def insert_item_and_taxes(cur, invoice_id: str, fields: Dict[str, Any]) -> None:
fields.get("global_discount_amount_value"),
fields.get("total_discount_amount_value"),
fields.get("taxable_amount_value"),
fields.get("total_amount_value"),
fields.get("total_value"),
fields.get("iva_code"),
fields.get("iva_percentage_value"),
fields.get("iva_amount_value"),
@ -396,8 +396,8 @@ def insert_item_and_taxes(cur, invoice_id: str, fields: Dict[str, Any]) -> None:
)
# logger.info("Inserting item tax %s code=%s base=%s tax=%s",
# item_id, fields.get('tax_code'), fields.get('total_amount_value'), fields.get('tax_amount'))
# item_id, fields.get('tax_code'), fields.get('total_value'), fields.get('tax_amount'))
# cur.execute(
# SQL.INSERT_INVOICE_ITEM_TAX, (str(uuid7()), item_id, fields.get('tax_code'),
# fields.get('total_amount_value'), fields.get('tax_amount'))
# fields.get('total_value'), fields.get('tax_amount'))
# )

View File

@ -67,7 +67,7 @@ def calc_discount_cents4(subtotal_cents4: int, disc_pct: Optional[Decimal | floa
pct = Decimal(str(disc_pct or 0))
# descuento = round(subtotal * pct / 100) en la MISMA escala (×10000)
disc = (Decimal(subtotal_cents4) * pct / Decimal(100)).to_integral_value(rounding=ROUND_HALF_UP)
return int(disc)
return disc
def apply_discount_cents4(subtotal_cents4: int, disc_pct: Optional[Decimal | float | int]) -> Tuple[int, int]:

View File

@ -67,7 +67,7 @@ def insert_item_and_taxes(fields) -> Dict[str, Any]:
"position": str(fields.get("position")),
"description": str(none_to_empty(fields.get("description"))),
"quantity_value": str(none_to_empty(fields.get("quantity_value"))),
"unit_amount_value": str(none_to_empty(fields.get("unit_amount_value"))),
"unit_value": str(none_to_empty(fields.get("unit_value"))),
"subtotal_amount_value": str(fields.get("subtotal_amount_value")),
"item_discount_percentage_value": str(none_to_empty(fields.get("item_discount_percentage_value"))),
"item_discount_amount_value": str(none_to_empty(fields.get("item_discount_amount_value"))),
@ -75,7 +75,7 @@ def insert_item_and_taxes(fields) -> Dict[str, Any]:
"global_discount_amount_value": str(none_to_empty(fields.get("global_discount_amount_value"))),
"total_discount_amount_value": str(fields.get("total_discount_amount_value")),
"taxable_amount_value": str(fields.get("taxable_amount_value")),
"total_amount_value": str(fields.get("total_amount_value")),
"total_value": str(fields.get("total_value")),
"iva_code": str(fields.get("iva_code")),
"iva_percentage_value": str(str(fields.get("iva_percentage_value"))),
"iva_amount_value": str(fields.get("iva_amount_value")),
@ -86,8 +86,8 @@ def insert_item_and_taxes(fields) -> Dict[str, Any]:
}
# logger.info("Inserting item tax %s code=%s base=%s tax=%s",
# item_id, fields.get('tax_code'), fields.get('total_amount_value'), fields.get('tax_amount'))
# item_id, fields.get('tax_code'), fields.get('total_value'), fields.get('tax_amount'))
# cur.execute(
# SQL.INSERT_INVOICE_ITEM_TAX, (str(uuid7()), item_id, fields.get('tax_code'),
# fields.get('total_amount_value'), fields.get('tax_amount'))
# fields.get('total_value'), fields.get('tax_amount'))
# )