home · contact · privacy
Fix todo.py task deselection bugs.
[misc] / ledger.py
index cd38b820da6354866cd23d64fb196a3ec9f40952..0ef3d441a8714c36e910b92f1c8e0fcfcd2ea7ce 100755 (executable)
--- a/ledger.py
+++ b/ledger.py
@@ -6,7 +6,7 @@ from urllib.parse import parse_qs, urlparse
 from plomlib import PlomDB, PlomException, run_server, PlomHandler 
 
 server_port = 8082
-db_path = '/home/plom/org/ledger2023.dat'
+db_path = '/home/plom/org/ledger2024.dat'
 
 html_head = """
 <style>
@@ -46,42 +46,41 @@ booking_html = """
 </table></p>
 """
 add_form_header = """<form method="POST" action="{{action|e}}">
-<input type="submit" name="check" value="check" />
-<input type="submit" name="revert" value="revert" />
+<input type="submit" name="check" value="check" tabindex="5"  />
+<input type="submit" name="revert" value="revert" tabindex="5"  />
 """
 add_form_footer = """
 <input type="hidden" name="start" value={{start}} />
 <input type="hidden" name="end" value={{end}} />
-<input type="submit" name="save" value="save!">
+<input type="submit" name="save" value="save!" tabindex="5" >
 </form>
 """
 add_free_html = """<br />
-<textarea name="booking" rows=10 cols=80>
-{% for line in lines %}{{ line }}
-{% endfor %}
-</textarea>
+<textarea name="booking" rows=10 cols=80>{% for line in lines %}{{ line }}{% if not loop.last %}
+{% endif %}{% endfor %}</textarea>
 """
 add_structured_html = """
-<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" />
-<input name="replace_from" />
-<input type="submit" name="replace" value="-> replace ->" />
-<input name="replace_to" />
+<input type="submit" name="add_taxes" value="add taxes" tabindex="5" />
+<input type="submit" name="add_taxes2" value="add taxes2" tabindex="5"  />
+<input type="submit" name="add_sink" value="add sink" tabindex="5"  />
+<input name="replace_from" tabindex="5"  />
+<input type="submit" name="replace" value="-> replace ->" tabindex="5"  />
+<input name="replace_to" tabindex="5"  />
+<input type="submit" name="add_mirror" value="add mirror" tabindex="5"  />
 <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="[+]" />
+<input name="date" value="{{date|e}}" size=9 tabindex="3"  />
+<input name="description" value="{{desc|e}}" list="descriptions" tabindex="3"  />
+<textarea name="line_0_comment" rows=1 cols=20 tabindex="3" >{{head_comment|e}}</textarea>
+<input type="submit" name="line_0_add" value="[+]" tabindex="5"  />
 <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>
+<input name="line_{{line.i}}_account" value="{{line.acc|e}}" size=40 list="accounts" tabindex="3" />
+<input type="number" name="line_{{line.i}}_amount" step=0.01 value="{{line.amt}}" size=10 tabindex="3" />
+<input name="line_{{line.i}}_currency" value="{{line.curr|e}}" size=3 list="currencies" tabindex="5" />
+<input type="submit" name="line_{{line.i}}_delete" value="[x]" tabindex="5" />
+<input type="submit" name="line_{{line.i}}_delete_after" value="[XX]" tabindex="5" />
+<input type="submit" name="line_{{line.i}}_add" value="[+]" tabindex="5" />
+<textarea name="line_{{line.i}}_comment" rows=1 cols={% if line.comm_cols %}{{line.comm_cols}}{% else %}20{% endif %} tabindex="3">{{line.comment|e}}</textarea>
 <br />
 {% endfor %}
 {% for name, items in datalist_sets.items() %}
@@ -358,7 +357,6 @@ class LedgerDB(PlomDB):
         self.write_text_to_db(text)
 
     def insert_at_date(self, lines, date):
-        print("DEBUG insert_at_date")
         start_at = 0 
         if len(self.bookings) > 0:
             if date >= self.bookings[-1].date_string:
@@ -371,27 +369,30 @@ class LedgerDB(PlomDB):
                     elif b.date_string > date:
                         start_at = b.start_line 
                         break
