- self.send_response(200)
- self.send_header("Content-type", "text/html")
- self.end_headers()
- db = Database()
- parsed_url = urlparse(self.path)
- page = self.header + ''
- params = parse_qs(parsed_url.query)
- start = int(params.get('start', ['0'])[0])
- end = int(params.get('end', ['0'])[0])
- if parsed_url.path == '/balance':
- stop_date = params.get('stop', [None])[0]
- plus = int(params.get('plus', [0])[0])
- page += self.balance_as_html(db, stop_date, plus)
- elif parsed_url.path == '/add_free':
- page += self.add_free(db, start, end)
- elif parsed_url.path == '/add_structured':
- page += self.add_structured(db, start, end)
- elif parsed_url.path == '/copy_free':
- page += self.add_free(db, start, end, copy=True)
- elif parsed_url.path == '/copy_structured':
- page += self.add_structured(db, start, end, copy=True)
- elif parsed_url.path == '/ledger2':
- page += self.ledger2_as_html(db)
- else:
- page += self.ledger_as_html(db)
- page += self.footer
- self.wfile.write(bytes(page, "utf-8"))
-
- def balance_as_html(self, db, stop_date=None, plus=0):
+ try:
+ parsed_url = urlparse(self.path)
+ params = parse_qs(parsed_url.query)
+ start = int(params.get('start', ['0'])[0])
+ end = int(params.get('end', ['0'])[0])
+ db = LedgerDB()
+ if parsed_url.path == '/balance':
+ stop = params.get('stop', [None])[0]
+ page = self.balance_as_html(db, stop)
+ elif parsed_url.path == '/add_free':
+ page = self.add_free(db, start, end)
+ elif parsed_url.path == '/add_structured':
+ page = self.add_structured(db, start, end)
+ elif parsed_url.path == '/copy_free':
+ page = self.add_free(db, start, end, copy=True)
+ elif parsed_url.path == '/copy_structured':
+ page = self.add_structured(db, start, end, copy=True)
+ elif parsed_url.path == '/move_up':
+ nth = self.move_up(db, start, end)
+ self.redirect(f'/#{nth}')
+ return
+ elif parsed_url.path == '/move_down':
+ nth = self.move_down(db, start, end)
+ self.redirect(f'/#{nth}')
+ return
+ else:
+ page = self.ledger_as_html(db)
+ self.send_HTML(page)
+ except PlomException as e:
+ self.fail_400(e)
+
+ def booking_lines_from_postvars(self, postvars, db):
+ add_empty_line = None
+ date = postvars['date'][0]
+ description = postvars['description'][0]
+ start_comment = postvars['line_0_comment'][0]
+ start_line = f'{date} {description}'
+ if start_comment.rstrip() != '':
+ start_line += f' ; {start_comment}'
+ lines = [start_line]
+ if 'line_0_add' in postvars.keys():
+ add_empty_line = 0
+ i = j = 1
+ while f'line_{i}_comment' in postvars.keys():
+ if f'line_{i}_delete' in postvars.keys():
+ i += 1
+ continue
+ elif f'line_{i}_delete_after' in postvars.keys():
+ break
+ elif f'line_{i}_add' in postvars.keys():
+ add_empty_line = j
+ account = postvars[f'line_{i}_account'][0]
+ amount = postvars[f'line_{i}_amount'][0]
+ currency = postvars[f'line_{i}_currency'][0]
+ comment = postvars[f'line_{i}_comment'][0]
+ i += 1
+ new_main = f' {account} {amount}'
+ if '' == new_main.rstrip() == comment.rstrip(): # don't write empty lines, ignore currency if nothing else set
+ continue
+ if len(amount.rstrip()) > 0:
+ new_main += f' {currency}'
+ j += 1
+ new_line = new_main
+ if comment.rstrip() != '':
+ new_line += f' ; {comment}'
+ lines += [new_line]
+ if 'add_sink' in postvars.keys():
+ temp_lines = lines.copy() + ['_']
+ try:
+ temp_bookings, _ = parse_lines(temp_lines)
+ for currency in temp_bookings[0].sink:
+ amount = temp_bookings[0].sink[currency]
+ lines += [f'Assets {amount:.2f} {currency}']
+ except PlomException:
+ pass
+ if 'add_taxes' in postvars.keys():
+ lines += db.add_taxes(lines, finish=False)
+ elif 'add_taxes2' in postvars.keys():
+ lines += db.add_taxes(lines, finish=True)
+ return lines, add_empty_line
+
+ def balance_as_html(self, db, until=None):
+ bookings = db.bookings[:until if until is None else int(until)]