Subscriptions: «Оплачено?» → paid status → «repeat?»; edit and create everything
- ops: paying marks the current charge paid (paid_for, persisted) instead of jumping to the next date; repeat_sub moves to the next period or closes the subscription. Paid-and-undecided subscriptions leave reminders and show in agenda['paid']. update_sub can set paid; tasks reopen when their status goes back; delete for subs and routines (BroV only, /api/delete); /api/tasks?all=1 and /api/subs?all=1. - Bot: «₽ Оплачено?» on reminders, then «🔁 Да, <date> / Нет, закрыть»; after paying by text it asks the same with buttons. - BroV: «Оплачено?» everywhere; «Оплачено — повторить?» section under the notch; «Весь план»: forms to edit or create tasks (title, description, status, due, company), subscriptions (amount, currency, period, date, reminder, company, paid, active) and routines, delete with confirmation, «+» per list, done tasks on demand. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+51
-13
@@ -101,6 +101,8 @@ class Ops:
|
||||
self.con.execute(f"UPDATE tasks SET {', '.join(sets)} WHERE id = ?", (*vals, task_id))
|
||||
if fields.get("status") == "done":
|
||||
self._finish(task_id)
|
||||
elif fields.get("status") in ("todo", "doing"):
|
||||
self.con.execute("UPDATE tasks SET done_at = NULL WHERE id = ?", (task_id,))
|
||||
return self.task(task_id)
|
||||
|
||||
def complete_task(self, task_id: int) -> dict:
|
||||
@@ -188,11 +190,17 @@ class Ops:
|
||||
|
||||
# MARK: - Subscriptions and payments
|
||||
|
||||
def _sub_row(self, row) -> dict:
|
||||
s = dict(row)
|
||||
s["paid"] = s.get("paid_for") == s["next_date"]
|
||||
s["next_if_repeat"] = add_period(date.fromisoformat(s["next_date"]), s["period"], s["every"]).isoformat()
|
||||
return s
|
||||
|
||||
def sub(self, sub_id: int) -> dict:
|
||||
row = self.con.execute("SELECT * FROM subscriptions WHERE id = ?", (sub_id,)).fetchone()
|
||||
if not row:
|
||||
raise OpsError(f"подписки #{sub_id} нет")
|
||||
return dict(row)
|
||||
return self._sub_row(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 = "", client: str = "") -> dict:
|
||||
@@ -212,7 +220,8 @@ class Ops:
|
||||
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", "client"}
|
||||
allowed = {"name", "amount", "currency", "period", "every", "next_date", "remind_days", "notes", "active", "client",
|
||||
"paid"}
|
||||
sets, vals = [], []
|
||||
for k, v in fields.items():
|
||||
if k not in allowed or v is None:
|
||||
@@ -223,28 +232,54 @@ class Ops:
|
||||
raise OpsError(f"период может быть только {', '.join(PERIODS)}")
|
||||
if k == "client":
|
||||
v = str(v).strip().lower()
|
||||
if k == "paid":
|
||||
continue # applied below, against the (possibly new) date
|
||||
sets.append(f"{k} = ?")
|
||||
vals.append(v)
|
||||
if sets:
|
||||
self.sub(sub_id)
|
||||
self.con.execute(f"UPDATE subscriptions SET {', '.join(sets)} WHERE id = ?", (*vals, sub_id))
|
||||
if fields.get("paid") is not None: # status edited by hand: the current charge paid or not
|
||||
self.con.execute("UPDATE subscriptions SET paid_for = CASE WHEN ? THEN next_date END WHERE id = ?",
|
||||
(1 if fields["paid"] else 0, sub_id))
|
||||
return self.sub(sub_id)
|
||||
|
||||
def pay_sub(self, sub_id: int, amount: float | None = None, paid_on=None) -> dict:
|
||||
"""Records the payment and moves the next charge one period forward."""
|
||||
"""Marks the current charge paid (status «Оплачено») and records the payment. The
|
||||
next charge appears only after «repeat?» is answered (repeat_sub)."""
|
||||
s = self.sub(sub_id)
|
||||
if s["paid"]:
|
||||
return s
|
||||
day = parse_day(paid_on) or self.today()
|
||||
self.con.execute("INSERT INTO payments (sub_id, paid_on, amount) VALUES (?, ?, ?)",
|
||||
(sub_id, day.isoformat(), float(amount if amount is not None else s["amount"])))
|
||||
nxt = add_period(date.fromisoformat(s["next_date"]), s["period"], s["every"])
|
||||
while nxt <= day: # paid late or far ahead: the next charge is still in the future
|
||||
nxt = add_period(nxt, s["period"], s["every"])
|
||||
self.con.execute("UPDATE subscriptions SET next_date = ? WHERE id = ?", (nxt.isoformat(), sub_id))
|
||||
self.con.execute("UPDATE subscriptions SET paid_for = next_date WHERE id = ?", (sub_id,))
|
||||
return self.sub(sub_id)
|
||||
|
||||
def repeat_sub(self, sub_id: int, repeat: bool = True) -> dict:
|
||||
"""After a payment: repeat → the next charge one period later (back to «Оплачено?»),
|
||||
don't → the subscription is closed (kept, paid, inactive)."""
|
||||
s = self.sub(sub_id)
|
||||
if not repeat:
|
||||
self.con.execute("UPDATE subscriptions SET active = 0 WHERE id = ?", (sub_id,))
|
||||
return self.sub(sub_id)
|
||||
nxt = add_period(date.fromisoformat(s["next_date"]), s["period"], s["every"])
|
||||
while nxt < self.today(): # several periods behind: the next charge is still ahead
|
||||
nxt = add_period(nxt, s["period"], s["every"])
|
||||
self.con.execute("UPDATE subscriptions SET next_date = ?, active = 1 WHERE id = ?", (nxt.isoformat(), sub_id))
|
||||
return self.sub(sub_id)
|
||||
|
||||
def delete_sub(self, sub_id: int) -> None:
|
||||
self.sub(sub_id)
|
||||
self.con.execute("DELETE FROM subscriptions WHERE id = ?", (sub_id,))
|
||||
|
||||
def delete_routine(self, rid: int) -> None:
|
||||
self.routine(rid)
|
||||
self.con.execute("DELETE FROM routines WHERE id = ?", (rid,))
|
||||
|
||||
def subs(self, active_only: bool = True) -> list[dict]:
|
||||
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)]
|
||||
return [self._sub_row(r) for r in self.con.execute(q)]
|
||||
|
||||
PER_MONTH = {"day": 30.44, "week": 4.345, "month": 1, "year": 1 / 12}
|
||||
|
||||
@@ -324,7 +359,7 @@ class Ops:
|
||||
def bucket(d: date) -> str:
|
||||
return "overdue" if d < today else ("today" if d == today else "soon")
|
||||
|
||||
out = {"today": today.isoformat(), "overdue": [], "due_today": [], "soon": [],
|
||||
out = {"today": today.isoformat(), "overdue": [], "due_today": [], "soon": [], "paid": [],
|
||||
"running": None, "monthly_cost": self.monthly_cost()}
|
||||
key = {"overdue": "overdue", "today": "due_today", "soon": "soon"}
|
||||
for t in self.con.execute("SELECT * FROM tasks WHERE status != 'done' AND due IS NOT NULL AND due <= ?",
|
||||
@@ -334,9 +369,12 @@ class Ops:
|
||||
"client": t["client"]})
|
||||
for s in self.subs():
|
||||
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"], "client": s["client"]})
|
||||
item = {"kind": "sub", "id": s["id"], "title": s["name"], "date": s["next_date"], "amount": s["amount"],
|
||||
"currency": s["currency"], "client": s["client"], "paid": s["paid"], "next_if_repeat": s["next_if_repeat"]}
|
||||
if s["paid"]:
|
||||
out["paid"].append(item) # paid, waiting for «repeat?»
|
||||
elif d <= horizon:
|
||||
out[key[bucket(d)]].append(item)
|
||||
for r in self.routines():
|
||||
d = date.fromisoformat(r["next_date"])
|
||||
if d <= horizon:
|
||||
@@ -363,7 +401,7 @@ class Ops:
|
||||
out.append({"kind": "task", "id": t["id"], "title": t["title"], "date": t["due"]})
|
||||
for s in self.subs():
|
||||
d = date.fromisoformat(s["next_date"])
|
||||
if d - timedelta(days=s["remind_days"]) <= today and ("sub", s["id"]) not in sent:
|
||||
if not s["paid"] and 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"], "client": s["client"]})
|
||||
for r in self.routines():
|
||||
|
||||
Reference in New Issue
Block a user