feat: Use customer filter in e-conomic API call

v1.3.140:
- Use /invoices/sent endpoint with customer.customerNumber filter
- More efficient: only fetch invoices for specific customer
- Apply 13-month date filter after fetching
- Simplified endpoint logic (single endpoint vs 8 endpoints)
This commit is contained in:
Christian 2026-01-27 07:17:26 +01:00
parent 2c524c9a05
commit 8d98e3f01c
2 changed files with 43 additions and 83 deletions

View File

@ -1 +1 @@
1.3.139
1.3.140

View File

@ -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: