feat: Use customer-specific invoices endpoint
v1.3.146:
- Changed to /customers/{customerNumber}/invoices/sent
- More direct approach - fetches only that customer's invoices
- Removed unnecessary customer filtering step
- Still applies 13-month date filter
This commit is contained in:
parent
e14aff89d7
commit
0d9f5a4332
@ -458,20 +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 from multiple endpoints to get all invoice types
|
# Try customer-specific invoices endpoint first
|
||||||
# Then filter by customer and date in code
|
# Format: /customers/{customerNumber}/invoices/sent
|
||||||
all_invoices = []
|
all_invoices = []
|
||||||
|
|
||||||
# Check drafts, booked, paid, unpaid endpoints
|
logger.info(f"📋 Fetching invoices for customer {customer_number} from customer-specific endpoint")
|
||||||
endpoints_to_try = [
|
endpoint = f"{self.api_url}/customers/{customer_number}/invoices/sent"
|
||||||
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:
|
try:
|
||||||
# Pagination: Keep fetching until no more pages
|
# Pagination: Keep fetching until no more pages
|
||||||
@ -490,56 +482,36 @@ class EconomicService:
|
|||||||
logger.info(f"✅ Fetched {len(invoices_from_endpoint)} invoices from {endpoint} page {page}")
|
logger.info(f"✅ Fetched {len(invoices_from_endpoint)} invoices from {endpoint} page {page}")
|
||||||
|
|
||||||
if not invoices_from_endpoint:
|
if not invoices_from_endpoint:
|
||||||
# No more invoices on this page
|
|
||||||
break
|
break
|
||||||
|
|
||||||
# Add invoices (track unique by invoice number to avoid duplicates)
|
all_invoices.extend(invoices_from_endpoint)
|
||||||
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:
|
if len(invoices_from_endpoint) < 1000:
|
||||||
break # Last page
|
break # Last page
|
||||||
|
|
||||||
page += 1
|
page += 1
|
||||||
else:
|
else:
|
||||||
error_text = await response.text()
|
error_text = await response.text()
|
||||||
logger.warning(f"⚠️ Endpoint {endpoint} returned {response.status}: {error_text[:200]}")
|
logger.warning(f"⚠️ Customer-specific endpoint returned {response.status}: {error_text[:200]}")
|
||||||
break
|
break
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"❌ Error fetching from {endpoint}: {e}")
|
logger.error(f"❌ Error fetching from customer endpoint: {e}")
|
||||||
|
|
||||||
if not all_invoices:
|
if not all_invoices:
|
||||||
logger.warning(f"⚠️ No invoices found in e-conomic")
|
logger.warning(f"⚠️ No invoices found for customer {customer_number}")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
logger.info(f"✅ Found {len(all_invoices)} total invoices")
|
logger.info(f"✅ Found {len(all_invoices)} invoices for customer {customer_number}")
|
||||||
|
|
||||||
# Filter invoices for this customer
|
# Debug: log response structure
|
||||||
customer_invoices = [
|
if all_invoices:
|
||||||
inv for inv in all_invoices
|
logger.info(f"🔍 [API] First invoice structure keys: {list(all_invoices[0].keys())}")
|
||||||
if inv.get('customer', {}).get('customerNumber') == customer_number
|
logger.info(f"🔍 [API] First invoice date: {all_invoices[0].get('date')}")
|
||||||
]
|
|
||||||
|
|
||||||
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
|
||||||
filtered_by_date = []
|
filtered_by_date = []
|
||||||
for inv in customer_invoices:
|
for inv in all_invoices:
|
||||||
invoice_date_str = inv.get('date')
|
invoice_date_str = inv.get('date')
|
||||||
if invoice_date_str:
|
if invoice_date_str:
|
||||||
try:
|
try:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user