feat: Add activity logging for BMC Office subscription imports
This commit is contained in:
parent
5f486578c7
commit
50fbb5ab92
@ -76,9 +76,39 @@ async def upload_bmc_office_subscriptions(file: UploadFile = File(...)):
|
||||
|
||||
logger.info(f"✅ Fundet {len(customers)} kunder i databasen")
|
||||
|
||||
# Get current statistics before deletion
|
||||
old_stats = execute_query("""
|
||||
SELECT
|
||||
COUNT(*) as total_records,
|
||||
ROUND(SUM(CASE WHEN active THEN total_inkl_moms ELSE 0 END)::numeric, 2) as total_value_dkk
|
||||
FROM bmc_office_subscription_totals
|
||||
""")[0]
|
||||
|
||||
old_total = old_stats['total_records']
|
||||
old_value = float(old_stats['total_value_dkk'] or 0)
|
||||
|
||||
# Get affected customers before deletion (for activity log)
|
||||
affected_customers = execute_query("""
|
||||
SELECT DISTINCT customer_id, COUNT(*) as count,
|
||||
ROUND(SUM(total_inkl_moms)::numeric, 2) as value
|
||||
FROM bmc_office_subscription_totals
|
||||
WHERE active = true
|
||||
GROUP BY customer_id
|
||||
""")
|
||||
|
||||
# Clear existing subscriptions
|
||||
execute_update("DELETE FROM bmc_office_subscriptions", ())
|
||||
logger.info("🗑️ Ryddet eksisterende abonnementer")
|
||||
logger.info(f"🗑️ Ryddet {old_total} eksisterende abonnementer (værdi: {old_value:,.2f} DKK)")
|
||||
|
||||
# Log deletion for each affected customer
|
||||
for customer in affected_customers:
|
||||
execute_update(
|
||||
"""INSERT INTO customer_activities (customer_id, activity_type, description, user_id)
|
||||
VALUES (%s, %s, %s, %s)""",
|
||||
(customer['customer_id'], 'bmc_office_sync',
|
||||
f"BMC Office abonnementer fjernet før ny import: {customer['count']} abonnementer (værdi: {float(customer['value']):,.2f} DKK)",
|
||||
1) # System user
|
||||
)
|
||||
|
||||
# Process rows
|
||||
imported = 0
|
||||
@ -118,6 +148,26 @@ 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:
|
||||
execute_update(
|
||||
"""INSERT INTO customer_activities (customer_id, activity_type, description, user_id)
|
||||
VALUES (%s, %s, %s, %s)""",
|
||||
(customer['customer_id'], 'bmc_office_sync',
|
||||
f"BMC Office abonnementer importeret: {customer['count']} abonnementer (værdi: {float(customer['value']):,.2f} DKK) - Produkter: {customer['products'][:200]}",
|
||||
1) # System user
|
||||
)
|
||||
|
||||
beskrivelse = str(row.get('Beskrivelse', '')).strip()
|
||||
faktura_firma_id = str(row.get('FakturaFirmaID', '')).strip()
|
||||
faktura_firma_name = str(row.get('FakturaFirma', '')).strip()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user