from __future__ import annotations from pathlib import Path from decimal import Decimal from pycheval import EN16931Invoice, Money, generate_xml from .models import Invoice from .calc import compute_totals def generate_facturx_xml(inv: Invoice, out_xml: Path, logger) -> Path: out_xml.parent.mkdir(parents=True, exist_ok=True) totals = compute_totals(inv) invoice = EN16931Invoice( invoice_number=inv.meta.invoice_number, invoice_date=inv.meta.invoice_date, currency=inv.meta.currency, grand_total=Money(str(Decimal(str(totals.gross_total)).quantize(Decimal("0.01"))), inv.meta.currency), ) xml_string = generate_xml(invoice) out_xml.write_text(xml_string, encoding="utf-8") logger.info("Wrote Factur-X XML: %s", out_xml) return out_xml