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:
parent
8d98e3f01c
commit
404e81a7a8
@ -458,13 +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:
|
||||||
# Fetch invoices directly by customer filter
|
# Fetch from /invoices/sent endpoint (covers most active invoices)
|
||||||
# e-conomic supports filtering by customer number via URL parameter
|
# Then filter by customer in code
|
||||||
all_invoices = []
|
all_invoices = []
|
||||||
|
|
||||||
# Use /invoices/sent endpoint with customer filter
|
|
||||||
endpoint = f"{self.api_url}/invoices/sent"
|
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:
|
try:
|
||||||
# Pagination: Keep fetching until no more pages
|
# Pagination: Keep fetching until no more pages
|
||||||
@ -472,7 +471,7 @@ class EconomicService:
|
|||||||
while True:
|
while True:
|
||||||
async with session.get(
|
async with session.get(
|
||||||
endpoint,
|
endpoint,
|
||||||
params={"pagesize": 1000, "skippages": page, "filter": f"customer.customerNumber$eq:{customer_number}"},
|
params={"pagesize": 1000, "skippages": page},
|
||||||
headers=self._get_headers()
|
headers=self._get_headers()
|
||||||
) as response:
|
) as response:
|
||||||
logger.info(f"🔍 [API] Response status from {endpoint} (page {page}): {response.status}")
|
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]}")
|
logger.warning(f"⚠️ Endpoint {endpoint} returned {response.status}: {error_text[:200]}")
|
||||||
break
|
break
|
||||||
except Exception as e:
|
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:
|
if not all_invoices:
|
||||||
logger.warning(f"⚠️ No invoices found for customer {customer_number}")
|
logger.warning(f"⚠️ No invoices found in e-conomic")
|
||||||
return []
|
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
|
# Filter invoices for this customer
|
||||||
if all_invoices:
|
customer_invoices = [
|
||||||
logger.info(f"🔍 [API] First invoice structure keys: {list(all_invoices[0].keys())}")
|
inv for inv in all_invoices
|
||||||
logger.info(f"🔍 [API] First invoice date: {all_invoices[0].get('date')}")
|
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)
|
# Apply date filter (13 months back)
|
||||||
from dateutil.parser import parse as parse_date
|
from dateutil.parser import parse as parse_date
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user