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,)
|
(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:
|
if not matched and name:
|
||||||
normalized = normalize_name(name)
|
normalized = normalize_name(name)
|
||||||
all_customers = execute_query("SELECT id, name FROM customers WHERE economic_customer_number IS NULL")
|
hub_customers = execute_query("SELECT id, name, economic_customer_number FROM customers WHERE economic_customer_number IS NULL")
|
||||||
for hub_customer in all_customers:
|
for hub_customer in hub_customers:
|
||||||
if normalize_name(hub_customer['name']) == normalized:
|
if normalize_name(hub_customer['name']) == normalized:
|
||||||
matched = [hub_customer]
|
matched = [hub_customer]
|
||||||
break
|
break
|
||||||
|
|
||||||
if matched:
|
if matched:
|
||||||
# Update Hub customer with e-conomic number
|
# Update Hub customer with e-conomic number
|
||||||
execute_query(
|
current_number = matched[0].get('economic_customer_number')
|
||||||
"UPDATE customers SET economic_customer_number = %s, last_synced_at = NOW() WHERE id = %s",
|
if current_number is None:
|
||||||
(customer_number, matched[0]['id'])
|
execute_query(
|
||||||
)
|
"UPDATE customers SET economic_customer_number = %s, last_synced_at = NOW() WHERE id = %s",
|
||||||
matched_count += 1
|
(customer_number, matched[0]['id'])
|
||||||
logger.info(f"🔗 Matchet: {matched[0]['name']} → e-conomic kunde #{customer_number} (CVR: {cvr or 'navn-match'})")
|
)
|
||||||
|
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:
|
else:
|
||||||
not_matched_count += 1
|
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")
|
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