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,47 +458,64 @@ 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 from /invoices/drafts endpoint (covers active invoices)
|
# Fetch from multiple endpoints to get all invoice types
|
||||||
# Then filter by customer in code
|
# Then filter by customer and date in code
|
||||||
all_invoices = []
|
all_invoices = []
|
||||||
|
|
||||||
endpoint = f"{self.api_url}/invoices/drafts"
|
# Check drafts, booked, paid, unpaid endpoints
|
||||||
logger.info(f"📋 Fetching invoices from {endpoint}")
|
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",
|
||||||
|
]
|
||||||
|
|
||||||
try:
|
for endpoint in endpoints_to_try:
|
||||||
# Pagination: Keep fetching until no more pages
|
logger.info(f"📋 Fetching invoices from {endpoint}")
|
||||||
page = 0
|
|
||||||
while True:
|
try:
|
||||||
async with session.get(
|
# Pagination: Keep fetching until no more pages
|
||||||
endpoint,
|
page = 0
|
||||||
params={"pagesize": 1000, "skippages": page},
|
while True:
|
||||||
headers=self._get_headers()
|
async with session.get(
|
||||||
) as response:
|
endpoint,
|
||||||
logger.info(f"🔍 [API] Response status from {endpoint} (page {page}): {response.status}")
|
params={"pagesize": 1000, "skippages": page},
|
||||||
|
headers=self._get_headers()
|
||||||
if response.status == 200:
|
) as response:
|
||||||
data = await response.json()
|
logger.info(f"🔍 [API] Response status from {endpoint} (page {page}): {response.status}")
|
||||||
invoices_from_endpoint = data.get('collection', [])
|
|
||||||
logger.info(f"✅ Fetched {len(invoices_from_endpoint)} invoices from {endpoint} page {page}")
|
|
||||||
|
|
||||||
if not invoices_from_endpoint:
|
if response.status == 200:
|
||||||
# No more invoices on this page
|
data = await response.json()
|
||||||
|
invoices_from_endpoint = data.get('collection', [])
|
||||||
|
logger.info(f"✅ Fetched {len(invoices_from_endpoint)} invoices from {endpoint} page {page}")
|
||||||
|
|
||||||
|
if not invoices_from_endpoint:
|
||||||
|
# No more invoices on this page
|
||||||
|
break
|
||||||
|
|
||||||
|
# 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:
|
||||||
|
break # Last page
|
||||||
|
|
||||||
|
page += 1
|
||||||
|
else:
|
||||||
|
error_text = await response.text()
|
||||||
|
logger.warning(f"⚠️ Endpoint {endpoint} returned {response.status}: {error_text[:200]}")
|
||||||
break
|
break
|
||||||
|
except Exception as e:
|
||||||
# Add invoices
|
logger.error(f"❌ Error fetching from {endpoint}: {e}")
|
||||||
all_invoices.extend(invoices_from_endpoint)
|
|
||||||
|
|
||||||
# Check if we got full page (1000) - if so, there might be more pages
|
|
||||||
if len(invoices_from_endpoint) < 1000:
|
|
||||||
break # Last page
|
|
||||||
|
|
||||||
page += 1
|
|
||||||
else:
|
|
||||||
error_text = await response.text()
|
|
||||||
logger.warning(f"⚠️ Endpoint {endpoint} returned {response.status}: {error_text[:200]}")
|
|
||||||
break
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"❌ Error fetching invoices: {e}")
|
|
||||||
|
|
||||||
logger.info(f"✅ Found {len(all_invoices)} total invoices")
|
logger.info(f"✅ Found {len(all_invoices)} total invoices")
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user