add: scripts for invoice-order matching and SKU discovery
Analysis scripts to match GoMag orders with Oracle invoices by date/client/total, then compare line items by price to discover SKU → id_articol mappings. Generates SQL for nom_articole codmat updates and CSV for ARTICOLE_TERTI repackaging/set mappings. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
112
scripts/debug_match.py
Normal file
112
scripts/debug_match.py
Normal file
@@ -0,0 +1,112 @@
|
||||
"""Debug matching for specific order 480102897"""
|
||||
import oracledb
|
||||
import os
|
||||
import sys
|
||||
import sqlite3
|
||||
from difflib import SequenceMatcher
|
||||
|
||||
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
|
||||
os.environ['PATH'] = r'C:\app\Server\product\18.0.0\dbhomeXE\bin' + ';' + os.environ.get('PATH','')
|
||||
oracledb.init_oracle_client()
|
||||
|
||||
# Get GoMag order
|
||||
db = sqlite3.connect(r'C:\gomag-vending\api\data\import.db')
|
||||
db.row_factory = sqlite3.Row
|
||||
c = db.cursor()
|
||||
|
||||
c.execute("SELECT * FROM orders WHERE order_number = '480102897'")
|
||||
order = dict(c.fetchone())
|
||||
c.execute("SELECT * FROM order_items WHERE order_number = '480102897'")
|
||||
items = [dict(r) for r in c.fetchall()]
|
||||
db.close()
|
||||
|
||||
print(f"=== GoMag Order 480102897 ===")
|
||||
print(f" Client: {order['customer_name']}")
|
||||
print(f" Billing: {order['billing_name']}")
|
||||
print(f" Shipping: {order['shipping_name']}")
|
||||
print(f" Date: {order['order_date']}")
|
||||
print(f" Total: {order['order_total']}")
|
||||
print(f" Status: {order['status']}")
|
||||
print(f" Items ({len(items)}):")
|
||||
for it in items:
|
||||
print(f" SKU={it['sku']:20s} qty={it['quantity']:6.1f} price={it['price']:8.2f} {it['product_name']}")
|
||||
|
||||
# Now search Oracle for ALL invoices around that date with similar total
|
||||
conn = oracledb.connect(user='VENDING', password='ROMFASTSOFT', dsn='ROA')
|
||||
cur = conn.cursor()
|
||||
|
||||
order_date = str(order['order_date'])[:10]
|
||||
order_total = order['order_total']
|
||||
|
||||
print(f"\n=== Oracle invoices near date {order_date}, total ~{order_total} ===")
|
||||
cur.execute("""
|
||||
SELECT v.id_vanzare, v.numar_act, v.serie_act,
|
||||
TO_CHAR(v.data_act, 'YYYY-MM-DD') as data_act,
|
||||
v.total_cu_tva, v.id_part,
|
||||
p.denumire as partener, p.prenume
|
||||
FROM vanzari v
|
||||
LEFT JOIN nom_parteneri p ON v.id_part = p.id_part
|
||||
WHERE v.sters = 0
|
||||
AND v.data_act >= TO_DATE(:1, 'YYYY-MM-DD') - 3
|
||||
AND v.data_act <= TO_DATE(:2, 'YYYY-MM-DD') + 3
|
||||
AND v.total_cu_tva > 0
|
||||
ORDER BY ABS(v.total_cu_tva - :3), v.data_act
|
||||
""", [order_date, order_date, order_total])
|
||||
|
||||
print(f"{'NR_FACT':>8s} {'SERIE':>5s} {'DATA':>12s} {'TOTAL_CU':>10s} {'DIFF':>10s} {'PARTENER':40s}")
|
||||
candidates = []
|
||||
for r in cur:
|
||||
total = float(r[4] or 0)
|
||||
diff = total - order_total
|
||||
partener = ((r[6] or '') + ' ' + (r[7] or '')).strip()
|
||||
print(f"{str(r[1]):>8s} {str(r[2] or ''):>5s} {r[3]:>12s} {total:10.2f} {diff:+10.2f} {partener:40s}")
|
||||
candidates.append({'numar_act': r[1], 'serie_act': r[2], 'data_act': r[3],
|
||||
'total': total, 'partener': partener, 'id_vanzare': r[0]})
|
||||
if len(candidates) >= 20:
|
||||
break
|
||||
|
||||
# Also search by client name
|
||||
print(f"\n=== Oracle invoices by name 'STOICA' or 'LIVIU' near that date ===")
|
||||
cur.execute("""
|
||||
SELECT v.id_vanzare, v.numar_act, v.serie_act,
|
||||
TO_CHAR(v.data_act, 'YYYY-MM-DD') as data_act,
|
||||
v.total_cu_tva,
|
||||
p.denumire as partener, p.prenume
|
||||
FROM vanzari v
|
||||
LEFT JOIN nom_parteneri p ON v.id_part = p.id_part
|
||||
WHERE v.sters = 0
|
||||
AND v.data_act >= TO_DATE(:1, 'YYYY-MM-DD') - 5
|
||||
AND v.data_act <= TO_DATE(:2, 'YYYY-MM-DD') + 5
|
||||
AND (UPPER(p.denumire) LIKE '%STOICA%' OR UPPER(p.prenume) LIKE '%STOICA%'
|
||||
OR UPPER(p.denumire) LIKE '%LIVIU%' OR UPPER(p.prenume) LIKE '%LIVIU%'
|
||||
OR UPPER(p.denumire) LIKE '%SLM%')
|
||||
ORDER BY v.data_act DESC
|
||||
""", [order_date, order_date])
|
||||
|
||||
for r in cur:
|
||||
total = float(r[4] or 0)
|
||||
partener = ((r[5] or '') + ' ' + (r[6] or '')).strip()
|
||||
print(f" {str(r[1]):>8s} {str(r[2] or ''):>5s} {r[3]:>12s} {total:10.2f} {partener}")
|
||||
|
||||
# Show details of invoice 4035
|
||||
print(f"\n=== Details of invoice VM4035 ===")
|
||||
cur.execute("""
|
||||
SELECT v.id_vanzare, TO_CHAR(v.data_act, 'YYYY-MM-DD'), v.total_cu_tva,
|
||||
p.denumire, p.prenume
|
||||
FROM vanzari v
|
||||
LEFT JOIN nom_parteneri p ON v.id_part = p.id_part
|
||||
WHERE v.numar_act = 4035 AND v.serie_act = 'VM' AND v.sters = 0
|
||||
""")
|
||||
row = cur.fetchone()
|
||||
if row:
|
||||
print(f" Date: {row[1]}, Total: {float(row[2]):.2f}, Client: {row[3]} {row[4]}")
|
||||
cur.execute("""
|
||||
SELECT a.codmat, a.denumire, vd.cantitate, vd.pret, vd.pret_cu_tva
|
||||
FROM vanzari_detalii vd
|
||||
LEFT JOIN nom_articole a ON vd.id_articol = a.id_articol
|
||||
WHERE vd.id_vanzare = :1 AND vd.sters = 0
|
||||
""", [row[0]])
|
||||
for r in cur:
|
||||
print(f" COD={r[0] or '':20s} qty={float(r[2]):6.1f} pret={float(r[3]):8.2f} pretcu={float(r[4]):8.2f} {r[1]}")
|
||||
|
||||
conn.close()
|
||||
Reference in New Issue
Block a user