Files
maziggy 4a85e033c0 fix(finance): show the currency the install is configured for (issue #3123)
The Finance page was the only surface in Bambuddy that read its currency
from a data row rather than the `currency` setting, and it fell back to EUR
where every other page falls back to USD. One variable drives every amount
on that page, so the personal balance, the cost-center budgets and the whole
transaction list were wrong together on any install not set to euros. It now
takes the configured currency from /settings/ui-flags, which is readable by
anyone who can see Finance -- /settings needs SETTINGS_READ, which a
cost_centers:read_own user does not have.

The backend was the other half. Of the four places that settle on a
currency, three wrote a hardcoded "EUR": the wallet the API mints on demand,
the wallet a print charge mints when none exists, and the balance returned
for a user with no wallet row at all. All four now go through one resolver,
which lives beside the rest of the balance logic.

The wallet's currency column is removed outright rather than merely ignored.
An install has one currency and nothing here converts between them, so a
per-wallet copy could only ever drift from the setting -- and a column
nothing reads is a trap for whoever finds it next. A startup migration drops
it on both SQLite and PostgreSQL, after the raw CREATE TABLE that would
otherwise re-add it on an install whose finance tables predate the ORM.
SQLite builds older than 3.35 have no DROP COLUMN and keep it, harmlessly,
since it has a default and no reader.

Saving settings now invalidates the ui-flags query too. Nothing did, so a
changed currency sat behind that query's staleTime before showing up. The
sponsor prompt's own EUR fallback is now USD, matching AppSettings.
2026-09-20 10:06:47 +02:00

69 lines
2.1 KiB
Python

from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from backend.app.models.finance import CostCenter, CostCenterMember, UserWallet
from backend.app.models.user import User
async def ensure_user_finance_defaults(db: AsyncSession, user: User) -> bool:
"""Ensure wallet and private cost center defaults exist for a user.
Returns True when database objects were created or changed.
"""
changed = False
wallet = (await db.execute(select(UserWallet).where(UserWallet.user_id == user.id))).scalar_one_or_none()
if wallet is None:
db.add(UserWallet(user_id=user.id, balance=0.0))
changed = True
private_center = (
(
await db.execute(
select(CostCenter)
.where(
CostCenter.is_private.is_(True),
CostCenter.owner_user_id == user.id,
)
.order_by(CostCenter.id.asc())
)
)
.scalars()
.first()
)
if private_center is None:
private_center = CostCenter(
name=user.username,
is_active=True,
is_private=True,
owner_user_id=user.id,
)
db.add(private_center)
await db.flush()
changed = True
else:
# A private center is the billing fallback for its owner and therefore
# must remain active. A zero budget is the supported way to prevent
# printing from it.
if not private_center.is_active:
private_center.is_active = True
changed = True
if private_center.name != user.username:
private_center.name = user.username
changed = True
membership = (
await db.execute(
select(CostCenterMember).where(
CostCenterMember.cost_center_id == private_center.id,
CostCenterMember.user_id == user.id,
)
)
).scalar_one_or_none()
if membership is None:
db.add(CostCenterMember(cost_center_id=private_center.id, user_id=user.id, can_print=True))
changed = True
return changed