From 2b5025fa7b07f791b198b0bca76fca3589ec6e71 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Mon, 20 Jan 2025 22:58:08 +0100
Subject: [PATCH] Properly store Booking.amount rather than just an ellipsized
 string.

---
 ledger.py              | 24 +++++++++++++++---------
 templates/_macros.tmpl |  6 +++---
 2 files changed, 18 insertions(+), 12 deletions(-)

diff --git a/ledger.py b/ledger.py
index 6085397..aa0619b 100755
--- a/ledger.py
+++ b/ledger.py
@@ -78,31 +78,37 @@ class IntroLine(BookingLine):
             self.error = 'not starting with properly formatted legal date'
 
 
-class TransactionLine(BookingLine):
+class TransferLine(BookingLine):
     """Non-first Booking line, expected to carry value movement."""
 
     def __init__(self, booking_id: int, code: str) -> None:
         super().__init__(booking_id)
-        self.acc, self.amt, self.curr = '', '', ''
+        self.account, self.currency = '', ''
+        self.amount: Optional[Decimal] = None
         if not code[0].isspace():
             self.error = 'non-intro line not indented'
             return
         toks = code.lstrip().split()
-        self.acc = toks[0]
+        self.account = toks[0]
         if len(toks) not in {1, 3}:
             self.error = 'illegal number of tokens'
             return
         if 3 == len(toks):
+            self.currency = toks[2]
             try:
-                amt_dec = Decimal(toks[1])
+                self.amount = Decimal(toks[1])
             except DecimalInvalidOperation:
                 self.error = 'improper amount value'
                 return
-            exp = amt_dec.as_tuple().exponent
+
+    @property
+    def amount_short(self) -> str:
+        """If no .amount, '', else printed – but if too long, ellipsized."""
+        if self.amount:
+            exp = self.amount.as_tuple().exponent
             assert isinstance(exp, int)
-            self.amt = (f'{amt_dec:.1f}…' if exp < -2
-                        else f'{amt_dec:.2f}')
-            self.curr = toks[2]
+            return f'{self.amount:.1f}…' if exp < -2 else f'{self.amount:.2f}'
+        return ''
 
 
 class Booking:
@@ -115,7 +121,7 @@ class Booking:
         self.dat_lines[0].booking_line = IntroLine(self.id_,
                                                    self.dat_lines[0].code)
         for dat_line in self.dat_lines[1:]:
-            dat_line.booking_line = TransactionLine(self.id_, dat_line.code)
+            dat_line.booking_line = TransferLine(self.id_, dat_line.code)
 
 
 class Handler(PlomHttpHandler):
diff --git a/templates/_macros.tmpl b/templates/_macros.tmpl
index 7ba713f..b3cb64a 100644
--- a/templates/_macros.tmpl
+++ b/templates/_macros.tmpl
@@ -28,9 +28,9 @@ td.curr { text-align: center; }
     {% if dat_line.is_intro %}
       <td class="code{% if dat_line.error %} invalid{% endif %}" colspan=3><a href="/booking/{{dat_line.booking_id}}">{{dat_line.code}}</a></td>
     {% elif not dat_line.error %}
-      <td class="amt">{{dat_line.booking_line.amt}}</td>
-      <td class="curr">{{dat_line.booking_line.curr|truncate(4,true,"…")}}</td>
-      <td>{{dat_line.booking_line.acc}}</td>
+      <td class="amt">{{dat_line.booking_line.amount_short}}</td>
+      <td class="curr">{{dat_line.booking_line.currency|truncate(4,true,"…")}}</td>
+      <td>{{dat_line.booking_line.account}}</td>
     {% else %}
       <td class="invalid" colspan=3>{{dat_line.code}}</td>
     {% endif %}
-- 
2.30.2