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)

View File

@@ -1,6 +1,9 @@
import argparse
import yaml
import locale
from pathlib import Path
from datetime import datetime
from invoice_generator import html_generator
@@ -26,10 +29,24 @@ def check_file(file_path):
return None
def parse_date(date_string):
return datetime.strptime(date_string, '%Y-%m-%d').date()
def merge_envelope_data_into_invoice_data(invoice_data, source_data):
if (source_data is None) or (invoice_data is None):
return
for key, value in source_data.items():
if not hasattr(invoice_data, key):
setattr(invoice_data, key, value)
# Utility function
def main():
locale.setlocale(locale.LC_ALL, 'de_DE.utf8')
print('Simple invoice generator for freelancers and small businesses by Torsten Ueberschar')
print()
parser = argparse.ArgumentParser(description='Read invoice and envelope data from yaml file')
@@ -73,6 +90,16 @@ def main():
print('Invoice data:')
invoice_data = DataObject(**invoice)
merge_envelope_data_into_invoice_data(invoice_data, envelope_data.Invoice)
selected_customer = next((x for x in envelope_data.Customers if x['CustomerId'] == invoice_data.CustomerId), None)
merge_envelope_data_into_invoice_data(invoice_data, selected_customer)
if not hasattr(invoice_data, 'InvoiceDate'):
invoice_data.InvoiceDate = datetime.now().date()
else:
invoice_data.InvoiceDate = parse_date(invoice_data.InvoiceDate)
if not hasattr(invoice_data, 'Id'):
invoice_data.Id = None