summaryrefslogtreecommitdiff
path: root/lasku.py
diff options
context:
space:
mode:
Diffstat (limited to 'lasku.py')
-rwxr-xr-xlasku.py91
1 files changed, 91 insertions, 0 deletions
diff --git a/lasku.py b/lasku.py
new file mode 100755
index 0000000..8f9e185
--- /dev/null
+++ b/lasku.py
@@ -0,0 +1,91 @@
+#!/usr/bin/env python3
+
+
+import sys
+
+import yaml
+
+
+def euros(cents):
+ return '%.02f' % (cents / 100.0)
+
+
+class Item:
+
+ def __init__(self, itemdict):
+ self.dict = itemdict
+
+ count = itemdict['count']
+ unit_cents = int(itemdict['unit'] * 100)
+ vatpc = float(itemdict['vatpc']) / 100.0
+
+ self.total_cents = count * unit_cents
+ self.vat_cents = int(unit_cents * vatpc)
+
+ self.dict.update({
+ 'total_cents': self.total_cents,
+ 'total': euros(self.total_cents),
+ 'vat_cents': self.vat_cents,
+ 'vat': euros(self.vat_cents),
+ 'vatsum': euros(self.vat_cents * count),
+ 'totalwithvat': euros(count * (unit_cents + self.vat_cents)),
+ })
+
+
+item_template = (
+ r'\raggedbottom #number# & '
+ r'\parbox[t]{5.5cm}{#desc#\\#date#} & '
+ r'\raggedbottom #count# & '
+ r'\raggedbottom #unit# & '
+ r'\raggedbottom #vat# & '
+ r'\raggedbottom #vatpc#\% & '
+ r'\raggedbottom #total# & '
+ r'\raggedbottom #vatsum# & '
+ r'\raggedbottom #totalwithvat# \\[2.2ex]'
+)
+
+
+def cat(filename):
+ with open(filename) as f:
+ return f.read()
+
+
+def substitute_all(text, subst):
+ for key, value in subst.items():
+ pattern = '#{}#'.format(key)
+ text = str(value).join(text.split(pattern))
+ return text
+
+
+text = cat('template.tex')
+
+with open(sys.argv[1]) as f:
+ company = yaml.safe_load(f)
+with open(sys.argv[2]) as f:
+ invoice = yaml.safe_load(f)
+
+items = invoice['invoiceitems']
+values = dict(company)
+values.update(invoice)
+
+itemtext = ''
+total_cents = 0
+total_vat = 0
+total_sum = 0
+
+for item in items:
+ it = Item(item)
+ itemtext += substitute_all(item_template, it.dict)
+ total_cents += it.total_cents
+ total_vat += it.vat_cents
+ total_sum += it.total_cents + it.vat_cents
+
+values['itemtext'] = itemtext
+values['totalraw'] = euros(total_cents)
+values['totalvat'] = euros(total_vat)
+values['totalsum'] = euros(total_sum)
+
+values['amount'] = values['totalsum']
+
+text = substitute_all(text, values)
+sys.stdout.write(text)