diff --git a/app/customers/backend/bmc_office_router.py b/app/customers/backend/bmc_office_router.py index b534e33..2b0621c 100644 --- a/app/customers/backend/bmc_office_router.py +++ b/app/customers/backend/bmc_office_router.py @@ -11,6 +11,7 @@ import logging from io import BytesIO from app.core.database import execute_query, execute_update +from app.services.customer_activity_logger import CustomerActivityLogger logger = logging.getLogger(__name__) @@ -146,24 +147,6 @@ async def upload_bmc_office_subscriptions(file: UploadFile = File(...)): antal = parse_danish_number(row.get('Antal', 1)) pris = parse_danish_number(row.get('Pris', 0)) rabat = parse_danish_number(row.get('Rabat', 0)) - # Get new subscriptions per customer for activity log - new_subscriptions = execute_query(""" - SELECT customer_id, COUNT(*) as count, - ROUND(SUM(total_inkl_moms)::numeric, 2) as value, - string_agg(text, ', ' ORDER BY text) as products - FROM bmc_office_subscription_totals - WHERE active = true - GROUP BY customer_id - """) - - # Log import for each customer - for customer in new_subscriptions: - CustomerActivityLogger.log( - customer_id=customer['customer_id'], - activity_type='bmc_office_sync', - description=f"BMC Office abonnementer importeret: {customer['count']} abonnementer (værdi: {float(customer['value']):,.2f} DKK) - Produkter: {customer['products'][:200]}" - ) - beskrivelse = str(row.get('Beskrivelse', '')).strip() faktura_firma_id = str(row.get('FakturaFirmaID', '')).strip() faktura_firma_name = str(row.get('FakturaFirma', '')).strip() @@ -194,6 +177,24 @@ async def upload_bmc_office_subscriptions(file: UploadFile = File(...)): FROM bmc_office_subscription_totals """)[0] + # Get new subscriptions per customer for activity log + new_subscriptions = execute_query(""" + SELECT customer_id, COUNT(*) as count, + ROUND(SUM(total_inkl_moms)::numeric, 2) as value, + string_agg(text, ', ' ORDER BY text) as products + FROM bmc_office_subscription_totals + WHERE active = true + GROUP BY customer_id + """) + + # Log import for each customer + for customer in new_subscriptions: + CustomerActivityLogger.log( + customer_id=customer['customer_id'], + activity_type='bmc_office_sync', + description=f"BMC Office abonnementer importeret: {customer['count']} abonnementer (værdi: {float(customer['value']):,.2f} DKK) - Produkter: {customer['products'][:200]}" + ) + logger.info(f"✅ Import færdig: {imported} importeret, {skipped} sprunget over") return {