X-Git-Url: https://plomlompom.com/repos/process_titles?a=blobdiff_plain;f=ledger.py;h=9ff0d3eaabc723170071ffbb0e3a4726a6a747d0;hb=92c86eae1c6c758defda091b628a259d84f39b18;hp=6653af6e8e7f341cb80901f0aed83d9466c8c968;hpb=71a0c94ce508dcd21165e06a955043d021efb89f;p=misc
diff --git a/ledger.py b/ledger.py
index 6653af6..9ff0d3e 100755
--- a/ledger.py
+++ b/ledger.py
@@ -65,6 +65,10 @@ add_structured_html = """
+
+
+
+
@@ -350,21 +354,28 @@ class LedgerDB(PlomDB):
return self.real_lines[start:end]
def write_db(self, text, mode='w'):
+ if text[-1] != '\n':
+ text += '\n'
self.write_text_to_db(text)
def insert_at_date(self, lines, date):
- start_at = len(self.real_lines)
- for b in self.bookings:
- if b.date_string >= date:
- start_at = b.start_line
- break
- elif b.date_string > date:
- break
- if start_at == len(self.real_lines):
- lines = [''] + lines
+ print("DEBUG insert_at_date")
+ start_at = 0
+ if len(self.bookings) > 0:
+ if date >= self.bookings[-1].date_string:
+ start_at = len(self.real_lines)
+ lines = [''] + lines
+ else:
+ for b in self.bookings:
+ if b.date_string == date:
+ start_at = b.start_line
+ elif b.date_string > date:
+ start_at = b.start_line
+ break
return self.write_lines_in_total_lines_at(self.real_lines, start_at, lines)
def update(self, start, end, lines, date):
+ print("DEBUG update")
total_lines = self.real_lines[:start] + self.real_lines[end:]
n_original_lines = end - start
start_at = len(total_lines)
@@ -513,6 +524,14 @@ class LedgerDB(PlomDB):
ret += [f' {acc_buffer} {-final_minus} ⬠; assume as to earn in year: {acc_buffer} + {12 - months_passed - 1} * this = {year_needed}']
return ret
+ def add_mirror(self, lines):
+ ret = []
+ bookings, _ = parse_lines(lines)
+ booking = bookings[0]
+ for line in booking.lines[1:]:
+ ret += [f'? {-line[1]} {line[2]}']
+ return ret
+
def ledger_as_html(self):
booking_tmpl = jinja2.Template(booking_html)
single_c_tmpl = jinja2.Template('
') ##
@@ -607,15 +626,12 @@ class LedgerDB(PlomDB):
start = end = 0
desc = head_comment = ''
if len(bookings) == 0:
- for i in range(1, 8):
- booking_lines += [{'i': i, 'acc': '', 'amt': '', 'curr': 'â¬', 'comment': ''}]
date=today
else:
booking = bookings[0]
desc = booking.description
date = today if copy else booking.date_string
head_comment=comments[0]
- last_line = len(comments)
for i in range(1, len(comments)):
account = amount = currency = ''
if i < len(booking.lines) and booking.lines[i] != '':
@@ -629,6 +645,8 @@ class LedgerDB(PlomDB):
'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,
@@ -657,12 +675,12 @@ class LedgerDB(PlomDB):
next_booking = b
break
start_at = next_booking.start_line + len(next_booking.lines) - (end - start) + 1
- self.make_move(start, end, start_at)
+ 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] + self.real_lines[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):
@@ -709,10 +727,15 @@ class LedgerDB(PlomDB):
lines += [f'Assets {amount:.2f} {currency}']
except PlomException:
pass
- if 'add_taxes' in postvars.keys():
+ elif '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)
+ elif 'replace' in postvars.keys():
+ for i, line in enumerate(lines):
+ lines[i] = line.replace(postvars['replace_from'][0], postvars['replace_to'][0])
+ elif 'add_mirror' in postvars.keys():
+ lines += self.add_mirror(lines)
return lines, add_empty_line
@@ -733,7 +756,6 @@ class LedgerHandler(PlomHandler):
parsed_url = urlparse(self.path)
length = int(self.headers['content-length'])
postvars = parse_qs(self.rfile.read(length).decode(), keep_blank_values=1)
- print("DEBUG", postvars['start'], postvars['end'])
start = int(postvars['start'][0])
end = int(postvars['end'][0])
db = LedgerDB(prefix)