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>
99 lines
3.4 KiB
Python
99 lines
3.4 KiB
Python
"""
|
|
Delete all imported orders from Oracle ROA and reset SQLite status.
|
|
Soft-delete: SET sters=1 on comenzi + comenzi_detalii.
|
|
Reset SQLite: clear id_comanda, id_partener, set status back to allow re-import.
|
|
"""
|
|
import sys, sqlite3, oracledb, os
|
|
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
|
|
|
|
DRY_RUN = '--execute' not in sys.argv
|
|
|
|
os.environ['PATH'] = r'C:\app\Server\product\18.0.0\dbhomeXE\bin' + ';' + os.environ.get('PATH','')
|
|
oracledb.init_oracle_client()
|
|
|
|
# Get imported orders
|
|
db = sqlite3.connect(r'C:\gomag-vending\api\data\import.db')
|
|
db.row_factory = sqlite3.Row
|
|
c = db.cursor()
|
|
|
|
c.execute("""
|
|
SELECT order_number, id_comanda, id_partener, customer_name
|
|
FROM orders
|
|
WHERE status = 'IMPORTED' AND id_comanda IS NOT NULL
|
|
""")
|
|
imported = [dict(r) for r in c.fetchall()]
|
|
|
|
print(f"Orders to delete: {len(imported)}")
|
|
if DRY_RUN:
|
|
print("*** DRY RUN — add --execute to actually delete ***\n")
|
|
|
|
# Step 1: Soft-delete in Oracle
|
|
conn = oracledb.connect(user='VENDING', password='ROMFASTSOFT', dsn='ROA')
|
|
cur = conn.cursor()
|
|
|
|
id_comandas = [o['id_comanda'] for o in imported]
|
|
|
|
# Verify none are invoiced
|
|
for id_cmd in id_comandas:
|
|
cur.execute("SELECT COUNT(*) FROM vanzari WHERE id_comanda = :1 AND sters = 0", [id_cmd])
|
|
cnt = cur.fetchone()[0]
|
|
if cnt > 0:
|
|
print(f" ERROR: comanda {id_cmd} has {cnt} active invoices! Aborting.")
|
|
sys.exit(1)
|
|
|
|
print("Oracle: no invoices found on any order — safe to delete")
|
|
|
|
for id_cmd in id_comandas:
|
|
order_num = [o['order_number'] for o in imported if o['id_comanda'] == id_cmd][0]
|
|
|
|
if DRY_RUN:
|
|
# Just show what would happen
|
|
cur.execute("SELECT COUNT(*) FROM comenzi_detalii WHERE id_comanda = :1 AND sters = 0", [id_cmd])
|
|
det_cnt = cur.fetchone()[0]
|
|
print(f" Would delete: comanda {id_cmd} (order {order_num}) + {det_cnt} detail lines")
|
|
else:
|
|
# Soft-delete detail lines
|
|
cur.execute("UPDATE comenzi_detalii SET sters = 1 WHERE id_comanda = :1 AND sters = 0", [id_cmd])
|
|
det_deleted = cur.rowcount
|
|
|
|
# Soft-delete order header
|
|
cur.execute("UPDATE comenzi SET sters = 1 WHERE id_comanda = :1 AND sters = 0", [id_cmd])
|
|
hdr_deleted = cur.rowcount
|
|
|
|
print(f" Deleted: comanda {id_cmd} (order {order_num}): header={hdr_deleted}, details={det_deleted}")
|
|
|
|
if not DRY_RUN:
|
|
conn.commit()
|
|
print(f"\nOracle: {len(id_comandas)} orders soft-deleted (sters=1)")
|
|
else:
|
|
print(f"\nOracle: DRY RUN — nothing changed")
|
|
|
|
conn.close()
|
|
|
|
# Step 2: Reset SQLite
|
|
if not DRY_RUN:
|
|
c.execute("""
|
|
UPDATE orders SET
|
|
status = 'SKIPPED',
|
|
id_comanda = NULL,
|
|
id_partener = NULL,
|
|
id_adresa_facturare = NULL,
|
|
id_adresa_livrare = NULL,
|
|
error_message = NULL,
|
|
factura_serie = NULL,
|
|
factura_numar = NULL,
|
|
factura_total_fara_tva = NULL,
|
|
factura_total_tva = NULL,
|
|
factura_total_cu_tva = NULL,
|
|
factura_data = NULL,
|
|
invoice_checked_at = NULL
|
|
WHERE status = 'IMPORTED' AND id_comanda IS NOT NULL
|
|
""")
|
|
db.commit()
|
|
print(f"SQLite: {c.rowcount} orders reset to SKIPPED (id_comanda/id_partener cleared)")
|
|
else:
|
|
print(f"SQLite: DRY RUN — would reset {len(imported)} orders to SKIPPED")
|
|
|
|
db.close()
|
|
print("\nDone!" if not DRY_RUN else "\nDone (dry run). Run with --execute to apply.")
|