feature/tu/first_implementation (#2)

Co-authored-by: Torsten Ueberschar <torsten@ueberschar.de>
Reviewed-on: torsten/freelance_invoice#2
This commit is contained in:
2024-09-17 14:24:19 +02:00
parent 17346d5197
commit e8c8085a07
30 changed files with 1082 additions and 158 deletions

View File

View File

@@ -0,0 +1,72 @@
from pathlib import Path
from jinja2 import FileSystemLoader, TemplateNotFound, Environment
from xhtml2pdf import pisa
from datetime import date, timedelta
import markdown
class HtmlTemplate:
def __init__(self, templates):
self.template_name = 'invoice.html'
if not Path(templates).exists():
raise FileNotFoundError(f'Inivalid path to template files: {templates}')
self.templates = templates
self.path_to_template = Path(templates)
def prepare_template(self, invoice_data, envelope_data):
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']) 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
def convert_html_to_pdf(self, source_html, output_filename):
# open output file for writing (truncated binary)
result_file = open(output_filename, "w+b")
# convert HTML to PDF
pisa_status = pisa.CreatePDF(
source_html, # the HTML to convert
path=str(self.path_to_template / 'fonts'),
dest=result_file
) # file handle to receive result
# close output file
result_file.close() # close output file
# return False on success and True on errors
return pisa_status.err

156
src/main.py Normal file
View File

@@ -0,0 +1,156 @@
import argparse
import yaml
import locale
from pathlib import Path, PurePath
from datetime import datetime
from invoice_generator import html_generator
class DataObject:
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
def parse_yaml_file(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
return yaml.safe_load(file)
def check_file(file_path):
try_out_extensions = ['.yaml', '.yml']
for ext in try_out_extensions:
if file_path.exists():
return file_path
file_path = file_path.with_suffix(ext)
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')
cwd = Path.cwd().resolve()
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')
parser.add_argument('-b', '--base', type=str, required=False, default=cwd,
help='base directory for invoice and envelope files (default: (current working directory) %(default)s)')
parser.add_argument('-e', '--envelope', type=str, required=False, default='envelope.yaml',
help='Envelope file name (default: %(default)s)')
parser.add_argument('-t', '--template', type=str, required=False, default='templates',
help='directory for template files (default: %(default)s)')
parser.add_argument('-i', '--invoice', type=str, required=True, help='Invoice file name')
args = parser.parse_args()
invoice_file = args.invoice
envelope_file = args.envelope
print(f'Searching for invoice files in: {args.base}')
invoice_file = Path(args.base) / invoice_file
envelope_file = Path(args.base) / envelope_file
invoice_file = check_file(invoice_file)
envelope_file = check_file(envelope_file)
print(f'Found invoice file: {invoice_file}')
print(f'Found envelope file: {envelope_file}')
if invoice_file is None:
print('Error: No invoice file found')
return
if envelope_file is None:
print('Error: No envelope file found')
return
try:
invoice = parse_yaml_file(invoice_file)
envelope = parse_yaml_file(envelope_file)
print('Envelope data:')
envelope_data = DataObject(**envelope)
print('<--->')
print(yaml.dump(envelope_data))
print('</--->')
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
if not hasattr(invoice_data, 'Positions'):
invoice_data.Positions = []
for position in invoice_data.Positions:
if 'PricePerUnit' not in position or position['PricePerUnit'] is None:
position['PricePerUnit'] = invoice_data.PricePerUnit
invoice_data.Id = invoice_data.Id or invoice_file.stem
print('<--->')
print(yaml.dump(invoice_data))
print('</--->')
except FileNotFoundError as e:
print(f'Error: {e}')
return
except yaml.YAMLError as e:
print(f'Error: {e}')
return
except Exception as e:
print(f'Error: {e}')
return
if not PurePath(args.template).is_absolute():
template_dir = (Path(args.base) / args.template).resolve()
else:
template_dir = Path(args.template).resolve()
generator = html_generator.HtmlTemplate(template_dir)
template = generator.prepare_template(invoice_data, envelope_data)
try:
print('Generating invoice...')
output_path = Path(args.base) / 'pdf'
output_path.mkdir(parents=True, exist_ok=True)
output_path = output_path / f'{invoice_data.Id}.pdf'
invoice_pdf = output_path.resolve()
print(f'Invoice PDF: {invoice_pdf}')
generator.convert_html_to_pdf(template, invoice_pdf)
except Exception as e:
print(f'Error: {e}')
return
if __name__ == "__main__":
main()