- 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)]
- lines = []
- account_tree, account_sums = bookings_to_account_tree(bookings)
- def print_subtree(lines, indent, node, subtree, path):
- line = f"{indent}{node}"
- n_tabs = 5 - (len(line) // 8)
- line += n_tabs * "\t"
- if "€" in account_sums[path + node].keys():
- amount = account_sums[path + node]["€"]
- line += f"{amount:9.2f} €\t"
- else:
- line += f"\t\t"
- for currency, amount in account_sums[path + node].items():
- if currency != '€' and amount > 0:
- line += f"{amount:5.2f} {currency}\t"
- lines += [line]
- indent += " "
- for k, v in sorted(subtree.items()):
- print_subtree(lines, indent, k, v, path + node + ":")
- for k, v in sorted(account_tree.items()):
- print_subtree(lines, "", k, v, "")
- content = "\n".join(lines)
- return f"<pre>{content}</pre>"
-
- def ledger_as_html(self, db):
- single_c_tmpl = jinja2.Template('<span class="comment">{{c|e}}</span><br />') ##
- elements_to_write = []
- last_i = i = 0 ##
- for nth, booking in enumerate(db.bookings):
- move_up = nth > 0 and db.bookings[nth - 1].date_string == booking.date_string
- move_down = nth < len(db.bookings) - 1 and db.bookings[nth + 1].date_string == booking.date_string
- booking_end = last_i = booking.start_line + len(booking.lines)
- booking_lines = []
- i = booking.start_line ##
- elements_to_write += [single_c_tmpl.render(c=c) for c in db.comments[last_i:i] if c != ''] ##
- for booking_line in booking.lines[1:]:
- i += 1 ##
- comment = db.comments[i] ##
- if booking_line == '':
- booking_lines += [{'acc': None, 'money': None, 'comment': comment}] ##
- continue
- account = booking_line[0]
- money = ''
- if booking_line[1] is not None:
- money = f'{booking_line[1]} {booking_line[2]}'
- booking_lines += [{'acc': booking_line[0], 'money':money, 'comment':comment}] ##
- elements_to_write += [self.booking_tmpl.render(
- nth=nth,
- start=booking.start_line,
- end=booking_end,
- date=booking.date_string,
- desc=booking.description,
- head_comment=db.comments[booking.start_line],
- move_up=move_up,
- move_down=move_down,
- booking_lines = booking_lines)]
- elements_to_write += [single_c_tmpl.render(c=c) for c in db.comments[last_i:] if c != ''] #
- return '\n'.join(elements_to_write)
-
- def add_free(self, db, start=0, end=0, copy=False):
- tmpl = jinja2.Template(self.add_form_header + """<br />
-<textarea name="booking" rows=10 cols=80>
-{% for line in lines %}{{ line }}
-{% endfor %}
-</textarea>
-""" + self.add_form_footer)
- lines = db.get_lines(start, end)
- if copy:
- start = end = 0
- return tmpl.render(action='add_free', start=start, end=end, lines=lines)
-
- def add_structured(self, db, start=0, end=0, copy=False, temp_lines=[], add_empty_line=None):
- tmpl = jinja2.Template(self.add_form_header + """
-<input type="submit" name="add_taxes" value="add taxes" />
-<input type="submit" name="add_taxes2" value="add taxes2" />
-<input type="submit" name="add_sink" value="add sink" />
-<br />
-<input name="date" value="{{date|e}}" size=9 />
-<input name="description" value="{{desc|e}}" list="descriptions" />
-<textarea name="line_0_comment" rows=1 cols=20>{{head_comment|e}}</textarea>
-<input type="submit" name="line_0_add" value="[+]" />
-<br />
-{% for line in booking_lines %}
-<input name="line_{{line.i}}_account" value="{{line.acc|e}}" size=40 list="accounts" />
-<input type="number" name="line_{{line.i}}_amount" step=0.01 value="{{line.amt}}" size=10 />
-<input name="line_{{line.i}}_currency" value="{{line.curr|e}}" size=3 list="currencies" />
-<input type="submit" name="line_{{line.i}}_delete" value="[x]" />
-<input type="submit" name="line_{{line.i}}_delete_after" value="[XX]" />
-<input type="submit" name="line_{{line.i}}_add" value="[+]" />
-<textarea name="line_{{line.i}}_comment" rows=1 cols={% if line.comm_cols %}{{line.comm_cols}}{% else %}20{% endif %}>{{line.comment|e}}</textarea>
-<br />
-{% endfor %}
-{% for name, items in datalist_sets.items() %}
-<datalist id="{{name}}">
-{% for item in items %}
- <option value="{{item|e}}">{{item|e}}</option>
-{% endfor %}
-</datalist>
-{% endfor %}
-""" + self.add_form_footer)
- lines = temp_lines if len(''.join(temp_lines)) > 0 else db.get_lines(start, end)
- bookings, comments = parse_lines(lines, validate_bookings=False)
- if len(bookings) > 1:
- raise PlomException('can only structurally edit single Booking')
- if add_empty_line is not None:
- comments = comments[:add_empty_line+1] + [''] + comments[add_empty_line+1:]
- booking = bookings[0]
- booking.lines = booking.lines[:add_empty_line+1] + [''] + booking.lines[add_empty_line+1:]
- action = 'add_structured'
- datalist_sets = {'descriptions': set(), 'accounts': set(), 'currencies': set()}
- for b in db.bookings:
- datalist_sets['descriptions'].add(b.description)
- for account, moneys in b.account_changes.items():
- datalist_sets['accounts'].add(account)
- for currency in moneys.keys():
- datalist_sets['currencies'].add(currency)
- content = ''
- today = str(datetime.now())[:10]
- booking_lines = []
- if copy:
- start = end = 0
- desc = head_comment = ''
- if len(bookings) == 0:
- for i in range(1, 3):
- booking_lines += [{'i': i, 'acc': '', 'amt': '', 'curr': '€', 'comment': ''}]
- date=today