84 lines
3.5 KiB
Python
84 lines
3.5 KiB
Python
import sys
|
|
import os
|
|
os.environ["CORE_DB_HOST"] = "localhost"
|
|
sys.path.append(os.path.join(os.getcwd(), 'backend'))
|
|
from core.database import get_core_db
|
|
# Import dependent models to ensure they are registered
|
|
from api.v1.modules.a76.pedmientos.models.pedimentos import Pedimentos
|
|
from api.v1.modules.a76.invoices.models import InvoiceHeader
|
|
from sqlalchemy import text
|
|
|
|
db = next(get_core_db())
|
|
|
|
try:
|
|
# PATCH: Update Invoice 147 to Tenant 1 using RAW SQL to bypass ORM FK checks
|
|
print("PATCHING: Updating Invoice 147 tenant_id to 1 (RAW SQL)")
|
|
db.execute(text("UPDATE a76.invoice_header SET tenant_id = 1 WHERE id = 147"))
|
|
db.commit()
|
|
|
|
print("Checking for invoices with operation_type='exp'...")
|
|
|
|
# Query for a sample of export invoices
|
|
invoices = db.query(InvoiceHeader).filter(
|
|
InvoiceHeader.operation_type == 'exp'
|
|
).limit(20).all()
|
|
|
|
print(f"Found {len(invoices)} export invoices.")
|
|
for inv in invoices:
|
|
manifest_num = inv.compliance_mx.manifest_number if inv.compliance_mx else "No Compliance"
|
|
print(f"ID: {inv.id}, Tenant: {inv.tenant_id}, Company: {inv.company_id}, DeletedAt: {inv.deleted_at}, System: '{inv.system}', Type: '{inv.invoice_type.key if hasattr(inv.invoice_type, 'key') else inv.invoice_type}', Op: '{inv.operation_type}', Manifest: '{manifest_num}'")
|
|
|
|
print("\n--- Simulating Backend Query ---")
|
|
from api.v1.modules.a76.invoices.models import InvoiceComplianceMx
|
|
|
|
# Simulate: company_id=1, manifest_number="48720384"
|
|
target_manifest = "48720384"
|
|
company_id = 1
|
|
|
|
print(f"\n--- Global Search for '{target_manifest}' ---")
|
|
|
|
# Check InvoiceComplianceMx (manifest_number)
|
|
compliance_matches = db.query(InvoiceComplianceMx).filter(
|
|
InvoiceComplianceMx.manifest_number.ilike(f"%{target_manifest}%")
|
|
).all()
|
|
print(f"Matches in InvoiceComplianceMx [manifest_number]: {len(compliance_matches)}")
|
|
for m in compliance_matches:
|
|
print(f" - ComplianceID: {m.id}, InvoiceID: {m.invoice_id}, Manifest: '{m.manifest_number}'")
|
|
# Get parent invoice info
|
|
inv = db.query(InvoiceHeader).get(m.invoice_id)
|
|
if inv:
|
|
print(f" -> Invoice: {inv.invoice_number}, System: {inv.system}, Tenant: {inv.tenant_id}, Company: {inv.company_id}, DeletedAt: {inv.deleted_at}")
|
|
|
|
# Check InvoiceHeader (invoice_number)
|
|
header_matches = db.query(InvoiceHeader).filter(
|
|
InvoiceHeader.invoice_number.ilike(f"%{target_manifest}%")
|
|
).all()
|
|
print(f"Matches in InvoiceHeader [invoice_number]: {len(header_matches)}")
|
|
for h in header_matches:
|
|
print(f" - ID: {h.id}, Number: '{h.invoice_number}', Tenant: {h.tenant_id}, Company: {h.company_id}")
|
|
|
|
# Check InvoiceHeader (invoice_ref)
|
|
ref_matches = db.query(InvoiceHeader).filter(
|
|
InvoiceHeader.invoice_ref.ilike(f"%{target_manifest}%")
|
|
).all()
|
|
print(f"Matches in InvoiceHeader [invoice_ref]: {len(ref_matches)}")
|
|
for h in ref_matches:
|
|
print(f" - ID: {h.id}, Ref: '{h.invoice_ref}', Tenant: {h.tenant_id}, Company: {h.company_id}")
|
|
|
|
except Exception as e:
|
|
print(f"Error: {e}")
|
|
|
|
|
|
print(f"\n--- Instpecting Invoice 147 ---")
|
|
inv147 = db.query(InvoiceHeader).get(147)
|
|
if inv147:
|
|
print(f"ID: {inv147.id}, Number: {inv147.invoice_number}, OpType: {inv147.operation_type}, System: {inv147.system}")
|
|
if inv147.compliance_mx:
|
|
print(f"Compliance: Manifest={inv147.compliance_mx.manifest_number}")
|
|
else:
|
|
print(f"Compliance: None")
|
|
else:
|
|
print("Invoice 147 not found")
|
|
|
|
|