Adjusts balance calculation for user perspective

Inverts the sign of Beancount balances to represent the user's perspective, where liabilities are positive and receivables are negative.

This change ensures that user balances accurately reflect the amount the castle owes the user (positive) or the amount the user owes the castle (negative). It simplifies the logic by consistently negating the Beancount balance rather than using conditional checks based on account type.
This commit is contained in:
padreug 2025-11-09 23:35:36 +01:00
parent 9350f05d74
commit 37fe34668f

View file

@ -220,15 +220,12 @@ class FavaClient:
for cost_str, amount in sats_positions.items(): for cost_str, amount in sats_positions.items():
amount_int = int(amount) amount_int = int(amount)
# Apply sign based on account type # For user balance perspective, negate Beancount balance
if "Payable" in account_name: # - Payable (Liability): negative in Beancount → positive (castle owes user)
# Liability: positive = castle owes user # - Receivable (Asset): positive in Beancount → negative (user owes castle)
total_sats += amount_int adjusted_amount = -amount_int
account_balance["sats"] += amount_int total_sats += adjusted_amount
elif "Receivable" in account_name: account_balance["sats"] += adjusted_amount
# Asset: positive = user owes castle (subtract from user balance)
total_sats -= amount_int
account_balance["sats"] -= amount_int
# Extract fiat amount from cost basis # Extract fiat amount from cost basis
# Format: "100.00 EUR" or "{100.00 EUR}" # Format: "100.00 EUR" or "{100.00 EUR}"
@ -243,23 +240,22 @@ class FavaClient:
if fiat_currency not in fiat_balances: if fiat_currency not in fiat_balances:
fiat_balances[fiat_currency] = Decimal(0) fiat_balances[fiat_currency] = Decimal(0)
# Apply same sign logic # Apply same sign adjustment to fiat
if "Payable" in account_name: # Cost basis is always positive, derive sign from amount
fiat_balances[fiat_currency] += fiat_amount if amount_int < 0:
elif "Receivable" in account_name: fiat_amount = -fiat_amount
fiat_balances[fiat_currency] -= fiat_amount adjusted_fiat = -fiat_amount
fiat_balances[fiat_currency] += adjusted_fiat
except (ValueError, IndexError): except (ValueError, IndexError):
logger.warning(f"Could not parse cost basis: {cost_str}") logger.warning(f"Could not parse cost basis: {cost_str}")
elif isinstance(sats_positions, (int, float)): elif isinstance(sats_positions, (int, float)):
# Simple number (no cost basis) # Simple number (no cost basis)
amount_int = int(sats_positions) amount_int = int(sats_positions)
if "Payable" in account_name: # Negate Beancount balance for user perspective
total_sats += amount_int adjusted_amount = -amount_int
account_balance["sats"] += amount_int total_sats += adjusted_amount
elif "Receivable" in account_name: account_balance["sats"] += adjusted_amount
total_sats -= amount_int
account_balance["sats"] -= amount_int
accounts.append(account_balance) accounts.append(account_balance)
@ -338,12 +334,10 @@ class FavaClient:
for cost_str, amount in sats_positions.items(): for cost_str, amount in sats_positions.items():
amount_int = int(amount) amount_int = int(amount)
if "Payable" in account_name: # Negate Beancount balance for user perspective
user_data[user_id]["balance"] += amount_int adjusted_amount = -amount_int
account_info["sats"] += amount_int user_data[user_id]["balance"] += adjusted_amount
elif "Receivable" in account_name: account_info["sats"] += adjusted_amount
user_data[user_id]["balance"] -= amount_int
account_info["sats"] -= amount_int
# Extract fiat # Extract fiat
if cost_str and cost_str != "SATS": if cost_str and cost_str != "SATS":
@ -357,21 +351,20 @@ class FavaClient:
if fiat_currency not in user_data[user_id]["fiat_balances"]: if fiat_currency not in user_data[user_id]["fiat_balances"]:
user_data[user_id]["fiat_balances"][fiat_currency] = Decimal(0) user_data[user_id]["fiat_balances"][fiat_currency] = Decimal(0)
if "Payable" in account_name: # Apply sign from amount to fiat
user_data[user_id]["fiat_balances"][fiat_currency] += fiat_amount if amount_int < 0:
elif "Receivable" in account_name: fiat_amount = -fiat_amount
user_data[user_id]["fiat_balances"][fiat_currency] -= fiat_amount adjusted_fiat = -fiat_amount
user_data[user_id]["fiat_balances"][fiat_currency] += adjusted_fiat
except (ValueError, IndexError): except (ValueError, IndexError):
pass pass
elif isinstance(sats_positions, (int, float)): elif isinstance(sats_positions, (int, float)):
amount_int = int(sats_positions) amount_int = int(sats_positions)
if "Payable" in account_name: # Negate Beancount balance for user perspective
user_data[user_id]["balance"] += amount_int adjusted_amount = -amount_int
account_info["sats"] += amount_int user_data[user_id]["balance"] += adjusted_amount
elif "Receivable" in account_name: account_info["sats"] += adjusted_amount
user_data[user_id]["balance"] -= amount_int
account_info["sats"] -= amount_int
user_data[user_id]["accounts"].append(account_info) user_data[user_id]["accounts"].append(account_info)