forked from jorgenschaefer/gnucashxml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-invoices-latex.py
223 lines (202 loc) · 7.99 KB
/
create-invoices-latex.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import json
from operator import attrgetter
import gnucashxml
import locale
import os
import subprocess
import decimal
gnucashfile = "/Users/angelo/Desktop/paperport/Hongens Automatisering/2018/gnucash/hongens-2018.gnucash"
yearfilter = "2018"
outputfolder = "/Users/angelo/Desktop/paperport/Hongens Automatisering/2018/facturen/tmp"
xelatex_path = "/Library/TeX/texbin/xelatex"
def formatcurrency(amount):
amount_decimal = decimal.Decimal(str(amount)).quantize(decimal.Decimal('.01'), rounding=decimal.ROUND_UP)
locale.setlocale(locale.LC_ALL, 'nl_NL.utf-8')
s = locale.currency(amount_decimal, grouping=True, symbol=False)
s = s.replace(" ", ".")
return s
def formatfloat(float):
locale.setlocale(locale.LC_ALL, 'nl_NL.utf-8')
s = locale.str(float)
return s
def getlatex(invoiceobj):
latex = ""
latex += "\\documentclass[a4paper]{letter}\n"
latex += "\n"
latex += "\\usepackage{graphicx}\n"
latex += "\\usepackage{eso-pic}\n"
latex += "\\usepackage{anysize}\n"
latex += "\\usepackage{eurosym}\n"
latex += "\\usepackage{color}\n"
latex += "\\usepackage{fontspec}\n"
latex += "\\setmainfont{Lato}\n"
latex += "\\usepackage{geometry}\n"
latex += "\\geometry{\n"
latex += " paperwidth=210mm,\n"
latex += " paperheight=297mm\n"
latex += "}\n"
latex += "\\thispagestyle{empty}\n"
latex += "\n"
latex += "\\newcommand\\BackgroundPic{\n"
latex += " \\put(0,0){\n"
latex += " \\parbox[b][\\paperheight]{\\paperwidth}{%\n"
latex += " \\vfill\n"
latex += " \\centering\n"
latex += " \\includegraphics[width=\\paperwidth,height=\\paperheight, keepaspectratio]{back.eps}%\n"
latex += " \\vfill\n"
latex += " }\n"
latex += "}}\n"
latex += "\\marginsize{2cm}{2cm}{0cm}{0cm}\n"
latex += "\n"
latex += "\n"
latex += "\\title{Invoice}\n"
latex += "\\begin{document}\n"
latex += "\\AddToShipoutPicture{\\BackgroundPic}\n"
latex += "\n"
latex += "%-------------------------------------\n"
latex += "%ADDRESSEE\n"
latex += "\\begin{tabular}{ l }\n"
latex += " {}\\\\\n".format(invoiceobj.customer.name)
for addressline in invoiceobj.customer.address:
addressline = addressline.replace(u'e\u0308', '\\"e')
latex += " {} \\\\\n".format(addressline)
latex += "\n"
latex += "\\end{tabular}\n"
latex += "%-------------------------------------\n"
latex += "\\vspace{1cm}\n"
latex += "\n"
latex += "\\LARGE\n"
latex += "\\textbf{Factuur}\n"
latex += "\\normalsize\n"
latex += "\\vspace{0.5cm}\n"
latex += "\n"
latex += "\\begin{tabular}{ l l l }\n"
latex += " \\textcolor{{gray}}{{Factuurnummer}} & & {} \\\\\n".format(invoiceobj.id)
latex += " \\textcolor{{gray}}{{Datum}} & & {0:%d-%m-%Y} \\\\\n".format(invoiceobj.date)
latex += " \\textcolor{gray}{Betalingswijze} & & per bank \\\\\n"
latex += "\\end{tabular}\n"
latex += "\n"
latex += "\n"
latex += "\\vspace{3.5cm}\n"
latex += "\\large\n"
latex += "\\textbf{Werkzaamheden}\n"
latex += "\\normalsize\n"
latex += "\\vspace{0.5cm}\n"
latex += "\n"
latex += "\\hrule\n"
latex += "%-------------------------------------\n"
latex += "% WERKZAAMHEDEN\n"
latex += "%-------------------------------------\n"
latex += "\\begin{tabular*}{\\textwidth}{@{}@{\\extracolsep{\\fill}} l r @{}}\n"
totaalexcl = 0
btwtabel = {}
entries = invoiceobj.entries
entries.sort(key=attrgetter('description'))
for entry in entries:
action = entry.action
qty = entry.qty
tarief = entry.price
if qty is None:
qty = 0
if tarief is None:
tarief = 0
totaalexcl_regel = qty * tarief
if int(entry.taxable) == 1:
taxtable = entry.taxtable
if len(taxtable.taxtable_entries) > 1:
print("Error, more than one taxtableentry in taxtable!")
exit(1)
taxtableentry = taxtable.taxtable_entries[0]
btw_tarief_naam = "{0} ({1}\%)".format(taxtable.name, taxtableentry.amount.normalize())
if btw_tarief_naam not in btwtabel.keys():
btwtabel[btw_tarief_naam] = 0
btw_hier = decimal.Decimal((totaalexcl_regel * taxtableentry.amount) / 100)
btwtabel[btw_tarief_naam] += btw_hier
totaalexcl_regel
if action == "Uren":
latex += " {0}, {1} uur \\`a \\EUR{{{2}}}. & \\EUR{{{3}}}\\\\\n".format(
entry.description,
formatfloat(qty),
formatcurrency(tarief),
formatcurrency(totaalexcl_regel)
)
else:
latex += " {0}, {1} x \\EUR{{{2}}}. & \\EUR{{{3}}}\\\\\n".format(
entry.description,
qty.normalize(),
formatcurrency(tarief),
formatcurrency(totaalexcl_regel)
)
totaalexcl += totaalexcl_regel
latex += "\\end{tabular*}\n"
latex += "%-------------------------------------\n"
latex += "\\vfill\n"
latex += "\n"
latex += "\\hrule\n"
latex += "\n"
latex += "\\begin{tabular*}{\\textwidth}{@{}@{\\extracolsep{\\fill}} l r @{}}\n"
latex += " Totaal exclusief BTW & \EUR{{{0}}}\\\\\n".format(formatcurrency(totaalexcl))
totaalbtw = 0
for btwkey in btwtabel.keys():
latex += " {0}\\ & \EUR{{{1}}}\\\\\n".format(btwkey, formatcurrency(btwtabel[btwkey]))
totaalbtw += btwtabel[btwkey]
latex += "\\end{tabular*}\n"
latex += "\n"
latex += "\\hrule\n"
latex += "\n"
latex += "\\begin{tabular*}{\\textwidth}{@{}@{\\extracolsep{\\fill}} l r @{}}\n"
totaalincl = totaalexcl + totaalbtw
latex += " Totaal inclusief BTW & \EUR{{{0}}}\\\\\n".format(formatcurrency(totaalincl))
latex += "\\end{tabular*}\n"
latex += "\n"
latex += "\\vspace{1cm}\n"
latex += "\n"
latex += "Met vriendelijke groet,\n"
latex += "\n"
latex += "\\includegraphics[width=40mm]{handtekening.eps}\n"
latex += "\n"
latex += "Angelo H\\\"ongens\n"
latex += "\n"
latex += "\\vspace{1cm}\n"
latex += "\n"
latex += "\\footnotesize\n"
latex += "Wij verzoeken u vriendelijk bij betaling per bank het bedrag binnen 14 dagen over te maken op " \
"bovenstaande bankrekening onder vermelding van het bovengenoemde factuurnummer.\n "
latex += "\n"
latex += "\\end{document}\n"
return latex
def runxelatex(fulltexfilename):
fullpdffilename = fulltexfilename.replace(".tex", ".pdf")
if not os.path.exists(fullpdffilename):
command_line = "{0} \"{1}\"".format(xelatex_path, fulltexfilename)
print("would run {0}".format(command_line))
command_result = subprocess.Popen(command_line, stdout=subprocess.PIPE, shell=True, cwd=outputfolder)
output = command_result.communicate()
command_exitcode = command_result.returncode
if command_exitcode == 0:
print("Successfully created {0}".format(fullpdffilename))
else:
print("Error running command '{0}', exit code {1}".format(command_line, command_exitcode))
print(output)
exit(1)
synctextpath = fulltexfilename.replace(".tex", ".synctex.gz")
if os.path.exists(synctextpath):
os.remove(synctextpath)
logpath = fulltexfilename.replace(".tex", ".log")
if os.path.exists(logpath):
os.remove(logpath)
auxpath = fulltexfilename.replace(".tex", ".aux")
if os.path.exists(auxpath):
os.remove(auxpath)
# begin
book = gnucashxml.from_filename(gnucashfile)
for invoice in book.invoices:
if invoice.customer is not None:
if yearfilter in invoice.id:
#if invoice.id == '2017.022':
latexcontent = getlatex(invoice)
filename = "{0} {1}.tex".format(invoice.id, invoice.customer.name)
fullpath = os.path.join(outputfolder, filename)
with open(fullpath, 'w') as outfile:
outfile.write(latexcontent)
runxelatex(fullpath)