]> piware.de Git - bin.git/blob - trechnung
initial checkin
[bin.git] / trechnung
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 import os, re, subprocess, sys
5
6 def search_substr(f, str):
7     for l in f:
8         if l.find(str) >= 0:
9             return l
10
11     return None
12
13 def get_price(str):
14     money_re = re.compile('^-?\d+,\d\d$')
15     fields = str.split()
16     fields.reverse()
17     for f in fields:
18         if money_re.match(f):
19             return float(f.replace(',', '.'))
20
21 def next_price(f, str):
22     p = search_substr(f, str)
23     if p:
24         price = get_price(p)
25         print 'next_price(%s) -> line "%s" -> %.2f' % (str, p.strip(), price)
26         return price
27     return None
28
29 def print_price(p):
30     ks = p.keys()
31     ks.sort()
32     for k in ks:
33         print '%-30s: %10.2f €' % (k, p[k])
34
35 if len(sys.argv) != 2:
36     print 'Usage:', sys.argv[0], '<PDF file>'
37     sys.exit(-1)
38
39 pdfpath = sys.argv[1]
40 txtpath = pdfpath[:-3] + 'txt'
41
42 # create text file
43 assert subprocess.call(['pdftotext', '-enc', 'UTF-8', '-layout', '-nopgbrk', pdfpath]) == 0
44
45 try:
46     f = open(txtpath)
47 finally:
48     os.unlink(txtpath) # so that we don't forget later
49
50 price = {}
51   
52 common = next_price(f, 'Monatliche Beträge')
53 #other = next_price(f, 'Sonstige Leistungen des Konzerns')
54 #if other:
55 #    common = common + other
56 bill_vat = next_price(f, 'Umsatzsteuer 16 %')
57 bill_gross = next_price(f, 'Rechnungsbetrag')
58
59 bill_item_re = re.compile('^\s*\d+\.\s*.*16')
60
61 # Telekom
62 assert search_substr(f, 'Summe Monatliche Beträge')
63 assert search_substr(f, ':::::::::::::')
64 for l in f:
65     if l.find('::::::::::::') >= 0:
66         break
67
68     if bill_item_re.match(l):
69         assert cur_number
70         p = get_price(l)
71         price[cur_number] = price.setdefault(cur_number, 0) + p
72         print 'adding %.2f to %s for "%s"' % (p, cur_number, l.strip())
73     if l.find('Summe Verbindungen für oben angegebene Rufnummer') >= 0:
74         cur_number = None
75     if l.find('Rufnummer (') >= 0:
76         cur_number = l.strip()
77         print l.strip(), ' -> switching to', cur_number
78
79 # other companies
80 if search_substr(f, 'Beträge anderer Anbieter'):
81     for l in f:
82         if l.find('Summe Beträge anderer Anbieter') >= 0:
83             break
84         if bill_item_re.match(l):
85             assert cur_number
86             p = get_price(l)
87             print 'adding %.2f to %s for "%s"' % (p, cur_number, l.strip())
88             price[cur_number] = price.setdefault(cur_number, 0) + p
89         if l.find('Rufnummer (') >= 0:
90             cur_number = l.strip()
91             print l.strip(), ' -> switching to', cur_number
92
93 print '----------------------------------------'
94 print 'Summen:'
95 print_price(price)
96 print 'Allgemeine Gebühren: %.2f' % common
97
98 common = common / len(price)
99 sum = 0
100 for k, v in price.iteritems():
101     price[k] = v + common
102     sum = sum + price[k]
103
104 print '----------------------------------------'
105 print 'Verrechnung allgemeine Gebühren (Aufschlag für jeden: %.2f)' % common
106 print_price(price)
107
108 vat = sum * 0.16
109 gross = sum + vat
110
111 for k, v in price.iteritems():
112     price[k] *= 1.16
113
114 print '----------------------------------------'
115 print 'Aufschlag Mehrwertsteuer:'
116 print_price(price)
117
118 print '----------------------------------------'
119 print 'Berechnete MwST: %.3f, Rechnungs-MwSt: %.2f' % (vat, bill_vat)
120 print 'Berechnete Bruttosume: %.3f, Rechnungs-Bruttosumme: %.2f' % (gross, bill_gross)
121