2017-01-19 21:44:22 +01:00
|
|
|
CREATE TABLE Account (
|
|
|
|
ID PRIMARY KEY NOT NULL,
|
2017-02-02 22:23:23 +01:00
|
|
|
name,
|
2017-01-19 21:44:22 +01:00
|
|
|
type NOT NULL,
|
|
|
|
altId NOT NULL, -- e.g. when type "member", no. in external member table
|
|
|
|
IBAN -- target account for returned payments (set '' to enable
|
|
|
|
-- outgoing bank transfers to commercial partners from that account).
|
|
|
|
);
|
2017-01-29 15:57:34 +01:00
|
|
|
|
|
|
|
CREATE TABLE Category (
|
|
|
|
ID INTEGER PRIMARY KEY,
|
|
|
|
label
|
|
|
|
);
|
|
|
|
|
2017-01-19 21:44:22 +01:00
|
|
|
CREATE TABLE Debit (
|
|
|
|
billId PRIMARY KEY NOT NULL,
|
|
|
|
debtor NOT NULL, -- Account charged
|
|
|
|
targetCredit INTEGER, -- record id in Credit table to pay into.
|
|
|
|
-- just understand it as virtual payment
|
|
|
|
-- NULL when debit is a bank transfer from the club account
|
|
|
|
date DATE NOT NULL,
|
|
|
|
purpose NOT NULL, -- description of receipt
|
2017-01-29 15:57:34 +01:00
|
|
|
category,
|
2017-01-19 21:44:22 +01:00
|
|
|
value INTEGER NOT NULL, -- Euro-Cent
|
|
|
|
paid INTEGER DEFAULT 0, -- Euro-Cent, set and changed automatically (Cache)
|
|
|
|
FOREIGN KEY (debtor) REFERENCES Account(ID),
|
|
|
|
FOREIGN KEY (targetCredit) REFERENCES Credit(credId),
|
2017-01-29 15:57:34 +01:00
|
|
|
FOREIGN KEY (category) REFERENCES Category(ID),
|
2017-01-19 21:44:22 +01:00
|
|
|
CHECK ( abs(cast(value as integer)) == value
|
|
|
|
AND abs(cast( paid as integer)) == paid
|
|
|
|
AND value > 0 AND value >= paid
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TABLE Credit (
|
|
|
|
credId INTEGER PRIMARY KEY NOT NULL,
|
|
|
|
account NOT NULL, -- Account des Begünstigten
|
|
|
|
date DATE NOT NULL,
|
|
|
|
purpose NOT NULL, -- as originally indicated in statement of bank account
|
2017-01-29 15:57:34 +01:00
|
|
|
category,
|
2017-01-19 21:44:22 +01:00
|
|
|
value INTEGER NOT NULL, -- Euro-Cent. Caution, two distinct cases need to be considered:
|
|
|
|
-- Either deposit by bank transfer (>0) or target of internal payments (=0)
|
|
|
|
spent INTEGER DEFAULT 0, -- Euro-Cent, set and changed automatically (Cache)
|
|
|
|
-- for later traceability, necessary when revoking transfers
|
|
|
|
FOREIGN KEY (account) REFERENCES Account(ID),
|
2017-01-29 15:57:34 +01:00
|
|
|
FOREIGN KEY (category) REFERENCES Category(ID),
|
2017-01-19 21:44:22 +01:00
|
|
|
CHECK ( abs(cast(value as integer)) == value
|
|
|
|
AND abs(cast(spent as integer)) == spent
|
|
|
|
AND value >= spent
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
-- Which credit pays/paid down which debt is recorded traceably so as to clarify any case of reminder,
|
|
|
|
-- without ambuiguity about, which debt is actually due yet, or to clarify which transfer is used for
|
|
|
|
-- which debts. The user specifies which credit is intended for which debt, in accordance with the
|
|
|
|
-- purpose if any is indicated in a received bank transfer. Following triggers verify this relation
|
|
|
|
-- and mark a debit as paid and/or a credit as paid.
|
|
|
|
-- Debits to which applies value > paid are meant to be suggested for assignment to newly inserted
|
|
|
|
-- credit records without indicated purpose. Likewise, credits without indicated purpose to which
|
|
|
|
-- applies value > spent are candidates for payment of newly inserted debts.
|
|
|
|
CREATE TABLE Transfer (
|
|
|
|
timestamp DATE DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
billId INTEGER NOT NULL,
|
|
|
|
credId INTEGER NOT NULL,
|
|
|
|
amount INTEGER, -- for later traceability, necessary when revoking transfers
|
|
|
|
note,
|
|
|
|
FOREIGN KEY (billId) REFERENCES Debit(billId),
|
|
|
|
FOREIGN KEY (credId) REFERENCES Credit(credId),
|
|
|
|
UNIQUE (billId, credId)
|
|
|
|
);
|
|
|
|
|
|
|
|
-- For internal purposes: Memory of rebalance triggers
|
2017-01-22 21:20:17 +01:00
|
|
|
-- Do not fiddle with it! I.e. if you do, don't expect any support.
|
2017-01-23 19:03:58 +01:00
|
|
|
CREATE TABLE __INTERNAL_TRIGGER_STACK (id integer primary key, d, c, m);
|
2017-01-19 21:44:22 +01:00
|
|
|
|
|
|
|
-- Only for use of HTTP interface
|
|
|
|
CREATE TABLE web_auth ( user_id primary key, password, grade not null, username, email );
|
|
|
|
|