Se introdujo el sistema de consolidados

This commit is contained in:
2026-01-21 09:47:28 -06:00
parent 53f3915de2
commit 3d39360f9c
17 changed files with 1666 additions and 38 deletions

View File

@@ -0,0 +1,97 @@
from decimal import Decimal
from typing import List, Optional, Union
from pydantic import BaseModel, field_validator
class ClienteSchema(BaseModel):
header: str
nombre: str
# Ponemos valor por defecto "" y permitimos que sea opcional
direccion: Optional[str] = ""
num_exterior: Optional[str] = ""
num_interior: Optional[str] = ""
colonia: Optional[str] = ""
codigo_postal: Optional[str] = ""
ciudad: Optional[str] = ""
estado: Optional[str] = ""
pais: Optional[str] = ""
tax_id: str
programa: Optional[str] = ""
autorizacion: Optional[str] = ""
prosec: Optional[str] = ""
reg_emp: Optional[str] = ""
cert: Optional[str] = ""
# Si llega un None, lo convertimos en "" automáticamente
@field_validator('direccion', 'nombre', mode='before')
@classmethod
def prevent_none(cls, v):
return v or ""
class FacturaSchema(BaseModel):
numero: str
fecha: str
tipo_cambio: float
moneda: str
# Campos de aduanas (Opcionales por si A76 aún no los tiene)
pedimento: str = ""
clave_pedimento: str = ""
remesa: str = ""
acuse_electronico: str = ""
representante_legal: str = ""
nombre_empresa: str = ""
agente_aduanal: str = ""
patente: str = ""
precinto: str = ""
regimen: str = ""
transportista: str = ""
scac: str = ""
caat: str = ""
incoterm: str = ""
transporte: str = ""
num_transporte: str = ""
placas: str = ""
placas_remolque: str = ""
licencia_conductor: str = ""
caat: str = ""
scac: str = ""
aduana: str = ""
destino: str = ""
observaciones: str = ""
class PartidaSchema(BaseModel):
numero_parte: str
descripcion: str
fraccion: str
origen: str
advalorem:Optional[str] = ""
preferencia:Optional[str] = ""
cantidad_importacion: Union[float, str]
unidad_medida: str
cantidad_bultos: int
clave_bultos: str
peso_neto: Union[float, str]
peso_bruto: Union[float, str]
valor_costo_unitario: Union[float, str]
valor_total: Union[float, str]
valor_estimado: Union[float, str] = "0.00"
class TotalesSchema(BaseModel):
cantidad_total: Union[float, str]
bultos_total: int
clave_bultos: str = ""
peso_neto_total: Union[float, str]
peso_bruto_total: Union[float, str]
valor_total_total: Union[float, str]
valor_total_dolares: Union[float, str]
valor_estimado_total: Union[float, str] = "0.00"
class FacturaImportacionCompleta(BaseModel):
cliente_proveedor: ClienteSchema
cliente_vendido: ClienteSchema
cliente_enviado: ClienteSchema
factura: FacturaSchema
partidas: List[PartidaSchema]
totales: TotalesSchema

View File

