Refactors journal entry lines to use single amount

Simplifies the representation of journal entry lines by replacing separate debit and credit fields with a single 'amount' field.

Positive amounts represent debits, while negative amounts represent credits, aligning with Beancount's approach. This change improves code readability and simplifies calculations for balancing entries.
This commit is contained in:
padreug 2025-11-08 11:48:08 +01:00
parent d0bec3ea5a
commit 4ae6a8f7d2
5 changed files with 35 additions and 45 deletions

View file

@ -71,8 +71,7 @@ CREATE TABLE entry_lines (
id TEXT PRIMARY KEY,
journal_entry_id TEXT NOT NULL,
account_id TEXT NOT NULL,
debit INTEGER NOT NULL DEFAULT 0, -- Amount in satoshis
credit INTEGER NOT NULL DEFAULT 0, -- Amount in satoshis
amount INTEGER NOT NULL, -- Amount in satoshis (positive = debit, negative = credit)
description TEXT,
metadata TEXT DEFAULT '{}' -- JSON: {fiat_currency, fiat_amount, fiat_rate, btc_rate}
);
@ -314,17 +313,20 @@ for account in user_accounts:
total_balance -= account_balance # Positive asset = User owes Castle, so negative balance
# Calculate fiat balance from metadata
# Beancount-style: positive amount = debit, negative amount = credit
for line in account_entry_lines:
if line.metadata.fiat_currency and line.metadata.fiat_amount:
if account.account_type == AccountType.LIABILITY:
if line.credit > 0:
# For liabilities, negative amounts (credits) increase what castle owes
if line.amount < 0:
fiat_balances[currency] += fiat_amount # Castle owes more
elif line.debit > 0:
else:
fiat_balances[currency] -= fiat_amount # Castle owes less
elif account.account_type == AccountType.ASSET:
if line.debit > 0:
# For assets, positive amounts (debits) increase what user owes
if line.amount > 0:
fiat_balances[currency] -= fiat_amount # User owes more (negative balance)
elif line.credit > 0:
else:
fiat_balances[currency] += fiat_amount # User owes less
```
@ -767,10 +769,8 @@ async def export_beancount(
beancount_name = format_account_name(account.name, account.user_id)
beancount_type = map_account_type(account.account_type)
if line.debit > 0:
amount = line.debit
else:
amount = -line.credit
# Beancount-style: amount is already signed (positive = debit, negative = credit)
amount = line.amount
lines.append(f" {beancount_type}:{beancount_name} {amount} SATS")