new rev sys

This commit is contained in:
zipfriis
2026-03-24 19:42:49 +01:00
parent a4c5c4cb1d
commit 7e2c332e60
9 changed files with 263 additions and 395 deletions

View File

@@ -11,9 +11,9 @@ import (
func InitDB(db *sql.DB) {
schema := `
CREATE TABLE IF NOT EXISTS currencies (
id INTEGER PRIMARY KEY AUTOINCREMENT,
code TEXT NOT NULL UNIQUE,
name TEXT NOT NULL
id INTEGER PRIMARY KEY AUTOINCREMENT,
code TEXT NOT NULL UNIQUE,
name TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS companies (
@@ -35,46 +35,28 @@ func InitDB(db *sql.DB) {
UNIQUE(type, year, idx)
);
CREATE TABLE IF NOT EXISTS revenue_reports (
CREATE TABLE IF NOT EXISTS category (
id INTEGER PRIMARY KEY AUTOINCREMENT,
company_id INTEGER NOT NULL,
period_id INTEGER NOT NULL,
parent_id INTEGER,
name TEXT NOT NULL,
FOREIGN KEY (company_id) REFERENCES companies(id),
FOREIGN KEY (period_id) REFERENCES periods(id),
UNIQUE(company_id, period_id)
FOREIGN KEY (parent_id) REFERENCES category(id),
UNIQUE(company_id, name)
);
CREATE TABLE IF NOT EXISTS revenue_entries (
id INTEGER PRIMARY KEY AUTOINCREMENT,
report_id INTEGER NOT NULL,
company_id INTEGER NOT NULL,
currency_id INTEGER NOT NULL,
category TEXT NOT NULL CHECK(category IN ('product', 'location', 'total')),
label TEXT NOT NULL,
category_id INTEGER NOT NULL,
period_id INTEGER NOT NULL,
value REAL NOT NULL,
FOREIGN KEY (report_id) REFERENCES revenue_reports(id),
FOREIGN KEY (currency_id) REFERENCES currencies(id)
);
-- one row per category type per company
-- e.g. Apple has "product" and "location"
CREATE TABLE IF NOT EXISTS category_defs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
company_id INTEGER NOT NULL,
name TEXT NOT NULL,
FOREIGN KEY (company_id) REFERENCES companies(id),
UNIQUE(company_id, name)
);
-- the actual labels belonging to each category
-- e.g. category_def "product" → "iPhone", "Mac", "Services"
CREATE TABLE IF NOT EXISTS category_labels (
id INTEGER PRIMARY KEY AUTOINCREMENT,
category_def_id INTEGER NOT NULL,
label TEXT NOT NULL,
FOREIGN KEY (category_def_id) REFERENCES category_defs(id),
UNIQUE(category_def_id, label)
);
`
FOREIGN KEY (company_id) REFERENCES companies(id),
FOREIGN KEY (currency_id) REFERENCES currencies(id),
FOREIGN KEY (category_id) REFERENCES category(id),
FOREIGN KEY (period_id) REFERENCES periods(id)
);`
if _, err := db.Exec(schema); err != nil {
log.Fatal("Failed to create tables:", err)