Subscriptions belong to a company; BroV owns its Keychain token

- ops: subscriptions get a client (company) column (migrated in place); the brain fills it
  from «компания сага / для планеты»; /subs and «Весь план» group by company with
  per-company monthly totals; the company shows in agenda rows and the calendar.
- BroV: the API token handed over in ops-token.import is saved by BroV itself, so the
  Keychain item belongs to BroV and macOS stops asking for the password.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
maksarsanjeev
2026-10-10 03:42:41 +03:00
parent 36326ba0b6
commit 18aed2beb9
8 changed files with 161 additions and 55 deletions
+28 -9
View File
@@ -28,6 +28,13 @@ def add_period(d: date, period: str, every: int = 1) -> date:
raise OpsError(f"неизвестный период: {period}")
CLIENT_NAMES = {"saga": "Сага", "planet9": "Planet9", "home": "Дом"}
def client_name(code: str) -> str:
return CLIENT_NAMES.get(code, code) if code else ""
def ru_day(iso) -> str:
"""2026-11-15 → 15.11.2026 (how dates are shown; storage stays ISO)."""
if not iso:
@@ -188,7 +195,7 @@ class Ops:
return dict(row)
def add_sub(self, name: str, amount: float, next_date, period: str = "month", every: int = 1,
currency: str = "RUB", remind_days: int = 3, notes: str = "") -> dict:
currency: str = "RUB", remind_days: int = 3, notes: str = "", client: str = "") -> dict:
if not (name or "").strip():
raise OpsError("у подписки нет названия")
if period not in PERIODS:
@@ -197,14 +204,15 @@ class Ops:
if not d:
raise OpsError("нужна дата следующего списания")
cur = self.con.execute(
"""INSERT INTO subscriptions (name, amount, currency, period, every, next_date, remind_days, notes)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
"""INSERT INTO subscriptions (name, amount, currency, period, every, next_date, remind_days, notes, client)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)""",
(name.strip(), float(amount or 0), (currency or "RUB").upper(), period, max(1, int(every or 1)),
d.isoformat(), max(0, int(remind_days if remind_days is not None else 3)), notes or ""))
d.isoformat(), max(0, int(remind_days if remind_days is not None else 3)), notes or "",
(client or "").strip().lower()))
return self.sub(cur.lastrowid)
def update_sub(self, sub_id: int, **fields) -> dict:
allowed = {"name", "amount", "currency", "period", "every", "next_date", "remind_days", "notes", "active"}
allowed = {"name", "amount", "currency", "period", "every", "next_date", "remind_days", "notes", "active", "client"}
sets, vals = [], []
for k, v in fields.items():
if k not in allowed or v is None:
@@ -213,6 +221,8 @@ class Ops:
v = parse_day(v).isoformat()
if k == "period" and v not in PERIODS:
raise OpsError(f"период может быть только {', '.join(PERIODS)}")
if k == "client":
v = str(v).strip().lower()
sets.append(f"{k} = ?")
vals.append(v)
if sets:
@@ -236,14 +246,23 @@ class Ops:
q = "SELECT * FROM subscriptions" + (" WHERE active = 1" if active_only else "") + " ORDER BY next_date"
return [dict(r) for r in self.con.execute(q)]
PER_MONTH = {"day": 30.44, "week": 4.345, "month": 1, "year": 1 / 12}
def monthly_cost(self) -> dict[str, float]:
"""Active subscriptions per month, by currency."""
per_month = {"day": 30.44, "week": 4.345, "month": 1, "year": 1 / 12}
out: dict[str, float] = {}
for s in self.subs():
out[s["currency"]] = out.get(s["currency"], 0) + s["amount"] * per_month[s["period"]] / s["every"]
out[s["currency"]] = out.get(s["currency"], 0) + s["amount"] * self.PER_MONTH[s["period"]] / s["every"]
return {k: round(v, 2) for k, v in out.items()}
def monthly_by_client(self) -> dict[str, dict[str, float]]:
"""Per month by company, then by currency ("" = no company)."""
out: dict[str, dict[str, float]] = {}
for s in self.subs():
c = out.setdefault(s["client"], {})
c[s["currency"]] = c.get(s["currency"], 0) + s["amount"] * self.PER_MONTH[s["period"]] / s["every"]
return {k: {cur: round(v, 2) for cur, v in d.items()} for k, d in out.items()}
# MARK: - Routines
def routine(self, rid: int) -> dict:
@@ -317,7 +336,7 @@ class Ops:
d = date.fromisoformat(s["next_date"])
if d <= horizon:
out[key[bucket(d)]].append({"kind": "sub", "id": s["id"], "title": s["name"], "date": s["next_date"],
"amount": s["amount"], "currency": s["currency"]})
"amount": s["amount"], "currency": s["currency"], "client": s["client"]})
for r in self.routines():
d = date.fromisoformat(r["next_date"])
if d <= horizon:
@@ -346,7 +365,7 @@ class Ops:
d = date.fromisoformat(s["next_date"])
if d - timedelta(days=s["remind_days"]) <= today and ("sub", s["id"]) not in sent:
out.append({"kind": "sub", "id": s["id"], "title": s["name"], "date": s["next_date"],
"amount": s["amount"], "currency": s["currency"]})
"amount": s["amount"], "currency": s["currency"], "client": s["client"]})
for r in self.routines():
if date.fromisoformat(r["next_date"]) <= today and ("routine", r["id"]) not in sent:
out.append({"kind": "routine", "id": r["id"], "title": r["title"], "date": r["next_date"]})