use date for invoice name
This commit is contained in:
@@ -234,7 +234,7 @@ def export_invoice_pdf(
|
||||
context: BrowserContext,
|
||||
page: Page,
|
||||
invoice: dict,
|
||||
output_path: Path,
|
||||
output_dir: Path,
|
||||
billing_url: str = BILLING_URL,
|
||||
) -> tuple[Page, Path]:
|
||||
invoice_page = None
|
||||
@@ -259,6 +259,25 @@ def export_invoice_pdf(
|
||||
fill_invoice_fields(invoice_page)
|
||||
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)
|
||||
logger.info("Saved: %s", output_path)
|
||||
|
||||
@@ -356,21 +375,13 @@ def download_all_invoices() -> list[Path]:
|
||||
|
||||
year_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
for idx, invoice in enumerate(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
|
||||
|
||||
for invoice in invoices:
|
||||
try:
|
||||
page, path = export_invoice_pdf(
|
||||
context,
|
||||
page,
|
||||
invoice,
|
||||
pdf_path,
|
||||
year_dir,
|
||||
billing_url=billing_url,
|
||||
)
|
||||
saved.append(path)
|
||||
|
||||
Reference in New Issue
Block a user