feat: Fetch from multiple e-conomic endpoints
v1.3.143: - Check drafts, booked, paid, unpaid endpoints - Deduplicate invoices by invoice number - Pagination support for each endpoint - Filter by customer + 13 months date after fetching
This commit is contained in:
parent
b764224eff
commit
ffffa8e004
@ -458,11 +458,19 @@ 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 from /invoices/drafts endpoint (covers active invoices)
|
||||
# Then filter by customer in code
|
||||
# Fetch from multiple endpoints to get all invoice types
|
||||
# Then filter by customer and date in code
|
||||
all_invoices = []
|
||||
|
||||
endpoint = f"{self.api_url}/invoices/drafts"
|
||||
# Check drafts, booked, paid, unpaid endpoints
|
||||
endpoints_to_try = [
|
||||
f"{self.api_url}/invoices/drafts",
|
||||
f"{self.api_url}/invoices/booked",
|
||||
f"{self.api_url}/invoices/paid",
|
||||
f"{self.api_url}/invoices/unpaid",
|
||||
]
|
||||
|
||||
for endpoint in endpoints_to_try:
|
||||
logger.info(f"📋 Fetching invoices from {endpoint}")
|
||||
|
||||
try:
|
||||
@ -485,8 +493,17 @@ class EconomicService:
|
||||
# No more invoices on this page
|
||||
break
|
||||
|
||||
# Add invoices
|
||||
all_invoices.extend(invoices_from_endpoint)
|
||||
# Add invoices (track unique by invoice number to avoid duplicates)
|
||||
existing_invoice_numbers = {inv.get('draftInvoiceNumber') or inv.get('bookedInvoiceNumber') for inv in all_invoices if inv.get('draftInvoiceNumber') or inv.get('bookedInvoiceNumber')}
|
||||
|
||||
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)
|
||||
elif not inv_num:
|
||||
# Include if no invoice number
|
||||
all_invoices.append(inv)
|
||||
|
||||
# Check if we got full page (1000) - if so, there might be more pages
|
||||
if len(invoices_from_endpoint) < 1000:
|
||||
@ -498,7 +515,7 @@ class EconomicService:
|
||||
logger.warning(f"⚠️ Endpoint {endpoint} returned {response.status}: {error_text[:200]}")
|
||||
break
|
||||
except Exception as e:
|
||||
logger.error(f"❌ Error fetching invoices: {e}")
|
||||
logger.error(f"❌ Error fetching from {endpoint}: {e}")
|
||||
|
||||
logger.info(f"✅ Found {len(all_invoices)} total invoices")
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user