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.
This commit is contained in:
padreug 2025-10-23 04:27:07 +02:00
parent 70013d1c29
commit b4408cd53d

View file

@ -826,24 +826,27 @@ async def api_settle_receivable(
# DR Cash/Bank (asset increased), CR Accounts Receivable (asset decreased) # DR Cash/Bank (asset increased), CR Accounts Receivable (asset decreased)
# This records that user paid their debt # This records that user paid their debt
# Convert amount to sats (minor units) # Determine the amount to record in the journal
# For fiat currencies, store as cents/minor units # IMPORTANT: Always record in satoshis to match the receivable account balance
# For satoshis, just convert to int
from decimal import Decimal from decimal import Decimal
if data.currency: if data.currency:
# Fiat currency payment (e.g., EUR, USD) # 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 = { line_metadata = {
"fiat_currency": data.currency, "fiat_currency": data.currency,
"fiat_amount": str(data.amount), "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: else:
# Satoshi payment # Satoshi payment
amount_minor_units = int(data.amount) amount_in_sats = int(data.amount)
line_metadata = {} line_metadata = {}
# Add meta information for audit trail # Add meta information for audit trail
@ -864,7 +867,7 @@ async def api_settle_receivable(
lines=[ lines=[
CreateEntryLine( CreateEntryLine(
account_id=payment_account.id, account_id=payment_account.id,
debit=amount_minor_units, debit=amount_in_sats,
credit=0, credit=0,
description=f"Payment received via {data.payment_method}", description=f"Payment received via {data.payment_method}",
metadata=line_metadata, metadata=line_metadata,
@ -872,7 +875,7 @@ async def api_settle_receivable(
CreateEntryLine( CreateEntryLine(
account_id=user_receivable.id, account_id=user_receivable.id,
debit=0, debit=0,
credit=amount_minor_units, credit=amount_in_sats,
description="Receivable settled", description="Receivable settled",
metadata=line_metadata, metadata=line_metadata,
), ),