fix: Fetch all invoices then filter by customer

v1.3.141:
- Removed unsupported customer filter from API params
- Fetch all invoices from /invoices/sent with pagination
- Filter by customer number in code
- Apply 13-month date filter after customer filter
This commit is contained in:
Christian 2026-01-27 07:18:36 +01:00
parent 8d98e3f01c
commit 404e81a7a8
2 changed files with 30 additions and 13 deletions

View File

@ -1 +1 @@
1.3.140
1.3.141

View File

@ -458,13 +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:
# Fetch invoices directly by customer filter
# e-conomic supports filtering by customer number via URL parameter
# Fetch from /invoices/sent endpoint (covers most active invoices)
# Then filter by customer in code
all_invoices = []
# 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}")
logger.info(f"📋 Fetching invoices from {endpoint}")
try:
# Pagination: Keep fetching until no more pages
@ -472,7 +471,7 @@ class EconomicService:
while True:
async with session.get(
endpoint,
params={"pagesize": 1000, "skippages": page, "filter": f"customer.customerNumber$eq:{customer_number}"},
params={"pagesize": 1000, "skippages": page},
headers=self._get_headers()
) as response:
logger.info(f"🔍 [API] Response status from {endpoint} (page {page}): {response.status}")
@ -499,18 +498,36 @@ class EconomicService:
logger.warning(f"⚠️ Endpoint {endpoint} returned {response.status}: {error_text[:200]}")
break
except Exception as e:
logger.warning(f"⚠️ Error trying endpoint {endpoint}: {e}")
logger.error(f"❌ Error fetching invoices: {e}")
logger.info(f"✅ Found {len(all_invoices)} total invoices")
# 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 all_invoices:
logger.warning(f"⚠️ No invoices found for customer {customer_number}")
logger.warning(f"⚠️ No invoices found in e-conomic")
return []
logger.info(f"✅ Found {len(all_invoices)} total invoices for customer {customer_number}")
logger.info(f"✅ Found {len(all_invoices)} total invoices")
# 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')}")
# 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}")
# Debug: log response structure if we have invoices
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)
from dateutil.parser import parse as parse_date