bmc_hub/scripts/import_economic_csv.py
Christian 42b766b31e Add scripts for managing e-conomic customer numbers
- Implemented fix_economic_numbers.sh to correct invalid 10-digit economic_customer_number entries in the database.
- Created import_economic_csv.py for importing customers from a CSV file into the Hub database, handling updates and new entries.
- Developed match_all_customers.sh to match all customers from a CSV file to the Hub database, updating or creating records as necessary.
- Added simple_match.sh for a straightforward matching process of customers from a CSV file to the Hub database.
2026-01-06 19:59:07 +01:00

86 lines
2.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
Import e-conomic customers fra CSV til Hub database
"""
import sys
import csv
import subprocess
def run_sql(sql):
"""Kør SQL kommando via podman"""
cmd = [
'sudo', 'podman', 'exec', '-i', 'bmc-hub-postgres-prod',
'psql', '-U', 'bmc_hub', '-d', 'bmc_hub', '-t', '-c', sql
]
result = subprocess.run(cmd, capture_output=True, text=True)
return result.stdout.strip()
def main():
if len(sys.argv) < 2:
print("Brug: python3 import_economic_csv.py <csv-file>")
sys.exit(1)
csv_file = sys.argv[1]
print(f"📂 Læser {csv_file}...")
updated = 0
created = 0
skipped = 0
with open(csv_file, 'r', encoding='utf-8-sig') as f:
# Skip første 3 linjer (header osv.)
for _ in range(3):
next(f)
reader = csv.DictReader(f, delimiter=';')
for row in reader:
nummer = row.get('Nr.', '').strip()
navn = row.get('Navn', '').strip()
if not nummer or not navn:
continue
# Skip non-numeric
if not nummer.isdigit():
continue
nummer_int = int(nummer)
# Skip for store numre
if nummer_int > 999999999:
print(f"⚠️ Springer over {navn} - Nummer for stort: {nummer}")
skipped += 1
continue
print(f"[{updated + created + skipped + 1}] {nummer} - {navn}")
# Escape quotes
navn_esc = navn.replace("'", "''")
# Find kunde
find_sql = f"SELECT id FROM customers WHERE LOWER(name) = LOWER('{navn_esc}') LIMIT 1;"
kunde_id = run_sql(find_sql).strip()
if kunde_id and kunde_id.isdigit():
# Update
update_sql = f"UPDATE customers SET economic_customer_number = {nummer_int} WHERE id = {kunde_id};"
run_sql(update_sql)
print(f" ✅ Opdateret ID {kunde_id}")
updated += 1
else:
# Create
create_sql = f"INSERT INTO customers (name, economic_customer_number, created_at, updated_at) VALUES ('{navn_esc}', {nummer_int}, NOW(), NOW());"
run_sql(create_sql)
print(f" Oprettet")
created += 1
print(f"\n✅ Færdig!")
print(f" Opdateret: {updated}")
print(f" Oprettet: {created}")
print(f" Sprunget over: {skipped}")
print(f" Total: {updated + created}")
if __name__ == '__main__':
main()