Fix: Brug economic_acc_number field fra Simply-CRM (ikke account_no)

This commit is contained in:
Christian 2026-01-05 11:28:45 +01:00
parent 1380369dff
commit 05ec5b5903

View File

@ -312,7 +312,18 @@ async def sync_economic_numbers_from_simplycrm():
async with SimplyCRMService() as simplycrm:
# Hent alle accounts fra Simply-CRM
logger.info("📥 Fetching accounts from Simply-CRM...")
query = "SELECT accountname, cf_854 FROM Accounts LIMIT 5000;"
# Først: Tjek hvilke felter der er tilgængelige
try:
test_query = "SELECT * FROM Accounts LIMIT 1;"
test_result = await simplycrm.query(test_query)
if test_result:
logger.info(f"📋 Available fields: {list(test_result[0].keys())}")
except Exception as e:
logger.warning(f"⚠️ Could not fetch sample fields: {e}")
# Query med standard felter + economic_acc_number
query = "SELECT id, accountname, economic_acc_number FROM Accounts LIMIT 5000;"
accounts = await simplycrm.query(query)
stats["simplycrm_accounts"] = len(accounts)
@ -326,19 +337,24 @@ async def sync_economic_numbers_from_simplycrm():
}
# Filter accounts med economic customer number
accounts_with_economic = [
acc for acc in accounts
if acc.get('cf_854') and str(acc.get('cf_854')).strip() not in ['', '0', 'null', 'NULL']
]
accounts_with_economic = []
for acc in accounts:
economic_number = acc.get('economic_acc_number')
if economic_number and str(economic_number).strip() not in ['', '0', 'null', 'NULL']:
accounts_with_economic.append({
'accountname': acc.get('accountname'),
'economic_number': str(economic_number).strip()
})
stats["accounts_with_economic_number"] = len(accounts_with_economic)
logger.info(f"{len(accounts_with_economic)} accounts have e-conomic customer numbers")
# Map company name → economic_customer_number
name_to_economic = {}
for acc in accounts_with_economic:
company_name = acc.get('accountname', '').strip()
economic_number = str(acc.get('cf_854', '')).strip()
for acc_data in accounts_with_economic:
company_name = acc_data['accountname'].strip()
economic_number = acc_data['economic_number']
if company_name and economic_number:
# Normalize navn til lowercase for matching