-
Notifications
You must be signed in to change notification settings - Fork 0
/
invoicecreator.py
160 lines (115 loc) · 4.84 KB
/
invoicecreator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import os
import shutil
import sys
import subprocess
from datetime import time, timedelta, datetime
import locale
import yaml
import argparse
import jinja2
from jinja2 import Environment, PackageLoader, select_autoescape
from jinja2.loaders import FileSystemLoader
import latex
from latex.jinja2 import make_env
from items import Invoice
def read_file(filename):
"""read a yml file"""
try:
with open(filename, 'r') as stream:
data_loaded = yaml.load(stream)
return data_loaded
except IOError:
print("Could not read file:", filename)
except TypeError:
print('something went wrong')
def create_pdf(filename, template, context):
"""compile tex to pdf and save in source directory"""
build_d = os.path.join(source_dir, 'results/')
output_dir = os.path.join(source_dir, 'output/')
out_file = '{}{}'.format(output_dir, filename)
if not os.path.exists(output_dir): # create directory for tex files if not existing
os.makedirs(output_dir)
with open(out_file + ".tex", "w") as f: # saves tex_code to output file
f.write(template.render(**context))
if not os.path.exists(build_d): # create the directory for the pdf if not existing
os.makedirs(build_d)
result = subprocess.call(['pdflatex',
'-output-directory', '{}'.format(os.path.realpath(build_d)),
'{}'.format(os.path.realpath(out_file + '.tex'))],
shell=False)
def collect_context(newInvoice):
"""add all variables to context dict for passing to template"""
context = {}
if (args.invoicetype == 'short'):
itemslist = newInvoice.prepare_shortinvoice()
elif (args.invoicetype == 'long'):
itemslist = newInvoice.invoiceitemlist_long
else:
print('please select an invoicetype')
sys.exit()
total_sum = newInvoice.calculate_total_sum(itemslist)
context['total_sum'] = locale.currency(total_sum, symbol=False)
vat = newInvoice.calculate_vat(total_sum)
context['vat'] = locale.currency(vat, symbol=False)
gross_sum = newInvoice.calculate_gross_sum(total_sum, vat)
context['gross_sum'] = locale.currency(gross_sum, symbol=False)
context['poref'] = newInvoice.poref
context['kstref'] = newInvoice.kstref
context['invoicenr'] = newInvoice.invoicenr
context['to'] = newInvoice.to
context['opening'] = newInvoice.opening
context['closing'] = newInvoice.closing
for item in itemslist:
item.sum = locale.currency(item.sum, symbol=False)
if item.rate != None:
item.rate = locale.currency(item.rate, symbol=False)
context['itemslist'] = itemslist
return context
def select_template():
"""choose tex template according to language set in yml"""
if new_invoice.language == 'en':
template = env.get_template('invoice-eng.tex')
else:
template = env.get_template('invoice.tex')
return template
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('filename', help='please enter filename')
parser.add_argument('--invoicetype', choices=['short', 'long'], default='short')
parser.add_argument('--timesheet', choices=['timesheet', 'no'], default='timesheet')
locale.setlocale(locale.LC_ALL, '')
args = parser.parse_args()
data = read_file(args.filename)
# split source directory and filename
source_dir, filename = os.path.split(args.filename)
filename = os.path.splitext(filename)[0]
working_dir = os.getcwd()
new_invoice = Invoice(data)
if new_invoice.language == 'en':
locale.setlocale(locale.LC_ALL, 'en_US.utf8')
new_invoice.prepare_data()
new_invoice.create_invoiceitemlist()
context = collect_context(new_invoice)
env = make_env(loader=FileSystemLoader(working_dir + '/templates/'))
template = select_template()
create_pdf(filename, template, context)
if (args.timesheet == 'timesheet'):
itemslist = new_invoice.invoiceitemlist
for item in itemslist:
item.sum = locale.currency(item.sum, symbol=False)
context['itemslist'] = itemslist
if new_invoice.language == 'eng':
template = env.get_template('stundennachweis-eng.tex')
else:
template = env.get_template('stundennachweis.tex')
create_pdf(filename + '_timesheet', template, context)
if new_invoice.expenses is not None:
itemslist = new_invoice.expenseslist
for item in itemslist:
item.sum = locale.currency(item.sum, symbol=False)
context['itemslist'] = itemslist
if new_invoice.language == 'eng':
template = env.get_template('reisekosten-eng.tex')
else:
template = env.get_template('reisekosten.tex')
create_pdf(filename + '_travel', template, context)