home · contact · privacy
Improve accounting scripts.
[misc] / ledger.py
1 import os
2 import decimal
3 from datetime import datetime, timedelta
4 from urllib.parse import parse_qs, urlparse
5 from jinja2 import Environment as JinjaEnv, FileSystemLoader as JinjaFSLoader 
6 from plomlib import PlomDB, PlomException, run_server, PlomHandler 
7
8 db_path = '/home/plom/org/ledger2024.dat'
9 server_port = 8082
10 j2env = JinjaEnv(loader=JinjaFSLoader('ledger_templates'))
11
12 class EditableException(PlomException):
13     def __init__(self, booking_index, *args, **kwargs):
14         self.booking_index = booking_index
15         super().__init__(*args, **kwargs)
16     
17
18 # html_head = """
19 # <style>
20 # body { color: #000000; }
21 # table { margin-bottom: 2em; }
22 # th, td { text-align: left }
23 # input[type=number] { text-align: right; font-family: monospace; }
24 # .money { font-family: monospace; text-align: right; }
25 # .comment { font-style: italic; color: #777777; }
26 # .meta { font-size: 0.75em; color: #777777; }
27 # .full_line_comment { display: block; white-space: nowrap; width: 0; }
28 # </style>
29 # <body>
30 # <a href="{{prefix}}/ledger">ledger</a>
31 # <a href="{{prefix}}/ledger2">ledger2</a>
32 # <a href="{{prefix}}/balance">balance</a>
33 # <a href="{{prefix}}/balance2">balance2</a>
34 # <a href="{{prefix}}/add_free">add free</a>
35 # <a href="{{prefix}}/add_structured">add structured</a>
36 # <a href="{{prefix}}/edit">add</a>
37 # <hr />
38 # """
39 # booking_html = """
40 # <p id="{{nth}}"><a href="{{prefix}}#{{nth}}">{{date}}</a> {{desc}} <span class="comment">{{head_comment|e}}</span><br />
41 # <span class="meta">[edit: <a href="{{prefix}}/add_structured?start={{start}}&end={{end}}">structured</a>
42 # / <a href="{{prefix}}/add_free?start={{start}}&end={{end}}">free</a>
43 # | copy:<a href="{{prefix}}/copy_structured?start={{start}}&end={{end}}">structured</a>
44 # / <a href="{{prefix}}/copy_free?start={{start}}&end={{end}}">free</a>
45 # | move {% if move_up %}<a href="{{prefix}}/move_up?start={{start}}&end={{end}}">up</a>{% else %}up{% endif %}/{% if move_down %}<a href="{{prefix}}/move_down?start={{start}}&end={{end}}">down</a>{% else %}down{% endif %}
46 # | <a href="{{prefix}}/balance?stop={{nth+1}}">balance after</a>
47 # ]</span>
48 # <table>
49 # {% for l in booking_lines %}
50 # {% if l.acc %}
51 # <tr><td>{{l.acc|e}}</td><td class="money">{{l.money|e}}</td><td class="comment">{{l.comment|e}}</td></tr>
52 # {% else %}
53 # <tr><td><div class="comment full_line_comment">{{l.comment|e}}</div></td></tr>
54 # {% endif %}
55 # {% endfor %}
56 # </table></p>
57 # """
58 # add_form_header = """<form method="POST" action="{{action|e}}">
59 # <input type="submit" name="check" value="check" tabindex="5"  />
60 # <input type="submit" name="revert" value="revert" tabindex="5"  />
61 # """
62 # add_form_footer = """
63 # <input type="hidden" name="start" value={{start}} />
64 # <input type="hidden" name="end" value={{end}} />
65 # <input type="submit" name="save" value="save!" tabindex="5" >
66 # </form>
67 # """
68 # add_free_html = """<br />
69 # <textarea name="booking" rows=10 cols=80>{% for line in lines %}{{ line }}{% if not loop.last %}
70 # {% endif %}{% endfor %}</textarea>
71 # """
72 # add_structured_html = """
73 # <input type="submit" name="add_taxes" value="add taxes" tabindex="5" />
74 # <input type="submit" name="add_taxes2" value="add taxes2" tabindex="5"  />
75 # <input type="submit" name="add_sink" value="add sink" tabindex="5"  />
76 # <input name="replace_from" tabindex="5"  />
77 # <input type="submit" name="replace" value="-> replace ->" tabindex="5"  />
78 # <input name="replace_to" tabindex="5"  />
79 # <input type="submit" name="add_mirror" value="add mirror" tabindex="5"  />
80 # <br />
81 # <input name="date" value="{{date|e}}" size=9 tabindex="3"  />
82 # <input name="description" value="{{desc|e}}" list="descriptions" tabindex="3"  />
83 # <textarea name="line_0_comment" rows=1 cols=20 tabindex="3" >{{head_comment|e}}</textarea>
84 # <input type="submit" name="line_0_add" value="[+]" tabindex="5"  />
85 # <br />
86 # {% for line in booking_lines %}
87 # <input name="line_{{line.i}}_account" value="{{line.acc|e}}" size=40 list="accounts" tabindex="3" />
88 # <input type="number" name="line_{{line.i}}_amount" step=0.01 value="{{line.amt}}" size=10 tabindex="3" />
89 # <input name="line_{{line.i}}_currency" value="{{line.curr|e}}" size=3 list="currencies" tabindex="5" />
90 # <input type="submit" name="line_{{line.i}}_delete" value="[x]" tabindex="5" />
91 # <input type="submit" name="line_{{line.i}}_delete_after" value="[XX]" tabindex="5" />
92 # <input type="submit" name="line_{{line.i}}_add" value="[+]" tabindex="5" />
93 # <textarea name="line_{{line.i}}_comment" rows=1 cols={% if line.comm_cols %}{{line.comm_cols}}{% else %}20{% endif %} tabindex="3">{{line.comment|e}}</textarea>
94 # <br />
95 # {% endfor %}
96 # {% for name, items in datalist_sets.items() %}
97 # <datalist id="{{name}}">
98 # {% for item in items %}
99 #   <option value="{{item|e}}">{{item|e}}</option>
100 # {% endfor %}
101 # </datalist>
102 # {% endfor %}
103 # """
104
105
106 class Wealth:
107
108     def __init__(self):
109         self.money_dict = {}
110
111     def add_money_dict(self, moneys):
112         for currency, amount in moneys.items():
113             if not currency in self.money_dict.keys():
114                 self.money_dict[currency] = 0
115             self.money_dict[currency] += amount
116
117     def add_wealth(self, moneys):
118         self.add_money_dict(moneys.money_dict)
119
120     @property
121     def as_sink_dict(self):
122         sink = {} 
123         for currency, amount in self.money_dict.items():
124             if 0 == amount:
125                 continue
126             sink[currency] = -amount
127         return sink 
128
129
130 class Account:
131
132     def __init__(self, own_name, parent):
133         self.own_name = own_name
134         self.parent = parent
135         if self.parent:
136             self.parent.children += [self]
137         self.own_moneys = Wealth() 
138         self.children = []
139
140     def add_wealth(self, moneys):
141         self.own_moneys.add_wealth(moneys)
142
143     @property
144     def full_name(self):
145         if self.parent and len(self.parent.own_name) > 0:
146             return f'{self.parent.full_name}:{self.own_name}'
147         else:
148             return self.own_name
149
150     @property
151     def full_moneys(self):
152         full_moneys = Wealth() 
153         full_moneys.add_wealth(self.own_moneys)
154         for child in self.children:
155             full_moneys.add_wealth(child.full_moneys)
156         return full_moneys
157
158
159 # def apply_booking_to_account_balances(account_sums, account, currency, amount):
160 #     if not account in account_sums:
161 #         account_sums[account] = {currency: amount}
162 #     elif not currency in account_sums[account].keys():
163 #         account_sums[account][currency] = amount
164 #     else:
165 #         account_sums[account][currency] += amount
166
167
168 # def bookings_to_account_tree(bookings):
169 #     account_sums = {}
170 #     for booking in bookings:
171 #         for account, changes in booking.account_changes.items():
172 #             for currency, amount in changes.items():
173 #                 apply_booking_to_account_balances(account_sums, account, currency, amount)
174 #     account_tree = {}
175 #     def collect_branches(account_name, path):
176 #         node = account_tree
177 #         path_copy = path[:]
178 #         while len(path_copy) > 0:
179 #             step = path_copy.pop(0)
180 #             node = node[step]
181 #         toks = account_name.split(":", maxsplit=1)
182 #         parent = toks[0]
183 #         if parent in node.keys():
184 #             child = node[parent]
185 #         else:
186 #             child = {}
187 #             node[parent] = child
188 #         if len(toks) == 2:
189 #             k, v = collect_branches(toks[1], path + [parent])
190 #             if k not in child.keys():
191 #                 child[k] = v
192 #             else:
193 #                 child[k].update(v)
194 #         return parent, child
195 #     for account_name in sorted(account_sums.keys()):
196 #         k, v = collect_branches(account_name, [])
197 #         if k not in account_tree.keys():
198 #             account_tree[k] = v
199 #         else:
200 #             account_tree[k].update(v)
201 #     def collect_totals(parent_path, tree_node):
202 #         for k, v in tree_node.items():
203 #             child_path = parent_path + ":" + k
204 #             for currency, amount in collect_totals(child_path, v).items():
205 #                 apply_booking_to_account_balances(account_sums, parent_path, currency, amount)
206 #         return account_sums[parent_path]
207 #     for account_name in account_tree.keys():
208 #         account_sums[account_name] = collect_totals(account_name, account_tree[account_name])
209 #     return account_tree, account_sums
210
211
212 def parse_lines2(lines, ignore_editable_exceptions=False):
213     lines = lines.copy() + ['']  # to simulate ending of last booking
214     bookings = []
215     inside_booking = False
216     booking_lines = []
217     booking_start_i = 0
218     last_date = '' 
219     for i, line in enumerate(lines):
220         intro = f'file line {i}'
221         stripped_line = line.rstrip()
222         if stripped_line == '':
223             if inside_booking:
224                 booking = Booking(lines=booking_lines, starts_at=booking_start_i)
225                 if last_date > booking.date and not ignore_editable_exceptions:
226                     raise EditableException(len(bookings), f'{intro}: out-of-order date (follows {last_date})')
227                 last_date = booking.date
228                 bookings += [booking]
229                 inside_booking =False
230                 booking_lines = []
231         else:
232             if not inside_booking:
233                 booking_start_i = i
234             inside_booking = True
235             booking_lines += [line]
236     if inside_booking:
237         raise PlomException(f'{intro}: last booking unfinished')
238     return bookings
239
240
241 # def parse_lines(lines, validate_bookings=True):
242 #     inside_booking = False
243 #     date_string, description = None, None
244 #     booking_lines = []
245 #     start_line = 0
246 #     bookings = []
247 #     comments = []
248 #     lines = lines.copy() + [''] # to ensure a booking-ending last line
249 #     last_date = ''
250 #     for i, line in enumerate(lines):
251 #         prefix = f"line {i}"
252 #         # we start with the case of an utterly empty line
253 #         comments += [""]
254 #         stripped_line = line.rstrip()
255 #         if stripped_line == '':
256 #             if inside_booking:
257 #                 # assume we finished a booking, finalize, and commit to DB
258 #                 if len(booking_lines) < 2:
259 #                     raise PlomException(f"{prefix} booking ends to early")
260 #                 booking = Booking(date_string, description, booking_lines, start_line, validate_bookings)
261 #                 bookings += [booking]
262 #             # expect new booking to follow so re-zeroall booking data
263 #             inside_booking = False
264 #             date_string, description = None, None
265 #             booking_lines = []
266 #             continue
267 #         # if non-empty line, first get comment if any, and commit to DB
268 #         split_by_comment = stripped_line.split(sep=";", maxsplit=1)
269 #         if len(split_by_comment) == 2:
270 #             comments[i] = split_by_comment[1].lstrip()
271 #         # if pre-comment empty: if inside_booking, this must be a comment-only line so we keep it for later ledger-output to capture those comments; otherwise, no more to process for this line
272 #         non_comment = split_by_comment[0].rstrip()
273 #         if non_comment.rstrip() == '':
274 #              if inside_booking:
275 #                  booking_lines += ['']
276 #              continue
277 #         # if we're starting a booking, parse by first-line pattern
278 #         if not inside_booking:
279 #             start_line = i
280 #             toks = non_comment.split(maxsplit=1)
281 #             date_string = toks[0]
282 #             try:
283 #                 datetime.strptime(date_string, '%Y-%m-%d')
284 #             except ValueError:
285 #                 raise PlomException(f"{prefix} bad date string: {date_string}")
286 #             # if last_date > date_string:
287 #             #     raise PlomException(f"{prefix} out-of-order-date")
288 #             last_date = date_string
289 #             try:
290 #                 description = toks[1]
291 #             except IndexError:
292 #                 raise PlomException(f"{prefix} bad description: {description}")
293 #             inside_booking = True
294 #             booking_lines += [non_comment]
295 #             continue
296 #         # otherwise, read as transfer data
297 #         toks = non_comment.split()  # ignore specification's allowance of single spaces in names
298 #         if len(toks) > 3:
299 #             raise PlomException(f"{prefix} too many booking line tokens: {toks}")
300 #         amount, currency = None, None
301 #         account_name = toks[0]
302 #         if account_name[0] == '[' and account_name[-1] == ']':
303 #             # ignore specification's differentiation of "virtual" accounts
304 #             account_name = account_name[1:-1]
305 #         decimal_chars = ".-0123456789"
306 #         if len(toks) == 3:
307 #             i_currency = 1
308 #             try:
309 #                 amount = decimal.Decimal(toks[1])
310 #                 i_currency = 2
311 #             except decimal.InvalidOperation:
312 #                 try:
313 #                     amount = decimal.Decimal(toks[2])
314 #                 except decimal.InvalidOperation:
315 #                     raise PlomException(f"{prefix} no decimal number in: {toks[1:]}")
316 #             currency = toks[i_currency]
317 #             if currency[0] in decimal_chars:
318 #                 raise PlomException(f"{prefix} currency starts with int, dot, or minus: {currency}")
319 #         elif len(toks) == 2:
320 #             value = toks[1]
321 #             inside_amount = False
322 #             inside_currency = False
323 #             amount_string = ""
324 #             currency = ""
325 #             dots_counted = 0
326 #             for i, c in enumerate(value):
327 #                 if i == 0:
328 #                     if c in decimal_chars:
329 #                         inside_amount = True
330 #                     else:
331 #                         inside_currency = True
332 #                 if inside_currency:
333 #                     if c in decimal_chars and len(amount_string) == 0:
334 #                         inside_currency = False
335 #                         inside_amount = True
336 #                     else:
337 #                         currency += c
338 #                         continue
339 #                 if inside_amount:
340 #                     if c not in decimal_chars:
341 #                         if len(currency) > 0:
342 #                             raise PlomException(f"{prefix} amount has non-decimal chars: {value}")
343 #                         inside_currency = True
344 #                         inside_amount = False
345 #                         currency += c
346 #                         continue
347 #                     if c == '-' and len(amount_string) > 1:
348 #                         raise PlomException(f"{prefix} amount has non-start '-': {value}")
349 #                     if c == '.':
350 #                         if dots_counted > 1:
351 #                             raise PlomException(f"{prefix} amount has multiple dots: {value}")
352 #                         dots_counted += 1
353 #                     amount_string += c
354 #             if len(currency) == 0:
355 #                 raise PlomException(f"{prefix} currency missing: {value}")
356 #             if len(amount_string) > 0:
357 #                 amount = decimal.Decimal(amount_string)
358 #         booking_lines += [(account_name, amount, currency)]
359 #     if inside_booking:
360 #         raise PlomException(f"{prefix} last booking unfinished")
361 #     return bookings, comments
362
363
364 class TransferLine:
365
366     def __init__(self, line=None, account='', amount=None, currency='', comment='', validate=True):
367         self.account = account 
368         self.amount = amount 
369         self.currency = currency 
370         self.comment = comment 
371         if line:
372             split_by_comment = line.rstrip().split(sep=';', maxsplit=1)
373             if len(split_by_comment) == 0:
374                 raise PlomException('line empty')
375             elif len(split_by_comment) == 2:
376                 self.comment = split_by_comment[1].lstrip()
377             non_comment = split_by_comment[0].rstrip()
378             toks = non_comment.split()
379             if (len(toks) not in {1, 3}):
380                 if validate:
381                     raise PlomException(f'number of non-comment tokens not 1 or 3')
382                 elif len(toks) == 2:
383                     toks += ['']
384                 else:
385                     toks = 3*['']
386             self.account = toks[0]
387             if len(toks) != 1:
388                 try:
389                     self.amount = decimal.Decimal(toks[1])
390                 except decimal.InvalidOperation:
391                     if validate:
392                         raise PlomException(f'invalid token for Decimal: {toks[1]}')
393                     else:
394                         self.amount = toks[1]
395                 self.currency = toks[2]
396
397     @property
398     def amount_fmt(self):
399         if self.amount is None:
400             return ''
401         elif self.amount.as_tuple().exponent < -2:
402             return f'{self.amount:.1f}…'
403         else:
404             return f'{self.amount:.2f}'
405
406     def for_writing(self):
407         line = f'  {self.account}'
408         if self.amount is not None:
409             line += f'  {self.amount} {self.currency}'
410         if self.comment != '':
411             line += f' ; {self.comment}' 
412         return line
413
414     @property
415     def comment_cols(self):
416         return max(20, len(self.comment))
417
418
419 class Booking:
420
421     def __init__(self, lines=None, starts_at='?', date='', description='', top_comment='', transfer_lines=None, validate=True):
422         self.validate = validate
423         self.starts_at = starts_at 
424         self.intro = f'booking starting at line {self.starts_at}'
425         self.clean()
426         if lines:
427             self.real_lines = lines 
428             self.parse_lines()
429         else:
430             self.date = date
431             self.description = description
432             self.top_comment = top_comment 
433             self.validate_head()
434             self.transfer_lines = transfer_lines
435             if len(self.transfer_lines) < 2 and self.validate:
436                 raise PlomException(f'{self.intro}: too few transfer lines')
437             self.calculate_account_changes()
438             self.real_lines = self.for_writing()
439
440     @classmethod
441     def from_postvars(cls, postvars, starts_at='?', validate=True):
442         date = postvars['date'][0] 
443         description = postvars['description'][0] 
444         top_comment = postvars['top_comment'][0] 
445         transfer_lines = []
446         for i, account in enumerate(postvars['account']):
447             if len(account) == 0:
448                 continue
449             amount = None
450             if len(postvars['amount'][i]) > 0:
451                 amount = decimal.Decimal(postvars['amount'][i])
452             transfer_lines += [TransferLine(None, account, amount, postvars['currency'][i], postvars['comment'][i])]
453         return cls(None, starts_at, date, description, top_comment, transfer_lines, validate=validate)
454
455     def clean(self):
456         self.transfer_lines = []
457         self.account_changes = {}
458
459     def parse_lines(self):
460         self.top_comment = '' 
461         if len(self.real_lines) < 3 and self.validate:
462             raise PlomException(f'{self.intro}: ends with less than 3 lines:' + str(self.real_lines))
463         split_by_comment = self.real_lines[0].rstrip().split(sep=";", maxsplit=1)
464         if len(split_by_comment) == 0 and self.validate:
465             raise PlomException('{self.intro}: headline empty')
466         elif len(split_by_comment) == 2 and self.validate:
467             self.top_comment = split_by_comment[1].lstrip()
468         non_comment = split_by_comment[0].rstrip()
469         toks = non_comment.split()
470         if len(toks) < 2:
471             if self.validate:
472                 raise PlomException(f'{self.intro}: headline missing elements: {non_comment}')
473             elif 0 == len(toks):
474                 toks = 2*['']
475             else:
476                 toks += ['']
477         self.date = toks[0]
478         self.description = toks[1]
479         self.validate_head()
480         for i, line in enumerate(self.real_lines[1:]):
481             try:
482                 self.transfer_lines += [TransferLine(line, validate=self.validate)]
483             except PlomException as e:
484                 raise PlomException(f'{self.intro}, transfer line {i}: {e}')
485         self.calculate_account_changes()
486
487     def calculate_account_changes(self):
488         sink_account = None 
489         money_changes = Wealth() 
490         for i, transfer_line in enumerate(self.transfer_lines):
491             intro = f'{self.intro}, transfer line {i}'
492             if transfer_line.amount is None:
493                 if sink_account is not None and self.validate:
494                     raise PlomException(f'{intro}: second sink found (only one allowed)')
495                 sink_account = transfer_line.account
496             else:
497                 if not transfer_line.account in self.account_changes.keys():
498                     self.account_changes[transfer_line.account] = Wealth()
499                 money = {transfer_line.currency: transfer_line.amount}
500                 self.account_changes[transfer_line.account].add_money_dict(money)
501                 money_changes.add_money_dict(money)
502         if sink_account is None and len(money_changes.as_sink_dict) > 0 and self.validate:
503             raise PlomException(f'{intro}: does not balance (unused non-empty sink)')
504         if sink_account is not None:
505             if not sink_account in self.account_changes.keys():
506                 self.account_changes[sink_account] = Wealth()
507             self.account_changes[sink_account].add_money_dict(money_changes.as_sink_dict)
508
509     def validate_head(self):
510         if not self.validate:
511             return
512         if len(self.date) == 0:
513             raise PlomException(f'{self.intro}: missing date')
514         if len(self.description) == 0:
515             raise PlomException(f'{self.intro}: missing description')
516         try:
517             datetime.strptime(self.date, '%Y-%m-%d')
518         except ValueError:
519             raise PlomException(f'{self.intro}: bad headline date format: {self.date}')
520
521     def fill_sink(self):
522         replacement_lines = []
523         for i, line in enumerate(self.transfer_lines):
524             if line.amount is None:
525                 for currency, amount in self.account_changes[line.account].money_dict.items():
526                     replacement_lines += [TransferLine(None, f'{line.account}', amount, currency).for_writing()]
527                 break
528         lines = self.real_lines[:i+1] + replacement_lines + self.real_lines[i+2:]
529         self.clean()
530         self.real_lines = lines
531         self.parse_lines()
532
533     def add_mirror(self):
534         new_transfer_lines = []
535         for line in self.transfer_lines:
536             new_transfer_lines += [TransferLine(None, f'({line.account})', line.amount, line.currency, line.comment)]
537         for transfer_line in new_transfer_lines:
538             self.real_lines += [transfer_line.for_writing()]
539         self.clean()
540         self.parse_lines()
541
542     def add_taxes(self):
543         acc_kk_add = 'Reserves:KrankenkassenBeitragsWachstum'
544         acc_kk_minimum = 'Reserves:Month:KrankenkassenDefaultBeitrag'
545         acc_kk = 'Expenses:KrankenKasse'
546         acc_est = 'Reserves:EinkommensSteuer'
547         acc_assets = 'Assets'
548         acc_buffer = 'Reserves:NeuAnfangsPuffer:Ausgaben'
549         buffer_expenses = 0  # FIXME: hardcoded for now 
550         kk_expenses = 0  # FIXME: hardcoded for now 
551         est_expenses = 0  # FIXME: hardcoded for now 
552         months_passed = 0  # FIXME: hardcoded for now 
553         last_monthbreak_assets = 0  # FIXME: hardcoded for now 
554         last_monthbreak_est = 0  # FIXME: hardcoded for now 
555         last_monthbreak_kk_minimum = 0  # FIXME: hardcoded for now 
556         last_monthbreak_kk_add = 0  # FIXME: hardcoded for now 
557         expenses_so_far = 0  # FIXME: hardcoded for now 
558         needed_netto = self.account_changes['Assets'].money_dict['€']
559         ESt_this_month = 0
560         left_over = needed_netto - ESt_this_month
561         too_low = 0
562         too_high = 2 * needed_netto 
563         E0 = decimal.Decimal(10908)
564         E1 = decimal.Decimal(15999)
565         E2 = decimal.Decimal(62809)
566         E3 = decimal.Decimal(277825)
567         while True:
568             zvE = buffer_expenses - kk_expenses + (12 - months_passed) * needed_netto 
569             # if finish:
570             #     zvE += last_monthbreak_assets + last_monthbreak_kk_add + last_monthbreak_kk_minimum
571             if zvE < E0:
572                 ESt = decimal.Decimal(0)
573             elif zvE < E1:
574                 y = (zvE - E0)/10000
575                 ESt = (decimal.Decimal(979.18) * y + 1400) * y
576             elif zvE < E2:
577                 y = (zvE - E1)/10000
578                 ESt = (decimal.Decimal(192.59) * y + 2397) * y + decimal.Decimal(966.53)
579             elif zvE < E3:
580                 ESt = decimal.Decimal(0.42) * (zvE - decimal.Decimal(62809))  + decimal.Decimal(16405.54)
581             else:
582                 ESt = decimal.Decimal(0.45) * (zvE - decimal.Decimal(277825)) + decimal.Decimal(106713.52)
583             ESt_this_month = (ESt + last_monthbreak_est - est_expenses) / (12 - months_passed)
584             left_over = needed_netto - ESt_this_month
585             if abs(left_over - expenses_so_far) < 0.001:
586                 break
587             elif left_over < expenses_so_far:
588                 too_low = needed_netto 
589             elif left_over > expenses_so_far:
590                 too_high = needed_netto 
591             needed_netto = too_low + (too_high - too_low)/2
592         ESt_this_month = ESt_this_month.quantize(decimal.Decimal('0.00'))
593         self.transfer_lines += [TransferLine(None, acc_est, ESt_this_month, '€')]
594         kk_minimum_income = 1131.67
595         kk_factor = decimal.Decimal(0.197)
596         kk_minimum_tax = decimal.Decimal(222.94).quantize(decimal.Decimal('0.00'))
597         kk_add_so_far = account_sums[acc_kk_add]['€'] if acc_kk_add in account_sums.keys() else 0
598         kk_add = needed_netto / (1 - kk_factor) - needed_netto - kk_minimum_tax
599         hit_kk_minimum_income_limit = False
600         if kk_add_so_far + kk_add < 0:
601             hit_kk_minimum_income_limit = True 
602             kk_add_uncorrect = kk_add
603             kk_add = -(kk_add + kk_add_so_far) 
604         kk_add = decimal.Decimal(kk_add).quantize(decimal.Decimal('0.00'))
605         self.transfer_lines += [TransferLine(None, acc_kk_minimum, kk_minimum_tx, '€')]
606         self.transfer_lines += [TransferLine(None, acc_kk_add, kk_add, '€')]
607         diff = - last_monthbreak_est + ESt_this_month - last_monthbreak_kk_add + kk_add
608         if not finish:
609             diff += kk_minimum_tax
610         final_minus = expenses_so_far - old_needed_income_before_anything + diff
611         diff += kk_minimum_tax
612         final_minus = expenses_so_far - old_needed_income_before_anything + diff
613         self.transfer_lines += [TransferLine(None, acc_assets, -diff, '€')]
614         self.transfer_lines += [TransferLine(None, acc_assets, final_minus, '€')]
615         self.transfer_lines += [TransferLine(None, acc_buffer, -final_minus, '€')]
616
617     def for_writing(self):
618         lines = [f'{self.date} {self.description}']
619         if self.top_comment is not None and self.top_comment.rstrip() != '':
620             lines[0] += f' ; {self.top_comment}'
621         for line in self.transfer_lines:
622             lines += [line.for_writing()]
623         return lines
624
625     @property
626     def comment_cols(self):
627         return max(20, len(self.top_comment))
628
629
630 # class Booking:
631
632 #     def __init__(self, date_string, description, booking_lines, start_line, process=True):
633 #         self.date_string = date_string
634 #         self.description = description
635 #         self.lines = booking_lines
636 #         self.start_line = start_line
637 #         if process:
638 #             self.validate_booking_lines()
639 #             self.sink = {}
640 #             self.account_changes = self.parse_booking_lines_to_account_changes()
641
642 #     def validate_booking_lines(self):
643 #         prefix = f"booking at line {self.start_line}"
644 #         sums = {}
645 #         empty_values = 0
646 #         for line in self.lines[1:]:
647 #             if line == '':
648 #                 continue
649 #             _, amount, currency = line
650 #             if amount is None:
651 #                 if empty_values > 0:
652 #                     raise PlomException(f"{prefix} relates more than one empty value of same currency {currency}")
653 #                 empty_values += 1
654 #                 continue
655 #             if currency not in sums:
656 #                 sums[currency] = 0
657 #             sums[currency] += amount
658 #         if empty_values == 0:
659 #             for k, v in sums.items():
660 #                 if v != 0:
661 #                     raise PlomException(f"{prefix} does not add up to zero / {k} {v}")
662 #         else:
663 #             sinkable = False
664 #             for k, v in sums.items():
665 #                 if v != 0:
666 #                     sinkable = True
667 #             if not sinkable:
668 #                 raise PlomException(f"{prefix} has empty value that cannot be filled")
669
670 #     def parse_booking_lines_to_account_changes(self):
671 #         account_changes = {}
672 #         debt = {}
673 #         sink_account = None
674 #         for line in self.lines[1:]:
675 #             if line == '':
676 #                 continue
677 #             account, amount, currency = line
678 #             if amount is None:
679 #                 sink_account = account
680 #                 continue
681 #             apply_booking_to_account_balances(account_changes, account, currency, amount)
682 #             if currency not in debt:
683 #                 debt[currency] = amount
684 #             else:
685 #                 debt[currency] += amount
686 #         if sink_account:
687 #             for currency, amount in debt.items():
688 #                 apply_booking_to_account_balances(account_changes, sink_account, currency, -amount)
689 #                 self.sink[currency] = -amount
690 #         return account_changes
691
692
693
694 class LedgerDB(PlomDB):
695
696     def __init__(self, prefix, ignore_editable_exceptions=False):
697         self.prefix = prefix 
698         self.bookings = []
699         self.comments = []
700         self.real_lines = []
701         super().__init__(db_path)
702         self.bookings = parse_lines2(self.real_lines, ignore_editable_exceptions)
703
704     def read_db_file(self, f):
705         self.real_lines += [l.rstrip() for l in f.readlines()]  # TODO is this necessary? (parser already removes lines?)
706
707     # def get_lines(self, start, end):
708     #     return self.real_lines[start:end]
709
710     # def write_db(self, text, mode='w'):
711     #     if text[-1] != '\n':
712     #         text += '\n'
713     #     self.write_text_to_db(text)
714
715     # def insert_at_date(self, lines, date):
716     #     start_at = 0 
717     #     if len(self.bookings) > 0:
718     #         if date >= self.bookings[-1].date_string:
719     #             start_at = len(self.real_lines)
720     #             lines = [''] + lines
721     #         else:
722     #             for b in self.bookings:
723     #                 if b.date_string == date:
724     #                     start_at = b.start_line 
725     #                 elif b.date_string > date:
726     #                     start_at = b.start_line 
727     #                     break
728     #             lines += ['']  # DEBUG is new
729     #     return self.write_lines_in_total_lines_at(self.real_lines, start_at, lines)
730
731     # def update(self, start, end, lines, date):
732     #     remaining_lines = self.real_lines[:start] + self.real_lines[end:]
733     #     n_original_lines = end - start
734     #     start_at = len(remaining_lines)
735     #     for b in self.bookings:
736     #         if b.date_string == date:
737     #             if start_at == len(remaining_lines) or b.start_line == start:
738     #                 start_at = b.start_line 
739     #                 if b.start_line > start:
740     #                     start_at -= n_original_lines
741     #         elif b.date_string > date:
742     #             break
743     #     # print("DEBUG update start_at", start_at, "len(remaining_lines)", len(remaining_lines), "len(self.real_lines)", len(self.real_lines), "end", end)
744     #     if start_at != 0 and end != len(self.real_lines) and start_at == len(remaining_lines):
745     #         # Add empty predecessor line if appending.
746     #         lines = [''] + lines
747     #     return self.write_lines_in_total_lines_at(remaining_lines, start_at, lines)
748
749     # def write_lines_in_total_lines_at(self, total_lines, start_at, lines):
750     #     # total_lines = total_lines[:start_at] + lines + [''] + total_lines[start_at:]
751     #     total_lines = total_lines[:start_at] + lines + total_lines[start_at:]
752     #     _, _ = parse_lines(lines)
753     #     text = '\n'.join(total_lines)
754     #     self.write_db(text)
755     #     return start_at
756
757     # def get_nth_for_booking_of_start_line(self, start_line):
758     #     nth = 0
759     #     for b in self.bookings:
760     #         if b.start_line >= start_line:
761     #             break
762     #         nth += 1
763     #     return nth
764
765     def insert_booking_at_date(self, booking):
766         place_at = 0
767         if len(self.bookings) > 0:
768             for i, iterated_booking in enumerate(self.bookings):
769                 if booking.date < iterated_booking.date:
770                     break
771                 elif booking.date == iterated_booking.date:
772                     place_at = i
773                     break
774                 else:
775                     place_at = i + 1
776         self.bookings = self.bookings[:place_at] + [booking] + self.bookings[place_at:]
777
778     def add_taxes(self, lines, finish=False):
779         ret = []
780         bookings, _ = parse_lines(lines)
781         date = bookings[0].date_string
782         acc_kk_add = 'Reserves:KrankenkassenBeitragsWachstum'
783         acc_kk_minimum = 'Reserves:Month:KrankenkassenDefaultBeitrag'
784         acc_kk = 'Expenses:KrankenKasse'
785         acc_est = 'Reserves:Einkommenssteuer'
786         acc_assets = 'Assets'
787         acc_buffer = 'Reserves:NeuAnfangsPuffer:Ausgaben'
788         last_monthbreak_assets = 0
789         last_monthbreak_est = 0
790         last_monthbreak_kk_minimum = 0
791         last_monthbreak_kk_add = 0
792         buffer_expenses = 0
793         kk_expenses = 0
794         est_expenses = 0
795         months_passed = -int(finish) 
796         for b in self.bookings:
797             if date == b.date_string:
798                 break
799             acc_keys = b.account_changes.keys()
800             if acc_buffer in acc_keys:
801                 buffer_expenses -= b.account_changes[acc_buffer]['€']
802             if acc_kk_add in acc_keys:
803                 kk_expenses += b.account_changes[acc_kk_add]['€']
804             if acc_kk in acc_keys:
805                 kk_expenses += b.account_changes[acc_kk]['€']
806             if acc_est in acc_keys:
807                 est_expenses += b.account_changes[acc_est]['€']
808             if acc_kk_add in acc_keys and acc_kk_minimum in acc_keys:
809                 months_passed += 1
810                 if finish:
811                     last_monthbreak_kk_add = b.account_changes[acc_kk_add]['€']
812                     last_monthbreak_est = b.account_changes[acc_est]['€']
813                     last_monthbreak_kk_minimum = b.account_changes[acc_kk_minimum]['€']
814                     last_monthbreak_assets = b.account_changes[acc_buffer]['€']
815         old_needed_income_before_anything = - last_monthbreak_assets - last_monthbreak_kk_add - last_monthbreak_kk_minimum - last_monthbreak_est
816         if finish:
817             ret += [f'  {acc_est}  {-last_monthbreak_est}€ ; for old assumption of needed income: {old_needed_income_before_anything}€']
818         _, account_sums = bookings_to_account_tree(bookings)
819         expenses_so_far = -1 * account_sums[acc_assets]['€'] + old_needed_income_before_anything
820         needed_income_before_kk = expenses_so_far
821         ESt_this_month = 0
822         left_over = needed_income_before_kk - ESt_this_month
823         too_low = 0
824         too_high = 2 * needed_income_before_kk
825         E0 = decimal.Decimal(10908)
826         E1 = decimal.Decimal(15999)
827         E2 = decimal.Decimal(62809)
828         E3 = decimal.Decimal(277825)
829         while True:
830             zvE = buffer_expenses - kk_expenses + (12 - months_passed) * needed_income_before_kk
831             if finish:
832                 zvE += last_monthbreak_assets + last_monthbreak_kk_add + last_monthbreak_kk_minimum
833             if zvE < E0:
834                 ESt = decimal.Decimal(0)
835             elif zvE < E1:
836                 y = (zvE - E0)/10000
837                 ESt = (decimal.Decimal(979.18) * y + 1400) * y
838             elif zvE < E2:
839                 y = (zvE - E1)/10000
840                 ESt = (decimal.Decimal(192.59) * y + 2397) * y + decimal.Decimal(966.53)
841             elif zvE < E3:
842                 ESt = decimal.Decimal(0.42) * (zvE - decimal.Decimal(62809))  + decimal.Decimal(16405.54)
843             else:
844                 ESt = decimal.Decimal(0.45) * (zvE - decimal.Decimal(277825)) + decimal.Decimal(106713.52)
845             ESt_this_month = (ESt + last_monthbreak_est - est_expenses) / (12 - months_passed)
846             left_over = needed_income_before_kk - ESt_this_month
847             if abs(left_over - expenses_so_far) < 0.001:
848                 break
849             elif left_over < expenses_so_far:
850                 too_low = needed_income_before_kk
851             elif left_over > expenses_so_far:
852                 too_high = needed_income_before_kk
853             needed_income_before_kk = too_low + (too_high - too_low)/2
854         ESt_this_month = ESt_this_month.quantize(decimal.Decimal('0.00'))
855         ret += [f'  {acc_est}  {ESt_this_month}€ ; expenses so far: {expenses_so_far:.2f}€; zvE: {zvE:.2f}€; ESt total: {ESt:.2f}€; needed before Krankenkasse: {needed_income_before_kk:.2f}€']
856         kk_minimum_income = 1131.67
857         if date < '2023-02-01':
858             kk_minimum_income = decimal.Decimal(1096.67)
859             kk_factor = decimal.Decimal(0.189)
860             kk_minimum_tax = decimal.Decimal(207.27).quantize(decimal.Decimal('0.00'))
861         elif date < '2023-08-01':
862             kk_factor = decimal.Decimal(0.191)
863             kk_minimum_tax = decimal.Decimal(216.15).quantize(decimal.Decimal('0.00'))
864         else:
865             kk_factor = decimal.Decimal(0.197)
866             kk_minimum_tax = decimal.Decimal(222.94).quantize(decimal.Decimal('0.00'))
867         kk_add_so_far = account_sums[acc_kk_add]['€'] if acc_kk_add in account_sums.keys() else 0
868         kk_add = needed_income_before_kk / (1 - kk_factor) - needed_income_before_kk - kk_minimum_tax
869         hit_kk_minimum_income_limit = False
870         if kk_add_so_far + kk_add < 0:
871             hit_kk_minimum_income_limit = True 
872             kk_add_uncorrect = kk_add
873             kk_add = -(kk_add + kk_add_so_far) 
874         kk_add = decimal.Decimal(kk_add).quantize(decimal.Decimal('0.00'))
875         if finish:
876             ret += [f'  {acc_kk_add}  {-last_monthbreak_kk_add}€  ; for old assumption of needed income']
877         else:
878             ret += [f'  {acc_kk_minimum}  {kk_minimum_tax}€  ; assumed minimum income {kk_minimum_income:.2f}€ * {kk_factor:.3f}']
879         if hit_kk_minimum_income_limit:
880             ret += [f'  {acc_kk_add}  {kk_add}€  ; {needed_income_before_kk:.2f}€ / (1 - {kk_factor:.3f}) - {needed_income_before_kk:.2f}€ - {kk_minimum_tax}€ = {kk_add_uncorrect:.2f}€ would reduce current {acc_kk_dd} ({kk_add_so_far:.2f}€) below 0']
881         else:
882             ret += [f'  {acc_kk_add}  {kk_add}€  ; {needed_income_before_kk:.2f}€ / (1 - {kk_factor:.3f}) - {needed_income_before_kk:.2f}€ - {kk_minimum_tax}€']
883         diff = - last_monthbreak_est + ESt_this_month - last_monthbreak_kk_add + kk_add
884         if not finish:
885             diff += kk_minimum_tax
886         final_minus = expenses_so_far - old_needed_income_before_anything + diff
887         ret += [f'  {acc_assets}  {-diff} €']
888         ret += [f'  {acc_assets}  {final_minus} €']
889         year_needed = buffer_expenses + final_minus + (12 - months_passed - 1) * final_minus 
890         if finish:
891             ret += [f'  {acc_buffer}  {-final_minus} €']
892         else:
893             ret += [f'  {acc_buffer}  {-final_minus} € ; assume as to earn in year: {acc_buffer} + {12 - months_passed - 1} * this = {year_needed}']
894         return ret
895
896     # def add_mirror(self, lines):
897     #     ret = []
898     #     bookings, _ = parse_lines(lines)
899     #     booking = bookings[0]
900     #     for line in booking.lines[1:]:
901     #         ret += [f'?  {-line[1]} {line[2]}']
902     #     return ret
903
904     def ledger_as_html(self):
905         for nth, booking in enumerate(self.bookings):
906             booking.can_up = nth > 0 and self.bookings[nth - 1].date == booking.date
907             booking.can_down = nth < len(self.bookings) - 1 and self.bookings[nth + 1].date == booking.date
908         return j2env.get_template('ledger.html').render(bookings=self.bookings)
909
910     # def ledger_as_html(self):
911     #     booking_tmpl = jinja2.Template(booking_html)
912     #     single_c_tmpl = jinja2.Template('<span class="comment">{{c|e}}</span><br />')  ##
913     #     elements_to_write = []
914     #     last_i = i = 0  ##
915     #     for nth, booking in enumerate(self.bookings):
916     #         move_up = nth > 0 and self.bookings[nth - 1].date_string == booking.date_string
917     #         move_down = nth < len(self.bookings) - 1 and self.bookings[nth + 1].date_string == booking.date_string
918     #         booking_end = last_i = booking.start_line + len(booking.lines)
919     #         booking_lines = []
920     #         i = booking.start_line  ##
921     #         elements_to_write += [single_c_tmpl.render(c=c) for c in self.comments[last_i:i] if c != '']  ##
922     #         for booking_line in booking.lines[1:]:
923     #              i += 1  ##
924     #              comment = self.comments[i]  ##
925     #              if booking_line == '':
926     #                  booking_lines += [{'acc': None, 'money': None, 'comment': comment}]  ##
927     #                  continue
928     #              account = booking_line[0]
929     #              money = ''
930     #              if booking_line[1] is not None:
931     #                  money = f'{booking_line[1]} {booking_line[2]}'
932     #              booking_lines += [{'acc': booking_line[0], 'money':money, 'comment':comment}]  ##
933     #         elements_to_write += [booking_tmpl.render(
934     #             prefix=self.prefix,
935     #             nth=nth,
936     #             start=booking.start_line,
937     #             end=booking_end,
938     #             date=booking.date_string,
939     #             desc=booking.description,
940     #             head_comment=self.comments[booking.start_line],
941     #             move_up=move_up,
942     #             move_down=move_down,
943     #             booking_lines = booking_lines)]
944     #     elements_to_write += [single_c_tmpl.render(c=c) for c in self.comments[last_i:] if c != '']  #
945     #     return '\n'.join(elements_to_write)
946
947     def balance_as_html(self, until=None):
948         bookings = self.bookings[:(until if until is None else int(until)+1)]
949         account_trunk = Account('', None)
950         accounts = {account_trunk.full_name: account_trunk}
951         for booking in bookings:
952             for full_account_name, moneys in booking.account_changes.items():
953                 toks = full_account_name.split(':')
954                 path = [] 
955                 for tok in toks:
956                     parent_name = ':'.join(path) 
957                     path += [tok] 
958                     account_name = ':'.join(path)
959                     if not account_name in accounts.keys():
960                         accounts[account_name] = Account(own_name=tok, parent=accounts[parent_name])
961                 accounts[full_account_name].add_wealth(moneys)
962         class Node:
963             def __init__(self, indent, name, moneys):
964                 self.indent = indent
965                 self.name = name
966                 self.moneys = moneys.money_dict
967         nodes = []
968         def walk_tree(nodes, indent, account):
969             nodes += [Node(indent, account.own_name, account.full_moneys)]
970             for child in account.children:
971                 walk_tree(nodes, indent+1, child)
972         for acc in account_trunk.children:
973             walk_tree(nodes, 0, acc)
974         return j2env.get_template('balance.html').render(nodes=nodes)
975
976     # def balance_as_html(self, until=None):
977     #     bookings = self.bookings[:until if until is None else int(until)]
978     #     lines = []
979     #     account_tree, account_sums = bookings_to_account_tree(bookings)
980     #     def print_subtree(lines, indent, node, subtree, path):
981     #         line = f"{indent}{node}"
982     #         n_tabs = 5 - (len(line) // 8)
983     #         line += n_tabs * "\t"
984     #         if "€" in account_sums[path + node].keys():
985     #             amount = account_sums[path + node]["€"]
986     #             line += f"{amount:9.2f} €\t"
987     #         else:
988     #             line += f"\t\t"
989     #         for currency, amount in account_sums[path + node].items():
990     #             if currency != '€' and amount != 0:
991     #                 line += f"{amount:5.2f} {currency}\t"
992     #         lines += [line]
993     #         indent += "  "
994     #         for k, v in sorted(subtree.items()):
995     #             print_subtree(lines, indent, k, v, path + node + ":")
996     #     for k, v in sorted(account_tree.items()):
997     #         print_subtree(lines, "", k, v, "")
998     #     content = "\n".join(lines)
999     #     return f"<pre>{content}</pre>"
1000
1001     def edit(self, index, sent=None, error_msg=None, edit_mode='table'):
1002         accounts = set() 
1003         if sent or -1 == index:
1004             content = sent if sent else ([] if 'textarea'==edit_mode else None)
1005         else:
1006             content = self.bookings[index]
1007         if 'textarea' == edit_mode and content:
1008             content = content.for_writing()
1009         else:
1010             for booking in self.bookings:
1011                 for transfer_line in booking.transfer_lines:
1012                     accounts.add(transfer_line.account)
1013         return j2env.get_template('edit.html').render(content=content, index=index, error_msg=error_msg, edit_mode=edit_mode, accounts=accounts)
1014
1015     # def add_free(self, start=0, end=0, copy=False):
1016     #     tmpl = jinja2.Template(add_form_header + add_free_html + add_form_footer) 
1017     #     lines = self.get_lines(start, end)
1018     #     if copy:
1019     #         start = end = 0
1020     #     return tmpl.render(action=self.prefix + '/add_free', start=start, end=end, lines=lines)
1021
1022     # def add_structured(self, start=0, end=0, copy=False, temp_lines=[], add_empty_line=None):
1023     #     tmpl = jinja2.Template(add_form_header + add_structured_html + add_form_footer) 
1024     #     lines = temp_lines if len(''.join(temp_lines)) > 0 else self.get_lines(start, end)
1025     #     bookings, comments = parse_lines(lines, validate_bookings=False)
1026     #     if len(bookings) > 1:
1027     #         raise PlomException('can only structurally edit single Booking')
1028     #     if add_empty_line is not None:
1029     #         comments = comments[:add_empty_line+1] + [''] + comments[add_empty_line+1:]
1030     #         booking = bookings[0]
1031     #         booking.lines = booking.lines[:add_empty_line+1] + [''] + booking.lines[add_empty_line+1:]
1032     #     action = self.prefix + '/add_structured'
1033     #     datalist_sets = {'descriptions': set(), 'accounts': set(), 'currencies': set()}
1034     #     for b in self.bookings:
1035     #         datalist_sets['descriptions'].add(b.description)
1036     #         for account, moneys in b.account_changes.items():
1037     #             datalist_sets['accounts'].add(account)
1038     #             for currency in moneys.keys():
1039     #                 datalist_sets['currencies'].add(currency)
1040     #     content = ''
1041     #     today = str(datetime.now())[:10]
1042     #     booking_lines = []
1043     #     if copy:
1044     #         start = end = 0
1045     #     desc = head_comment = ''
1046     #     if len(bookings) == 0:
1047     #         date=today
1048     #     else:
1049     #         booking = bookings[0]
1050     #         desc = booking.description
1051     #         date = today if copy else booking.date_string
1052     #         head_comment=comments[0]
1053     #         for i in range(1, len(comments)):
1054     #             account = amount = currency = ''
1055     #             if i < len(booking.lines) and booking.lines[i] != '':
1056     #                 account = booking.lines[i][0]
1057     #                 amount = booking.lines[i][1]
1058     #                 currency = booking.lines[i][2]
1059     #             booking_lines += [{
1060     #                     'i': i,
1061     #                     'acc': account,
1062     #                     'amt': amount,
1063     #                     'curr': currency if currency else '€',
1064     #                     'comment': comments[i],
1065     #                     'comm_cols': len(comments[i])}]
1066     #     for i in range(len(comments), len(comments) + 8):
1067     #         booking_lines += [{'i': i, 'acc': '', 'amt': '', 'curr': '€', 'comment': ''}]
1068     #     content += tmpl.render(
1069     #             action=action,
1070     #             date=date,
1071     #             desc=desc,
1072     #             head_comment=head_comment,
1073     #             booking_lines=booking_lines,
1074     #             datalist_sets=datalist_sets,
1075     #             start=start,
1076     #             end=end)
1077     #     return content
1078
1079     def move_up(self, nth):
1080         return self.move(nth, -1) 
1081
1082     def move_down(self, nth):
1083         return self.move(nth, +1) 
1084
1085     def move(self, nth, direction):
1086         to_move = self.bookings[nth]
1087         swap_nth = nth+1*(direction)
1088         to_swap = self.bookings[swap_nth]
1089         self.bookings[nth] = to_swap 
1090         self.bookings[nth+1*(direction)] = to_move 
1091         return swap_nth
1092
1093     def write_db(self):
1094         lines = []
1095         for i, booking in enumerate(self.bookings):
1096             if i > 0:
1097                 lines += ['']
1098             lines += booking.for_writing()
1099         self.write_text_to_db('\n'.join(lines) + '\n')
1100
1101     # def move_up(self, start, end):
1102     #     prev_booking = None
1103     #     for redir_nth, b in enumerate(self.bookings):
1104     #         if b.start_line >= start:
1105     #             break
1106     #         prev_booking = b
1107     #     start_at = prev_booking.start_line 
1108     #     self.make_move(start, end, start_at)
1109     #     return redir_nth - 1
1110
1111     # def move_down(self, start, end):
1112     #     next_booking = None
1113     #     for redir_nth, b in enumerate(self.bookings):
1114     #         if b.start_line > start:
1115     #             next_booking = b
1116     #             break
1117     #     # start_at = next_booking.start_line + len(next_booking.lines) - (end - start) + 1 
1118     #     # self.make_move(start, end, start_at-1)
1119     #     start_at = next_booking.start_line + len(next_booking.lines) - (end - start)
1120     #     self.make_move(start, end, start_at)
1121     #     return redir_nth
1122
1123     # def make_move(self, start, end, start_at):
1124     #     # FIXME currently broken due to changed self.write_lines_in_total_lines_at, easy fix would be lines += [""] maybe?
1125     #     lines = self.get_lines(start, end)
1126     #     if start == 0:
1127     #         total_lines = self.real_lines[end+1:]
1128     #         lines = [''] + lines
1129     #         start_at += 1
1130     #     else: 
1131     #         total_lines = self.real_lines[:start-1] + self.real_lines[end:]  # -1 because we reduce the original position's two empty limit lines to one in-between line
1132     #         lines += ['']
1133     #     self.write_lines_in_total_lines_at(total_lines, start_at, lines)
1134
1135     # def booking_lines_from_postvars(self, postvars):
1136     #     add_empty_line = None
1137     #     date = postvars['date'][0]
1138     #     description = postvars['description'][0]
1139     #     start_comment = postvars['line_0_comment'][0]
1140     #     start_line = f'{date} {description}'
1141     #     if start_comment.rstrip() != '':
1142     #         start_line += f' ; {start_comment}' 
1143     #     lines = [start_line]
1144     #     if 'line_0_add' in postvars.keys():
1145     #         add_empty_line = 0
1146     #     i = j = 1
1147     #     while f'line_{i}_comment' in postvars.keys():
1148     #         if f'line_{i}_delete' in postvars.keys():
1149     #             i += 1
1150     #             continue
1151     #         elif f'line_{i}_delete_after' in postvars.keys():
1152     #             break 
1153     #         elif f'line_{i}_add' in postvars.keys():
1154     #             add_empty_line = j
1155     #         account = postvars[f'line_{i}_account'][0]
1156     #         amount = postvars[f'line_{i}_amount'][0]
1157     #         currency = postvars[f'line_{i}_currency'][0]
1158     #         comment = postvars[f'line_{i}_comment'][0]
1159     #         i += 1
1160     #         new_main = f'  {account}  {amount}'
1161     #         if '' == new_main.rstrip() == comment.rstrip():  # don't write empty lines, ignore currency if nothing else set
1162     #             continue
1163     #         if len(amount.rstrip()) > 0:
1164     #             new_main += f' {currency}'
1165     #         j += 1
1166     #         new_line = new_main
1167     #         if comment.rstrip() != '':
1168     #             new_line += f'  ; {comment}'
1169     #         lines += [new_line]
1170     #     if 'add_sink' in postvars.keys():
1171     #         temp_lines = lines.copy() + ['_']
1172     #         try:
1173     #             temp_bookings, _ = parse_lines(temp_lines)
1174     #             for currency in temp_bookings[0].sink:
1175     #                 amount = temp_bookings[0].sink[currency]
1176     #                 # lines += [f'Assets  {amount:.2f} {currency}']
1177     #                 lines += [f'Assets  {amount} {currency}']
1178     #         except PlomException:
1179     #             pass
1180     #     elif 'add_taxes' in postvars.keys():
1181     #         lines += self.add_taxes(lines, finish=False)
1182     #     elif 'add_taxes2' in postvars.keys():
1183     #         lines += self.add_taxes(lines, finish=True)
1184     #     elif 'replace' in postvars.keys():
1185     #         for i, line in enumerate(lines):
1186     #             lines[i] = line.replace(postvars['replace_from'][0], postvars['replace_to'][0])
1187     #     elif 'add_mirror' in postvars.keys():
1188     #         lines += self.add_mirror(lines)
1189     #     return lines, add_empty_line
1190
1191
1192
1193 class LedgerHandler(PlomHandler):
1194     
1195     def app_init(self, handler):
1196         default_path = '/ledger'
1197         handler.add_route('GET', default_path, self.forward_gets) 
1198         handler.add_route('POST', default_path, self.forward_posts)
1199         return 'ledger', default_path 
1200
1201     def do_POST(self):
1202         self.try_do(self.forward_posts)
1203
1204     def forward_posts(self):
1205         prefix = self.apps['ledger'] if hasattr(self, 'apps') else '' 
1206         length = int(self.headers['content-length'])
1207         postvars = parse_qs(self.rfile.read(length).decode(), keep_blank_values=1)
1208         db = LedgerDB(prefix, ignore_editable_exceptions=True)
1209         index = 0
1210         parsed_url = urlparse(self.path)
1211         for string in {'save', 'copy', 'check', 'mirror', 'fill sink', 'as textarea', 'as table', 'move up', 'move down', 'add taxes'}:
1212             if string in postvars.keys():
1213                 submit_button = string
1214                 break
1215         if prefix + '/ledger' == parsed_url.path:
1216             if submit_button == 'move up':
1217                 index = db.move_up(int(postvars['move up'][0]))
1218             elif submit_button == 'move down':
1219                 index = db.move_down(int(postvars['move down'][0]))
1220         elif prefix + '/edit' == parsed_url.path:
1221             index = int(postvars['index'][0])
1222             starts_at = '?' if index == -1 else db.bookings[index].starts_at
1223             edit_mode = postvars['edit_mode'][0]
1224             validate = submit_button in {'save', 'copy', 'check'}
1225             if 'textarea' == edit_mode:
1226                 lines = postvars['booking'][0].rstrip().split('\n')
1227                 booking = Booking(lines, starts_at, validate=validate)
1228             else:
1229                 booking = Booking.from_postvars(postvars, starts_at, validate)
1230             if submit_button in {'save', 'copy'}:
1231                 if index != -1 and submit_button != 'copy':
1232                      if booking.date == db.bookings[index].date:
1233                         db.bookings[index] = booking 
1234                         booking_is_placed = True
1235                      else:
1236                         db.bookings = db.bookings[:index] + db.bookings[index+1:]
1237                         db.insert_booking_at_date(booking)
1238                 else: 
1239                     db.insert_booking_at_date(booking)
1240             else:
1241                 error_msg = None
1242                 if 'check' == submit_button:
1243                     error_msg = 'All looks fine!'
1244                 elif 'mirror' == submit_button:
1245                     booking.add_mirror()
1246                 elif 'fill sink' == submit_button:
1247                     booking.fill_sink()
1248                 elif 'add taxes' == submit_button:
1249                     booking.add_taxes()
1250                 elif submit_button in {'as textarea', 'as table'}:
1251                     edit_mode = submit_button[len('as '):]
1252                 page = db.edit(index, booking, error_msg=error_msg, edit_mode=edit_mode)
1253                 self.send_HTML(page)
1254                 return
1255         db.write_db() 
1256         index = index if index >= 0 else len(db.bookings) - 1
1257         self.redirect(prefix + f'/ledger#{index}')
1258
1259     # def forward_posts(self):
1260     #     prefix = self.apps['ledger'] if hasattr(self, 'apps') else '' 
1261     #     parsed_url = urlparse(self.path)
1262     #     length = int(self.headers['content-length'])
1263     #     postvars = parse_qs(self.rfile.read(length).decode(), keep_blank_values=1)
1264     #     start = int(postvars['start'][0])
1265     #     end = int(postvars['end'][0])
1266     #     print("DEBUG start, end", start, end)
1267     #     db = LedgerDB(prefix)
1268     #     add_empty_line = None
1269     #     lines = []
1270     #     # get inputs
1271     #     if prefix + '/add_structured' == parsed_url.path and not 'revert' in postvars.keys():
1272     #         lines, add_empty_line = db.booking_lines_from_postvars(postvars) 
1273     #     elif prefix + '/add_free' == parsed_url.path and not 'revert' in postvars.keys():
1274     #         lines = postvars['booking'][0].splitlines()
1275     #     # validate where appropriate
1276     #     if ('save' in postvars.keys()) or ('check' in postvars.keys()):
1277     #         _, _ = parse_lines(lines)
1278     #     # if saving, process where to and where to redirect after
1279     #     if 'save' in postvars.keys():
1280     #         last_date = str(datetime.now())[:10]
1281     #         if len(db.bookings) > 0:
1282     #             last_date = db.bookings[-1].date_string
1283     #         target_date = last_date[:] 
1284     #         first_line_tokens = lines[0].split() if len(lines) > 0 else ''
1285     #         first_token = first_line_tokens[0] if len(first_line_tokens) > 0 else ''
1286     #         try:
1287     #             datetime.strptime(first_token, '%Y-%m-%d')
1288     #             target_date = first_token
1289     #         except ValueError:
1290     #              pass
1291     #         if start == end == 0:
1292     #             start = db.insert_at_date(lines, target_date)
1293     #             nth = db.get_nth_for_booking_of_start_line(start) 
1294     #         else:
1295     #             new_start = db.update(start, end, lines, target_date)
1296     #             print("DEBUG save", new_start, start, end, lines)
1297     #             nth = db.get_nth_for_booking_of_start_line(new_start)
1298     #             if new_start > start: 
1299     #                 nth -= 1 
1300     #         self.redirect(prefix + f'/#{nth}')
1301     #     # otherwise just re-build editing form
1302     #     else:
1303     #         if prefix + '/add_structured' == parsed_url.path: 
1304     #             edit_content = db.add_structured(start, end, temp_lines=lines, add_empty_line=add_empty_line)
1305     #         else:
1306     #             edit_content = db.add_free(start, end)
1307     #         header = jinja2.Template(html_head).render(prefix=prefix)
1308     #         self.send_HTML(header + edit_content)
1309
1310     def do_GET(self):
1311         self.try_do(self.forward_gets)
1312
1313     def forward_gets(self):
1314         prefix = self.apps['ledger'] if hasattr(self, 'apps') else '' 
1315         try:
1316             db = LedgerDB(prefix=prefix)
1317         except EditableException as e:
1318             db = LedgerDB(prefix=prefix, ignore_editable_exceptions=True)
1319             page = db.edit(index=e.booking_index, error_msg=f'ERROR: {e}')
1320             self.send_HTML(page)
1321             return
1322         parsed_url = urlparse(self.path)
1323         params = parse_qs(parsed_url.query)
1324         if parsed_url.path == prefix + '/balance':
1325             stop = params.get('stop', [None])[0]
1326             page = db.balance_as_html(stop)
1327         elif parsed_url.path == prefix + '/edit':
1328             index = params.get('i', [-1])[0]
1329             page = db.edit(int(index))
1330         else:
1331             page = db.ledger_as_html()
1332         self.send_HTML(page)
1333
1334
1335
1336 if __name__ == "__main__":  
1337     run_server(server_port, LedgerHandler)