- suffix = f' {lines[i]}' if len(lines[i]) > 0 else ''
- value = f' {booking_line[1]} {booking_line[2]}' if booking_line[1] else ''
- lines[i] = f'{booking_line[0]}{value}{suffix}'
- lines[i] = lines[i][:-len(line_sep)] + f"""</p>
-edit:
-<a href="/add_structured?start={booking.start_line}&end={i+1}">structured</a>
-/ <a href="/add_free?start={booking.start_line}&end={i+1}">free</a>
-<br />"""
- return '\n'.join(lines)
-
- def header_add_form(self, action):
- return f"<form method=\"POST\" action=\"/{action}\">\n"
-
- def footer_add_form(self, start, end):
- return f"""
-<input type="hidden" name="start" value={start} />
-<input type="hidden" name="end" value={end} />
-<input type="submit">
-</form>"""
-
- def add_free(self, db, start=0, end=0):
- content = html.escape(''.join(db.get_lines(start, end)))
- return f'{self.header_add_form("add_free")}<textarea name="booking" rows="8" cols="80">{content}</textarea>{self.footer_add_form(start, end)}'
-
- def add_structured(self, db, start=0, end=0, bonus_lines=10):
- lines = db.get_lines(start, end)
- bookings, comments = parse_lines(lines)
+ account = booking_line[0] ##
+ account_toks = account.split(':') ##
+ path = '' ##
+ for tok in account_toks: ##
+ path += tok ##
+ if not path in account_sums.keys(): ##
+ account_sums[path] = {} ##
+ path += ':' ##
+ moneys = [] ##
+ money = ''
+ if booking_line[1] is not None:
+ moneys += [(booking_line[1], booking_line[2])] ##
+ money = f'{moneys[0][0]} {moneys[0][1]}'
+ else: ##
+ for currency, amount in booking.sink.items(): ##
+ moneys += {(amount, currency)} ##
+ money = '[' ##
+ for m in moneys: ##
+ money += f'{m[0]} {m[1]} ' ##
+ money += ']' ##
+ balance = '' ##
+ for amount, currency in moneys: ##
+ path = '' ##
+ for tok in account_toks: ##
+ path += tok ##
+ if not currency in account_sums[path].keys(): ##
+ account_sums[path][currency] = 0 ##
+ account_sums[path][currency] += amount ##
+ path += ':' ##
+ balance += f'{account_sums[account][currency]} {currency}' ##
+ booking_lines += [{'acc': booking_line[0], 'money':money, 'comment':balance}] ##
+ elements_to_write += [self.booking_tmpl.render(
+ start=booking.start_line,
+ end=booking_end,
+ date=booking.date_string,
+ desc=booking.description,
+ head_comment=db.comments[booking.start_line],
+ steps_after_date=nth_of_same_date,
+ booking_lines = booking_lines)]
+ return '\n'.join(elements_to_write)
+
+ def ledger2_as_html(self, db):
+ single_c_tmpl = jinja2.Template('<span class="comment">{{c|e}}</span><br />')
+ elements_to_write = []
+ last_i = i = 0 ##
+ nth_of_same_date = 0
+ same_date = ''
+ for booking in db.bookings:
+ if booking.date_string == same_date:
+ nth_of_same_date += 1
+ else:
+ same_date = booking.date_string
+ nth_of_same_date = 0
+ i = booking.start_line ##
+ elements_to_write += [single_c_tmpl.render(c=c) for c in db.comments[last_i:i] if c != ''] ##
+ booking_end = last_i = booking.start_line + len(booking.lines)
+ booking_lines = []
+ 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(
+ start=booking.start_line,
+ end=booking_end,
+ date=booking.date_string,
+ desc=booking.description,
+ head_comment=db.comments[booking.start_line],
+ steps_after_date=nth_of_same_date,
+ 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("""
+<form method="POST" action="{{action|e}}">
+<textarea name="booking" rows=10 cols=80>
+{% for line in lines %}{{ line }}
+{% endfor %}
+</textarea>
+<input type="hidden" name="start" value={{start}} />
+<input type="hidden" name="end" value={{end}} />
+<input type="submit" name="save" value="save!">
+</form>
+""")
+ 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("""
+<form method="POST" action="{{action|e}}">
+<input type="submit" name="check" value="check" />
+<input type="submit" name="revert" value="revert" />
+<input type="submit" name="add_taxes" value="add taxes" />
+<input type="submit" name="add_taxes2" value="add taxes2" />
+<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 %}
+<input type="hidden" name="start" value={{start}} />
+<input type="hidden" name="end" value={{end}} />
+<input type="submit" name="save" value="save!">
+</form>
+""")
+ import datetime
+ lines = temp_lines if len(''.join(temp_lines)) > 0 else db.get_lines(start, end)
+ bookings, comments = parse_lines(lines, validate_bookings=False)