feat: Fetch from multiple e-conomic endpoints for complete data

v1.3.149:
- Added /invoices/booked (sent invoices)
- Added /invoices/paid (paid invoices)
- Added /invoices/unpaid (unpaid invoices)
- Keep /invoices/drafts (draft invoices)
- Track unique invoices to avoid duplicates
- Apply customer and date filters correctly after fetching
This commit is contained in:
Christian 2026-01-27 08:42:22 +01:00
parent 04a472d204
commit 31fc285342
2 changed files with 63 additions and 34 deletions

View File

@ -1 +1 @@
1.3.148 1.3.149

View File

@ -458,46 +458,75 @@ class EconomicService:
logger.info(f"📅 Will filter invoices from {start_date} onwards (13 months for yearly billed items)") logger.info(f"📅 Will filter invoices from {start_date} onwards (13 months for yearly billed items)")
async with aiohttp.ClientSession() as session: 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 # Then filter by customer and date in code
all_invoices = [] all_invoices = []
logger.info(f"📋 Fetching all draft invoices from e-conomic") # Endpoints to try (in order of priority)
endpoint = f"{self.api_url}/invoices/drafts" 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: logger.info(f"📋 Fetching invoices from {len(endpoints_to_try)} e-conomic endpoints")
# Pagination: Keep fetching until no more pages
page = 0 for endpoint in endpoints_to_try:
while True: logger.info(f" Trying endpoint: {endpoint}")
async with session.get(
endpoint, try:
params={"pagesize": 1000, "skippages": page}, # Pagination: Keep fetching until no more pages
headers=self._get_headers() page = 0
) as response: while True:
logger.info(f"🔍 [API] Response status from {endpoint} (page {page}): {response.status}") async with session.get(
endpoint,
if response.status == 200: params={"pagesize": 1000, "skippages": page},
data = await response.json() headers=self._get_headers()
invoices_from_endpoint = data.get('collection', []) ) as response:
logger.info(f"✅ Fetched {len(invoices_from_endpoint)} invoices from {endpoint} page {page}") 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 break
except Exception as e:
all_invoices.extend(invoices_from_endpoint) logger.error(f" ❌ Error fetching from {endpoint}: {e}")
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}")
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: if not all_invoices:
logger.warning(f"⚠️ No invoices found in e-conomic") logger.warning(f"⚠️ No invoices found in e-conomic")