From 9aa70eb71739449639b342adefa610ba6d1c1b14 Mon Sep 17 00:00:00 2001 From: ai-bot Date: Mon, 16 Nov 2026 19:32:55 +0000 Subject: [PATCH] Refuse an over-drawn real account and exempt the bank --- ledger.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/ledger.py b/ledger.py index 9352194..5eb0d04 100644 --- a/ledger.py +++ b/ledger.py @@ -393,18 +393,27 @@ def _picks(conn, tx_id): return [dict(row) for row in rows] -def _move_kroner(balances, tx): - """Track the derived kroner balance and refuse an over-drawn transfer. +def _bank_market_id(conn): + """The account that pays the costs (docs/spec.md section 3.11).""" + row = conn.execute( + "SELECT value FROM settings WHERE key = 'bank_market_id'" + ).fetchone() + return int(row["value"]) if row else None - Kroner are never stored, so the balance is carried along the replay. Only a - transfer is refused: a buy that overdraws is allowed (section 3.11). + +def _move_kroner(balances, tx, bank_id): + """Track the derived kroner balance and refuse an over-drawn account. + + Kroner are never stored, so the balance is carried along the replay. No real + account may go negative; the bank may, and its negative balance is how much + has been drawn from it (docs/spec.md section 3.11). """ market_id = tx["market_id"] if market_id is None: return balance = balances.get(market_id, 0) out_ore = tx["leg_out_ore"] or 0 - if out_ore and tx["type"] == "transfer_out" and balance < out_ore: + if out_ore and balance < out_ore and market_id != bank_id: raise LedgerError( "Kontoen har ikkje nok kroner: det manglar %s kr." % rates.kroner_whole(out_ore - balance)