fix: Use /invoices/drafts with proper customer filtering

v1.3.147:
- Changed from /customers/{id}/invoices/sent (404) to /invoices/drafts
- Fetch all drafts, then filter by customer number
- Apply date filter ONLY on customer invoices (fixes bug)
- Proper order: fetch all -> filter customer -> filter date
This commit is contained in:
Christian 2026-01-27 08:31:22 +01:00
parent 0d9f5a4332
commit 41716ba683
2 changed files with 26 additions and 14 deletions

View File

@ -1 +1 @@
1.3.146 1.3.147

View File

@ -458,12 +458,12 @@ 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:
# Try customer-specific invoices endpoint first # Fetch from /invoices/drafts endpoint (main active invoices)
# Format: /customers/{customerNumber}/invoices/sent # Then filter by customer and date in code
all_invoices = [] all_invoices = []
logger.info(f"📋 Fetching invoices for customer {customer_number} from customer-specific endpoint") logger.info(f"📋 Fetching all draft invoices from e-conomic")
endpoint = f"{self.api_url}/customers/{customer_number}/invoices/sent" endpoint = f"{self.api_url}/invoices/drafts"
try: try:
# Pagination: Keep fetching until no more pages # Pagination: Keep fetching until no more pages
@ -492,26 +492,38 @@ class EconomicService:
page += 1 page += 1
else: else:
error_text = await response.text() 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 break
except Exception as e: 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: 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}") logger.warning(f"⚠️ No invoices found for customer {customer_number}")
return [] return []
logger.info(f"✅ Found {len(all_invoices)} invoices for customer {customer_number}")
# Debug: log response structure # Debug: log response structure
if all_invoices: if customer_invoices:
logger.info(f"🔍 [API] First invoice structure keys: {list(all_invoices[0].keys())}") logger.info(f"🔍 [API] First invoice structure keys: {list(customer_invoices[0].keys())}")
logger.info(f"🔍 [API] First invoice date: {all_invoices[0].get('date')}") 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 from dateutil.parser import parse as parse_date
filtered_by_date = [] filtered_by_date = []
for inv in all_invoices: for inv in customer_invoices:
invoice_date_str = inv.get('date') invoice_date_str = inv.get('date')
if invoice_date_str: if invoice_date_str:
try: try: