fix: Apply date filter in code instead of API parameter
v1.3.139: - Removed date filter from API params (causes 404 on some endpoints) - Apply 13-month filter in code after fetching invoices - Parse invoice dates and filter >= start_date - More reliable filtering across all e-conomic endpoints
This commit is contained in:
parent
e0909d4586
commit
2c524c9a05
@ -453,8 +453,9 @@ class EconomicService:
|
||||
# Calculate first day of the month 13 months ago (for yearly billed items)
|
||||
from dateutil.relativedelta import relativedelta
|
||||
now = datetime.now()
|
||||
start_date = (now - relativedelta(months=13)).replace(day=1).strftime('%Y-%m-%d')
|
||||
logger.info(f"📅 Fetching invoices from {start_date} onwards (13 months for yearly billed items)")
|
||||
start_date_obj = (now - relativedelta(months=13)).replace(day=1)
|
||||
start_date = start_date_obj.strftime('%Y-%m-%d')
|
||||
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
|
||||
@ -482,11 +483,7 @@ class EconomicService:
|
||||
while True:
|
||||
async with session.get(
|
||||
endpoint,
|
||||
params={
|
||||
"pagesize": 1000,
|
||||
"skippages": page,
|
||||
"filter": f"date$gte:{start_date}"
|
||||
},
|
||||
params={"pagesize": 1000, "skippages": page},
|
||||
headers=self._get_headers()
|
||||
) as response:
|
||||
logger.info(f"🔍 [API] Response status from {endpoint} (page {page}): {response.status}")
|
||||
@ -555,6 +552,29 @@ class EconomicService:
|
||||
|
||||
logger.info(f"📊 Filtered to {len(customer_invoices)} invoices for customer {customer_number}")
|
||||
|
||||
# Apply date filter (13 months back)
|
||||
from dateutil.parser import parse as parse_date
|
||||
filtered_by_date = []
|
||||
for inv in customer_invoices:
|
||||
invoice_date_str = inv.get('date')
|
||||
if invoice_date_str:
|
||||
try:
|
||||
invoice_date = parse_date(invoice_date_str)
|
||||
if invoice_date.replace(tzinfo=None) >= start_date_obj:
|
||||
filtered_by_date.append(inv)
|
||||
else:
|
||||
logger.debug(f" Skipped invoice {inv.get('draftInvoiceNumber') or inv.get('bookedInvoiceNumber')} dated {invoice_date_str} (before {start_date})")
|
||||
except Exception as e:
|
||||
logger.warning(f" Could not parse invoice date '{invoice_date_str}': {e}")
|
||||
# Include if we can't parse the date
|
||||
filtered_by_date.append(inv)
|
||||
else:
|
||||
# Include if no date field
|
||||
filtered_by_date.append(inv)
|
||||
|
||||
logger.info(f"📅 After date filter ({start_date}+): {len(filtered_by_date)} invoices")
|
||||
customer_invoices = filtered_by_date
|
||||
|
||||
# Fetch full invoice data with lines if requested
|
||||
if include_lines and customer_invoices:
|
||||
full_invoices = []
|
||||
|
||||
Loading…
Reference in New Issue
Block a user