748 lines
32 KiB
Python
748 lines
32 KiB
Python
import logging
|
||
import os
|
||
from contextlib import asynccontextmanager
|
||
from datetime import date, datetime, timedelta
|
||
from decimal import Decimal, InvalidOperation
|
||
from urllib.parse import quote
|
||
|
||
from apscheduler.schedulers.background import BackgroundScheduler
|
||
from dateutil.relativedelta import relativedelta
|
||
from fastapi import Depends, FastAPI, File, Form, HTTPException, Request, UploadFile
|
||
from fastapi.responses import JSONResponse, RedirectResponse
|
||
from fastapi.staticfiles import StaticFiles
|
||
from fastapi.templating import Jinja2Templates
|
||
from markupsafe import Markup
|
||
from sqlalchemy import func, or_, select
|
||
from sqlalchemy.orm import Session, selectinload
|
||
|
||
import enablebanking as eb
|
||
import prices
|
||
import recurring as rec_mod
|
||
from categorize import load_rules, rule_matches, seed, categorize, normalize
|
||
from db import SessionLocal, engine, get_db
|
||
from importers import parse
|
||
from models import (Account, Asset, Base, Category, Debt, DebtPayment, PortfolioSnapshot, Recurring, Rule, Trade,
|
||
Transaction)
|
||
from portfolio import holdings, totals
|
||
from services import category_totals, debt_status, ingest, match_debt_payments, monthly_totals, own_ibans
|
||
|
||
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s: %(message)s")
|
||
log = logging.getLogger("finance")
|
||
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||
|
||
|
||
# ------------------------------------------------------------------ background jobs
|
||
|
||
def _job(fn, name):
|
||
def run():
|
||
db = SessionLocal()
|
||
try:
|
||
fn(db)
|
||
except Exception: # noqa: BLE001
|
||
log.exception("job %s failed", name)
|
||
finally:
|
||
db.close()
|
||
return run
|
||
|
||
|
||
def _sync_and_detect(db):
|
||
if eb.configured():
|
||
for m in eb.sync(db):
|
||
log.info("bank sync: %s", m)
|
||
rec_mod.detect(db)
|
||
|
||
|
||
@asynccontextmanager
|
||
async def lifespan(app: FastAPI):
|
||
Base.metadata.create_all(engine)
|
||
with SessionLocal() as db:
|
||
seed(db)
|
||
sched = None
|
||
if os.getenv("DISABLE_SCHEDULER") != "1":
|
||
sched = BackgroundScheduler(timezone=os.getenv("TZ", "Europe/Amsterdam"))
|
||
sched.add_job(_job(prices.refresh_prices, "prices"), "interval",
|
||
minutes=int(os.getenv("PRICE_REFRESH_MINUTES", "60")), next_run_time=datetime.now())
|
||
sched.add_job(_job(_sync_and_detect, "sync"), "interval", hours=int(os.getenv("EB_SYNC_HOURS", "6")),
|
||
next_run_time=datetime.now() + timedelta(minutes=1))
|
||
sched.start()
|
||
yield
|
||
if sched:
|
||
sched.shutdown(wait=False)
|
||
|
||
|
||
app = FastAPI(title="Finance", lifespan=lifespan, docs_url=None, redoc_url=None)
|
||
app.mount("/static", StaticFiles(directory=os.path.join(BASE_DIR, "static")), name="static")
|
||
templates = Jinja2Templates(directory=os.path.join(BASE_DIR, "templates"))
|
||
|
||
|
||
def eur(v, decimals=2, sign=False):
|
||
if v is None:
|
||
return "–"
|
||
v = Decimal(v)
|
||
s = f"{abs(v):,.{decimals}f}".replace(",", "X").replace(".", ",").replace("X", ".")
|
||
prefix = "-" if v < 0 else ("+" if sign and v > 0 else "")
|
||
return f"{prefix}€ {s}"
|
||
|
||
|
||
def num(v, decimals=4):
|
||
if v is None:
|
||
return "–"
|
||
s = f"{Decimal(v):,.{decimals}f}".rstrip("0").rstrip(".") if decimals else f"{Decimal(v):,.0f}"
|
||
return s.replace(",", "X").replace(".", ",").replace("X", ".")
|
||
|
||
|
||
def pct(v):
|
||
return "–" if v is None else f"{v:+.1f}%".replace(".", ",")
|
||
|
||
|
||
def icon(name: str, cls: str = "") -> Markup:
|
||
return Markup(f'<svg class="ico {cls}" aria-hidden="true"><use href="#i-{name}"></use></svg>')
|
||
|
||
|
||
templates.env.filters.update(eur=eur, num=num, pct=pct)
|
||
templates.env.globals.update(icon=icon)
|
||
templates.env.globals.update(today=date.today, rec_state=rec_mod.state, monthly_eq=rec_mod.monthly_equivalent)
|
||
|
||
|
||
def render(request: Request, name: str, **ctx):
|
||
ctx.setdefault("msg", request.query_params.get("msg"))
|
||
ctx.setdefault("user", request.headers.get("X-authentik-name") or request.headers.get("X-authentik-username"))
|
||
with SessionLocal() as db: # badge counter for the nav
|
||
recs = db.scalars(select(Recurring).where(Recurring.status != "ignored"))
|
||
ctx.setdefault("nav_missed", sum(1 for r in recs if rec_mod.state(r) == "missed"))
|
||
return templates.TemplateResponse(request, name, ctx)
|
||
|
||
|
||
def back(url: str, msg: str | None = None):
|
||
if msg:
|
||
url += ("&" if "?" in url else "?") + "msg=" + quote(msg)
|
||
return RedirectResponse(url, status_code=303)
|
||
|
||
|
||
def dec(s: str | None, default=None) -> Decimal | None:
|
||
if s is None or str(s).strip() == "":
|
||
return default
|
||
try:
|
||
return Decimal(str(s).strip().replace(" ", "").replace(",", "."))
|
||
except InvalidOperation:
|
||
raise HTTPException(400, f"Not a number: {s!r}")
|
||
|
||
|
||
def d(s: str | None) -> date:
|
||
return date.fromisoformat(s) if s else date.today()
|
||
|
||
|
||
def get_or_404(db: Session, model, id_):
|
||
obj = db.get(model, id_)
|
||
if obj is None:
|
||
raise HTTPException(404, f"{model.__name__} {id_} not found")
|
||
return obj
|
||
|
||
|
||
def f(v) -> float:
|
||
return float(v or 0)
|
||
|
||
|
||
# ------------------------------------------------------------------ dashboard
|
||
|
||
@app.get("/")
|
||
def dashboard(request: Request, account: int | None = None, month: str | None = None, db: Session = Depends(get_db)):
|
||
today = date.today()
|
||
start = date.fromisoformat(month + "-01") if month else today.replace(day=1)
|
||
end = start + relativedelta(months=1)
|
||
accounts = list(db.scalars(select(Account).order_by(Account.name)))
|
||
|
||
cats = category_totals(db, start, end, account)
|
||
months = monthly_totals(db, 12, account)
|
||
cur = next((m for m in months if m["month"] == start.strftime("%Y-%m")), {"income": 0, "expense": 0})
|
||
|
||
rows = holdings(db)
|
||
pt = totals(rows)
|
||
debts = list(db.scalars(select(Debt).options(selectinload(Debt.payments)).where(Debt.closed.is_(False))))
|
||
dstat = [(x, debt_status(x)) for x in debts]
|
||
i_owe = sum((s["remaining"] for x, s in dstat if x.direction == "i_owe"), Decimal(0))
|
||
owed = sum((s["remaining"] for x, s in dstat if x.direction == "owed_to_me"), Decimal(0))
|
||
cash = sum((a.balance * a.share_pct / 100 for a in accounts if a.balance is not None), Decimal(0))
|
||
invest_value = pt.get("all", {}).get("value", Decimal(0))
|
||
|
||
recs = list(db.scalars(select(Recurring).where(Recurring.status != "ignored")))
|
||
missed = [r for r in recs if rec_mod.state(r) == "missed"]
|
||
due = sorted([r for r in recs if rec_mod.state(r) == "due-soon"], key=lambda r: r.next_date)
|
||
subs_month = sum((rec_mod.monthly_equivalent(r) for r in recs if r.amount < 0), Decimal(0))
|
||
uncategorised = db.scalar(select(func.count(Transaction.id)).where(Transaction.category_id.is_(None))) or 0
|
||
|
||
snaps = list(db.scalars(select(PortfolioSnapshot).where(PortfolioSnapshot.date >= today - timedelta(days=365))
|
||
.order_by(PortfolioSnapshot.date)))
|
||
hist: dict[str, dict] = {}
|
||
for s in snaps:
|
||
h = hist.setdefault(s.date.isoformat(), {"value": 0.0, "cost": 0.0})
|
||
h["value"] += f(s.value)
|
||
h["cost"] += f(s.cost)
|
||
|
||
charts = {
|
||
"months": {"labels": [m["month"] for m in months], "income": [f(m["income"]) for m in months],
|
||
"expense": [f(m["expense"]) for m in months]},
|
||
"cats": {"labels": [c["name"] for c in cats], "data": [f(c["total"]) for c in cats],
|
||
"colors": [c["color"] for c in cats],
|
||
"links": [f"/transactions?month={start:%Y-%m}&category={c['id'] or 'none'}"
|
||
+ (f"&account={account}" if account else "") for c in cats]},
|
||
"portfolio": {"labels": list(hist), "value": [h["value"] for h in hist.values()],
|
||
"cost": [h["cost"] for h in hist.values()]},
|
||
}
|
||
return render(request, "dashboard.html", accounts=accounts, account=account, start=start, cats=cats, cur=cur,
|
||
pt=pt, i_owe=i_owe, owed=owed, cash=cash, invest_value=invest_value,
|
||
net_worth=cash + invest_value + owed - i_owe, missed=missed, due=due, subs_month=subs_month,
|
||
dstat=dstat, uncategorised=uncategorised, charts=charts,
|
||
prev_month=(start - relativedelta(months=1)).strftime("%Y-%m"),
|
||
next_month=end.strftime("%Y-%m") if end <= today else None)
|
||
|
||
|
||
# ------------------------------------------------------------------ transactions
|
||
|
||
PAGE = 100
|
||
|
||
|
||
@app.get("/transactions")
|
||
def transactions(request: Request, account: str = "", category: str = "", month: str = "", q: str = "",
|
||
page: int = 1, db: Session = Depends(get_db)):
|
||
query = select(Transaction).options(selectinload(Transaction.account), selectinload(Transaction.category))
|
||
if account:
|
||
query = query.where(Transaction.account_id == int(account))
|
||
if category == "none":
|
||
query = query.where(Transaction.category_id.is_(None))
|
||
elif category:
|
||
query = query.where(Transaction.category_id == int(category))
|
||
if month:
|
||
s = date.fromisoformat(month + "-01")
|
||
query = query.where(Transaction.date >= s, Transaction.date < s + relativedelta(months=1))
|
||
if q:
|
||
like = f"%{q}%"
|
||
query = query.where(or_(Transaction.counterparty.ilike(like), Transaction.description.ilike(like),
|
||
Transaction.notes.ilike(like)))
|
||
total = db.scalar(select(func.count()).select_from(query.subquery()))
|
||
total_amount = db.scalar(select(func.sum(query.subquery().c.amount))) or 0
|
||
txs = list(db.scalars(query.order_by(Transaction.date.desc(), Transaction.id.desc())
|
||
.offset((page - 1) * PAGE).limit(PAGE)))
|
||
return render(request, "transactions.html", txs=txs, total=total, total_amount=total_amount, page=page,
|
||
pages=max(1, -(-total // PAGE)),
|
||
accounts=list(db.scalars(select(Account).order_by(Account.name))),
|
||
categories=list(db.scalars(select(Category).order_by(Category.name))),
|
||
f={"account": account, "category": category, "month": month, "q": q})
|
||
|
||
|
||
@app.post("/transactions/{tx_id}/category")
|
||
def set_category(tx_id: int, category_id: str = Form(""), db: Session = Depends(get_db)):
|
||
tx = get_or_404(db, Transaction, tx_id)
|
||
tx.category_id = int(category_id) if category_id else None
|
||
tx.category_locked = bool(category_id)
|
||
db.commit()
|
||
return JSONResponse({"ok": True})
|
||
|
||
|
||
@app.post("/transactions/{tx_id}/notes")
|
||
def set_notes(tx_id: int, notes: str = Form(""), db: Session = Depends(get_db)):
|
||
tx = get_or_404(db, Transaction, tx_id)
|
||
tx.notes = notes
|
||
db.commit()
|
||
return JSONResponse({"ok": True})
|
||
|
||
|
||
# ------------------------------------------------------------------ import
|
||
|
||
@app.get("/import")
|
||
def import_page(request: Request, db: Session = Depends(get_db)):
|
||
return render(request, "import.html", accounts=list(db.scalars(select(Account).order_by(Account.name))),
|
||
results=None)
|
||
|
||
|
||
@app.post("/import")
|
||
async def import_upload(request: Request, files: list[UploadFile] = File(...), account_id: str = Form("auto"),
|
||
db: Session = Depends(get_db)):
|
||
results = []
|
||
for up in files:
|
||
name = up.filename or "file"
|
||
try:
|
||
res = parse(await up.read())
|
||
if account_id != "auto":
|
||
acc = db.get(Account, int(account_id))
|
||
elif res.own_iban:
|
||
iban = res.own_iban.replace(" ", "").upper()
|
||
acc = db.scalar(select(Account).where(Account.iban == iban))
|
||
if acc is None:
|
||
acc = Account(name=f"ING {iban[-4:]}", bank="ing", iban=iban)
|
||
db.add(acc)
|
||
db.flush()
|
||
else:
|
||
acc = db.scalar(select(Account).where(Account.bank == res.fmt).order_by(Account.id))
|
||
if acc is None:
|
||
acc = Account(name="Revolut" if res.fmt == "revolut" else name, bank=res.fmt)
|
||
db.add(acc)
|
||
db.flush()
|
||
r = ingest(db, acc, res.txs, "csv", res.balance, res.balance_date)
|
||
results.append({"file": name, "ok": True, "account": acc.name, "fmt": res.fmt, "skipped": res.skipped, **r})
|
||
except Exception as e: # noqa: BLE001
|
||
db.rollback()
|
||
results.append({"file": name, "ok": False, "error": str(e)})
|
||
try:
|
||
rec_mod.detect(db)
|
||
except Exception: # noqa: BLE001
|
||
log.exception("recurring detection failed")
|
||
return render(request, "import.html", accounts=list(db.scalars(select(Account).order_by(Account.name))),
|
||
results=results)
|
||
|
||
|
||
# ------------------------------------------------------------------ recurring
|
||
|
||
@app.get("/recurring")
|
||
def recurring_page(request: Request, show: str = "active", db: Session = Depends(get_db)):
|
||
q = select(Recurring).options(selectinload(Recurring.account), selectinload(Recurring.category))
|
||
if show == "active":
|
||
q = q.where(Recurring.status != "ignored")
|
||
elif show == "ignored":
|
||
q = q.where(Recurring.status == "ignored")
|
||
recs = list(db.scalars(q))
|
||
order = {"missed": 0, "due-soon": 1, "ok": 2, "ignored": 3}
|
||
recs.sort(key=lambda r: (order[rec_mod.state(r)], r.next_date or date.max))
|
||
out_total = sum((rec_mod.monthly_equivalent(r) for r in recs if r.amount < 0 and r.status != "ignored"), Decimal(0))
|
||
in_total = sum((rec_mod.monthly_equivalent(r) for r in recs if r.amount > 0 and r.status != "ignored"), Decimal(0))
|
||
return render(request, "recurring.html", recs=recs, show=show, out_total=out_total, in_total=in_total,
|
||
accounts=list(db.scalars(select(Account))),
|
||
categories=list(db.scalars(select(Category).order_by(Category.name))))
|
||
|
||
|
||
@app.post("/recurring/detect")
|
||
def recurring_detect(db: Session = Depends(get_db)):
|
||
n = rec_mod.detect(db)
|
||
return back("/recurring", f"Detection finished: {n} new recurring payment(s) found.")
|
||
|
||
|
||
@app.post("/recurring/add")
|
||
def recurring_add(name: str = Form(...), match: str = Form(...), amount: str = Form(...),
|
||
frequency: str = Form("monthly"), next_date: str = Form(...), direction: str = Form("out"),
|
||
db: Session = Depends(get_db)):
|
||
amt = abs(dec(amount)) * (-1 if direction == "out" else 1)
|
||
r = Recurring(name=name, match=normalize(match) or match.lower(), amount=amt, frequency=frequency,
|
||
next_date=d(next_date), source="manual", status="confirmed")
|
||
db.add(r)
|
||
db.flush()
|
||
rec_mod.refresh(db)
|
||
db.commit()
|
||
return back("/recurring", f"Added {name}.")
|
||
|
||
|
||
@app.post("/recurring/{rid}/update")
|
||
def recurring_update(rid: int, status: str = Form(None), name: str = Form(None), frequency: str = Form(None),
|
||
amount: str = Form(None), db: Session = Depends(get_db)):
|
||
r = db.get(Recurring, rid)
|
||
if status:
|
||
r.status = status
|
||
if name:
|
||
r.name = name
|
||
if frequency:
|
||
r.frequency = frequency
|
||
if r.last_date:
|
||
r.next_date = r.last_date + rec_mod.step(frequency)
|
||
if amount:
|
||
r.amount = -abs(dec(amount)) if r.amount < 0 else abs(dec(amount))
|
||
if frequency or amount:
|
||
r.source = "manual" # stop auto-detection from overwriting your edits
|
||
db.commit()
|
||
return back("/recurring")
|
||
|
||
|
||
@app.post("/recurring/{rid}/delete")
|
||
def recurring_delete(rid: int, db: Session = Depends(get_db)):
|
||
db.delete(db.get(Recurring, rid))
|
||
db.commit()
|
||
return back("/recurring", "Removed. (Detected items may come back on the next scan; use Ignore to hide for good.)")
|
||
|
||
|
||
# ------------------------------------------------------------------ investments & gold
|
||
|
||
def _portfolio(request, db, kinds: list[str], template: str, title: str):
|
||
rows = [r for r in holdings(db) if r["asset"].kind in kinds]
|
||
t = totals(rows)
|
||
snaps = list(db.scalars(select(PortfolioSnapshot).where(PortfolioSnapshot.kind.in_(kinds))
|
||
.order_by(PortfolioSnapshot.date)))
|
||
hist: dict[str, dict] = {}
|
||
for s in snaps:
|
||
h = hist.setdefault(s.date.isoformat(), {"value": 0.0, "cost": 0.0})
|
||
h["value"] += f(s.value)
|
||
h["cost"] += f(s.cost)
|
||
chart = {"labels": list(hist), "value": [h["value"] for h in hist.values()],
|
||
"cost": [h["cost"] for h in hist.values()],
|
||
"alloc_labels": [r["asset"].name for r in rows if r["value"]],
|
||
"alloc_values": [f(r["value"]) for r in rows if r["value"]]}
|
||
assets = list(db.scalars(select(Asset).where(Asset.kind.in_(kinds)).order_by(Asset.name)))
|
||
trades = list(db.scalars(select(Trade).join(Asset).where(Asset.kind.in_(kinds))
|
||
.options(selectinload(Trade.asset)).order_by(Trade.date.desc())))
|
||
return render(request, template, rows=rows, t=t, kinds=kinds, chart=chart, assets=assets, trades=trades,
|
||
title=title)
|
||
|
||
|
||
@app.get("/investments")
|
||
def investments(request: Request, db: Session = Depends(get_db)):
|
||
return _portfolio(request, db, ["stock", "crypto"], "investments.html", "Investments")
|
||
|
||
|
||
@app.get("/gold")
|
||
def gold(request: Request, db: Session = Depends(get_db)):
|
||
return _portfolio(request, db, ["gold"], "investments.html", "Gold")
|
||
|
||
|
||
@app.post("/assets/add")
|
||
def asset_add(request: Request, name: str = Form(...), kind: str = Form(...), symbol: str = Form(""),
|
||
unit: str = Form("unit"), purity: str = Form("1"), db: Session = Depends(get_db)):
|
||
if kind == "gold":
|
||
symbol = symbol or "GC=F"
|
||
unit = unit if unit in ("gram", "oz") else "gram"
|
||
a = Asset(name=name, kind=kind, symbol=symbol.strip(), unit=unit, purity=dec(purity, Decimal(1)))
|
||
db.add(a)
|
||
db.commit()
|
||
try:
|
||
prices.refresh_prices(db)
|
||
except Exception: # noqa: BLE001
|
||
log.exception("price refresh failed")
|
||
return back("/gold" if kind == "gold" else "/investments", f"Added {name}. Now add your purchases.")
|
||
|
||
|
||
@app.post("/assets/{aid}/update")
|
||
def asset_update(aid: int, manual_price: str = Form(""), symbol: str = Form(None), db: Session = Depends(get_db)):
|
||
a = db.get(Asset, aid)
|
||
a.manual_price = dec(manual_price)
|
||
if a.manual_price is not None:
|
||
a.price_error = None
|
||
if symbol:
|
||
a.symbol = symbol.strip()
|
||
db.commit()
|
||
prices.snapshot(db)
|
||
return back("/gold" if a.kind == "gold" else "/investments", "Saved.")
|
||
|
||
|
||
@app.post("/assets/{aid}/delete")
|
||
def asset_delete(aid: int, db: Session = Depends(get_db)):
|
||
a = db.get(Asset, aid)
|
||
kind = a.kind
|
||
db.delete(a)
|
||
db.commit()
|
||
return back("/gold" if kind == "gold" else "/investments", "Deleted.")
|
||
|
||
|
||
@app.post("/trades/add")
|
||
def trade_add(asset_id: int = Form(...), side: str = Form("buy"), date_: str = Form(..., alias="date"),
|
||
quantity: str = Form(...), price: str = Form(""), total: str = Form(""), fees: str = Form("0"),
|
||
notes: str = Form(""), db: Session = Depends(get_db)):
|
||
a = db.get(Asset, asset_id)
|
||
qty = dec(quantity)
|
||
p = dec(price)
|
||
if p is None:
|
||
tot = dec(total)
|
||
if tot is None or not qty:
|
||
raise HTTPException(400, "Give either a price per unit or a total amount.")
|
||
p = tot / qty
|
||
db.add(Trade(asset_id=a.id, side=side, date=d(date_), quantity=qty, price=p, fees=dec(fees, Decimal(0)),
|
||
notes=notes))
|
||
db.commit()
|
||
prices.snapshot(db)
|
||
return back("/gold" if a.kind == "gold" else "/investments", "Saved.")
|
||
|
||
|
||
@app.post("/trades/{tid}/delete")
|
||
def trade_delete(tid: int, db: Session = Depends(get_db)):
|
||
t = db.get(Trade, tid)
|
||
kind = t.asset.kind
|
||
db.delete(t)
|
||
db.commit()
|
||
return back("/gold" if kind == "gold" else "/investments", "Deleted.")
|
||
|
||
|
||
@app.post("/prices/refresh")
|
||
def prices_refresh(request: Request, db: Session = Depends(get_db)):
|
||
errors = prices.refresh_prices(db)
|
||
ref = request.headers.get("referer") or "/investments"
|
||
path = "/" + ref.split("/", 3)[3].split("?")[0] if ref.count("/") >= 3 else "/investments"
|
||
return back(path, "Prices updated." if not errors else "Some prices failed: " + "; ".join(errors)[:400])
|
||
|
||
|
||
# ------------------------------------------------------------------ debts
|
||
|
||
@app.get("/debts")
|
||
def debts_page(request: Request, show_closed: int = 0, db: Session = Depends(get_db)):
|
||
q = select(Debt).options(selectinload(Debt.payments)).order_by(Debt.name)
|
||
if not show_closed:
|
||
q = q.where(Debt.closed.is_(False))
|
||
debts = [(x, debt_status(x)) for x in db.scalars(q)]
|
||
sums = {k: sum((s["remaining"] for x, s in debts if x.direction == k), Decimal(0)) for k in ("i_owe", "owed_to_me")}
|
||
return render(request, "debts.html", debts=debts, sums=sums, show_closed=show_closed)
|
||
|
||
|
||
@app.get("/debts/{did}")
|
||
def debt_detail(request: Request, did: int, db: Session = Depends(get_db)):
|
||
x = get_or_404(db, Debt, did)
|
||
s = debt_status(x)
|
||
running, bal = [], x.principal
|
||
for p in x.payments:
|
||
bal -= p.amount
|
||
running.append((p, bal))
|
||
return render(request, "debt_detail.html", d=x, s=s, running=list(reversed(running)))
|
||
|
||
|
||
def _debt_fields(name, counterparty, direction, principal, start_date, interest_rate, monthly_payment, due_day,
|
||
match_pattern, notes):
|
||
return dict(name=name, counterparty=counterparty, direction=direction, principal=dec(principal),
|
||
start_date=d(start_date), interest_rate=dec(interest_rate), monthly_payment=dec(monthly_payment),
|
||
due_day=int(due_day) if due_day else None, match_pattern=(match_pattern or "").strip() or None,
|
||
notes=notes)
|
||
|
||
|
||
@app.post("/debts/save")
|
||
def debt_save(id: str = Form(""), name: str = Form(...), counterparty: str = Form(""), direction: str = Form(...),
|
||
principal: str = Form(...), start_date: str = Form(""), interest_rate: str = Form(""),
|
||
monthly_payment: str = Form(""), due_day: str = Form(""), match_pattern: str = Form(""),
|
||
notes: str = Form(""), db: Session = Depends(get_db)):
|
||
fields = _debt_fields(name, counterparty, direction, principal, start_date, interest_rate, monthly_payment,
|
||
due_day, match_pattern, notes)
|
||
x = db.get(Debt, int(id)) if id else Debt()
|
||
for k, v in fields.items():
|
||
setattr(x, k, v)
|
||
db.add(x)
|
||
db.commit()
|
||
n = _rematch(db, x)
|
||
return back(f"/debts/{x.id}", f"Saved. {n} payment(s) matched from bank transactions." if n else "Saved.")
|
||
|
||
|
||
def _rematch(db: Session, x: Debt) -> int:
|
||
if not x.match_pattern:
|
||
return 0
|
||
txs = list(db.scalars(select(Transaction).where(Transaction.date >= x.start_date)))
|
||
n = match_debt_payments(db, txs)
|
||
db.commit()
|
||
return n
|
||
|
||
|
||
@app.post("/debts/{did}/rematch")
|
||
def debt_rematch(did: int, db: Session = Depends(get_db)):
|
||
n = _rematch(db, db.get(Debt, did))
|
||
return back(f"/debts/{did}", f"{n} new payment(s) matched.")
|
||
|
||
|
||
@app.post("/debts/{did}/payment")
|
||
def debt_payment(did: int, date_: str = Form("", alias="date"), amount: str = Form(...), notes: str = Form(""),
|
||
db: Session = Depends(get_db)):
|
||
db.add(DebtPayment(debt_id=did, date=d(date_), amount=abs(dec(amount)), notes=notes))
|
||
db.commit()
|
||
return back(f"/debts/{did}", "Payment added.")
|
||
|
||
|
||
@app.post("/debts/{did}/toggle")
|
||
def debt_toggle(did: int, db: Session = Depends(get_db)):
|
||
x = db.get(Debt, did)
|
||
x.closed = not x.closed
|
||
db.commit()
|
||
return back(f"/debts/{did}")
|
||
|
||
|
||
@app.post("/debts/{did}/delete")
|
||
def debt_delete(did: int, db: Session = Depends(get_db)):
|
||
db.delete(db.get(Debt, did))
|
||
db.commit()
|
||
return back("/debts", "Deleted.")
|
||
|
||
|
||
@app.post("/payments/{pid}/delete")
|
||
def payment_delete(pid: int, db: Session = Depends(get_db)):
|
||
p = db.get(DebtPayment, pid)
|
||
did = p.debt_id
|
||
if p.transaction_id:
|
||
tx = db.get(Transaction, p.transaction_id)
|
||
if tx:
|
||
tx.debt_id = None
|
||
db.delete(p)
|
||
db.commit()
|
||
return back(f"/debts/{did}", "Payment removed.")
|
||
|
||
|
||
# ------------------------------------------------------------------ settings: accounts, categories, rules, bank sync
|
||
|
||
@app.get("/settings")
|
||
def settings_page(request: Request, db: Session = Depends(get_db)):
|
||
from models import Setting
|
||
eb_valid = {s.key.split(":", 1)[1]: s.value for s in db.scalars(select(Setting).where(Setting.key.like("eb_valid_until:%")))}
|
||
rules = list(db.scalars(select(Rule).options(selectinload(Rule.category)).order_by(Rule.priority, Rule.id)))
|
||
counts = dict(db.execute(select(Transaction.category_id, func.count()).group_by(Transaction.category_id)).all())
|
||
return render(request, "settings.html", accounts=list(db.scalars(select(Account).order_by(Account.name))),
|
||
categories=list(db.scalars(select(Category).order_by(Category.kind, Category.name))),
|
||
rules=rules, counts=counts, eb_configured=eb.configured(), eb_valid=eb_valid,
|
||
prefill=request.query_params)
|
||
|
||
|
||
@app.post("/accounts/save")
|
||
def account_save(id: str = Form(""), name: str = Form(...), bank: str = Form("ing"), iban: str = Form(""),
|
||
share_pct: int = Form(100), db: Session = Depends(get_db)):
|
||
a = db.get(Account, int(id)) if id else Account()
|
||
a.name, a.bank, a.share_pct = name, bank, max(0, min(100, share_pct))
|
||
a.iban = iban.replace(" ", "").upper() or None
|
||
db.add(a)
|
||
db.commit()
|
||
return back("/settings", "Account saved.")
|
||
|
||
|
||
@app.post("/accounts/{aid}/delete")
|
||
def account_delete(aid: int, db: Session = Depends(get_db)):
|
||
a = db.get(Account, aid)
|
||
for tx in db.scalars(select(Transaction).where(Transaction.account_id == aid)):
|
||
db.delete(tx)
|
||
db.delete(a)
|
||
db.commit()
|
||
return back("/settings", "Account and its transactions deleted.")
|
||
|
||
|
||
@app.post("/categories/save")
|
||
def category_save(id: str = Form(""), name: str = Form(...), kind: str = Form("expense"), color: str = Form("#888888"),
|
||
db: Session = Depends(get_db)):
|
||
c = db.get(Category, int(id)) if id else Category()
|
||
c.name, c.kind, c.color = name, kind, color
|
||
db.add(c)
|
||
db.commit()
|
||
return back("/settings#categories", "Category saved.")
|
||
|
||
|
||
@app.post("/categories/{cid}/delete")
|
||
def category_delete(cid: int, db: Session = Depends(get_db)):
|
||
db.delete(db.get(Category, cid))
|
||
db.commit()
|
||
return back("/settings#categories", "Category deleted.")
|
||
|
||
|
||
@app.post("/rules/add")
|
||
def rule_add(pattern: str = Form(...), field: str = Form("any"), category_id: int = Form(...),
|
||
priority: int = Form(10), apply: str = Form("1"), db: Session = Depends(get_db)):
|
||
r = Rule(pattern=pattern.strip(), field=field, category_id=category_id, priority=priority)
|
||
db.add(r)
|
||
db.flush()
|
||
n = 0
|
||
if apply:
|
||
for tx in db.scalars(select(Transaction).where(Transaction.category_locked.is_(False))):
|
||
if rule_matches(r, tx):
|
||
tx.category_id = category_id
|
||
n += 1
|
||
db.commit()
|
||
return back("/settings#rules", f"Rule added and applied to {n} transaction(s).")
|
||
|
||
|
||
@app.post("/rules/{rid}/delete")
|
||
def rule_delete(rid: int, db: Session = Depends(get_db)):
|
||
db.delete(db.get(Rule, rid))
|
||
db.commit()
|
||
return back("/settings#rules", "Rule deleted.")
|
||
|
||
|
||
@app.post("/rules/apply")
|
||
def rules_apply(only_uncategorised: str = Form(""), db: Session = Depends(get_db)):
|
||
q = select(Transaction).where(Transaction.category_locked.is_(False))
|
||
if only_uncategorised:
|
||
q = q.where(Transaction.category_id.is_(None))
|
||
txs = list(db.scalars(q))
|
||
if not only_uncategorised:
|
||
for tx in txs:
|
||
tx.category_id = None
|
||
n = categorize(db, txs, own_ibans(db), load_rules(db))
|
||
db.commit()
|
||
return back("/settings#rules", f"Re-categorised: {n} of {len(txs)} transactions matched a rule.")
|
||
|
||
|
||
@app.post("/eb/connect")
|
||
def eb_connect(request: Request, aspsp: str = Form(...), country: str = Form("NL"), db: Session = Depends(get_db)):
|
||
base = os.getenv("PUBLIC_URL") or str(request.base_url).rstrip("/")
|
||
try:
|
||
url = eb.start_auth(db, aspsp, country, f"{base}/eb/callback")
|
||
except Exception as e: # noqa: BLE001
|
||
return back("/settings#bank", f"Could not start: {e}")
|
||
return RedirectResponse(url, status_code=303)
|
||
|
||
|
||
@app.get("/eb/callback")
|
||
def eb_callback(code: str = "", state: str = "", error: str = "", error_description: str = "",
|
||
db: Session = Depends(get_db)):
|
||
if error:
|
||
return back("/settings#bank", f"Bank returned an error: {error} {error_description}")
|
||
try:
|
||
linked = eb.finish_auth(db, code, state)
|
||
msgs = eb.sync(db)
|
||
rec_mod.detect(db)
|
||
except Exception as e: # noqa: BLE001
|
||
return back("/settings#bank", f"Linking failed: {e}")
|
||
return back("/settings#bank", f"Linked {len(linked)} account(s). " + " | ".join(msgs))
|
||
|
||
|
||
@app.post("/eb/sync")
|
||
def eb_sync(db: Session = Depends(get_db)):
|
||
msgs = eb.sync(db)
|
||
rec_mod.detect(db)
|
||
return back("/settings#bank", " | ".join(msgs) or "Nothing to sync.")
|
||
|
||
|
||
@app.get("/eb/banks")
|
||
def eb_banks(country: str = "NL"):
|
||
try:
|
||
return JSONResponse([a["name"] for a in eb.aspsps(country)])
|
||
except Exception as e: # noqa: BLE001
|
||
return JSONResponse({"error": str(e)}, status_code=502)
|
||
|
||
|
||
# ------------------------------------------------------------------ live numbers (polled by app.js)
|
||
|
||
@app.get("/api/live")
|
||
def live(account: int | None = None, month: str | None = None, db: Session = Depends(get_db)):
|
||
"""Every number the pages can update in place, keyed by the data-live attribute."""
|
||
today = date.today()
|
||
start = date.fromisoformat(month + "-01") if month else today.replace(day=1)
|
||
months = monthly_totals(db, 12, account)
|
||
cur = next((m for m in months if m["month"] == start.strftime("%Y-%m")), None)
|
||
if cur is None: # month outside the 12-month window
|
||
cur = monthly_totals(db, (today.year - start.year) * 12 + today.month - start.month + 1, account)[0]
|
||
rows = holdings(db)
|
||
pt = totals(rows)
|
||
accounts = list(db.scalars(select(Account)))
|
||
debts = [(x, debt_status(x)) for x in db.scalars(select(Debt).options(selectinload(Debt.payments))
|
||
.where(Debt.closed.is_(False)))]
|
||
i_owe = sum((s["remaining"] for x, s in debts if x.direction == "i_owe"), Decimal(0))
|
||
owed = sum((s["remaining"] for x, s in debts if x.direction == "owed_to_me"), Decimal(0))
|
||
cash = sum((a.balance * a.share_pct / 100 for a in accounts if a.balance is not None), Decimal(0))
|
||
v = {"cash": cash, "i_owe": i_owe, "owed": owed, "debt_net": owed - i_owe,
|
||
"spent": cur["expense"], "income": cur["income"], "net_month": cur["income"] - cur["expense"]}
|
||
for kind in ("all", "stock", "crypto", "gold"):
|
||
t = pt.get(kind) or {"value": 0, "cost": 0, "pnl": 0, "pnl_pct": None, "realized": 0}
|
||
for k in ("value", "cost", "pnl", "pnl_pct", "realized"):
|
||
v[f"{kind}_{k}"] = t[k]
|
||
for k in ("value", "cost", "pnl", "realized"):
|
||
v[f"inv_{k}"] = Decimal(str(v[f"stock_{k}"])) + Decimal(str(v[f"crypto_{k}"]))
|
||
v["inv_pnl_pct"] = float(v["inv_pnl"] / v["inv_cost"] * 100) if v["inv_cost"] else None
|
||
v["net_worth"] = cash + v["all_value"] + owed - i_owe
|
||
for r in rows:
|
||
a = r["asset"].id
|
||
v.update({f"a{a}_price": r["price"], f"a{a}_value": r["value"], f"a{a}_pnl": r["pnl"],
|
||
f"a{a}_pnl_pct": r["pnl_pct"]})
|
||
for a in accounts:
|
||
v[f"acc{a.id}_balance"] = a.balance
|
||
for x, s in debts:
|
||
v[f"d{x.id}_remaining"] = s["remaining"]
|
||
v[f"d{x.id}_paid"] = s["paid"]
|
||
recs = [r for r in db.scalars(select(Recurring)) if rec_mod.state(r) == "missed"]
|
||
v["missed_count"] = len(recs)
|
||
newest = db.scalar(select(func.max(Transaction.id))) or 0
|
||
priced = db.scalar(select(func.max(Asset.last_price_at)))
|
||
return JSONResponse({"values": {k: (None if x is None else float(x)) for k, x in v.items()},
|
||
"version": f"{newest}:{priced}", "at": datetime.now().strftime("%H:%M:%S")})
|
||
|
||
|
||
@app.post("/api/prices/refresh")
|
||
def api_prices_refresh(db: Session = Depends(get_db)):
|
||
errors = prices.refresh_prices(db)
|
||
return JSONResponse({"ok": not errors, "errors": errors})
|
||
|
||
|
||
@app.get("/healthz")
|
||
def healthz():
|
||
return {"ok": True}
|