Improve e-conomic sync: Only match new customers, verify existing ones
This commit is contained in:
parent
5d8617bed3
commit
030071e8d5
@ -350,25 +350,32 @@ async def sync_from_economic() -> Dict[str, Any]:
|
||||
(cvr,)
|
||||
)
|
||||
|
||||
# If no CVR match, try normalized name
|
||||
# If no CVR match, try normalized name (only for customers without economic number)
|
||||
if not matched and name:
|
||||
normalized = normalize_name(name)
|
||||
all_customers = execute_query("SELECT id, name FROM customers WHERE economic_customer_number IS NULL")
|
||||
for hub_customer in all_customers:
|
||||
hub_customers = execute_query("SELECT id, name, economic_customer_number FROM customers WHERE economic_customer_number IS NULL")
|
||||
for hub_customer in hub_customers:
|
||||
if normalize_name(hub_customer['name']) == normalized:
|
||||
matched = [hub_customer]
|
||||
break
|
||||
|
||||
if matched:
|
||||
# Update Hub customer with e-conomic number
|
||||
execute_query(
|
||||
"UPDATE customers SET economic_customer_number = %s, last_synced_at = NOW() WHERE id = %s",
|
||||
(customer_number, matched[0]['id'])
|
||||
)
|
||||
matched_count += 1
|
||||
logger.info(f"🔗 Matchet: {matched[0]['name']} → e-conomic kunde #{customer_number} (CVR: {cvr or 'navn-match'})")
|
||||
current_number = matched[0].get('economic_customer_number')
|
||||
if current_number is None:
|
||||
execute_query(
|
||||
"UPDATE customers SET economic_customer_number = %s, last_synced_at = NOW() WHERE id = %s",
|
||||
(customer_number, matched[0]['id'])
|
||||
)
|
||||
matched_count += 1
|
||||
logger.info(f"🔗 Matchet: {matched[0]['name']} → e-conomic kunde #{customer_number} (CVR: {cvr or 'navn-match'})")
|
||||
else:
|
||||
# Already has number, just update sync timestamp
|
||||
execute_query("UPDATE customers SET last_synced_at = NOW() WHERE id = %s", (matched[0]['id'],))
|
||||
logger.debug(f"✓ Verificeret: {matched[0]['name']} → #{customer_number}")
|
||||
else:
|
||||
not_matched_count += 1
|
||||
logger.debug(f"❌ Ikke matchet: {name} (CVR: {cvr or 'ingen'})")
|
||||
|
||||
logger.info(f"✅ e-conomic sync fuldført: {matched_count} matchet, {not_matched_count} ikke matchet af {len(economic_customers)} totalt")
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user