REFACTOR Migrates to single 'amount' field for transactions
Refactors the data model to use a single 'amount' field for journal entry lines, aligning with the Beancount approach. This simplifies the model, enhances compatibility, and eliminates invalid states. Includes a database migration to convert existing debit/credit columns to the new 'amount' field. Updates balance calculation logic to utilize the new amount field for improved accuracy and efficiency.
This commit is contained in:
parent
0b50ba0f82
commit
5cc2630777
7 changed files with 196 additions and 144 deletions
48
views_api.py
48
views_api.py
|
|
@ -439,15 +439,13 @@ async def api_create_expense_entry(
|
|||
lines=[
|
||||
CreateEntryLine(
|
||||
account_id=expense_account.id,
|
||||
debit=amount_sats,
|
||||
credit=0,
|
||||
amount=amount_sats, # Positive = debit (expense increase)
|
||||
description=f"Expense paid by user {wallet.wallet.user[:8]}",
|
||||
metadata=metadata,
|
||||
),
|
||||
CreateEntryLine(
|
||||
account_id=user_account.id,
|
||||
debit=0,
|
||||
credit=amount_sats,
|
||||
amount=-amount_sats, # Negative = credit (liability/equity increase)
|
||||
description=f"{'Equity contribution' if data.is_equity else 'Amount owed to user'}",
|
||||
metadata=metadata,
|
||||
),
|
||||
|
|
@ -525,15 +523,13 @@ async def api_create_receivable_entry(
|
|||
lines=[
|
||||
CreateEntryLine(
|
||||
account_id=user_receivable.id,
|
||||
debit=amount_sats,
|
||||
credit=0,
|
||||
amount=amount_sats, # Positive = debit (asset increase - user owes castle)
|
||||
description=f"Amount owed by user {data.user_id[:8]}",
|
||||
metadata=metadata,
|
||||
),
|
||||
CreateEntryLine(
|
||||
account_id=revenue_account.id,
|
||||
debit=0,
|
||||
credit=amount_sats,
|
||||
amount=-amount_sats, # Negative = credit (revenue increase)
|
||||
description="Revenue earned",
|
||||
metadata=metadata,
|
||||
),
|
||||
|
|
@ -580,14 +576,12 @@ async def api_create_revenue_entry(
|
|||
lines=[
|
||||
CreateEntryLine(
|
||||
account_id=payment_account.id,
|
||||
debit=data.amount,
|
||||
credit=0,
|
||||
amount=data.amount, # Positive = debit (asset increase)
|
||||
description="Payment received",
|
||||
),
|
||||
CreateEntryLine(
|
||||
account_id=revenue_account.id,
|
||||
debit=0,
|
||||
credit=data.amount,
|
||||
amount=-data.amount, # Negative = credit (revenue increase)
|
||||
description="Revenue earned",
|
||||
),
|
||||
],
|
||||
|
|
@ -871,15 +865,13 @@ async def api_record_payment(
|
|||
lines=[
|
||||
CreateEntryLine(
|
||||
account_id=lightning_account.id,
|
||||
debit=amount_sats,
|
||||
credit=0,
|
||||
amount=amount_sats, # Positive = debit (asset increase)
|
||||
description="Lightning payment received",
|
||||
metadata=line_metadata,
|
||||
),
|
||||
CreateEntryLine(
|
||||
account_id=user_receivable.id,
|
||||
debit=0,
|
||||
credit=amount_sats,
|
||||
amount=-amount_sats, # Negative = credit (asset decrease - receivable settled)
|
||||
description="Payment applied to balance",
|
||||
metadata=line_metadata,
|
||||
),
|
||||
|
|
@ -927,14 +919,12 @@ async def api_pay_user(
|
|||
lines=[
|
||||
CreateEntryLine(
|
||||
account_id=user_payable.id,
|
||||
debit=amount,
|
||||
credit=0,
|
||||
amount=amount, # Positive = debit (liability decrease)
|
||||
description="Payment made to user",
|
||||
),
|
||||
CreateEntryLine(
|
||||
account_id=lightning_account.id,
|
||||
debit=0,
|
||||
credit=amount,
|
||||
amount=-amount, # Negative = credit (asset decrease)
|
||||
description="Lightning payment sent",
|
||||
),
|
||||
],
|
||||
|
|
@ -1070,15 +1060,13 @@ async def api_settle_receivable(
|
|||
lines=[
|
||||
CreateEntryLine(
|
||||
account_id=payment_account.id,
|
||||
debit=amount_in_sats,
|
||||
credit=0,
|
||||
amount=amount_in_sats, # Positive = debit (asset increase)
|
||||
description=f"Payment received via {data.payment_method}",
|
||||
metadata=line_metadata,
|
||||
),
|
||||
CreateEntryLine(
|
||||
account_id=user_receivable.id,
|
||||
debit=0,
|
||||
credit=amount_in_sats,
|
||||
amount=-amount_in_sats, # Negative = credit (asset decrease - receivable settled)
|
||||
description="Receivable settled",
|
||||
metadata=line_metadata,
|
||||
),
|
||||
|
|
@ -1216,15 +1204,13 @@ async def api_pay_user(
|
|||
lines=[
|
||||
CreateEntryLine(
|
||||
account_id=user_payable.id,
|
||||
debit=amount_in_sats,
|
||||
credit=0,
|
||||
amount=amount_in_sats, # Positive = debit (liability decrease)
|
||||
description="Payable settled",
|
||||
metadata=line_metadata,
|
||||
),
|
||||
CreateEntryLine(
|
||||
account_id=payment_account.id,
|
||||
debit=0,
|
||||
credit=amount_in_sats,
|
||||
amount=-amount_in_sats, # Negative = credit (asset decrease)
|
||||
description=f"Payment sent via {data.payment_method}",
|
||||
metadata=line_metadata,
|
||||
),
|
||||
|
|
@ -1510,14 +1496,12 @@ async def api_approve_manual_payment_request(
|
|||
lines=[
|
||||
CreateEntryLine(
|
||||
account_id=liability_account.id,
|
||||
debit=request.amount, # Decrease liability (castle owes less)
|
||||
credit=0,
|
||||
amount=request.amount, # Positive = debit (liability decrease - castle owes less)
|
||||
description="Payment to user",
|
||||
),
|
||||
CreateEntryLine(
|
||||
account_id=lightning_account.id,
|
||||
debit=0,
|
||||
credit=request.amount, # Decrease asset (lightning balance reduced)
|
||||
amount=-request.amount, # Negative = credit (asset decrease)
|
||||
description="Payment from castle",
|
||||
),
|
||||
],
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue