diff --git a/app/customers/backend/router.py b/app/customers/backend/router.py index 6dfbd2c..db53da2 100644 --- a/app/customers/backend/router.py +++ b/app/customers/backend/router.py @@ -1355,3 +1355,51 @@ async def get_subscription_comment(customer_id: int): except Exception as e: logger.error(f"❌ Error fetching subscription comment: {e}") raise HTTPException(status_code=500, detail=str(e)) + + +@router.get("/customers/{customer_id}/subscriptions/billing-matrix") +async def get_subscription_billing_matrix( + customer_id: int, + months: int = Query(default=12, ge=1, le=60, description="Number of months to show") +): + """ + Get subscription billing matrix showing monthly invoiced amounts per product + + Rows: Products from e-conomic invoices + Columns: Months + Cells: Amount, status (paid/invoiced/missing), invoice reference + + Data source: e-conomic sales invoices only + """ + try: + # Verify customer exists + customer = execute_query_single( + "SELECT id, economic_customer_number FROM customers WHERE id = %s", + (customer_id,) + ) + + if not customer: + raise HTTPException(status_code=404, detail=f"Customer {customer_id} not found") + + if not customer.get('economic_customer_number'): + logger.warning(f"⚠️ Customer {customer_id} has no e-conomic number") + return { + "customer_id": customer_id, + "error": "Customer not linked to e-conomic", + "products": [] + } + + # Generate matrix + from app.services.subscription_matrix import get_subscription_matrix_service + matrix_service = get_subscription_matrix_service() + matrix = await matrix_service.generate_billing_matrix(customer_id, months) + + logger.info(f"✅ Generated billing matrix for customer {customer_id}") + return matrix + + except HTTPException: + raise + except Exception as e: + logger.error(f"❌ Error generating billing matrix: {e}") + raise HTTPException(status_code=500, detail=str(e)) + diff --git a/app/customers/frontend/customer_detail.html b/app/customers/frontend/customer_detail.html index 23a62ae..c8b5796 100644 --- a/app/customers/frontend/customer_detail.html +++ b/app/customers/frontend/customer_detail.html @@ -301,6 +301,11 @@ Abonnnents tjek +