write complete invoice

This commit is contained in:
Torsten Ueberschar
2024-02-18 11:11:04 +01:00
parent 1720f1b89e
commit 7c2703f2fe
25 changed files with 316 additions and 83 deletions

View File

@@ -2,6 +2,9 @@ from pathlib import Path
from jinja2 import FileSystemLoader, TemplateNotFound, Environment
from xhtml2pdf import pisa
from datetime import date, timedelta
import markdown
class HtmlTemplate:
@@ -15,12 +18,42 @@ class HtmlTemplate:
try:
loader = FileSystemLoader(self.templates)
env = Environment(loader=loader)
env.globals['format_float'] = self.format_float
env.globals['calculate_total'] = self.calculate_total
env.filters['markdown_to_html'] = self.markdown_to_html
env.filters['named_replace'] = self.named_replace
env.filters['add_days'] = self.add_days
template = env.get_template(self.template_name)
return template.render(invoice=invoice_data, envelope=envelope_data)
except TemplateNotFound:
raise FileNotFoundError(f'Could not find template file: {self.template_name}')
@staticmethod
def calculate_total(invoice_data):
return sum(
(pos['Quantity'] * (pos['PricePerUnit'] or invoice_data.PricePerUnit)) for pos in invoice_data.Positions)
@staticmethod
def named_replace(value, **replacements):
for key, replacement in replacements.items():
value = value.replace(f"%%{key}%%", str(replacement))
return value
@staticmethod
def format_float(value):
return f'{value:,.2f}'
@staticmethod
def add_days(date, number_of_days):
return date + timedelta(days=number_of_days)
@staticmethod
def markdown_to_html(md_content):
html_content = markdown.markdown(md_content)
return html_content
@staticmethod
def convert_html_to_pdf(source_html, output_filename):
# open output file for writing (truncated binary)