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