From b4408cd53de6d21a51d3478b0ea36c0a98410e13 Mon Sep 17 00:00:00 2001 From: padreug Date: Thu, 23 Oct 2025 04:27:07 +0200 Subject: [PATCH] Ensures journal entries use satoshis for settlements Updates the `api_settle_receivable` function to ensure all journal entries are recorded in satoshis, regardless of the currency used for payment. This change standardizes the journal entries to match the receivable account balance, simplifying reconciliation. It also enforces that `amount_sats` must be provided when settling with fiat currency to correctly calculate the sats equivalent. --- views_api.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/views_api.py b/views_api.py index f6a41dc..103452f 100644 --- a/views_api.py +++ b/views_api.py @@ -826,24 +826,27 @@ async def api_settle_receivable( # DR Cash/Bank (asset increased), CR Accounts Receivable (asset decreased) # This records that user paid their debt - # Convert amount to sats (minor units) - # For fiat currencies, store as cents/minor units - # For satoshis, just convert to int + # Determine the amount to record in the journal + # IMPORTANT: Always record in satoshis to match the receivable account balance from decimal import Decimal if data.currency: # Fiat currency payment (e.g., EUR, USD) - amount_minor_units = int(data.amount * 100) # Convert to cents + # Use the sats equivalent for the journal entry to match the receivable + if not data.amount_sats: + raise HTTPException( + status_code=HTTPStatus.BAD_REQUEST, + detail="amount_sats is required when settling with fiat currency" + ) + amount_in_sats = data.amount_sats line_metadata = { "fiat_currency": data.currency, "fiat_amount": str(data.amount), + "exchange_rate": data.amount_sats / float(data.amount) } - if data.amount_sats: - line_metadata["sats_equivalent"] = data.amount_sats - line_metadata["exchange_rate"] = data.amount_sats / float(data.amount) else: # Satoshi payment - amount_minor_units = int(data.amount) + amount_in_sats = int(data.amount) line_metadata = {} # Add meta information for audit trail @@ -864,7 +867,7 @@ async def api_settle_receivable( lines=[ CreateEntryLine( account_id=payment_account.id, - debit=amount_minor_units, + debit=amount_in_sats, credit=0, description=f"Payment received via {data.payment_method}", metadata=line_metadata, @@ -872,7 +875,7 @@ async def api_settle_receivable( CreateEntryLine( account_id=user_receivable.id, debit=0, - credit=amount_minor_units, + credit=amount_in_sats, description="Receivable settled", metadata=line_metadata, ),