+                lines += ['']  # DEBUG is new
         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:]
+        remaining_lines = self.real_lines[:start] + self.real_lines[end:]
         n_original_lines = end - start
-        start_at = len(total_lines)
+        start_at = len(remaining_lines)
         for b in self.bookings:
             if b.date_string == date:
-                if start_at == len(total_lines) or b.start_line == start:
+                if start_at == len(remaining_lines) or b.start_line == start:
                     start_at = b.start_line 
                     if b.start_line > start:
                         start_at -= n_original_lines
             elif b.date_string > date:
                 break
-        if start_at == len(total_lines):
+        # print("DEBUG update start_at", start_at, "len(remaining_lines)", len(remaining_lines), "len(self.real_lines)", len(self.real_lines), "end", end)
+        if start_at != 0 and end != len(self.real_lines) and start_at == len(remaining_lines):
+            # Add empty predecessor line if appending.
             lines = [''] + lines
-        return self.write_lines_in_total_lines_at(total_lines, start_at, lines)
+        return self.write_lines_in_total_lines_at(remaining_lines, start_at, lines)
 
     def write_lines_in_total_lines_at(self, total_lines, start_at, lines):
-        total_lines = total_lines[:start_at] + lines + [''] + total_lines[start_at:]
+        # total_lines = total_lines[:start_at] + lines + [''] + total_lines[start_at:]
+        total_lines = total_lines[:start_at] + lines + total_lines[start_at:]
         _, _ = parse_lines(lines)
         text = '\n'.join(total_lines)
         self.write_db(text)
@@ -523,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('<span class="comment">{{c|e}}</span><br />')  ##
@@ -574,7 +583,7 @@ class LedgerDB(PlomDB):
             else:
                 line += f"\t\t"
             for currency, amount in account_sums[path + node].items():
-                if currency != '€' and amount > 0:
+                if currency != '€' and amount != 0:
                     line += f"{amount:5.2f} {currency}\t"
             lines += [line]
             indent += "  "
@@ -665,13 +674,23 @@ class LedgerDB(PlomDB):
             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)
+        # start_at = next_booking.start_line + len(next_booking.lines) - (end - start) + 1 
+        # self.make_move(start, end, start_at-1)
+        start_at = next_booking.start_line + len(next_booking.lines) - (end - start)
+        print("DEBUG", start, end, start_at)
+        self.make_move(start, end, start_at)
         return redir_nth
 
     def make_move(self, start, end, start_at):
+        # FIXME currently broken due to changed self.write_lines_in_total_lines_at, easy fix would be lines += [""] maybe?
         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
+        if start == 0:
+            total_lines = self.real_lines[end+1:]
+            lines = [''] + lines
+            start_at += 1
+        else: 
+            total_lines = self.real_lines[:start-1] + self.real_lines[end:]  # -1 because we reduce the original position's two empty limit lines to one in-between line
+            lines += ['']
         self.write_lines_in_total_lines_at(total_lines, start_at, lines)
 
     def booking_lines_from_postvars(self, postvars):
@@ -715,7 +734,8 @@ class LedgerDB(PlomDB):
                 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}']
+                    # lines += [f'Assets  {amount:.2f} {currency}']
+                    lines += [f'Assets  {amount} {currency}']
             except PlomException:
                 pass
         elif 'add_taxes' in postvars.keys():
@@ -725,6 +745,8 @@ class LedgerDB(PlomDB):
         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
 
 
@@ -747,6 +769,7 @@ class LedgerHandler(PlomHandler):
         postvars = parse_qs(self.rfile.read(length).decode(), keep_blank_values=1)
         start = int(postvars['start'][0])
         end = int(postvars['end'][0])
+        print("DEBUG start, end", start, end)
         db = LedgerDB(prefix)
         add_empty_line = None
         lines = []
@@ -776,6 +799,7 @@ class LedgerHandler(PlomHandler):
                 nth = db.get_nth_for_booking_of_start_line(start) 
             else:
                 new_start = db.update(start, end, lines, target_date)
+                print("DEBUG save", new_start, start, end, lines)
                 nth = db.get_nth_for_booking_of_start_line(new_start)
                 if new_start > start: 
                     nth -= 1