77 lines
3.3 KiB
Python
77 lines
3.3 KiB
Python
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()
|