diff --git a/VERSION b/VERSION index a719df1..4737f4e 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.3.146 \ No newline at end of file +1.3.147 \ No newline at end of file diff --git a/app/services/economic_service.py b/app/services/economic_service.py index b4c357a..83bd647 100644 --- a/app/services/economic_service.py +++ b/app/services/economic_service.py @@ -458,12 +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: - # Try customer-specific invoices endpoint first - # Format: /customers/{customerNumber}/invoices/sent + # Fetch from /invoices/drafts endpoint (main active invoices) + # Then filter by customer and date in code all_invoices = [] - logger.info(f"📋 Fetching invoices for customer {customer_number} from customer-specific endpoint") - endpoint = f"{self.api_url}/customers/{customer_number}/invoices/sent" + logger.info(f"📋 Fetching all draft invoices from e-conomic") + endpoint = f"{self.api_url}/invoices/drafts" try: # Pagination: Keep fetching until no more pages @@ -492,26 +492,38 @@ class EconomicService: page += 1 else: error_text = await response.text() - logger.warning(f"⚠️ Customer-specific endpoint returned {response.status}: {error_text[:200]}") + logger.warning(f"⚠️ Endpoint returned {response.status}: {error_text[:200]}") break except Exception as e: - logger.error(f"❌ Error fetching from customer endpoint: {e}") + logger.error(f"❌ Error fetching invoices: {e}") + + logger.info(f"✅ Found {len(all_invoices)} total invoices in e-conomic") if not all_invoices: + logger.warning(f"⚠️ No invoices found in e-conomic") + return [] + + # 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 customer_invoices: logger.warning(f"⚠️ No invoices found for customer {customer_number}") return [] - logger.info(f"✅ Found {len(all_invoices)} 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 date: {all_invoices[0].get('date')}") + 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) + # Apply date filter (13 months back) on customer invoices only from dateutil.parser import parse as parse_date filtered_by_date = [] - for inv in all_invoices: + for inv in customer_invoices: invoice_date_str = inv.get('date') if invoice_date_str: try: