diff --git a/VERSION b/VERSION index 968c853..7ddcb77 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.3.148 \ No newline at end of file +1.3.149 \ No newline at end of file diff --git a/app/services/economic_service.py b/app/services/economic_service.py index 83bd647..a6462d5 100644 --- a/app/services/economic_service.py +++ b/app/services/economic_service.py @@ -458,46 +458,75 @@ 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 from /invoices/drafts endpoint (main active invoices) + # Fetch from multiple e-conomic endpoints to get all invoice types # Then filter by customer and date in code all_invoices = [] - logger.info(f"📋 Fetching all draft invoices from e-conomic") - endpoint = f"{self.api_url}/invoices/drafts" + # Endpoints to try (in order of priority) + endpoints_to_try = [ + f"{self.api_url}/invoices/booked", # Booked/sent invoices (most important) + f"{self.api_url}/invoices/drafts", # Draft invoices + f"{self.api_url}/invoices/paid", # Paid invoices + f"{self.api_url}/invoices/unpaid", # Unpaid invoices + ] - 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}") - - 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}") + logger.info(f"📋 Fetching invoices from {len(endpoints_to_try)} e-conomic endpoints") + + for endpoint in endpoints_to_try: + logger.info(f" Trying 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" 🔍 Response status (page {page}): {response.status}") - if not invoices_from_endpoint: + if response.status == 200: + data = await response.json() + invoices_from_endpoint = data.get('collection', []) + logger.info(f" ✅ Fetched {len(invoices_from_endpoint)} invoices from page {page}") + + if not invoices_from_endpoint: + break + + # Add invoices (track unique by invoice number to avoid duplicates) + existing_invoice_numbers = { + inv.get('draftInvoiceNumber') or inv.get('bookedInvoiceNumber') + for inv in all_invoices + if inv.get('draftInvoiceNumber') or inv.get('bookedInvoiceNumber') + } + + new_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_count += 1 + elif not inv_num: + # Include if no invoice number + all_invoices.append(inv) + new_count += 1 + + logger.info(f" Added {new_count} new unique invoices") + + if len(invoices_from_endpoint) < 1000: + break # Last page + + page += 1 + else: + error_text = await response.text() + logger.warning(f" ⚠️ Endpoint returned {response.status}: {error_text[:200]}") break - - all_invoices.extend(invoices_from_endpoint) - - if len(invoices_from_endpoint) < 1000: - break # Last page - - page += 1 - else: - error_text = await response.text() - logger.warning(f"⚠️ Endpoint returned {response.status}: {error_text[:200]}") - break - except Exception as e: - logger.error(f"❌ Error fetching invoices: {e}") + except Exception as e: + logger.error(f" ❌ Error fetching from {endpoint}: {e}") - logger.info(f"✅ Found {len(all_invoices)} total invoices in e-conomic") + logger.info(f"✅ Found {len(all_invoices)} total invoices across all endpoints") if not all_invoices: logger.warning(f"⚠️ No invoices found in e-conomic")