+ return f"<pre>{content}</pre>"
+
+ def add_free(self, start=0, end=0, copy=False):
+ tmpl = jinja2.Template(add_form_header + add_free_html + add_form_footer)
+ lines = self.get_lines(start, end)
+ if copy:
+ start = end = 0
+ return tmpl.render(action=self.prefix + '/add_free', start=start, end=end, lines=lines)
+
+ def add_structured(self, start=0, end=0, copy=False, temp_lines=[], add_empty_line=None):
+ tmpl = jinja2.Template(add_form_header + add_structured_html + add_form_footer)
+ lines = temp_lines if len(''.join(temp_lines)) > 0 else self.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 = self.prefix + '/add_structured'
+ datalist_sets = {'descriptions': set(), 'accounts': set(), 'currencies': set()}
+ for b in self.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:
+ date=today
+ else:
+ booking = bookings[0]
+ desc = booking.description
+ date = today if copy else booking.date_string
+ head_comment=comments[0]
+ for i in range(1, len(comments)):
+ account = amount = currency = ''
+ if i < len(booking.lines) and booking.lines[i] != '':
+ account = booking.lines[i][0]
+ amount = booking.lines[i][1]
+ currency = booking.lines[i][2]
+ booking_lines += [{
+ 'i': i,
+ 'acc': account,
+ 'amt': amount,
+ 'curr': currency if currency else '€',
+ 'comment': comments[i],
+ 'comm_cols': len(comments[i])}]
+ for i in range(len(comments), len(comments) + 8):
+ booking_lines += [{'i': i, 'acc': '', 'amt': '', 'curr': '€', 'comment': ''}]
+ content += tmpl.render(
+ action=action,
+ date=date,
+ desc=desc,
+ head_comment=head_comment,
+ booking_lines=booking_lines,
+ datalist_sets=datalist_sets,
+ start=start,
+ end=end)
+ return content
+
+ def move_up(self, start, end):
+ prev_booking = None
+ for redir_nth, b in enumerate(self.bookings):
+ if b.start_line >= start:
+ break
+ prev_booking = b
+ start_at = prev_booking.start_line
+ self.make_move(start, end, start_at)
+ return redir_nth - 1
+
+ def move_down(self, start, end):
+ next_booking = None
+ for redir_nth, b in enumerate(self.bookings):
+ if b.start_line > start:
+ next_booking = b
+ break
+ start_at = next_booking.start_line + len(next_booking.lines) - (end - start) + 1
+ self.make_move(start, end, start_at-1)
+ return redir_nth
+
+ def make_move(self, start, end, start_at):
+ lines = self.get_lines(start, end)
+ total_lines = self.real_lines[:start-1] + self.real_lines[end:] # +1 because we reduce the original position's two empty border lines to in-between line
+ self.write_lines_in_total_lines_at(total_lines, start_at, lines)
+
+ def booking_lines_from_postvars(self, postvars):
+ 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 += self.add_taxes(lines, finish=False)
+ elif 'add_taxes2' in postvars.keys():
+ lines += self.add_taxes(lines, finish=True)
+ return lines, add_empty_line
+