diff --git a/main.py b/main.py index ec24bf4..3b6e2c3 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,4 @@ -# SplitBuddy — Flask app with ratio-based splits (You vs Idan) +# SplitBuddy — Flask app with bills/transfer + payer toggle from __future__ import annotations import os, sqlite3, csv, io, datetime as dt from typing import Optional @@ -8,7 +8,7 @@ app = Flask(__name__) DB_PATH = os.environ.get("SPLITBUDDY_DB", "splitbuddy.db") CURRENCY = "₪" -PERSON_A = os.environ.get("SPLITBUDDY_ME", "Me") # you +PERSON_A = os.environ.get("SPLITBUDDY_ME", "Me") # you PERSON_B = os.environ.get("SPLITBUDDY_ROOMIE", "Idan") # roommate # ------------------------- DB helpers --------------------------- # @@ -26,38 +26,36 @@ def close_db(_=None): def init_db(): db = get_db() - # Base table db.execute(""" CREATE TABLE IF NOT EXISTS entries ( id INTEGER PRIMARY KEY AUTOINCREMENT, created_at TEXT NOT NULL, - total REAL NOT NULL DEFAULT 0, -- total bill amount - payer TEXT NOT NULL DEFAULT 'A', -- 'A' (you) or 'B' (Idan) - a_share REAL NOT NULL DEFAULT 0.5, -- your share as fraction (0..1) + kind TEXT NOT NULL DEFAULT 'bill', -- 'bill' | 'transfer' + total REAL NOT NULL DEFAULT 0, -- total amount + payer TEXT NOT NULL DEFAULT 'A', -- 'A' (you) or 'B' (Idan) + a_share REAL NOT NULL DEFAULT 0.5, -- your share (0..1), only for bills method TEXT NOT NULL DEFAULT 'cash', note TEXT ) """) - # Migrate older schema (from signed amount version) - # If old columns exist, add new if missing - try: - db.execute("ALTER TABLE entries ADD COLUMN total REAL") - except sqlite3.OperationalError: - pass - try: - db.execute("ALTER TABLE entries ADD COLUMN payer TEXT") - except sqlite3.OperationalError: - pass - try: - db.execute("ALTER TABLE entries ADD COLUMN a_share REAL") - except sqlite3.OperationalError: - pass - # If we had old 'amount' signed records and 'total' is NULL, map amount→total and infer payer/a_share=0.5 + # Migrations for older versions + for col, ddl in [ + ("kind", "ALTER TABLE entries ADD COLUMN kind TEXT NOT NULL DEFAULT 'bill'"), + ("total", "ALTER TABLE entries ADD COLUMN total REAL"), + ("payer", "ALTER TABLE entries ADD COLUMN payer TEXT"), + ("a_share","ALTER TABLE entries ADD COLUMN a_share REAL"), + ]: + try: + db.execute(ddl) + except sqlite3.OperationalError: + pass + # Normalize NULLs from legacy rows db.execute(""" UPDATE entries - SET total = COALESCE(total, 0), - payer = COALESCE(payer, CASE WHEN total IS NOT NULL THEN 'A' ELSE 'A' END), - a_share = COALESCE(a_share, 0.5) + SET kind = COALESCE(kind, 'bill'), + total = COALESCE(total, 0), + payer = COALESCE(payer, 'A'), + a_share= COALESCE(a_share, 0.5) """) db.commit() @@ -71,7 +69,7 @@ BASE = r"""