@@ -0,0 +1,565 @@
import shutil
import base64
import pdfkit
from pathlib import Path
from decimal import Decimal
from typing import Tuple, List, Callable, Optional
from jinja2 import Environment, FileSystemLoader, select_autoescape
from fastapi import HTTPException
from sqlalchemy.orm import Session
# --- MODELOS ---
from api.v1.modules.a76.invoices.models import InvoiceHeader, InvoiceLogistics, InvoiceComplianceMx
from api.v1.modules.a76.items.line_financials.models import LineFinancial
from api.v1.modules.a76.items.line_quantities.models import LineQuantity
from api.v1.modules.a76.items.line_items.models import LineItem
from api.v1.modules.a76.clients_and_providers.models import (
ClientProvider, ClientProviderAddress, ClientProviderPrograms
)
from api.v1.modules.a76.parts.models import Part
from api.v1.modules.a76.pedmientos.models import Pedimentos
from api.v1.modules.a76.general_catalogs.company.models import Company
from api.v1.modules.a76.customs_brokers.models import CustomsBroker
from api.v1.modules.a76.items.models import Item
# --- TRANSPORTATION MODELS ---
from api.v1.modules.a76.transportation.transporters.models import Transporter
from api.v1.modules.a76.transportation.vehicles.models import Vehicle
from api.v1.modules.a76.transportation.trailers.models import Trailer
from api.v1.modules.a76.transportation.drivers.models import Driver
# --- MODELO DE FRACCIONES ---
from api.v1.modules.a76.general_catalogs.tariff_fractions.models import TariffFraction
# --- SCHEMAS ---
from .schemas import (
ClienteSchema, PartidaSchema, TotalesSchema,
FacturaSchema, FacturaImportacionCompleta
)
class ConsolidadoImportacionMexService:
def __init__(self):
self.template_dir = Path(__file__).parent.parent / "templates"
self.jinja_env = Environment(
loader=FileSystemLoader(self.template_dir),
autoescape=select_autoescape(['html', 'xml'])
)
self.template = self.jinja_env.get_template('cons_mex_ver.html')
def _get_wkhtmltopdf_config(self):
path = shutil.which("wkhtmltopdf") or "/usr/local/bin/wkhtmltopdf"
if not Path(path).exists():
raise RuntimeError("wkhtmltopdf no encontrado.")
return pdfkit.configuration(wkhtmltopdf=path)
def formatear_numero(self, valor, decimales: int = 2):
if valor is None: return 0.0
try:
return round(float(valor), decimales)
except: return 0.0
def _format_fraccion_fallback(self, fraccion_raw: str) -> str:
if not fraccion_raw or len(fraccion_raw) < 8:
return fraccion_raw
return f"{fraccion_raw[:4]}.{fraccion_raw[4:6]}.{fraccion_raw[6:]}"
def _obtener_datos_cliente(self, db: Session, client_id: int, rol: str) -> ClienteSchema:
main = db.query(ClientProvider).filter(ClientProvider.id == client_id).first()
if not main:
return ClienteSchema(header=rol, nombre="Desconocido", direccion="", tax_id="", codigo_postal="", ciudad="", estado="", pais="MEX")
addr = db.query(ClientProviderAddress).filter(ClientProviderAddress.client_id == client_id).first()
prog = db.query(ClientProviderPrograms).filter(ClientProviderPrograms.client_id == client_id).first()
return ClienteSchema(
header=rol,
nombre=(main.name or main.short_name) or "S/N",
direccion=(addr.streets or "") if addr else "",
num_exterior=(addr.exterior_number or "") if addr else "",
num_interior=(addr.interior_number or "") if addr else "",
colonia=(addr.neighborhood or "") if addr else "",
codigo_postal=(addr.postal_code or "") if addr else "",
ciudad=(addr.city or "") if addr else "",
estado=(addr.state or "") if addr else "",
pais=(addr.country or "MEX") if addr else "MEX",
tax_id=prog.tax_id if (prog and prog.tax_id) else (getattr(main, 'rfc', "") or ""),
programa="IMMEX" if (prog and prog.program) else "",
autorizacion=prog.program_number if prog else "",
prosec=prog.prosec_authorization if (prog and prog.prosec and prog.prosec_authorization) else "",
reg_emp=prog.val_certified_company_registry if (prog and hasattr(prog, 'val_certified_company_registry')) else (
prog.certified_company_registry if (prog and prog.certified_company_registry) else ""
),
cert=prog.is_certified_company if (prog and prog.is_certified_company) else ""
)
def obtener_datos(self, db: Session, invoice_id: int, company_id: int, progress_callback: Optional[Callable] = None) -> FacturaImportacionCompleta:
try:
if progress_callback: progress_callback(10, "Buscando factura...")
header = db.query(InvoiceHeader).filter(InvoiceHeader.id == invoice_id, InvoiceHeader.company_id == company_id).first()
if not header: raise HTTPException(status_code=404, detail="Factura no encontrada")
compliance = header.compliance_mx
logistics = header.logistics if header.logistics else None
financials = header.financials if header.financials else None
if progress_callback: progress_callback(20, "Obteniendo datos de pedimento...")
pedimento_id = compliance.pedimento_id if (compliance and compliance.pedimento_id) else header.related_doc_id
pedimento = db.query(Pedimentos).filter(Pedimentos.id == pedimento_id).first() if pedimento_id else None
if progress_callback: progress_callback(30, "Obteniendo cliente y proveedor...")
proveedor_id = compliance.provider_id if compliance else None
cliente_proveedor = self._obtener_datos_cliente(db, proveedor_id, "Proveedor / supplier:") if proveedor_id else ClienteSchema(header="Proveedor", nombre="No Asignado", direccion="", tax_id="", codigo_postal="", ciudad="", estado="", pais="")
nombre_agente = ""
if compliance and compliance.customs_broker_id:
broker = db.query(CustomsBroker).filter(CustomsBroker.id == compliance.customs_broker_id).first()
if broker: nombre_agente = broker.name
company = db.query(Company).filter(Company.id == header.company_id).first()
# Datos Default (Company/Importer) - Used for fallback or Right Side (Enviado A)
# Default Header (Company)
cliente_default = ClienteSchema(
header="Importer / Consignee:",
nombre=getattr(company, 'name', "Empresa Local"),
direccion="DOMICILIO FISCAL",
num_exterior="", colonia="", codigo_postal="", ciudad="", estado="", pais="MEX",
tax_id=getattr(company, 'rfc', ""),
programa=getattr(company, 'program', "IMMEX"), autorizacion=getattr(company, 'program_number', "")
)
# Left Side Logic (Consignatario / Sold To)
cliente_vendido = cliente_default
if compliance and compliance.sold_to_id:
# Map known headers or default to Sold To / Vendido a
raw = (compliance.sold_to_header or "").upper()
if "CONSIGN" in raw:
clean_header = "Consignee / Consignatario:"
else:
clean_header = "Sold To / Vendido a:"
cliente_vendido = self._obtener_datos_cliente(db, compliance.sold_to_id, clean_header)
# Right Side Logic (Enviado A / Shipped To)
cliente_enviado = cliente_default
if compliance and compliance.shipped_to_id:
# Map to Shipped To / Enviado a
clean_header_shipped = "Shipped To / Enviado a:"
# Fetch client data
cliente_enviado = self._obtener_datos_cliente(db, compliance.shipped_to_id, clean_header_shipped)
remesa_valor = str(compliance.remesa) if (compliance and compliance.remesa) else ""
acuse_valor = str(compliance.edocument) if (compliance and compliance.edocument) else "N/A"
patente_val = ""
if pedimento and pedimento.license:
patente_val = pedimento.license
elif 'broker' in locals() and broker and broker.license:
patente_val = broker.license
# --- Transport Data Fetching ---
transporte_txt = str(logistics.transport_type) if (logistics and logistics.transport_type) else ""
num_transporte_val = (logistics.trailer_num or "") if logistics else ""
# Init values
placas_val = (logistics.license_plate or "") if logistics else "" # Placas Tracto
placas_remolque_val = ""
transportista_val = (logistics.carrier_id or "") if logistics else ""
caat_val = ""
scac_val = ""
licencia_cond_val = ""
if logistics:
# 1. Transporter (CAAT / SCAC)
if logistics.carrier_id:
transporter_obj = db.query(Transporter).filter(Transporter.transporter_key == logistics.carrier_id).first()
if transporter_obj:
caat_val = transporter_obj.caat_code or ""
scac_val = transporter_obj.transport_code or "" # Mapping transport_code to SCAC
transportista_val = transporter_obj.name or logistics.carrier_id
# 2. Vehicle (Placas Tracto) - Try transport_id first
if logistics.transport_id:
veh_obj = db.query(Vehicle).filter(Vehicle.vehicle_key == logistics.transport_id).first()
if veh_obj:
placas_val = veh_obj.plate_number or placas_val
elif logistics.vehicle_num: # Fallback to vehicle_num if populated and transport_id failed/empty
veh_obj = db.query(Vehicle).filter(Vehicle.vehicle_key == logistics.vehicle_num).first()
if veh_obj:
placas_val = veh_obj.plate_number or placas_val
# 3. Trailer (Placas Remolque)
if logistics.trailer_num:
trl_obj = db.query(Trailer).filter(Trailer.trailer_number == logistics.trailer_num).first()
if trl_obj:
placas_remolque_val = trl_obj.plate_number or ""
# 4. Driver (License)
if logistics.carrier_id and logistics.driver_name:
# Attempt to find driver by name + carrier
drv_obj = db.query(Driver).filter(
Driver.transporter_key == logistics.carrier_id,
Driver.driver_name == logistics.driver_name
).first()
if drv_obj:
licencia_cond_val = drv_obj.license_number or ""
factura_schema = FacturaSchema(
numero=header.invoice_number or "S/N",
fecha=str(header.invoice_date) if header.invoice_date else "",
tipo_cambio=float(financials.exchange_rate) if (financials and financials.exchange_rate) else (float(pedimento.exchange_rate) if pedimento and pedimento.exchange_rate else 1.0),
moneda=getattr(header, 'currency', "USD") or "USD",
incoterm=(logistics.incoterm or "") if logistics else "",
observaciones=header.observation_es or header.observation_en or "",
pedimento=f"{pedimento.year} {pedimento.customs_office[:2] if pedimento.customs_office else ''} {pedimento.license} {pedimento.pedimento_number}" if pedimento else "",
clave_pedimento=pedimento.pedimento_code if pedimento else "",
regimen=header.document_type or "",
patente=patente_val,
agente_aduanal=nombre_agente,
transporte=transporte_txt,
num_transporte=num_transporte_val,
placas=placas_val,
placas_remolque=placas_remolque_val,
transportista=transportista_val,
caat=caat_val,
scac=scac_val,
licencia_conductor=licencia_cond_val,
aduana=compliance.aduana if (compliance and compliance.aduana) else (pedimento.customs_office[:2] if (pedimento and pedimento.customs_office) else ""),
precinto=(logistics.seal_number or "") if logistics else "",
destino=(logistics.destination_goods or "") if logistics else "",
remesa=remesa_valor, acuse_electronico=acuse_valor,
representante_legal=getattr(company, 'responsible', "") or "",
nombre_empresa=getattr(company, 'name', "") or ""
)
if progress_callback: progress_callback(50, "Procesando partidas...")
# --- Fetch Lines from SINGLE Invoice (Requested Scope Change) ---
# User requested to ONLY report items from the specific selected invoice,
# NOT consolidating all invoices from the same Pedimento.
target_invoice_ids = [header.id]
lines = db.query(LineItem).join(Item, LineItem.item_id == Item.id).filter(
Item.invoice_id.in_(target_invoice_ids)
).all()
partidas_list = []
# --- AGGREGATION LOGIC (Refactoring based on Clarion) ---
from collections import defaultdict
# Key: (us_fraction_code, origin_country)
# Value: Object with accumulated fields
aggregated_data = defaultdict(lambda: {
"qty": 0.0,
"net_weight_kgs": 0.0,
"gross_weight_kgs": 0.0,
"total_value": 0.0,
"est_total_value": 0.0,
"description": "",
"advalorem_txt": "0%",
"unit_measure": "PZA", # Placeholder, takes first one found
"hts_code_print": "",
"part_number_display": "CONSOLIDADO"
})
# Pre-fetch US Tariff Fractions for efficiency if possible, or query inside loop (caching recommended)
# For simplicity in this step, we query inside or rely on Part data.
# Ideally fetch USTariffFraction from DB based on Part.us_fraction
from api.v1.modules.a76.general_catalogs.us_tariff_fractions.models import USTariffFraction
for line in lines:
qty = db.query(LineQuantity).filter(LineQuantity.item_line_id == line.id).first()
fin = db.query(LineFinancial).filter(LineFinancial.item_line_id == line.id).first()
part_master = db.query(Part).filter(Part.id == line.part_number).first()
# --- Resolver Identificadores ---
us_fraction_raw = ""
origin_final = "MEX"
if part_master:
origin_final = part_master.fa_data.origin_country if (part_master.fa_data and part_master.fa_data.origin_country) else "MEX"
us_fraction_raw = part_master.us_fraction if part_master.us_fraction else ""
# Cleaning Fraction
us_frac_clean = us_fraction_raw.strip()
# Key for aggregation
agg_key = (us_frac_clean, origin_final)
# --- Weights & Qty ---
# --- Optimización: Cargar Facturas en Memoria (Evitar N+1 y arreglar AttributeError) ---
# Pre-fetch invoices explicitly since line.item.invoice relationship might not exist
invoices_list = db.query(InvoiceHeader).filter(InvoiceHeader.id.in_(target_invoice_ids)).all()
invoice_map = {inv.id: inv for inv in invoices_list}
for line in lines:
qty = db.query(LineQuantity).filter(LineQuantity.item_line_id == line.id).first()
fin = db.query(LineFinancial).filter(LineFinancial.item_line_id == line.id).first()
part_master = db.query(Part).filter(Part.id == line.part_number).first()
# --- Weights & Qty ---
q_line = float(qty.quantity) if (qty and qty.quantity) else 0.0
nw_line = float(qty.net_weight) if qty else 0.0
gw_line = float(qty.gross_weight) if qty else 0.0
# --- Multi-Currency Normalization Logic ---
# Determine Line Currency context
# Use manual lookup instead of specific attribute
invoice_id = line.item.invoice_id if line.item else None
line_invoice = invoice_map.get(invoice_id) if invoice_id else None
line_currency_is_mxn = False
line_exchange_rate = 1.0
if line_invoice and line_invoice.financials:
# Check explicit currency string AND code
curr_desc = str(line_invoice.financials.currency or "").upper()
curr_code = str(line_invoice.financials.currency_type or "").upper()
# Logic: It is MXN if description says PESO/MX or code is MXN/MN
is_mx_desc = ("MX" in curr_desc or "PESO" in curr_desc)
is_mx_code = ("MXN" in curr_code or "MN" == curr_code)
# But if code allows clarifying USD, prioritize that
is_usd_code = ("USD" in curr_code)
if is_usd_code:
line_currency_is_mxn = False
elif is_mx_code or is_mx_desc:
line_currency_is_mxn = True
else:
line_currency_is_mxn = False # Default to Foreign/USD if unsure
line_exchange_rate = float(line_invoice.financials.exchange_rate or 1.0)
# Target Report Currency
report_is_mxn = (factura_schema.moneda == 'MXN')
# DEBUG LOGGING
if line_invoice:
print(f"DEBUG: Line {line.id} - Inv {line_invoice.id} - CurrDesc: '{curr_desc}' Code: '{curr_code}' - Rate: {line_exchange_rate}")
print(f"DEBUG: Is MXN Context? {line_currency_is_mxn}. Report is MXN? {report_is_mxn}")
# --- Get Financials for Line (Raw) ---
v_total_raw = 0.0
v_unitario_raw = 0.0
if fin:
# NEW PRIORITY LOGIC (To avoid Inflation from dirty Customs Unit Cost)
# Priority 1: Use 'fin.value_usd' if it exists and > 0.
# Priority 2: Use 'fin.total_commercial_value' if it exists and > 0.
# Priority 3: Calculate using 'fin.unit_cost_commercial_usd' * 'q_line'.
# Priority 4: Only use 'fin.unit_cost_usd' * 'q_line' if commercial data is also missing.
val_usd = float(fin.value_usd or 0.0)
total_comm = float(fin.total_commercial_value or 0.0)
unit_comm_usd = float(fin.unit_cost_commercial_usd or 0.0)
unit_usd = float(fin.unit_cost_usd or 0.0)
# 1. Direct Total: Custom Value (Best case)
if val_usd > 0:
v_total_raw = val_usd
# 2. Direct Total: Commercial Total
elif total_comm > 0:
# Convert if invoice currency is MXN
if line_currency_is_mxn and line_exchange_rate > 0:
v_total_raw = total_comm / line_exchange_rate
else:
v_total_raw = total_comm
# 3. Calc from Commercial Unit Cost (Safe Fallback)
elif unit_comm_usd > 0 and q_line > 0:
v_total_raw = unit_comm_usd * q_line
# 4. Calc from Customs Unit Cost (Unknown Risk - Last Resort)
elif unit_usd > 0 and q_line > 0:
v_total_raw = unit_usd * q_line
else:
v_total_raw = 0.0
# NOTE: v_unitario_raw is left as 0.0 here.
# It will be calculated in the 'Calculation Gap Fill' block below:
# v_unitario_raw = v_total_raw / q_line
# This guarantees consistency and avoids the inflated unit cost record (198.00).
# --- Calculation Gap Fill (Raw) ---
if q_line > 0:
if v_total_raw == 0 and v_unitario_raw > 0:
v_total_raw = v_unitario_raw * q_line
if v_unitario_raw == 0 and v_total_raw > 0:
v_unitario_raw = v_total_raw / q_line
# --- Conversion to Report Currency (DISABLED TEMPORARILY) ---
# User confirms all are USD. Forcing direct sum to avoid logic errors in detection.
v_total_line = v_total_raw
v_unitario_line = v_unitario_raw
# if report_is_mxn and not line_currency_is_mxn:
# # USD -> MXN
# v_total_line = v_total_raw * line_exchange_rate
# v_unitario_line = v_unitario_raw * line_exchange_rate
# elif not report_is_mxn and line_currency_is_mxn:
# # MXN -> USD
# if line_exchange_rate > 0:
# v_total_line = v_total_raw / line_exchange_rate
# v_unitario_line = v_unitario_raw / line_exchange_rate
# else:
# v_total_line = 0.0
# v_unitario_line = 0.0
print(f"DEBUG: ValRaw: {v_total_raw} -> ValFinal: {v_total_line}")
# --- Resolve Fraction Details (Description & Rate) ---
# Only if this is the first time we see this key (or overwrite, doesn't matter much as they should be same for same HTS)
# We check if we already have description set to avoid re-querying if we want optimization,
# but relying on DB query per distinct fraction is safer.
current_agg = aggregated_data[agg_key]
if not current_agg["description"]:
us_frac_db = db.query(USTariffFraction).filter(USTariffFraction.code == us_frac_clean).first()
if us_frac_db:
current_agg["description"] = us_frac_db.description or "Sin Descripción"
# Parse AdValorem from DB if available, else 0 ??
# Creating logical placeholder. The provided Clarion code used `FraAme.Adv`
adv_val = us_frac_db.ad_valorem # Assuming field exists based on viewing file later?
# Wait, in us-tariff-fractions.ts I saw `ad_valorem: number | null`.
current_agg["advalorem_txt"] = f"{adv_val}%" if adv_val is not None else "0%"
else:
current_agg["description"] = part_master.description_spanish if part_master else "S/D"
current_agg["hts_code_print"] = us_frac_clean
current_agg["unit_measure"] = qty.weight_unit if qty else "KGS" # Default to first found
# --- Calculate Estimated Tax for this Line ---
rate = 0.0
try:
clean_adv = current_agg["advalorem_txt"].replace("%", "").strip()
rate = float(clean_adv) / 100.0
except: rate = 0.0
v_est_line = v_total_line * rate
# --- Accumulate ---
current_agg["qty"] += q_line
current_agg["net_weight_kgs"] += nw_line
current_agg["gross_weight_kgs"] += gw_line
current_agg["total_value"] += v_total_line
current_agg["est_total_value"] += v_est_line
# --- Convert Aggregated Data to Schema List ---
partidas_list = []
for (hts, origin), data in aggregated_data.items():
# Calculate Unit Price based on Total Value / Total Qty
unit_price = 0.0
if data["qty"] > 0:
unit_price = data["total_value"] / data["qty"]
partidas_list.append(PartidaSchema(
numero_parte="VARIOS", # Or empty
descripcion=data["description"],
fraccion=data["hts_code_print"],
origen=origin,
advalorem=data["advalorem_txt"],
preferencia="General",
cantidad_importacion=self.formatear_numero(data["qty"]),
unidad_medida=data["unit_measure"],
cantidad_bultos=0, # Summing bultos might be tricky if not homogeneous, check logic later
clave_bultos="",
peso_neto=self.formatear_numero(data["net_weight_kgs"]),
peso_bruto=self.formatear_numero(data["gross_weight_kgs"]),
valor_costo_unitario=self.formatear_numero(unit_price),
valor_total=self.formatear_numero(data["total_value"]),
valor_estimado=self.formatear_numero(data["est_total_value"])
))
# Sort by Fraction (HTS Code)
partidas_list.sort(key=lambda x: x.fraccion)
totales = self.calcular_totales(partidas_list, Decimal(factura_schema.tipo_cambio))
return FacturaImportacionCompleta(
cliente_proveedor=cliente_proveedor, cliente_vendido=cliente_vendido,
cliente_enviado=cliente_enviado, factura=factura_schema,
partidas=partidas_list, totales=totales
)
except Exception as e:
print(f"Error Service A76: {e}")
raise HTTPException(status_code=500, detail=f"Error: {str(e)}")
def calcular_totales(self, partidas: List[PartidaSchema], tipo_cambio: Decimal) -> TotalesSchema:
cant = sum(p.cantidad_importacion for p in partidas)
valor = sum(p.valor_total for p in partidas)
peso_n = sum(p.peso_neto for p in partidas)
peso_b = sum(p.peso_bruto for p in partidas)
bultos = sum(p.cantidad_bultos for p in partidas)
claves = [p.clave_bultos for p in partidas if p.clave_bultos]
clave_comun = max(set(claves), key=claves.count) if claves else ""
if bultos > 1 and clave_comun and not clave_comun.endswith("S"): clave_comun += "S"
v_est = sum(p.valor_estimado for p in partidas if isinstance(p.valor_estimado, (int, float, Decimal)))
tc = float(tipo_cambio) if tipo_cambio else 1.0
return TotalesSchema(
cantidad_total=self.formatear_numero(cant), bultos_total=bultos, clave_bultos=clave_comun,
peso_neto_total=self.formatear_numero(peso_n), peso_bruto_total=self.formatear_numero(peso_b),
valor_total_total=self.formatear_numero(valor), valor_total_dolares=self.formatear_numero(valor / tc if tc > 0 else 0),
valor_estimado_total=self.formatear_numero(v_est)
)
def generar_factura_completa(self, db: Session, invoice_id: int, company_id: int, formato: str = "pdf", progress_callback: Optional[Callable] = None) -> Tuple[bytes, str, str]:
if progress_callback: progress_callback(5, "Iniciando servicio de reporte...")
datos = self.obtener_datos(db, invoice_id, company_id, progress_callback)
if progress_callback: progress_callback(80, "Renderizando plantilla...")
# LOGO LOGIC
logo_b64 = None
try:
# Fetch company to get logo path
comp_logo = db.query(Company).filter(Company.id == company_id).first()
if comp_logo and comp_logo.logo:
p = Path(comp_logo.logo)
# Logic robusta de búsqueda (igual que en routes.py)
target_path = p
if not target_path.exists():
# Intentar en la ruta estándar: app_data/logos/{id}/{nombre}
# Esto cubre el caso donde solo se guardó el nombre del archivo o la ruta absoluta cambió
fallback = Path(f"app_data/logos/{company_id}") / p.name
if fallback.exists():
target_path = fallback
if target_path.exists():
with open(target_path, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
# Detect MIME type loosely
mime = "image/png"
if target_path.suffix.lower() in ['.jpg', '.jpeg']: mime = "image/jpeg"
logo_b64 = f"data:{mime};base64,{encoded_string}"
except Exception as e:
print(f"Error loading logo: {e}")
context = {
'cliente_proveedor': datos.cliente_proveedor.model_dump(), 'cliente_vendido': datos.cliente_vendido.model_dump(),
'cliente_enviado': datos.cliente_enviado.model_dump(), 'factura': datos.factura.model_dump(),
'partidas': [p.model_dump() for p in datos.partidas], 'totales': datos.totales.model_dump(),
'logo_b64': logo_b64
}
html_content = self.template.render(**context)
nombre = f"Consolidado_{datos.factura.numero}.{formato}"
if formato == "html": return html_content.encode('utf-8'), nombre, "text/html"
if progress_callback: progress_callback(90, "Generando PDF final...")
options = {'page-size': 'Letter', 'margin-top': '0.5in', 'margin-right': '0.5in', 'margin-bottom': '0.5in', 'margin-left': '0.5in', 'encoding': "UTF-8", 'enable-local-file-access': None}
pdf = pdfkit.from_string(html_content, False, options=options, configuration=self._get_wkhtmltopdf_config())
if progress_callback: progress_callback(100, "Completado")
return pdf, nombre, "application/pdf"

View File

@@ -0,0 +1,48 @@
from enum import Enum
from typing import Dict, Any
from fastapi import APIRouter, Depends, Query, Response, HTTPException
from sqlalchemy.orm import Session
from celery.result import AsyncResult
from core.celery_app import celery_app
from core.database import get_core_db
from core.security import get_current_user, validate_access_to_resource
from .mex.service import ConsolidadoImportacionMexService
from .task import generar_pdf_consolidado_async
router = APIRouter()
servicio_mex = ConsolidadoImportacionMexService()
@router.get("/tasks/{task_id}")
async def get_task_status(
task_id: str,
current_user: Dict[str, Any] = Depends(get_current_user),
db: Session = Depends(get_core_db)
):
task_result = AsyncResult(task_id, app=celery_app)
response = {
"task_id": task_id,
"state": task_result.state,
"result": None,
"info": None
}
if task_result.state == 'FAILURE':
response["result"] = str(task_result.result)
elif task_result.state == 'SUCCESS':
response["result"] = task_result.result
elif task_result.state == 'PROCESSING':
response["info"] = task_result.info
return response
@router.post("/{invoice_id}/download-async")
async def trigger_descarga_consolidado(
invoice_id: int,
company_id: int = Query(..., description="ID de la empresa"),
current_user: Dict[str, Any] = Depends(get_current_user),
db: Session = Depends(get_core_db)
):
validate_access_to_resource(db, company_id, current_user)
task = generar_pdf_consolidado_async.delay(invoice_id, company_id)
return {"task_id": task.id, "message": "Generación iniciada"}

View File

@@ -0,0 +1,52 @@
import base64
import logging
from core.celery_app import celery_app
from celery import current_task, states
from core.database import CoreSessionLocal
from .mex.service import ConsolidadoImportacionMexService
logger = logging.getLogger(__name__)
@celery_app.task(name="generar_pdf_consolidado_async", bind=True)
def generar_pdf_consolidado_async(self, invoice_id: int, company_id: int):
# 1. Abrimos conexión a la DB
db = CoreSessionLocal()
try:
logger.info(f"Worker procesando consolidado {invoice_id}...")
# 2. Instanciamos el servicio de reportes
service = ConsolidadoImportacionMexService()
# Update state to PROCESSING
self.update_state(state='PROCESSING', meta={'current': 5, 'total': 100, 'status': 'Iniciando generación...'})
def progress_callback(progress: int, status: str):
self.update_state(state='PROCESSING', meta={'current': progress, 'total': 100, 'status': status})
# 3. Generamos los bytes del PDF
pdf_bytes, nombre, media_type = service.generar_factura_completa(
db=db,
invoice_id=invoice_id,
company_id=company_id,
progress_callback=progress_callback
)
# 4. Codificamos a base64 para que viaje seguro por Valkey
pdf_base64 = base64.b64encode(pdf_bytes).decode('utf-8')
return {
"status": "success",
"file_name": nombre,
"content": pdf_base64,
"media_type": media_type
}
except Exception as e:
logger.error(f"Error en Celery Worker: {str(e)}")
return {"status": "error", "message": str(e)}
finally:
# 5. MUY IMPORTANTE: Cerramos la conexión para no saturar Postgres
db.close()

View File

@@ -0,0 +1,535 @@
<!DOCTYPE html>
<html lang="es" xml:lang="es">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Consolidado Importacion Mexicana - {{ factura.numero }}</title>
<style type="text/css">
/* ESTILOS EXACTOS DE SCAPII PARA MANTENER EL FORMATO */
* {
margin: 0;
padding: 0;
font-family: Tahoma, sans-serif;
color: black;
}
.titulo {
font-size: 14pt;
padding: 3pt 0 0 6pt;
}
.grande {
font-size: 13pt;
padding-left: 5pt;
}
.medio-bold {
font-size: 9pt;
font-weight: bold;
padding: 3pt 0 0 3pt;
}
.normal {
font-size: 8pt;
}
.small-bold {
font-size: 8pt;
font-weight: bold;
}
.tiny-bold {
font-size: 7pt;
font-weight: bold;
}
.tiny {
font-size: 7pt;
}
.mini {
font-size: 5pt;
}
h1 {
font-weight: bold;
font-size: 8pt;
}
p {
font-size: 7pt;
}
.cliente {
border-bottom: 1pt solid black;
text-decoration: none;
display: block;
width: 100%
}
.center {
text-align: center;
}
.right {
text-align: right;
}
.border {
border: 1pt solid #808080;
}
.p-t-1 {
padding-top: 1pt;
}
.p-t-2 {
padding-top: 2pt;
}
.p-t-3 {
padding-top: 3pt;
}
.p-t-4 {
padding-top: 4pt;
}
.p-t-5 {
padding-top: 5pt;
}
.p-t-8 {
padding-top: 8pt;
}
.p-t-9 {
padding-top: 9pt;
}
.p-l-2 {
padding-left: 2pt;
}
.p-l-3 {
padding-left: 3pt;
}
.p-l-5 {
padding-left: 5pt;
}
.p-l-6 {
padding-left: 6pt;
}
.p-l-7 {
padding-left: 7pt;
}
.p-l-8 {
padding-left: 8pt;
}
.p-l-9 {
padding-left: 9pt;
}
.p-l-10 {
padding-left: 10pt;
}
.p-r-1 {
padding-right: 1pt;
}
.p-r-2 {
padding-right: 2pt;
}
.p-r-4 {
padding-right: 4pt;
}
.p-r-5 {
padding-right: 5pt;
}
.p-r-9 {
padding-right: 9pt;
}
.p-b-1 {
padding-bottom: 1pt;
}
.h-10 {
height: 10pt;
}
.h-11 {
height: 11pt;
}
.h-14 {
height: 14pt;
}
.h-15 {
height: 15pt;
}
.h-18 {
height: 18pt;
}
.h-22 {
height: 22pt;
}
.h-82 {
height: 82pt;
}
.h-384 {
height: 384pt;
}
.line-1 {
line-height: 1pt;
}
.line-7 {
line-height: 7pt;
}
.line-8 {
line-height: 8pt;
}
.line-9 {
line-height: 9pt;
}
.line-10 {
line-height: 10pt;
}
.m-l-3 {
margin-left: 3pt;
}
.m-l-5 {
margin-left: 5.74pt;
}
.flex-container {
width: 100%;
overflow: hidden;
}
.flex-container-end {
width: 100%;
overflow: hidden;
}
.width-48 {
width: 48%;
float: left;
}
.width-48-right {
width: 48%;
float: right;
text-align: right;
}
.position-relative-centered {
position: relative;
width: 48%;
float: left;
}
.full-width-block {
display: block;
width: 100%;
}
.clearfix::after {
content: "";
display: table;
clear: both;
}
table,
tbody {
vertical-align: top;
overflow: visible;
}
</style>
</head>
<body>
<header>
<div class="flex-container clearfix">
<div class="width-48" style="position: relative;">
<p class="titulo">HTS Code / Fracción Americana <span class="mini">(temporal)</span></p>
<div class="cliente" style="height: 1px;"></div>
<p class="cliente p-t-1 p-b-1"></p>
</div>
<div class="width-48-right">
<p class="p-l-5 line-1"><span></span></p>
<p class="p-t-2"><br></p>
</div>
</div>
<div class="flex-container-end clearfix" style="overflow: visible;">
<div class="position-relative-centered">
{% if logo_b64 %}
<div style="position: absolute; top: 10pt; left: 0;">
<img src="{{ logo_b64 }}" style="max-height: 70pt; max-width: 120pt;" />
</div>
{% endif %}
<div style="margin-left: 130pt; padding-top: 25pt; text-align: left;">
<h1 class="cliente">{{ cliente_proveedor.header }}</h1>
<p>{{ cliente_proveedor.nombre }}</p>
<p>{{ cliente_proveedor.direccion }}
{% if cliente_proveedor.num_exterior %} Ext: {{ cliente_proveedor.num_exterior }}{% endif %}
{% if cliente_proveedor.num_interior %} Int: {{ cliente_proveedor.num_interior }}{% endif %}
</p>
<p>{{ cliente_proveedor.colonia }} {% if cliente_proveedor.codigo_postal %} CP: {{
cliente_proveedor.codigo_postal }}{% endif %}</p>
<p>{{ cliente_proveedor.ciudad }}, {{ cliente_proveedor.estado }}, {{ cliente_proveedor.pais }}</p>
<p>TAX ID: {{ cliente_proveedor.tax_id }}
{% if cliente_proveedor.programa and cliente_proveedor.programa != 'Ninguno' %}
{{ cliente_proveedor.programa }}: {{ cliente_proveedor.autorizacion }}
{% endif %}
</p>
<p><br></p>
</div>
</div>
<div style="position: relative;">
<table cellspacing="0" class="m-l-3" style="float: right; text-align: left;">
<tbody>
<tr class="h-18">
<td bgcolor="#E4E4E4" class="border" style="width:90pt">
<p class="medio-bold p-l-3">Invoice No. / Num. Factura:</p>
</td>
<td class="border" style="width:138pt">
<p class="grande center">{{ factura.numero }}</p>
</td>
</tr>
<tr class="h-18">
<td bgcolor="#E4E4E4" class="border">
<p class="medio-bold p-l-3 line-9">Date / Fecha:</p>
</td>
<td class="border">
<p class="normal center line-9">{{ factura.fecha }}</p>
</td>
</tr>
<tr class="h-18">
<td bgcolor="#E4E4E4" class="border">
<p class="medio-bold p-l-3 line-9">Rate / Cambio:</p>
</td>
<td class="border">
<p class="normal center line-9">{{ factura.tipo_cambio }}</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="flex-container clearfix">
<div class="width-48">
<h1 class="p-t-5 p-l-5 line-10 cliente">{{ cliente_vendido.header }}</h1>
<p class="p-l-5 line-8">{{ cliente_vendido.nombre }}</p>
<p class="p-l-5">{{ cliente_vendido.direccion }}
{% if cliente_vendido.num_exterior %} Ext: {{ cliente_vendido.num_exterior }}{% endif %}
{% if cliente_vendido.num_interior %} Int: {{ cliente_vendido.num_interior }}{% endif %}
</p>
<p class="p-l-5">{{ cliente_vendido.colonia }} {% if cliente_vendido.codigo_postal %} CP: {{
cliente_vendido.codigo_postal }}{% endif %}</p>
<p class="p-l-5">{{ cliente_vendido.ciudad }}, {{ cliente_vendido.estado }}, {{ cliente_vendido.pais }}
</p>
<p class="p-l-5">RFC: {{ cliente_vendido.tax_id }}
{% if cliente_vendido.programa and cliente_vendido.programa != 'Ninguno' %}
{{ cliente_vendido.programa }}: {{ cliente_vendido.autorizacion }}
{% endif %}
</p>
<p class="p-l-5">
{% if cliente_vendido.prosec %}PROSEC: {{ cliente_vendido.prosec }} {% endif %}
{% if cliente_vendido.reg_emp %}REG EMP: {{ cliente_vendido.reg_emp }} {% endif %}
{% if cliente_vendido.cert %}CERT: {{ cliente_vendido.cert }}{% endif %}
</p>
</div>
<div class="width-48-right" style="text-align: left;">
<h1 class="p-t-5 p-l-5 line-10 cliente full-width-block">{{ cliente_enviado.header }}</h1>
<p class="p-l-5 line-8">{{ cliente_enviado.nombre }}</p>
<p class="p-l-5">{{ cliente_enviado.direccion }}
{% if cliente_enviado.num_exterior %} Ext: {{ cliente_enviado.num_exterior }}{% endif %}
{% if cliente_enviado.num_interior %} Int: {{ cliente_enviado.num_interior }}{% endif %}
</p>
<p class="p-l-5">{{ cliente_enviado.colonia }} {% if cliente_enviado.codigo_postal %} CP: {{
cliente_enviado.codigo_postal }}{% endif %}</p>
<p class="p-l-5">{{ cliente_enviado.ciudad }}, {{ cliente_enviado.estado }}, {{ cliente_enviado.pais }}
</p>
<p class="p-l-5">RFC: {{ cliente_enviado.tax_id }}
{% if cliente_enviado.programa and cliente_enviado.programa != 'Ninguno' %}
{{ cliente_enviado.programa }}: {{ cliente_enviado.autorizacion }}
{% endif %}
</p>
<p class="p-l-5">
{% if cliente_enviado.prosec %}PROSEC: {{ cliente_enviado.prosec }} {% endif %}
{% if cliente_enviado.reg_emp %}REG EMP: {{ cliente_enviado.reg_emp }} {% endif %}
{% if cliente_enviado.cert %}CERT: {{ cliente_enviado.cert }}{% endif %}
</p>
</div>
</div>
<p class="p-t-8"><br /></p>
</header>
<table cellspacing="0" class="m-l-5" style="width: 99%; max-width: 580pt;">
<thead>
<tr class="h-10">
<td class="border" style="width:15%">
<p class="tiny-bold center p-t-3">HTS Code / Fracción Americana <span class="mini">(temporal)</span>
</p>
</td>
<td class="border" style="width:10%">
<p class="tiny-bold center p-t-3">Country</p>
</td>
<td class="border" style="width:10%">
<p class="tiny-bold center p-t-3">Comm. Qty</p>
</td>
<td class="border" style="width:5%">
<p class="tiny-bold center p-t-3">Unit</p>
</td>
<td class="border" colspan="2" style="width:15%">
<p class="tiny-bold center line-9">Weight (KGS)</p>
<div class="clearfix">
<div class="width-48" style="width: 50%; text-align: center;">
<p class="mini">Net</p>
</div>
<div class="width-48-right" style="width: 50%; text-align: center;">
<p class="mini">Gross</p>
</div>
</div>
</td>
<td class="border" colspan="2" style="width:25%">
<p class="tiny-bold center line-9">Dutiable Values</p>
<div class="clearfix">
<div class="width-48" style="width: 50%; text-align: center;">
<p class="mini">Unit</p>
</div>
<div class="width-48-right" style="width: 50%; text-align: center;">
<p class="mini">Totals</p>
</div>
</div>
</td>
<td class="border" style="width:5%">
<p class="tiny-bold center p-t-3">Rate</p>
</td>
<td class="border" style="width:15%">
<p class="tiny-bold center p-t-3">Est. Duties</p>
</td>
</tr>
</thead>
<tbody class="h-384" style="width: 100%;">
{% for partida in partidas %}
<tr>
<td class="border">
<p class="mini p-t-2 center">{{ partida.fraccion }}</p>
</td>
<td class="border">
<p class="mini p-t-2 center">{{ partida.origen or 'MEX' }}</p>
</td>
<td class="border">
<p class="mini p-t-2 center">{{ partida.cantidad_importacion }}</p>
</td>
<td class="border">
<p class="mini p-t-2 center">{{ partida.unidad_medida }}</p>
</td>
<td class="border" style="border-right: 0;">
<p class="mini p-t-2 center">{{ partida.peso_neto }}</p>
</td>
<td class="border" style="border-left: 0;">
<p class="mini p-t-2 center">{{ partida.peso_bruto }}</p>
</td>
<td class="border" style="border-right: 0;">
<p class="mini p-t-2 center">${{ partida.valor_costo_unitario }}</p>
</td>
<td class="border" style="border-left: 0;">
<p class="mini p-t-2 center">${{ partida.valor_total }}</p>
</td>
<td class="border">
<p class="mini p-t-2 center">{{ partida.advalorem }}</p>
</td>
<td class="border">
<p class="mini p-t-2 right p-r-2">${{ partida.valor_estimado }}</p>
</td>
</tr>
{% endfor %}
</tbody>
<tfoot>
<tr class="h-14">
<td bgcolor="#E4E4E4" class="border" colspan="2" style="width:25%">
<p class="small-bold p-t-2 p-l-3 line-10">
<span>TOTALS</span>
</p>
</td>
<td class="border" style="width:10%">
<p class="tiny p-t-5 center line-8">{{ totales.cantidad_total }}</p>
</td>
<td class="border" style="width:5%">
<p class="tiny p-t-4 center line-8">
{% if totales.bultos_total != 0 %}{{ totales.bultos_total }}{% endif %}
<span>{{ totales.clave_bultos or '' }}</span>
</p>
</td>
<td class="border" style="width:7.5%">
<p class="tiny p-t-5 center line-8">{{ totales.peso_neto_total }}</p>
</td>
<td class="border" style="width:7.5%">
<p class="tiny p-t-5 center line-8">{{ totales.peso_bruto_total }}</p>
</td>
<td class="border" style="width:12.5%">
<!-- Unitario Total N/A -->
</td>
<td class="border" style="width:12.5%">
<p class="tiny p-t-5 right line-8 p-r-2">${{ totales.valor_total_total }}</p>
</td>
<td class="border" style="width:5%">
<!-- Tasa N/A -->
</td>
<td class="border" style="width:15%">
<p class="tiny p-t-5 right p-r-2">${{ totales.valor_estimado_total }}</p>
</td>
</tr>
<tr>
<td colspan="2" class="border" style="height: 100pt; text-align:start; vertical-align: top;">
<p class="mini p-l-2 p-t-2">{{ factura.observaciones }}</p>
</td>
<td colspan="8" style="width:80%; vertical-align: bottom; height: 100%;">
<p style="border-bottom: 1pt solid black; width: 80%; margin: 60pt auto 2pt auto;"></p>
<p class="normal center" style="margin-bottom: 0;">{{ factura.representante_legal }}</p>
<p class="normal center" style="margin-bottom: 0;">{{ factura.nombre_empresa }}</p>
<p style="margin-bottom: 0;"><br /></p>
</td>
</tr>
<tr>
<td colspan="10" style="width:100%; vertical-align: top; height: 100%;">
<p class="p-t-8"><br /></p>
<p class="p-l-5 line-8 tiny-bold">Normal Por Parte</p>
<p class="normal p-l-5 line-9">Declaro bajo protesta de decir verdad que la información contenida en
este documento es verdadera y me hago responsable de comprobar lo aquí declarado.</p>
</td>
</tr>
</tfoot>
</table>
</body>
</html>

View File

@@ -244,6 +244,10 @@ class FacturaImportacionMexService:
desc_final = part_master.description_spanish or part_master.description_english or "Sin Desc."
num_parte_final = part_master.part_number
fraccion_raw = part_master.fraction if part_master.fraction else ""
# Fetch Origin from Master Catalog (FaPart)
if part_master.fa_data and part_master.fa_data.origin_country:
origen_final = part_master.fa_data.origin_country
fraccion_limpia = fraccion_raw.replace(".", "").strip()
@@ -280,16 +284,27 @@ class FacturaImportacionMexService:
# 1. Try Specific Currency Columns First
if is_mxn:
v_unitario = fin.unit_cost_commercial_mxn or 0.0
v_total = fin.value_commercial_mxn or 0.0
v_unitario = float(fin.unit_cost_commercial_mxn or 0.0)
v_total = float(fin.value_commercial_mxn or 0.0)
else:
v_unitario = fin.unit_cost_commercial_usd or 0.0
v_total = fin.value_commercial_usd or 0.0
v_unitario = float(fin.unit_cost_commercial_usd or 0.0)
v_total = float(fin.value_commercial_usd or 0.0)
# 2. Fallback to Generic if Specific is 0
if v_unitario == 0 or v_total == 0:
v_unitario = fin.commercial_unit_cost or 0.0
v_total = fin.total_commercial_value or 0.0
# 2. Fallback to Generic independently if Specific is 0
if not v_unitario:
v_unitario = float(fin.commercial_unit_cost or 0.0)
if not v_total:
v_total = float(fin.total_commercial_value or 0.0)
# 3. Calculate from Quantity if still missing
cantidad = float(qty.quantity) if (qty and qty.quantity) else 0.0
if cantidad > 0:
if v_unitario > 0 and v_total == 0:
v_total = v_unitario * cantidad
elif v_total > 0 and v_unitario == 0:
v_unitario = v_total / cantidad
partidas_list.append(PartidaSchema(
numero_parte=num_parte_final,
@@ -301,7 +316,7 @@ class FacturaImportacionMexService:
cantidad_importacion=self.formatear_numero(qty.quantity if qty else 0),
unidad_medida=qty.weight_unit if qty else "PZA",
cantidad_bultos=int(qty.package_quantity) if qty and qty.package_quantity else 0,
clave_bultos=qty.package_key if qty else "",
clave_bultos=(qty.package_key or "") if qty else "",
peso_neto=self.formatear_numero(qty.net_weight if qty else 0),
peso_bruto=self.formatear_numero(qty.gross_weight if qty else 0),
valor_costo_unitario=self.formatear_numero(v_unitario),

View File

@@ -10,9 +10,7 @@ logger = logging.getLogger(__name__)
@celery_app.task(name="generar_pdf_factura_async", bind=True)
def generar_pdf_factura_async(self, invoice_id: int, company_id: int):
"""
Esta tarea la ejecuta el Worker. No usa FastAPI, usa directamente SQLAlchemy.
"""
# 1. Abrimos conexión a la DB
db = CoreSessionLocal()
try:

View File

@@ -50,6 +50,7 @@ from api.v1.modules.public.reference_data.material_types.routes import router as
# --- NUEVO IMPORT PARA REPORTES DE FACTURAS ---
from .reports.importacion.facturas.routes import router as invoices_reports_router
from .reports.importacion.consolidados.routes import router as consolidated_reports_router
# Router principal
@@ -123,4 +124,10 @@ router.include_router(
invoices_reports_router,
prefix="/a76/reports/importacion/facturas",
tags=["a76 / reports"]
)
router.include_router(
consolidated_reports_router,
prefix="/a76/reports/importacion/consolidados",
tags=["a76 / reports"]
)

View File

@@ -8,7 +8,10 @@ celery_app = Celery(
"anexo76_tasks",
broker=valkey_url,
backend=valkey_url,
include=["api.v1.modules.a76.reports.importacion.facturas.task"] # Ruta al módulo donde están las tareas
include=[
"api.v1.modules.a76.reports.importacion.facturas.task",
"api.v1.modules.a76.reports.importacion.consolidados.task"
] # Ruta al módulo donde están las tareas
)
# Configuraciones adicionales

76
debug_values.py Normal file
View File

@@ -0,0 +1,76 @@
from sqlalchemy import create_engine, text
from sqlalchemy.orm import sessionmaker
from api.v1.modules.a76.items.line_items.models import LineItem
from api.v1.modules.a76.items.line_quantities.models import LineQuantity
from api.v1.modules.a76.items.line_financials.models import LineFinancial
from api.v1.modules.a76.parts.models import Part
from api.v1.modules.a76.invoices.models import InvoiceHeader
from api.v1.modules.a76.pedmientos.models import Pedimentos
# Setup DB (Adjust connection string if needed, checking environment assumption)
# Assuming local connection string or deriving from environment/config
# For this environment, I'll attempt a standard connection or reuse existing if possible.
# Since I cannot easily import 'db' from main app without setup, I will rely on standard raw SQL or simple ORM setup if I can import 'Session'.
# Using the imports available in the user's file.
import sys
sys.path.append('/home/josmar/dev/anexo76/backend')
from core.database import CoreSessionLocal
# Manual Session creation
db = CoreSessionLocal()
try:
# 1. Find the lines matching the description (Qty 1500 + HTS)
# The user said HTS: 2710190650
# Qty: 1500
print("--- SEARCHING FOR LINES ---")
# We look for lines with quantity 1500 first
candidates = db.query(LineItem).join(LineQuantity).filter(
LineQuantity.quantity == 1500
).all()
found = False
for line in candidates:
part = db.query(Part).filter(Part.id == line.part_number).first()
fin = db.query(LineFinancial).filter(LineFinancial.item_line_id == line.id).first()
inv = db.query(InvoiceHeader).filter(InvoiceHeader.id == line.item.invoice_id).first() if line.item else None
us_frac = part.us_fraction if part else "N/A"
# Check fraction match (loose match)
if "2710190650" in us_frac.replace(".","").replace(" ",""):
found = True
print(f"\nMATCH FOUND: Line ID {line.id} - Invoice {inv.invoice_number if inv else 'N/A'}")
print(f"HTS: {us_frac}")
print(f"Qty: {1500}")
print(f"Inv Currency: {inv.financials.currency} / {inv.financials.currency_type} Rate: {inv.financials.exchange_rate}")
print("\nFINANCIALS:")
if fin:
print(f" value_usd: {fin.value_usd}")
print(f" value_mxn: {fin.value_mxn}")
print(f" value_commercial_usd: {fin.value_commercial_usd}")
print(f" value_commercial_mxn: {fin.value_commercial_mxn}")
print(f" unit_cost_usd: {fin.unit_cost_usd}")
print(f" unit_cost_commercial_usd: {fin.unit_cost_commercial_usd}")
print(f" unit_cost_mxn: {fin.unit_cost_mxn}")
# Check calculation hypothesis
val_usd = float(fin.value_usd or 0)
val_mxn = float(fin.value_mxn or 0)
rate = float(inv.financials.exchange_rate or 1)
print(f"\n If using ValueMXN/Rate: {val_mxn} / {rate} = {val_mxn/rate}")
print(f" If using Max(MXN, USD): {max(val_mxn, val_usd)}")
else:
print(" No Financials found.")
if not found:
print("No matching line (1500 qty, HTS 2710190650) found.")
finally:
db.close()

View File

@@ -0,0 +1,35 @@
const BASE_URL = import.meta.env.VITE_API_URL || '';
export const consolidatedReportsApi = {
triggerPdfGeneration: async (invoiceId: number, companyId: number) => {
const params = new URLSearchParams({ company_id: companyId.toString() });
const endpoint = `${BASE_URL}/v1/a76/reports/importacion/consolidados/${invoiceId}/download-async?${params.toString()}`;
const token = localStorage.getItem('access_token');
const response = await fetch(endpoint, {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
}
});
if (!response.ok) throw new Error('Error al iniciar la generación del consolidado');
return await response.json();
},
getTaskStatus: async (taskId: string) => {
const endpoint = `${BASE_URL}/v1/a76/reports/importacion/consolidados/tasks/${taskId}`;
const token = localStorage.getItem('access_token');
const response = await fetch(endpoint, {
method: 'GET',
headers: { 'Authorization': `Bearer ${token}` }
});
if (!response.ok) throw new Error('Error al consultar estado del consolidado');
return await response.json();
}
};

View File

@@ -0,0 +1,146 @@
<script lang="ts">
import { Button } from "$lib/components/ui/button";
import { Input } from "$lib/components/ui/input";
import * as Dialog from "$lib/components/ui/dialog";
import * as Table from "$lib/components/ui/table";
import { toast } from "svelte-sonner";
import { Search, Loader2, Hash } from "lucide-svelte";
import { getUSTariffFractions, type USTariffFraction } from "$lib/api/dashboard/a76/general_catalogs/us-tariff-fractions";
import { companyStore } from "$lib/stores/company.svelte";
// --- PROPS ---
let {
open = $bindable(false),
onSelect
}: {
open: boolean,
onSelect: (item: USTariffFraction) => void
} = $props();
// --- ESTADO ---
let items = $state<USTariffFraction[]>([]);
let loading = $state(false);
let searchTerm = $state("");
let loaded = $state(false);
// Filtro local
let filteredItems = $derived(
items.filter(i =>
(i.code || "").includes(searchTerm) ||
(i.description || "").toLowerCase().includes(searchTerm.toLowerCase())
)
);
// Cargar datos al abrir
$effect(() => {
if (open && !loaded && companyStore.activeCompany?.id) {
loadFractions();
}
});
async function loadFractions() {
if (!companyStore.activeCompany?.id) {
toast.error("No hay empresa seleccionada");
return;
}
loading = true;
try {
const response = await getUSTariffFractions(1, 1000, companyStore.activeCompany.id);
if (response.error) {
console.error("Error al cargar fracciones americanas:", response.error);
toast.error(`Error: ${response.error}`);
return;
}
if (response.data?.items) {
items = response.data.items;
loaded = true;
} else {
console.warn("No se encontraron fracciones americanas:", response);
toast.info("No se encontraron fracciones americanas registradas");
}
} catch (e: any) {
console.error("Excepción cargando fracciones americanas:", e);
toast.error(`Error de conexión: ${e.message || e}`);
} finally {
loading = false;
}
}
function handleSelect(item: USTariffFraction) {
if (onSelect) onSelect(item);
open = false;
}
</script>
<Dialog.Root bind:open={open}>
<Dialog.Content class="sm:max-w-[800px] max-h-[80vh] flex flex-col">
<Dialog.Header>
<Dialog.Title>Seleccionar Fracción Americana</Dialog.Title>
<Dialog.Description>
Seleccione la fracción arancelaria (HTS) del catálogo.
</Dialog.Description>
</Dialog.Header>
<div class="relative w-full my-2">
<Search class="absolute left-2.5 top-2.5 h-4 w-4 text-muted-foreground" />
<Input
type="search"
placeholder="Buscar por código o descripción..."
class="pl-9"
bind:value={searchTerm}
/>
</div>
<div class="flex-1 overflow-y-auto border rounded-md min-h-[300px]">
{#if loading}
<div class="flex flex-col items-center justify-center h-48 gap-2 text-muted-foreground">
<Loader2 class="h-8 w-8 animate-spin text-primary" />
<p>Cargando catálogo...</p>
</div>
{:else if filteredItems.length === 0}
<div class="flex flex-col items-center justify-center h-48 text-muted-foreground">
<p>No se encontraron fracciones.</p>
</div>
{:else}
<Table.Root>
<Table.Header>
<Table.Row>
<Table.Head class="w-[150px]">Código (HTS)</Table.Head>
<Table.Head>Descripción</Table.Head>
</Table.Row>
</Table.Header>
<Table.Body>
{#each filteredItems as item}
<Table.Row
class="cursor-pointer hover:bg-accent/50 transition-colors"
onclick={() => handleSelect(item)}
>
<Table.Cell>
<div class="flex items-center gap-2">
<Hash class="h-3 w-3 text-blue-500" />
<span class="font-mono font-bold text-blue-600 dark:text-blue-400">
{item.code}
</span>
</div>
</Table.Cell>
<Table.Cell class="font-medium text-sm">
{item.description || '-'}
</Table.Cell>
</Table.Row>
{/each}
</Table.Body>
</Table.Root>
{/if}
</div>
<Dialog.Footer>
<div class="text-xs text-muted-foreground self-center mr-auto">
{filteredItems.length} registros encontrados
</div>
<Button variant="outline" onclick={() => open = false}>Cancelar</Button>
</Dialog.Footer>
</Dialog.Content>
</Dialog.Root>

View File

@@ -36,6 +36,7 @@
import CurrencySelectorDialog from '$lib/components/dashboard/goods/modales/currency-selector-dialog.svelte';
import CountrySelectorDialog from '$lib/components/dashboard/goods/modales/country-selector-dialog.svelte';
import FractionSelectorDialog from '$lib/components/dashboard/goods/modales/fraction-selector-dialog.svelte';
import USFractionSelectorDialog from '$lib/components/dashboard/goods/modales/us-fraction-selector-dialog.svelte';
// --- PROPS ---
let { partId = null, formType = 'inv' }: { partId?: number | null, formType?: 'inv' | 'fa' } = $props();
@@ -57,6 +58,7 @@
let showCurrencyModal = $state(false);
let showCountryModal = $state(false);
let showFractionModal = $state(false);
let showUSFractionModal = $state(false);
// Descripciones Visuales
let selectedClientName = $state("");
@@ -222,6 +224,7 @@
selectedCountryName = country.description_es;
}
function handleFractionSelect(item: any) { formData.fraction = item.fraction; }
function handleUSFractionSelect(item: any) { formData.us_fraction = item.code; }
// --- SUBMIT ---
async function handleSubmit() {
@@ -436,7 +439,10 @@
</div>
<div class="md:col-span-6 space-y-2">
<Label for="fa_us_fraction">Fracción Americana</Label>
<Input id="fa_us_fraction" bind:value={formData.us_fraction} maxlength={10} class="font-mono" placeholder="HTS Code"/>
<div class="flex gap-2">
<Input id="fa_us_fraction" bind:value={formData.us_fraction} maxlength={10} class="font-mono cursor-pointer" placeholder="HTS Code" readonly onclick={() => showUSFractionModal = true}/>
<Button variant="outline" size="icon" type="button" onclick={() => showUSFractionModal = true} class="shrink-0"><FolderSearch class="h-4 w-4" /></Button>
</div>
</div>
<div class="md:col-span-6 space-y-2">
<Label for="fa_sector">Sector</Label>
@@ -691,7 +697,10 @@
<div class="grid grid-cols-1 md:grid-cols-12 gap-6 pt-4 border-t">
<div class="md:col-span-6 space-y-2">
<Label for="frac_us">Fracción Americana (HTS)</Label>
<Input id="frac_us" bind:value={formData.us_fraction} maxlength={16} placeholder="Ej: 8501.10.00" class="font-mono"/>
<div class="flex gap-2">
<Input id="frac_us" bind:value={formData.us_fraction} maxlength={16} placeholder="Ej: 8501.10.00" class="font-mono cursor-pointer" readonly onclick={() => showUSFractionModal = true}/>
<Button variant="outline" size="icon" type="button" onclick={() => showUSFractionModal = true} class="shrink-0"><FolderSearch class="h-4 w-4" /></Button>
</div>
</div>
</div>
@@ -885,6 +894,7 @@
<CurrencySelectorDialog bind:open={showCurrencyModal} onSelect={handleCurrencySelect} />
<CountrySelectorDialog bind:open={showCountryModal} onSelect={handleCountrySelect} />
<FractionSelectorDialog bind:open={showFractionModal} onSelect={handleFractionSelect} />
<USFractionSelectorDialog bind:open={showUSFractionModal} onSelect={handleUSFractionSelect} />
<style>
:global(.required::after) {

View File

@@ -11,6 +11,8 @@
export let onClose: () => void;
export let onComplete: (result: any) => void;
export let getStatus: ((taskId: string) => Promise<any>) | null = null;
let progress = 0;
let statusMessage = "Iniciando...";
let pollingInterval: any = null;
@@ -42,7 +44,8 @@
if (!taskId) return;
try {
const response = await invoicesReportsApi.getTaskStatus(taskId);
const apiCall = getStatus || invoicesReportsApi.getTaskStatus;
const response = await apiCall(taskId);
if (response.state === 'PROCESSING' && response.info) {
progress = response.info.current || 0;
@@ -118,7 +121,7 @@
<Dialog.Footer>
{#if hasError}
<Button variant="secondary" on:click={onClose}>Cerrar</Button>
<Button variant="secondary" onclick={onClose}>Cerrar</Button>
{/if}
</Dialog.Footer>
</Dialog.Content>

View File

@@ -40,15 +40,15 @@ class CompanyStore {
if (preloadedCompanies && preloadedCompanies.length > 0) {
// Detectar si el tenant ha cambiado
const newTenantId = preloadedCompanies[0].tenant_id;
// Si el tenant cambió, limpiar el store primero
if (this._currentTenantId !== null && this._currentTenantId !== newTenantId) {
this.clear();
}
this._currentTenantId = newTenantId;
this._companies = preloadedCompanies;
// Si hay compañías y no hay una activa, seleccionar la primera o la guardada
if (this._companies.length > 0 && !this._activeCompany) {
// Intentar restaurar la compañía guardada
@@ -67,13 +67,13 @@ class CompanyStore {
}
return;
}
// Si no hay datos pre-cargados, hacer fetch (fallback)
// Solo en el navegador, nunca durante SSR
if (!browser) {
return;
}
this._loading = true;
try {
const response = await fetch('/api/v1/a76/company/my-companies', {
@@ -81,22 +81,22 @@ class CompanyStore {
});
if (response.ok) {
const newCompanies = await response.json();
// Detectar si el tenant ha cambiado
if (newCompanies.length > 0) {
const newTenantId = newCompanies[0].tenant_id;
// Si el tenant cambió, limpiar el store primero
if (this._currentTenantId !== null && this._currentTenantId !== newTenantId) {
this.clear();
}
this._currentTenantId = newTenantId;
}
this._companies = newCompanies;
// Si hay compañías y no hay una activa, seleccionar la primera
if (this._companies.length > 0 && !this._activeCompany) {
this.setActiveCompany(this._companies[0], true); // silent=true para inicialización
@@ -123,23 +123,23 @@ class CompanyStore {
setActiveCompany(company: Company, silent: boolean = false) {
const previousCompanyId = this._activeCompany?.id;
this._activeCompany = company;
// Guardar en localStorage para persistencia
if (typeof window !== 'undefined') {
localStorage.setItem('activeCompanyId', company.id.toString());
}
// Guardar en cookie para acceso desde el servidor (SSR)
if (typeof document !== 'undefined') {
document.cookie = `active_company_id=${company.id}; path=/; max-age=${60 * 60 * 24 * 30}; SameSite=Lax`;
}
// Despachar evento personalizado solo si:
// 1. No es silent (no es inicialización)
// 2. Y realmente cambió la compañía (el ID es diferente)
if (!silent && typeof window !== 'undefined' && previousCompanyId !== company.id) {
window.dispatchEvent(new CustomEvent('companyChanged', {
detail: { companyId: company.id }
window.dispatchEvent(new CustomEvent('companyChanged', {
detail: { companyId: company.id }
}));
}
}
@@ -167,12 +167,12 @@ class CompanyStore {
this._companies = [];
this._loading = false;
this._currentTenantId = null;
// Limpiar localStorage
if (typeof window !== 'undefined') {
localStorage.removeItem('activeCompanyId');
}
// Limpiar cookie
if (typeof document !== 'undefined') {
document.cookie = 'active_company_id=; path=/; max-age=0';

View File

@@ -3,6 +3,7 @@
import { page } from '$app/stores';
import { invoicesApi, type Invoice, type OperationType } from '$lib/api/dashboard/a76/invoices';
import { invoicesReportsApi } from '$lib/api/dashboard/a76/reports/reports-invoices';
import { consolidatedReportsApi } from '$lib/api/dashboard/a76/reports/reports-consolidated';
import DataTable from '$lib/components/dashboard/invoices/data-table.svelte';
import { createColumns } from '$lib/components/dashboard/invoices/columns.js';
import * as Card from '$lib/components/ui/card';
@@ -12,7 +13,7 @@
import type { PageData } from './$types';
import { browser } from '$app/environment';
import { companyStore } from '$lib/stores/company.svelte';
import { Plus, RefreshCw, FileDown, RotateCcw } from 'lucide-svelte';
import { Plus, RefreshCw, FileText, RotateCcw, Boxes } from 'lucide-svelte';
// IMPORTANTE: Asegúrate de tener instalada svelte-sonner para las notificaciones
import { toast } from "svelte-sonner";
@@ -334,6 +335,7 @@
// Estado para el diálogo de progreso
let showProgressDialog = $state(false);
let currentTaskId = $state<string | null>(null);
let currentStatusFunction = $state<((taskId: string) => Promise<any>) | null>(null);
// Utilidad para convertir Base64 a Blob
function base64ToBlob(base64: string, type: string) {
@@ -361,6 +363,7 @@
// 2. Abrir diálogo de progreso
currentTaskId = task_id;
currentStatusFunction = invoicesReportsApi.getTaskStatus;
showProgressDialog = true;
} catch (error) {
@@ -369,6 +372,30 @@
}
}
async function handleDownloadConsolidated(invoice: any) {
if (!companyStore.activeCompany) {
toast.error("No hay empresa seleccionada");
return;
}
try {
// 1. Trigger: Iniciar la tarea en Celery (Consolidado)
const { task_id } = await consolidatedReportsApi.triggerPdfGeneration(
invoice.id,
companyStore.activeCompany.id
);
// 2. Abrir diálogo de progreso
currentTaskId = task_id;
currentStatusFunction = consolidatedReportsApi.getTaskStatus;
showProgressDialog = true;
} catch (error) {
console.error(error);
toast.error("No se pudo iniciar la descarga del consolidado");
}
}
function onPdfComplete(result: any) {
// Esta función se llama cuando el diálogo reporta SUCCESS
try {
@@ -454,6 +481,7 @@
function closeProgressDialog() {
showProgressDialog = false;
currentTaskId = null;
currentStatusFunction = null;
}
// --- AQUÍ PASAMOS LA FUNCIÓN DE DESCARGA A LAS COLUMNAS ---
@@ -579,6 +607,7 @@
<PdfProgressDialog
bind:open={showProgressDialog}
taskId={currentTaskId}
getStatus={currentStatusFunction}
onComplete={onPdfComplete}
onClose={closeProgressDialog}
/>
@@ -597,8 +626,12 @@
Desactualizar
</Button>
<Button variant="outline" size="sm" onclick={() => selectedInvoice && handleDownloadPdf(selectedInvoice)} disabled={!selectedInvoice}>
<FileDown class="h-4 w-4 mr-2" />
Descargar PDF
<FileText class="h-4 w-4 mr-2" />
Factura
</Button>
<Button variant="outline" size="sm" onclick={() => selectedInvoice && handleDownloadConsolidated(selectedInvoice)} disabled={!selectedInvoice}>
<Boxes class="h-4 w-4 mr-2" />
Consolidado
</Button>
</div>
</div>

5
reinicio.sh Executable file
View File

@@ -0,0 +1,5 @@
#bin/bash
docker compose down
docker compose up -d --build