From 4aa69f1601319ceeef640a4c56f9d2b1eb25cd6b Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Mon, 4 Dec 2023 16:47:49 +0100
Subject: [PATCH] Improve accounting scripts.

---
 ledger.py  | 35 ++++++++++++++++++++---------------
 plomlib.py |  1 +
 todo.json  |  1 -
 3 files changed, 21 insertions(+), 16 deletions(-)
 delete mode 100644 todo.json

diff --git a/ledger.py b/ledger.py
index 6653af6..817def6 100755
--- a/ledger.py
+++ b/ledger.py
@@ -350,21 +350,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)
@@ -607,15 +614,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 +633,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 +663,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):
@@ -733,7 +739,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)
diff --git a/plomlib.py b/plomlib.py
index d1b4c4c..d95a35c 100644
--- a/plomlib.py
+++ b/plomlib.py
@@ -37,6 +37,7 @@ class PlomDB:
         mtimes_to_paths = {}
         for path in [path for path in os.listdir(os.path.dirname(bak_prefix))
                      if path.startswith(os.path.basename(bak_prefix))]:
+            path = os.path.dirname(bak_prefix) + f'/{path}'
             mod_time = os.path.getmtime(path)
             print(f'DEBUG pre-exists: {path} {mod_time}')
             mtimes_to_paths[str(datetime.fromtimestamp(mod_time))] = path
diff --git a/todo.json b/todo.json
deleted file mode 100644
index e2a3342..0000000
--- a/todo.json
+++ /dev/null
@@ -1 +0,0 @@
-{"today": {"date": "2023-11-27a", "tasks": {"f9197573-a001-4fbe-adb0-14391cc68101": {"weight": "1", "done": true, "template": "deb8d796-404a-4894-8f0d-ab8e5c4b71e1"}, "d49c78c9-cdf6-4d35-b306-a69a9b03a911": {"weight": "3.5", "done": true, "template": "038c7597-03c2-4008-969a-8d4df3c17e04"}, "e881720e-a629-42a4-98d5-86ac61651f4b": {"weight": "0.2", "done": true, "template": "d36ccc8a-92c6-438d-b680-ac3e077959e1"}, "e3dda9ae-8a56-4170-8a58-4d8ad6518dbd": {"weight": "0.3", "done": true, "template": "25c42212-bced-4745-b268-638e8e373319"}, "95eda507-6dc6-45c1-bec3-40d2e46b7066": {"weight": "0.3", "done": true, "template": "2cb16551-6a93-41fa-8348-11c1d426fffe"}, "0415be6c-4c13-4694-8b12-72efc3d5fc62": {"weight": "1", "done": true, "template": "453c1251-b44b-4845-bac7-e35fb2d586b0"}, "b0bbaa82-4f39-47b7-93ed-25a258895217": {"weight": "0.5", "done": true, "template": "f63e1ca8-00a2-4ebc-a6b2-edae761f969a"}, "34d0be67-aaf0-45f9-9244-89bf10e754ce": {"weight": "0.2", "done": true, "template": "4bea5933-2bdd-49e0-8692-78588e3b7753"}, "725ee070-ceb0-4e7c-b9ca-e71a16ddf5c4": {"weight": "0.2", "done": true, "template": "3e9c535c-2539-423d-9666-4e434b895a46"}}}, "tt_filter": [], "task_templates": [{"uuid": "038c7597-03c2-4008-969a-8d4df3c17e04", "title": "duschen", "weight": "3", "tags": "duschen;bodily", "weight_history": [[1, "2023-11-28"], ["4", "2023-11-28"], ["3", "2023-11-28"]]}, {"uuid": "deb8d796-404a-4894-8f0d-ab8e5c4b71e1", "title": "erstes z\u00e4hneputzen", "weight": "1", "tags": "daily;duschen;bodily", "weight_history": [[1, "2023-11-28"], ["1", "2023-11-28"]]}, {"uuid": "d36ccc8a-92c6-438d-b680-ac3e077959e1", "title": "haare k\u00e4mmen", "weight": "0.2", "tags": "daily;duschen;bodily", "weight_history": [[1, "2023-11-28"], ["1", "2023-11-28"], ["0.2", "2023-11-28"]]}, {"uuid": "4bea5933-2bdd-49e0-8692-78588e3b7753", "title": "h\u00e4nde waschen", "weight": "0.2", "tags": "daily;bodily", "weight_history": [[1, "2023-11-28"], ["0.2", "2023-11-28"]]}, {"uuid": "3e9c535c-2539-423d-9666-4e434b895a46", "title": "kalorientabelle nachf\u00fcllen", "weight": "0.2", "tags": "daily;oncomputer", "weight_history": [[1, "2023-11-28"], ["0.2", "2023-11-28"]]}, {"uuid": "25c42212-bced-4745-b268-638e8e373319", "title": "l\u00fcften", "weight": "0.4", "tags": "daily;bodily", "weight_history": [[1, "2023-11-28"], ["0.3", "2023-11-28"], ["0.4", "2023-11-28"]]}, {"uuid": "f63e1ca8-00a2-4ebc-a6b2-edae761f969a", "title": "mail-junk aufr\u00e4umen", "weight": "0.5", "tags": "daily;oncomputer", "weight_history": [[1, "2023-11-28"], ["1", "2023-11-28"], ["0.5", "2023-11-28"]]}, {"uuid": "2cb16551-6a93-41fa-8348-11c1d426fffe", "title": "paroxetin nehmen", "weight": "0.3", "tags": "daily", "weight_history": [[1, "2023-11-28"], ["0.3", "2023-11-28"]]}, {"uuid": "ecdeaa70-676a-4ef0-9b94-93691dadcbd0", "title": "relevante Mails anschauen", "weight": "0.5", "tags": "daily;oncomputer", "weight_history": [[1, "2023-11-28"], ["0.5", "2023-11-28"]]}, {"uuid": "db1f747c-35e3-4ff4-a3c4-213d9d132d90", "title": "schn\u00fcrsenkelschuhe anziehen", "weight": "0.5", "tags": "daily;bodily", "weight_history": [[1, "2023-11-28"], ["0.5", "2023-11-28"]]}, {"uuid": "ce84225c-6728-45df-a030-2de490b9790f", "title": "schn\u00fcrsenkelschuhe ausziehen", "weight": "0.3", "tags": "daily;bodily", "weight_history": [[1, "2023-11-28"], ["0.3", "2023-11-28"]]}, {"uuid": "453c1251-b44b-4845-bac7-e35fb2d586b0", "title": "zweites z\u00e4hneputzen", "weight": "1", "tags": "daily;duschen;bodily", "weight_history": [[1, "2023-11-28"], ["1", "2023-11-28"], ["2", "2023-11-28"], ["1", "2023-11-28"]]}], "days": []}
\ No newline at end of file
-- 
2.30.2