feat: create validated ZUGFeRD invoices in one console command
This commit is contained in:
1 parent
e0c15fc7f2
commit
a643cf5227
30 files changed
+1509
-207
No files matched your search
@@ -6,16 +6,20 @@ import shutil
|
||||
from jinja2 import Environment, FileSystemLoader, select_autoescape
|
||||
|
||||
from .models import Invoice
|
||||
from .calc import compute_totals
|
||||
from .calc import compute_totals, line_net
|
||||
from .types import RenderResult
|
||||
from .utils import money_fmt
|
||||
from .utils import money_fmt, number_fmt
|
||||
|
||||
def render_html(inv: Invoice, template_path: Path) -> str:
|
||||
env = Environment(
|
||||
loader=FileSystemLoader(str(template_path.parent)),
|
||||
autoescape=select_autoescape(["html", "xml"]),
|
||||
autoescape=select_autoescape(["html", "xml", "j2"]),
|
||||
)
|
||||
env.globals["money"] = lambda x: money_fmt(x, inv.meta.currency)
|
||||
env.globals["number"] = number_fmt
|
||||
env.filters["de_date"] = lambda d: d.strftime("%d.%m.%Y") if d else ""
|
||||
env.globals["unit_label"] = lambda unit: {"HUR": "Std.", "C62": "Stück", "DAY": "Tage"}.get(unit, unit)
|
||||
env.globals["price"] = lambda x: f"{number_fmt(x)} {inv.meta.currency}"
|
||||
tpl = env.get_template(template_path.name)
|
||||
|
||||
totals = compute_totals(inv)
|
||||
@@ -23,7 +27,7 @@ def render_html(inv: Invoice, template_path: Path) -> str:
|
||||
items = []
|
||||
for it in inv.items:
|
||||
d = it.model_dump()
|
||||
d["line_total_net"] = float(it.quantity) * float(it.unit_price_net)
|
||||
d["line_total_net"] = line_net(it)
|
||||
items.append(type("Obj", (), d))
|
||||
|
||||
ctx = {
|
||||
@@ -50,7 +54,7 @@ def render_pdf(html: str, out_pdf: Path, logger, prefer_weasyprint: bool = True,
|
||||
logger.info("Rendering PDF using WeasyPrint")
|
||||
try:
|
||||
from weasyprint import HTML # type: ignore
|
||||
HTML(string=html, base_url=base_url).write_pdf(str(out_pdf))
|
||||
HTML(string=html, base_url=base_url).write_pdf(str(out_pdf), pdf_variant="pdf/a-3b")
|
||||
return RenderResult(ok=True, backend="weasyprint", path=out_pdf)
|
||||
except Exception as e:
|
||||
logger.exception("WeasyPrint rendering failed; trying wkhtmltopdf if available: %s", e)
|
||||
|
||||
Reference in new issue
Block a user