From c1ff9550705d92cdc04c26ab6ec3aeef655c82ce Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Thu, 23 Jan 2025 08:19:16 +0100
Subject: [PATCH] Add ?cutoff to /balance.

---
 ledger.py              | 13 +++++++------
 templates/_macros.tmpl |  2 +-
 2 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/ledger.py b/ledger.py
index f4dde03..413b8fb 100755
--- a/ledger.py
+++ b/ledger.py
@@ -249,7 +249,8 @@ class Handler(PlomHttpHandler):
             ctx['id'] = int(self.path_toks[2])
             ctx['dat_lines'] = self.server.bookings[ctx['id']].dat_lines
         if self.pagename == 'balance':
-            valid, balance_roots = self.server.balance_roots
+            valid, balance_roots = self.server.balance_roots(
+                    int(self.params.first('cutoff') or '0'))
             self.send_rendered(Path('balance.tmpl'),
                                ctx | {'roots': balance_roots, 'valid': valid})
         elif self.pagename == 'booking':
@@ -310,12 +311,12 @@ class Server(PlomHttpServer):
         """Return only those .data_lines with .code or .comment."""
         return [dl for dl in self.dat_lines if not dl.is_empty]
 
-    @property
-    def balance_roots(self) -> tuple[bool, list[Account]]:
-        """Return tree of calculated Accounts over all .bookings."""
+    def balance_roots(self, cutoff: int) -> tuple[bool, list[Account]]:
+        """Return tree of calculated Accounts over .bookings[:cutoff]."""
         account_names = set()
         valid = True
-        for booking in self.bookings:
+        to_balance = self.bookings[:cutoff] if cutoff else self.bookings
+        for booking in to_balance:
             valid = valid if not booking.is_questionable else False
             for account_name in booking.account_changes:
                 account_names.add(account_name)
@@ -331,7 +332,7 @@ class Server(PlomHttpServer):
                         full_names_to_accounts[parent_name] if parent_name
                         else None,
                         step_name)
-        for booking in self.bookings:
+        for booking in to_balance:
             for account_name in booking.account_changes:
                 full_names_to_accounts[account_name].local_wealth +=\
                         booking.account_changes[account_name]
diff --git a/templates/_macros.tmpl b/templates/_macros.tmpl
index 85c032e..c1a9a40 100644
--- a/templates/_macros.tmpl
+++ b/templates/_macros.tmpl
@@ -16,7 +16,7 @@ td.invalid, tr.warning td.invalid { background-color: #ff0000; }
   <tr{% if dat_line.is_questionable %} class="warning"{% endif %}>
   {% if not single %}
     {% if dat_line.is_intro %}
-      <td id="{{dat_line.booking_id}}"><a href="#{{dat_line.booking_id}}">#</a></td>
+      <td id="{{dat_line.booking_id}}"><a href="#{{dat_line.booking_id}}">#</a>/<a href="/balance?cutoff={{dat_line.booking_id+1}}">b</a></td>
     {% else %}
       <td></td>
     {% endif %}
-- 
2.30.2