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
8 db_path = '/home/plom/org/ledger2024.dat'
10 j2env = JinjaEnv(loader=JinjaFSLoader('ledger_templates'))
12 class EditableException(PlomException):
13 def __init__(self, booking_index, *args, **kwargs):
14 self.booking_index = booking_index
15 super().__init__(*args, **kwargs)
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; }
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>
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>
49 # {% for l in booking_lines %}
51 # <tr><td>{{l.acc|e}}</td><td class="money">{{l.money|e}}</td><td class="comment">{{l.comment|e}}</td></tr>
53 # <tr><td><div class="comment full_line_comment">{{l.comment|e}}</div></td></tr>
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" />
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" >
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>
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" />
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" />
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>
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>
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
117 def add_wealth(self, moneys):
118 self.add_money_dict(moneys.money_dict)
121 def as_sink_dict(self):
123 for currency, amount in self.money_dict.items():
126 sink[currency] = -amount
132 def __init__(self, own_name, parent):
133 self.own_name = own_name
136 self.parent.children += [self]
137 self.own_moneys = Wealth()
140 def add_wealth(self, moneys):
141 self.own_moneys.add_wealth(moneys)
145 if self.parent and len(self.parent.own_name) > 0:
146 return f'{self.parent.full_name}:{self.own_name}'
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)
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
165 # account_sums[account][currency] += amount
168 # def bookings_to_account_tree(bookings):
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)
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)
181 # toks = account_name.split(":", maxsplit=1)
183 # if parent in node.keys():
184 # child = node[parent]
187 # node[parent] = child
189 # k, v = collect_branches(toks[1], path + [parent])
190 # if k not in child.keys():
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
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
212 def parse_lines2(lines, ignore_editable_exceptions=False):
213 lines = lines.copy() + [''] # to simulate ending of last booking
215 inside_booking = False
219 for i, line in enumerate(lines):
220 intro = f'file line {i}'
221 stripped_line = line.rstrip()
222 if stripped_line == '':
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
232 if not inside_booking:
234 inside_booking = True
235 booking_lines += [line]
237 raise PlomException(f'{intro}: last booking unfinished')
241 # def parse_lines(lines, validate_bookings=True):
242 # inside_booking = False
243 # date_string, description = None, None
248 # lines = lines.copy() + [''] # to ensure a booking-ending last line
250 # for i, line in enumerate(lines):
251 # prefix = f"line {i}"
252 # # we start with the case of an utterly empty line
254 # stripped_line = line.rstrip()
255 # if stripped_line == '':
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
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() == '':
275 # booking_lines += ['']
277 # # if we're starting a booking, parse by first-line pattern
278 # if not inside_booking:
280 # toks = non_comment.split(maxsplit=1)
281 # date_string = toks[0]
283 # datetime.strptime(date_string, '%Y-%m-%d')
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
290 # description = toks[1]
292 # raise PlomException(f"{prefix} bad description: {description}")
293 # inside_booking = True
294 # booking_lines += [non_comment]
296 # # otherwise, read as transfer data
297 # toks = non_comment.split() # ignore specification's allowance of single spaces in names
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"
309 # amount = decimal.Decimal(toks[1])
311 # except decimal.InvalidOperation:
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:
321 # inside_amount = False
322 # inside_currency = False
326 # for i, c in enumerate(value):
328 # if c in decimal_chars:
329 # inside_amount = True
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
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
347 # if c == '-' and len(amount_string) > 1:
348 # raise PlomException(f"{prefix} amount has non-start '-': {value}")
350 # if dots_counted > 1:
351 # raise PlomException(f"{prefix} amount has multiple dots: {value}")
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)]
360 # raise PlomException(f"{prefix} last booking unfinished")
361 # return bookings, comments
366 def __init__(self, line=None, account='', amount=None, currency='', comment='', validate=True):
367 self.account = account
369 self.currency = currency
370 self.comment = comment
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}):
381 raise PlomException(f'number of non-comment tokens not 1 or 3')
386 self.account = toks[0]
389 self.amount = decimal.Decimal(toks[1])
390 except decimal.InvalidOperation:
392 raise PlomException(f'invalid token for Decimal: {toks[1]}')
394 self.amount = toks[1]
395 self.currency = toks[2]
398 def amount_fmt(self):
399 if self.amount is None:
401 elif self.amount.as_tuple().exponent < -2:
402 return f'{self.amount:.1f}…'
404 return f'{self.amount:.2f}'
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}'
415 def comment_cols(self):
416 return max(20, len(self.comment))
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}'
427 self.real_lines = lines
431 self.description = description
432 self.top_comment = top_comment
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()
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]
446 for i, account in enumerate(postvars['account']):
447 if len(account) == 0:
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)
456 self.transfer_lines = []
457 self.account_changes = {}
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()
472 raise PlomException(f'{self.intro}: headline missing elements: {non_comment}')
478 self.description = toks[1]
480 for i, line in enumerate(self.real_lines[1:]):
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()
487 def calculate_account_changes(self):
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
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)
509 def validate_head(self):
510 if not self.validate:
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')
517 datetime.strptime(self.date, '%Y-%m-%d')
519 raise PlomException(f'{self.intro}: bad headline date format: {self.date}')
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()]
528 lines = self.real_lines[:i+1] + replacement_lines + self.real_lines[i+2:]
530 self.real_lines = lines
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()]
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['€']
560 left_over = needed_netto - ESt_this_month
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)
568 zvE = buffer_expenses - kk_expenses + (12 - months_passed) * needed_netto
570 # zvE += last_monthbreak_assets + last_monthbreak_kk_add + last_monthbreak_kk_minimum
572 ESt = decimal.Decimal(0)
575 ESt = (decimal.Decimal(979.18) * y + 1400) * y
578 ESt = (decimal.Decimal(192.59) * y + 2397) * y + decimal.Decimal(966.53)
580 ESt = decimal.Decimal(0.42) * (zvE - decimal.Decimal(62809)) + decimal.Decimal(16405.54)
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:
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
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, '€')]
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()]
626 def comment_cols(self):
627 return max(20, len(self.top_comment))
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
638 # self.validate_booking_lines()
640 # self.account_changes = self.parse_booking_lines_to_account_changes()
642 # def validate_booking_lines(self):
643 # prefix = f"booking at line {self.start_line}"
646 # for line in self.lines[1:]:
649 # _, amount, currency = line
651 # if empty_values > 0:
652 # raise PlomException(f"{prefix} relates more than one empty value of same currency {currency}")
655 # if currency not in sums:
657 # sums[currency] += amount
658 # if empty_values == 0:
659 # for k, v in sums.items():
661 # raise PlomException(f"{prefix} does not add up to zero / {k} {v}")
664 # for k, v in sums.items():
668 # raise PlomException(f"{prefix} has empty value that cannot be filled")
670 # def parse_booking_lines_to_account_changes(self):
671 # account_changes = {}
673 # sink_account = None
674 # for line in self.lines[1:]:
677 # account, amount, currency = line
679 # sink_account = account
681 # apply_booking_to_account_balances(account_changes, account, currency, amount)
682 # if currency not in debt:
683 # debt[currency] = amount
685 # debt[currency] += amount
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
694 class LedgerDB(PlomDB):
696 def __init__(self, prefix, ignore_editable_exceptions=False):
701 super().__init__(db_path)
702 self.bookings = parse_lines2(self.real_lines, ignore_editable_exceptions)
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?)
707 # def get_lines(self, start, end):
708 # return self.real_lines[start:end]
710 # def write_db(self, text, mode='w'):
711 # if text[-1] != '\n':
713 # self.write_text_to_db(text)
715 # def insert_at_date(self, lines, date):
717 # if len(self.bookings) > 0:
718 # if date >= self.bookings[-1].date_string:
719 # start_at = len(self.real_lines)
720 # lines = [''] + lines
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
728 # lines += [''] # DEBUG is new
729 # return self.write_lines_in_total_lines_at(self.real_lines, start_at, lines)
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:
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)
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)
757 # def get_nth_for_booking_of_start_line(self, start_line):
759 # for b in self.bookings:
760 # if b.start_line >= start_line:
765 def insert_booking_at_date(self, booking):
767 if len(self.bookings) > 0:
768 for i, iterated_booking in enumerate(self.bookings):
769 if booking.date < iterated_booking.date:
771 elif booking.date == iterated_booking.date:
776 self.bookings = self.bookings[:place_at] + [booking] + self.bookings[place_at:]
778 def add_taxes(self, lines, finish=False):
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
795 months_passed = -int(finish)
796 for b in self.bookings:
797 if date == b.date_string:
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:
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
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
822 left_over = needed_income_before_kk - ESt_this_month
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)
830 zvE = buffer_expenses - kk_expenses + (12 - months_passed) * needed_income_before_kk
832 zvE += last_monthbreak_assets + last_monthbreak_kk_add + last_monthbreak_kk_minimum
834 ESt = decimal.Decimal(0)
837 ESt = (decimal.Decimal(979.18) * y + 1400) * y
840 ESt = (decimal.Decimal(192.59) * y + 2397) * y + decimal.Decimal(966.53)
842 ESt = decimal.Decimal(0.42) * (zvE - decimal.Decimal(62809)) + decimal.Decimal(16405.54)
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:
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'))
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'))
876 ret += [f' {acc_kk_add} {-last_monthbreak_kk_add}€ ; for old assumption of needed income']
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']
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
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
891 ret += [f' {acc_buffer} {-final_minus} €']
893 ret += [f' {acc_buffer} {-final_minus} € ; assume as to earn in year: {acc_buffer} + {12 - months_passed - 1} * this = {year_needed}']
896 # def add_mirror(self, lines):
898 # bookings, _ = parse_lines(lines)
899 # booking = bookings[0]
900 # for line in booking.lines[1:]:
901 # ret += [f'? {-line[1]} {line[2]}']
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)
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 = []
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)
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:]:
924 # comment = self.comments[i] ##
925 # if booking_line == '':
926 # booking_lines += [{'acc': None, 'money': None, 'comment': comment}] ##
928 # account = booking_line[0]
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,
936 # start=booking.start_line,
938 # date=booking.date_string,
939 # desc=booking.description,
940 # head_comment=self.comments[booking.start_line],
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)
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(':')
956 parent_name = ':'.join(path)
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)
963 def __init__(self, indent, name, moneys):
966 self.moneys = moneys.money_dict
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)
976 # def balance_as_html(self, until=None):
977 # bookings = self.bookings[:until if until is None else int(until)]
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"
989 # for currency, amount in account_sums[path + node].items():
990 # if currency != '€' and amount != 0:
991 # line += f"{amount:5.2f} {currency}\t"
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>"
1001 def edit(self, index, sent=None, error_msg=None, edit_mode='table'):
1003 if sent or -1 == index:
1004 content = sent if sent else ([] if 'textarea'==edit_mode else None)
1006 content = self.bookings[index]
1007 if 'textarea' == edit_mode and content:
1008 content = content.for_writing()
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)
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)
1020 # return tmpl.render(action=self.prefix + '/add_free', start=start, end=end, lines=lines)
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)
1041 # today = str(datetime.now())[:10]
1042 # booking_lines = []
1045 # desc = head_comment = ''
1046 # if len(bookings) == 0:
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 += [{
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(
1072 # head_comment=head_comment,
1073 # booking_lines=booking_lines,
1074 # datalist_sets=datalist_sets,
1079 def move_up(self, nth):
1080 return self.move(nth, -1)
1082 def move_down(self, nth):
1083 return self.move(nth, +1)
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
1095 for i, booking in enumerate(self.bookings):
1098 lines += booking.for_writing()
1099 self.write_text_to_db('\n'.join(lines) + '\n')
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:
1107 # start_at = prev_booking.start_line
1108 # self.make_move(start, end, start_at)
1109 # return redir_nth - 1
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:
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)
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)
1127 # total_lines = self.real_lines[end+1:]
1128 # lines = [''] + lines
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
1133 # self.write_lines_in_total_lines_at(total_lines, start_at, lines)
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
1147 # while f'line_{i}_comment' in postvars.keys():
1148 # if f'line_{i}_delete' in postvars.keys():
1151 # elif f'line_{i}_delete_after' in postvars.keys():
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]
1160 # new_main = f' {account} {amount}'
1161 # if '' == new_main.rstrip() == comment.rstrip(): # don't write empty lines, ignore currency if nothing else set
1163 # if len(amount.rstrip()) > 0:
1164 # new_main += f' {currency}'
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() + ['_']
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:
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
1193 class LedgerHandler(PlomHandler):
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
1202 self.try_do(self.forward_posts)
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)
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
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)
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
1236 db.bookings = db.bookings[:index] + db.bookings[index+1:]
1237 db.insert_booking_at_date(booking)
1239 db.insert_booking_at_date(booking)
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:
1248 elif 'add taxes' == submit_button:
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)
1256 index = index if index >= 0 else len(db.bookings) - 1
1257 self.redirect(prefix + f'/ledger#{index}')
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
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 ''
1287 # datetime.strptime(first_token, '%Y-%m-%d')
1288 # target_date = first_token
1289 # except ValueError:
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)
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:
1300 # self.redirect(prefix + f'/#{nth}')
1301 # # otherwise just re-build editing form
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)
1306 # edit_content = db.add_free(start, end)
1307 # header = jinja2.Template(html_head).render(prefix=prefix)
1308 # self.send_HTML(header + edit_content)
1311 self.try_do(self.forward_gets)
1313 def forward_gets(self):
1314 prefix = self.apps['ledger'] if hasattr(self, 'apps') else ''
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)
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))
1331 page = db.ledger_as_html()
1332 self.send_HTML(page)
1336 if __name__ == "__main__":
1337 run_server(server_port, LedgerHandler)