Excludes pending transactions from balance queries
Modifies balance queries to exclude pending transactions (flag='!') and only include cleared/completed transactions (flag='*'). This ensures accurate balance calculations by reflecting only settled transactions.
This commit is contained in:
parent
56a3e9d4e9
commit
57e6b3de1d
1 changed files with 19 additions and 7 deletions
|
|
@ -109,7 +109,7 @@ class FavaClient:
|
||||||
|
|
||||||
async def get_account_balance(self, account_name: str) -> Dict[str, Any]:
|
async def get_account_balance(self, account_name: str) -> Dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
Get balance for a specific account.
|
Get balance for a specific account (excluding pending transactions).
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
account_name: Full account name (e.g., "Assets:Receivable:User-abc123")
|
account_name: Full account name (e.g., "Assets:Receivable:User-abc123")
|
||||||
|
|
@ -119,6 +119,10 @@ class FavaClient:
|
||||||
- sats: int (balance in satoshis)
|
- sats: int (balance in satoshis)
|
||||||
- positions: dict (currency → amount with cost basis)
|
- positions: dict (currency → amount with cost basis)
|
||||||
|
|
||||||
|
Note:
|
||||||
|
Excludes pending transactions (flag='!') from balance calculation.
|
||||||
|
Only cleared/completed transactions (flag='*') are included.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
balance = await fava_client.get_account_balance("Assets:Receivable:User-abc")
|
balance = await fava_client.get_account_balance("Assets:Receivable:User-abc")
|
||||||
# Returns: {
|
# Returns: {
|
||||||
|
|
@ -126,7 +130,7 @@ class FavaClient:
|
||||||
# "positions": {"SATS": {"{100.00 EUR}": 200000}}
|
# "positions": {"SATS": {"{100.00 EUR}": 200000}}
|
||||||
# }
|
# }
|
||||||
"""
|
"""
|
||||||
query = f"SELECT sum(position) WHERE account = '{account_name}'"
|
query = f"SELECT sum(position) WHERE account = '{account_name}' AND flag != '!'"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with httpx.AsyncClient(timeout=self.timeout) as client:
|
async with httpx.AsyncClient(timeout=self.timeout) as client:
|
||||||
|
|
@ -171,8 +175,8 @@ class FavaClient:
|
||||||
Get user's total balance (what castle owes user).
|
Get user's total balance (what castle owes user).
|
||||||
|
|
||||||
Aggregates:
|
Aggregates:
|
||||||
- Liabilities:Payable:User-{user_id} (positive = castle owes)
|
- Liabilities:Payable:User-{user_id} (negative balance = castle owes)
|
||||||
- Assets:Receivable:User-{user_id} (positive = user owes, so negate)
|
- Assets:Receivable:User-{user_id} (positive balance = user owes)
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
user_id: User ID
|
user_id: User ID
|
||||||
|
|
@ -183,11 +187,15 @@ class FavaClient:
|
||||||
"fiat_balances": {"EUR": Decimal("100.50")},
|
"fiat_balances": {"EUR": Decimal("100.50")},
|
||||||
"accounts": [list of account dicts with balances]
|
"accounts": [list of account dicts with balances]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Note:
|
||||||
|
Excludes pending transactions (flag='!') from balance calculation.
|
||||||
|
Only cleared/completed transactions (flag='*') are included.
|
||||||
"""
|
"""
|
||||||
# Query for all accounts matching user
|
# Query for all accounts matching user (excluding pending)
|
||||||
query = f"""
|
query = f"""
|
||||||
SELECT account, sum(position)
|
SELECT account, sum(position)
|
||||||
WHERE account ~ 'User-{user_id[:8]}'
|
WHERE account ~ 'User-{user_id[:8]}' AND flag != '!'
|
||||||
GROUP BY account
|
GROUP BY account
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
@ -286,10 +294,14 @@ class FavaClient:
|
||||||
},
|
},
|
||||||
...
|
...
|
||||||
]
|
]
|
||||||
|
|
||||||
|
Note:
|
||||||
|
Excludes pending transactions (flag='!') from balance calculation.
|
||||||
|
Only cleared/completed transactions (flag='*') are included.
|
||||||
"""
|
"""
|
||||||
query = """
|
query = """
|
||||||
SELECT account, sum(position)
|
SELECT account, sum(position)
|
||||||
WHERE account ~ 'Payable:User-|Receivable:User-'
|
WHERE account ~ 'Payable:User-|Receivable:User-' AND flag != '!'
|
||||||
GROUP BY account
|
GROUP BY account
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue