-
Notifications
You must be signed in to change notification settings - Fork 6
/
invoice.py
371 lines (338 loc) · 10.2 KB
/
invoice.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import xml.etree.ElementTree as ET
import argparse
one_to_twenty_n = [
'ένα',
'δύο',
'τρία',
'τέσσερα',
'πέντε',
'έξι',
'επτά',
'οκτώ',
'εννέα',
'δέκα',
'έντεκα',
'δώδεκα',
'δεκατρία',
'δεκατέσσερα',
'δεκαπέντε',
'δεκαέξι',
'δεκαεπτά',
'δεκαοκτώ',
'δεκαεννέα'
]
one_to_twenty_f = [
'μία',
'δύο',
'τρεις',
'τέσσερεις',
'πέντε',
'έξι',
'επτά',
'οκτώ',
'εννέα',
'δέκα',
'έντεκα',
'δώδεκα',
'δεκατρείς',
'δεκατέσσερεις',
'δεκαπέντε',
'δεκαέξι',
'δεκαεπτά',
'δεκαοκτώ',
'δεκαεννέα'
]
one_to_twenty_en = [
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
'ten',
'eleven',
'twelve',
'thirteen',
'fourteen',
'fifteen',
'sixteen',
'seventeen',
'eighteen',
'nineteen'
]
tens = [
'δέκα',
'είκοσι',
'τριάντα',
'σαράντα',
'πενήντα',
'εξήντα',
'εβδομήντα',
'ογδόντα',
'ενενήντα'
]
tens_en = [
'ten',
'twenty',
'thirty',
'forty',
'fifty',
'sixty',
'seventy',
'eighty',
'ninety'
]
hundreds_n = [
'εκατό',
'διακόσια',
'τριακόσια',
'τετρακόσια',
'πεντακόσια',
'εξακόσια',
'επτακόσια',
'οκτακόσια',
'εννιακόσια'
]
hundreds_f = [
'εκατό',
'διακόσιες',
'τριακόσιες',
'τετρακόσιες',
'πεντακόσιες',
'εξακόσιες',
'επτακόσιες',
'οκτακόσιες',
'εννιακόσιες'
]
hundreds_en = [ x + ' hundred' for x in one_to_twenty_en[0:10]]
thousands = [
'χίλια',
'χιλιάδες'
]
thousands_en = [
'one thousand',
'thousand'
]
millions = [
'εκατομμύριο',
'εκατομμύρια'
]
millions_en = [
'one million',
'million'
]
billions = [
'δισεκατομμύριο',
'δισεκατομμύρια'
]
billions_en = [
'one billion',
'billion'
]
def num_to_text_hundreds(number, f, english=False):
parts = []
h, mod100 = divmod(number, 100)
t, mod10 = divmod(mod100, 10)
if english:
one_to_twenty_arr_f = one_to_twenty_en
one_to_twenty_arr_n = one_to_twenty_en
hundreds_arr_f = hundreds_en
hundreds_arr_n = hundreds_en
tens_arr = tens_en
else:
one_to_twenty_arr_f = one_to_twenty_f
one_to_twenty_arr_n = one_to_twenty_n
hundreds_arr_f = hundreds_f
hundreds_arr_n = hundreds_n
tens_arr = tens
if h > 0:
if h == 1 and mod100 > 0 and english:
parts.append(hundreds_arr_n[h - 1] + 'ν')
else:
if f == True:
parts.append(hundreds_arr_f[h - 1])
else:
parts.append(hundreds_arr_n[h - 1])
if t > 1:
parts.append(tens_arr[t - 1])
if mod10 > 0:
if english:
parts[-1] = parts[-1] + '-' + one_to_twenty_arr_f[mod10 - 1]
elif f == True:
parts.append(one_to_twenty_arr_f[mod10 - 1])
else:
parts.append(one_to_twenty_arr_n[mod10 - 1])
elif t == 1:
parts.append(one_to_twenty_arr_n[10 + mod10 - 1])
elif mod10 > 0:
if f == True:
parts.append(one_to_twenty_arr_f[mod10 - 1])
else:
parts.append(one_to_twenty_arr_n[mod10 - 1])
return ' '.join(parts)
def num_to_text_thousands(number, english=False):
th, r = divmod(number, 1000)
if english:
thousands_arr = thousands_en
else:
thousands_arr = thousands
if th > 1:
return "{0} {1} {2}".format(num_to_text_hundreds(th, True, english),
thousands_arr[1],
num_to_text_hundreds(r, False, english))
elif th == 1:
return "{0} {1}".format(thousands_arr[0],
num_to_text_hundreds(r, False, english))
else:
return num_to_text_hundreds(r, False, english)
def num_to_text_millions(number, english=False):
m, r = divmod(number, 1000000)
if english:
millions_arr = millions_en
else:
millions_arr = millions
if m > 1:
return "{0} {1} {2}".format(num_to_text_hundreds(m, False),
millions_arr[1],
num_to_text_thousands(r, english))
elif m == 1:
return "{0} {1} {2}".format(one_to_twenty_n[0],
millions_arr[0],
num_to_text_thousands(r, english))
else:
return num_to_text_thousands(number, english)
def num_to_text_billions(number, english=False):
m, r = divmod(number, 1000000000)
if english:
billions_arr = billions_en
else:
billions_arr = billions
if m > 1:
return "{0} {1} {2}".format(num_to_text_hundreds(m, False),
billions_arr[1],
num_to_text_millions(r, english))
elif m == 1:
return "{0} {1} {2}".format(one_to_twenty_n[0],
billions_arr[0],
num_to_text_millions(r, english))
else:
return num_to_text_millions(number, english)
def num_to_text(number, english=False):
return num_to_text_billions(number, english)
def make_percentage(number):
percentage = "{:.2f}".format(number * 100)
if percentage.endswith('.00'):
percentage = percentage.replace('.00', '')
return percentage
parser = argparse.ArgumentParser(description='Invoice generator')
parser.add_argument('invoice_data')
parser.add_argument('-t', '--template', dest='template',
default='invoice.tex',
help='use TEMPLATE as LaTeX template')
parser.add_argument('-e', '--english', dest='english',
action='store_true',
default=False,
help='include English output')
args = parser.parse_args()
tree = ET.parse(args.invoice_data)
root = tree.getroot()
num = root.find('num').text
date = root.find('date').text
stamp = root.find('stamp').text
client = root.find('client').text
occupation = root.find('occupation').text
taxoffice = root.find('taxoffice').text
address = root.find('address').text
taxnumber = root.find('taxnumber').text
description = root.find('description').text
value_f = float(root.find('value').text)
tax_rate_el = root.find('tax_rate')
uni_rate_el = root.find('uni_rate')
if tax_rate_el is not None:
tax_rate = tax_rate_el.text
else:
tax_rate = "0.20" # default value
tax_rate_f = float(tax_rate)
tax_rate_prc = make_percentage(tax_rate_f)
vat_rate_el = root.find('vat_rate')
if vat_rate_el is not None:
vat_rate = vat_rate_el.text
else:
vat_rate = "0.24" # default value
vat_rate_f = float(vat_rate)
vat_rate_prc = make_percentage(vat_rate_f)
if uni_rate_el is not None:
uni_rate = uni_rate_el.text
else:
uni_rate = '0.07'
uni_rate_f = float(uni_rate)
uni_rate_prc = make_percentage(uni_rate_f)
value = "{:.2f}".format(value_f)
tax_f = value_f * tax_rate_f
vat_element = root.find('vat')
if vat_element is not None:
vat_f = float(vat_element.text)
else:
vat_f = value_f * vat_rate_f
total_f = value_f + vat_f
tax = "{:.2f}".format(tax_f)
vat = "{:.2f}".format(vat_f)
total = "{:.2f}".format(total_f)
(intpart, floatpart) = total.split('.')
numbertext = "{0} ευρώ".format(num_to_text(int(intpart)))
uni_f = value_f * uni_rate_f
uni = "{:.2f}".format(uni_f)
if args.english:
numbertext_en = "{0} euros".format(num_to_text(int(intpart), True))
else:
numbertext_en = ""
if floatpart != '' :
floatpart_i = int(floatpart)
if floatpart_i > 0:
if floatpart_i > 1:
dec_desc = 'λεπτά'
else:
dec_desc = 'λεπτό'
numbertext = "{0} και {1} {2}".format(numbertext,
num_to_text(int(floatpart)),
dec_desc)
if args.english:
if floatpart_i > 1:
dec_desc = 'cents'
else:
dec_desc = 'cent'
numbertext_en = "{0} and {1} {2}".format(numbertext_en,
num_to_text(
int(floatpart),
True),
dec_desc)
outfn = 'invoice_' + num + '.tex'
with open(args.template, mode='r', encoding='utf-8') as inf:
with open(outfn, mode='w', encoding='utf-8') as outf:
for line in inf:
line = line.replace("{{NUM}}", num)
line = line.replace("{{DATE}}", date)
line = line.replace("{{STAMP}}", stamp)
line = line.replace("{{CLIENT}}", client)
line = line.replace("{{OCCUPATION}}", occupation)
line = line.replace("{{TAXOFFICE}}", taxoffice)
line = line.replace("{{ADDRESS}}", address)
line = line.replace("{{TAXNUMBER}}", taxnumber)
line = line.replace("{{DESCRIPTION}}", description)
line = line.replace("{{VALUE}}", value)
line = line.replace("{{TAXRATE}}", tax_rate_prc)
line = line.replace("{{TAX}}", tax)
line = line.replace("{{VATRATE}}", vat_rate_prc)
line = line.replace("{{VAT}}", vat)
line = line.replace("{{TOTAL}}", total)
line = line.replace("{{NUMBERTEXT}}", numbertext.capitalize())
line = line.replace("{{NUMBERTEXTEN}}", numbertext_en.capitalize())
line = line.replace("{{UNIRATE}}", uni_rate_prc)
line = line.replace("{{UNI}}", uni)
outf.write(line)