use date for invoice name

This commit is contained in:
Jan Bader
2026-04-08 09:57:54 +02:00
parent c2bde3b192
commit 5a3d60e45e

View File

@@ -234,7 +234,7 @@ def export_invoice_pdf(
context: BrowserContext, context: BrowserContext,
page: Page, page: Page,
invoice: dict, invoice: dict,
output_path: Path, output_dir: Path,
billing_url: str = BILLING_URL, billing_url: str = BILLING_URL,
) -> tuple[Page, Path]: ) -> tuple[Page, Path]:
invoice_page = None invoice_page = None
@@ -259,6 +259,25 @@ def export_invoice_pdf(
fill_invoice_fields(invoice_page) fill_invoice_fields(invoice_page)
time.sleep(0.5) time.sleep(0.5)
date_text = ""
date_container = invoice_page.locator(".b2-invoice-customer-right").first
if date_container.count() > 0:
lines = [line.strip() for line in date_container.inner_text().splitlines() if line.strip()]
if lines:
date_text = lines[0].replace(" UTC", "")
if not date_text:
date_text = "unknown-date"
invoice_id = str(invoice.get("reference_id", "unknown"))
filename = sanitize_filename(f"{date_text} Invoice {invoice_id}")
output_path = output_dir / f"{filename}.pdf"
if output_path.exists():
logger.info("Skipping (exists): %s", output_path)
if invoice_page is not page:
invoice_page.close()
return page, output_path
invoice_page.pdf(path=str(output_path), format="A4", print_background=True) invoice_page.pdf(path=str(output_path), format="A4", print_background=True)
logger.info("Saved: %s", output_path) logger.info("Saved: %s", output_path)
@@ -356,21 +375,13 @@ def download_all_invoices() -> list[Path]:
year_dir.mkdir(parents=True, exist_ok=True) year_dir.mkdir(parents=True, exist_ok=True)
for idx, invoice in enumerate(invoices): for invoice in invoices:
label = sanitize_filename(invoice["label"]) or f"invoice_{idx + 1}"
pdf_path = year_dir / f"{label}.pdf"
if pdf_path.exists():
logger.info("Skipping (exists): %s", pdf_path)
saved.append(pdf_path)
continue
try: try:
page, path = export_invoice_pdf( page, path = export_invoice_pdf(
context, context,
page, page,
invoice, invoice,
pdf_path, year_dir,
billing_url=billing_url, billing_url=billing_url,
) )
saved.append(path) saved.append(path)