- # def ledger_as_html(self, db):
- # lines = []
- # line_sep = '<br />'
- # for comment in db.comments:
- # line = f' ; {comment}' if comment != '' else ''
- # lines += [line + line_sep]
- # for booking in db.bookings:
- # i = booking.start_line
- # suffix = lines[i]
- # lines[i] = f'<p>{booking.date_string} {booking.description}{suffix}'
- # for booking_line in booking.lines[1:]:
- # i += 1
- # if booking_line == '':
- # continue
- # 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 h ref="/add_structured?start={booking.start_line}&end={i+1}">structured</a>
-# / <a href="/add_free?start={booking.start_line}&end={i+1}">free</a>
-# | co py:
-# <a h ref="/copy_structured?start={booking.start_line}&end={i+1}">structured</a>
-# / <a href="/copy_free?start={booking.start_line}&end={i+1}">free</a>
-# <br />"""
-# return '\n'.join(lines)
+ def ledger2_as_html(self, db):
+ single_c_tmpl = jinja2.Template('<span class="comment">{{c|e}}</span><br />')
+ booking_tmpl = jinja2.Template("""
+<p id="{{start}}"><a href="#{{start}}">{{date}}</a> {{desc}} <span class="comment">{{head_comment|e}}</span>
+[edit: <a href="/add_structured?start={{start}}&end={{end}}">structured</a>
+/ <a href="/add_free?start={{start}}&end={{end}}">free</a>
+| copy:<a href="/copy_structured?start={{start}}&end={{end}}">structured</a>
+/ <a href="/copy_free?start={{start}}&end={{end}}">free</a>]
+<table>
+{% for l in booking_lines %}
+<tr><td>{{l.acc|e}}</td><td class="money">{{l.money|e}}</td><td class="money">{{l.balance|e}}</td></tr>
+{% endfor %}
+</table></p>
+""")
+ elements_to_write = []
+ account_sums = {}
+ for booking in db.bookings:
+ i = booking.start_line
+ booking_end = booking.start_line + len(booking.lines)
+ booking_lines = []
+ for booking_line in booking.lines[1:]:
+ if booking_line == '':
+ continue
+ 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, 'balance':balance}]
+ elements_to_write += [booking_tmpl.render(
+ start=booking.start_line,
+ end=booking_end,
+ date=booking.date_string,
+ desc=booking.description,
+ head_comment=db.comments[booking.start_line],
+ booking_lines = booking_lines)]
+ return '\n'.join(elements_to_write)