diff --git a/VERSION b/VERSION index f6859c8..3817580 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.3.139 \ No newline at end of file +1.3.140 \ No newline at end of file diff --git a/app/services/economic_service.py b/app/services/economic_service.py index 56509e4..5771045 100644 --- a/app/services/economic_service.py +++ b/app/services/economic_service.py @@ -458,104 +458,64 @@ class EconomicService: logger.info(f"📅 Will filter invoices from {start_date} onwards (13 months for yearly billed items)") async with aiohttp.ClientSession() as session: - # Try multiple endpoints to find invoices - # Include drafts, paid, unpaid, booked, sent, archived, etc. - endpoints_to_try = [ - f"{self.api_url}/invoices/archive", # ARCHIVED invoices (primary source) - f"{self.api_url}/invoices/drafts", # Draft invoices - f"{self.api_url}/invoices/sent", # Sent invoices - f"{self.api_url}/invoices/booked", # Booked invoices - f"{self.api_url}/invoices/paid", # Paid invoices - f"{self.api_url}/invoices/unpaid", # Unpaid invoices - f"{self.api_url}/invoices/sales", # All sales invoices - f"{self.api_url}/invoices", # Generic endpoint (returns links) - ] - + # Fetch invoices directly by customer filter + # e-conomic supports filtering by customer number via URL parameter all_invoices = [] - # TRY ALL ENDPOINTS AND AGGREGATE RESULTS (don't break after first success) - for endpoint in endpoints_to_try: - logger.info(f"📋 Trying invoice endpoint: {endpoint}") - - try: - # Pagination: Keep fetching until no more pages - page = 0 - while True: - async with session.get( - endpoint, - params={"pagesize": 1000, "skippages": page}, - headers=self._get_headers() - ) as response: - logger.info(f"🔍 [API] Response status from {endpoint} (page {page}): {response.status}") + # 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}") + + try: + # Pagination: Keep fetching until no more pages + page = 0 + while True: + async with session.get( + endpoint, + params={"pagesize": 1000, "skippages": page, "filter": f"customer.customerNumber$eq:{customer_number}"}, + headers=self._get_headers() + ) as response: + logger.info(f"🔍 [API] Response status from {endpoint} (page {page}): {response.status}") + + if response.status == 200: + data = await response.json() + invoices_from_endpoint = data.get('collection', []) + logger.info(f"✅ Fetched {len(invoices_from_endpoint)} invoices from {endpoint} page {page}") - if response.status == 200: - data = await response.json() - invoices_from_endpoint = data.get('collection', []) - logger.info(f"✅ Fetched {len(invoices_from_endpoint)} invoices from {endpoint} page {page}") - - if not invoices_from_endpoint: - # No more invoices on this page - break - - # Add new invoices (avoid duplicates by tracking invoice numbers) - existing_invoice_numbers = set() - for inv in all_invoices: - inv_num = inv.get('draftInvoiceNumber') or inv.get('bookedInvoiceNumber') - if inv_num: - existing_invoice_numbers.add(inv_num) - - new_invoices_count = 0 - for inv in invoices_from_endpoint: - inv_num = inv.get('draftInvoiceNumber') or inv.get('bookedInvoiceNumber') - if inv_num and inv_num not in existing_invoice_numbers: - all_invoices.append(inv) - existing_invoice_numbers.add(inv_num) - new_invoices_count += 1 - - logger.info(f" Added {new_invoices_count} new unique invoices") - - # Check if we got full page (1000) - if so, there might be more pages - if len(invoices_from_endpoint) < 1000: - break # Last page - - page += 1 - else: - error_text = await response.text() - logger.warning(f"⚠️ Endpoint {endpoint} returned {response.status}: {error_text[:200]}") + if not invoices_from_endpoint: + # No more invoices on this page break - except Exception as e: - logger.warning(f"⚠️ Error trying endpoint {endpoint}: {e}") + + # Add invoices + all_invoices.extend(invoices_from_endpoint) + + # Check if we got full page (1000) - if so, there might be more pages + if len(invoices_from_endpoint) < 1000: + break # Last page + + page += 1 + else: + error_text = await response.text() + logger.warning(f"⚠️ Endpoint {endpoint} returned {response.status}: {error_text[:200]}") + break + except Exception as e: + logger.warning(f"⚠️ Error trying endpoint {endpoint}: {e}") if not all_invoices: - logger.warning(f"⚠️ No invoices found from any endpoint") + logger.warning(f"⚠️ No invoices found for customer {customer_number}") return [] - logger.info(f"✅ Found {len(all_invoices)} total invoices in e-conomic") + logger.info(f"✅ Found {len(all_invoices)} total invoices for customer {customer_number}") # 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 customer field: {all_invoices[0].get('customer')}") - # Log unique customer numbers in response - customer_numbers = set() - for inv in all_invoices[:20]: # Check first 20 - cust_num = inv.get('customer', {}).get('customerNumber') - if cust_num: - customer_numbers.add(cust_num) - logger.info(f"🔍 [API] Unique customer numbers in response (first 20): {customer_numbers}") - - # 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}") + logger.info(f"🔍 [API] First invoice date: {all_invoices[0].get('date')}") # Apply date filter (13 months back) from dateutil.parser import parse as parse_date filtered_by_date = [] - for inv in customer_invoices: + for inv in all_invoices: invoice_date_str = inv.get('date') if invoice_date_str: try: