06/03 Optimize codes, save synced balances

This commit is contained in:
2026-06-03 10:33:04 -04:00
parent 395a2d9535
commit faacd4d91a
3 changed files with 18 additions and 14 deletions
+12 -8
View File
@@ -59,8 +59,20 @@ def index():
if tab not in ('bank', 'credit'):
tab = 'bank'
from app.models.teller_enrollment import TellerAccount
all_accounts = Account.query.filter_by(is_active=True).order_by(Account.name).all()
# Build Teller map first so we can skip calc_balance for Teller-linked accounts.
# Their balance comes from Teller (live refresh or transaction sync) and must not
# be overwritten by the transaction-computed sum on every page load.
teller_accounts = TellerAccount.query.filter(
TellerAccount.pfm_account_id.in_([a.id for a in all_accounts]),
TellerAccount.is_active == True,
).all()
teller_map = {ta.pfm_account_id: ta for ta in teller_accounts}
for a in all_accounts:
if a.id not in teller_map:
calc_balance(a.id)
all_accounts = Account.query.filter_by(is_active=True).order_by(Account.name).all()
@@ -83,14 +95,6 @@ def index():
).group_by(Transaction.account_id).all()
monthly_charges = {row[0]: float(row[1]) for row in rows}
# Teller account mapping: pfm_account_id → TellerAccount
from app.models.teller_enrollment import TellerAccount
teller_accounts = TellerAccount.query.filter(
TellerAccount.pfm_account_id.in_([a.id for a in all_accounts]),
TellerAccount.is_active == True,
).all()
teller_map = {ta.pfm_account_id: ta for ta in teller_accounts}
return render_template('accounts/index.html',
accounts=accounts,
tab=tab,
+1 -2
View File
@@ -7,7 +7,7 @@ from app.models.transaction import Transaction
from app.models.category import Category
from app.services.fx_service import get_today_rate, get_rate_history, force_refresh
from app.services.ai_service import get_latest_daily_insight
from app.services.account_service import get_total_assets, get_total_liabilities, recalc_all
from app.services.account_service import get_total_assets, get_total_liabilities
from datetime import date, datetime, timedelta
import calendar
@@ -62,7 +62,6 @@ def index():
net_cash_flow = float(total_income) - float(total_expense)
# ── Accounts ─────────────────────────────────────
recalc_all()
accounts = Account.query.filter_by(is_active=True).order_by(Account.name).all()
total_assets = get_total_assets()
total_liabilities = get_total_liabilities()
+4 -3
View File
@@ -316,9 +316,10 @@ def refresh_balance(teller_account_id):
ledger = float(bal_data.get('ledger') or bal_data.get('available') or 0)
# Credit cards: 'available' is the remaining credit line, not what's owed.
# Use 'ledger' (amount owed, negative in Teller's convention) for credit cards.
# Use 'ledger' for credit cards and force it negative (our debt convention).
# Teller may return ledger as positive or negative depending on the bank.
is_credit = ta.pfm_account.account_type == 'credit_card'
balance_to_store = ledger if is_credit else available
balance_to_store = -abs(ledger) if is_credit else available
ta.pfm_account.balance = balance_to_store
db.session.commit()
@@ -353,7 +354,7 @@ def refresh_all_balances():
available = float(bal_data.get('available') or bal_data.get('ledger') or 0)
ledger = float(bal_data.get('ledger') or bal_data.get('available') or 0)
is_credit = ta.pfm_account.account_type == 'credit_card'
ta.pfm_account.balance = ledger if is_credit else available
ta.pfm_account.balance = -abs(ledger) if is_credit else available
refreshed.append(ta.account_name)
log.info('[teller] balance refreshed: %s = %.2f', ta.account_name, ta.pfm_account.balance)
except Exception as exc: