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, ),