From 404e81a7a81ae65535925b61ffebb9790749a444 Mon Sep 17 00:00:00 2001 From: Christian Date: Tue, 27 Jan 2026 07:18:36 +0100 Subject: [PATCH] fix: Fetch all invoices then filter by customer v1.3.141: - Removed unsupported customer filter from API params - Fetch all invoices from /invoices/sent with pagination - Filter by customer number in code - Apply 13-month date filter after customer filter --- VERSION | 2 +- app/services/economic_service.py | 41 ++++++++++++++++++++++---------- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/VERSION b/VERSION index 3817580..71600e9 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.3.140 \ No newline at end of file +1.3.141 \ No newline at end of file diff --git a/app/services/economic_service.py b/app/services/economic_service.py index 5771045..711bc02 100644 --- a/app/services/economic_service.py +++ b/app/services/economic_service.py @@ -458,13 +458,12 @@ class EconomicService: logger.info(f"📅 Will filter invoices from {start_date} onwards (13 months for yearly billed items)") async with aiohttp.ClientSession() as session: - # Fetch invoices directly by customer filter - # e-conomic supports filtering by customer number via URL parameter + # Fetch from /invoices/sent endpoint (covers most active invoices) + # Then filter by customer in code all_invoices = [] - # Use /invoices/sent endpoint with customer filter endpoint = f"{self.api_url}/invoices/sent" - logger.info(f"📋 Fetching invoices for customer {customer_number} from {endpoint}") + logger.info(f"📋 Fetching invoices from {endpoint}") try: # Pagination: Keep fetching until no more pages @@ -472,7 +471,7 @@ class EconomicService: while True: async with session.get( endpoint, - params={"pagesize": 1000, "skippages": page, "filter": f"customer.customerNumber$eq:{customer_number}"}, + params={"pagesize": 1000, "skippages": page}, headers=self._get_headers() ) as response: logger.info(f"🔍 [API] Response status from {endpoint} (page {page}): {response.status}") @@ -499,18 +498,36 @@ class EconomicService: logger.warning(f"⚠️ Endpoint {endpoint} returned {response.status}: {error_text[:200]}") break except Exception as e: - logger.warning(f"⚠️ Error trying endpoint {endpoint}: {e}") + logger.error(f"❌ Error fetching invoices: {e}") + + logger.info(f"✅ Found {len(all_invoices)} total invoices") + + # Filter invoices for this customer + customer_invoices = [ + inv for inv in all_invoices + if inv.get('customer', {}).get('customerNumber') == customer_number + ] + + logger.info(f"📊 Filtered to {len(customer_invoices)} invoices for customer {customer_number}") if not all_invoices: - logger.warning(f"⚠️ No invoices found for customer {customer_number}") + logger.warning(f"⚠️ No invoices found in e-conomic") return [] - logger.info(f"✅ Found {len(all_invoices)} total invoices for customer {customer_number}") + logger.info(f"✅ Found {len(all_invoices)} total invoices") - # Debug: log response structure - if all_invoices: - logger.info(f"🔍 [API] First invoice structure keys: {list(all_invoices[0].keys())}") - logger.info(f"🔍 [API] First invoice date: {all_invoices[0].get('date')}") + # Filter invoices for this customer + customer_invoices = [ + inv for inv in all_invoices + if inv.get('customer', {}).get('customerNumber') == customer_number + ] + + logger.info(f"📊 Filtered to {len(customer_invoices)} invoices for customer {customer_number}") + + # Debug: log response structure if we have invoices + if customer_invoices: + logger.info(f"🔍 [API] First invoice structure keys: {list(customer_invoices[0].keys())}") + logger.info(f"🔍 [API] First invoice date: {customer_invoices[0].get('date')}") # Apply date filter (13 months back) from dateutil.parser import parse as parse_date