1 DROP SCHEMA money CASCADE;
7 CREATE TABLE money.billable_xact (
8 id BIGSERIAL PRIMARY KEY,
9 usr INT NOT NULL, -- actor.usr.id
10 xact_start TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
11 xact_finish TIMESTAMP WITH TIME ZONE
13 CREATE INDEX m_b_x_open_xacts_idx ON money.billable_xact (usr) WHERE xact_finish IS NULL;
15 CREATE TABLE money.billing (
16 id BIGSERIAL PRIMARY KEY,
17 xact BIGINT NOT NULL, -- money.billable_xact.id
18 billing_ts TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
19 voided BOOL NOT NULL DEFAULT FALSE,
20 amount NUMERIC(6,2) NOT NULL,
21 billing_type TEXT NOT NULL,
24 CREATE INDEX m_b_xact_idx ON money.billing (xact);
26 CREATE TABLE money.payment (
27 id BIGSERIAL PRIMARY KEY,
28 xact BIGINT NOT NULL, -- money.billable_xact.id
29 payment_ts TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
30 voided BOOL NOT NULL DEFAULT FALSE,
31 amount NUMERIC(6,2) NOT NULL,
34 CREATE INDEX m_p_xact_idx ON money.payment (xact);
36 CREATE OR REPLACE VIEW money.payment_view AS
37 SELECT p.*,c.relname AS payment_type
39 JOIN pg_class c ON (p.tableoid = c.oid);
41 CREATE OR REPLACE VIEW money.transaction_billing_type_summary AS
43 billing_type AS last_billing_type,
44 LAST(note) AS last_billing_note,
45 MAX(billing_ts) AS last_billing_ts,
46 SUM(COALESCE(amount,0)) AS total_owed
49 GROUP BY xact,billing_type
50 ORDER BY MAX(billing_ts);
52 CREATE OR REPLACE VIEW money.transaction_billing_summary AS
54 LAST(billing_type) AS last_billing_type,
55 LAST(note) AS last_billing_note,
56 MAX(billing_ts) AS last_billing_ts,
57 SUM(COALESCE(amount,0)) AS total_owed
61 ORDER BY MAX(billing_ts);
63 CREATE OR REPLACE VIEW money.transaction_payment_summary AS
65 LAST(payment_type) AS last_payment_type,
66 LAST(note) AS last_payment_note,
67 MAX(payment_ts) as last_payment_ts,
68 SUM(COALESCE(amount,0)) AS total_paid
69 FROM money.payment_view
72 ORDER BY MAX(payment_ts);
74 CREATE OR REPLACE VIEW money.open_transaction_billing_type_summary AS
76 billing_type AS last_billing_type,
77 LAST(note) AS last_billing_note,
78 MAX(billing_ts) AS last_billing_ts,
79 SUM(COALESCE(amount,0)) AS total_owed
82 GROUP BY xact,billing_type
83 ORDER BY MAX(billing_ts);
85 CREATE OR REPLACE VIEW money.open_transaction_billing_summary AS
87 LAST(billing_type) AS last_billing_type,
88 LAST(note) AS last_billing_note,
89 MAX(billing_ts) AS last_billing_ts,
90 SUM(COALESCE(amount,0)) AS total_owed
94 ORDER BY MAX(billing_ts);
96 CREATE OR REPLACE VIEW money.open_transaction_payment_summary AS
98 LAST(payment_type) AS last_payment_type,
99 LAST(note) AS last_payment_note,
100 MAX(payment_ts) as last_payment_ts,
101 SUM(COALESCE(amount,0)) AS total_paid
102 FROM money.payment_view
103 WHERE voided IS FALSE
105 ORDER BY MAX(payment_ts);
107 CREATE OR REPLACE VIEW money.billable_xact_summary AS
108 SELECT xact.id AS id,
110 xact.xact_start AS xact_start,
111 xact.xact_finish AS xact_finish,
113 credit.last_payment_ts,
114 credit.last_payment_note,
115 credit.last_payment_type,
117 debit.last_billing_ts,
118 debit.last_billing_note,
119 debit.last_billing_type,
120 COALESCE(debit.total_owed,0) - COALESCE(credit.total_paid,0) AS balance_owed,
121 p.relname AS xact_type
122 FROM money.billable_xact xact
123 JOIN pg_class p ON (xact.tableoid = p.oid)
124 LEFT JOIN money.transaction_billing_summary debit ON (xact.id = debit.xact)
125 LEFT JOIN money.transaction_payment_summary credit ON (xact.id = credit.xact);
127 CREATE OR REPLACE VIEW money.open_billable_xact_summary AS
128 SELECT xact.id AS id,
130 xact.xact_start AS xact_start,
131 xact.xact_finish AS xact_finish,
133 credit.last_payment_ts,
134 credit.last_payment_note,
135 credit.last_payment_type,
137 debit.last_billing_ts,
138 debit.last_billing_note,
139 debit.last_billing_type,
140 COALESCE(debit.total_owed,0) - COALESCE(credit.total_paid,0) AS balance_owed,
141 p.relname AS xact_type
142 FROM money.billable_xact xact
143 JOIN pg_class p ON (xact.tableoid = p.oid)
144 LEFT JOIN money.transaction_billing_summary debit ON (xact.id = debit.xact)
145 LEFT JOIN money.transaction_payment_summary credit ON (xact.id = credit.xact)
146 WHERE xact.xact_finish IS NULL;
148 CREATE OR REPLACE VIEW money.open_usr_summary AS
150 SUM(total_paid) AS total_paid,
151 SUM(total_owed) AS total_owed,
152 SUM(balance_owed) AS balance_owed
153 FROM money.open_billable_xact_summary
156 CREATE OR REPLACE VIEW money.open_usr_circulation_summary AS
158 SUM(total_paid) AS total_paid,
159 SUM(total_owed) AS total_owed,
160 SUM(balance_owed) AS balance_owed
161 FROM money.open_billable_xact_summary
162 WHERE xact_type = 'circulation'
165 CREATE OR REPLACE VIEW money.usr_summary AS
167 SUM(total_paid) AS total_paid,
168 SUM(total_owed) AS total_owed,
169 SUM(balance_owed) AS balance_owed
170 FROM money.billable_xact_summary
173 CREATE OR REPLACE VIEW money.usr_circulation_summary AS
175 SUM(total_paid) AS total_paid,
176 SUM(total_owed) AS total_owed,
177 SUM(balance_owed) AS balance_owed
178 FROM money.billable_xact_summary
179 WHERE xact_type = 'circulation'
182 CREATE TABLE money.bnm_payment (
183 amount_collected NUMERIC(6,2) NOT NULL,
184 accepting_usr INT NOT NULL
185 ) INHERITS (money.payment);
187 CREATE TABLE money.forgive_payment () INHERITS (money.bnm_payment);
188 CREATE INDEX money_forgive_payment_xact_idx ON money.forgive_payment (xact);
189 CREATE INDEX money_forgive_payment_accepting_usr_idx ON money.forgive_payment (accepting_usr);
191 CREATE TABLE money.work_payment () INHERITS (money.bnm_payment);
192 CREATE INDEX money_work_payment_xact_idx ON money.work_payment (xact);
193 CREATE INDEX money_work_payment_accepting_usr_idx ON money.work_payment (accepting_usr);
195 CREATE TABLE money.credit_payment () INHERITS (money.bnm_payment);
196 CREATE INDEX money_credit_payment_xact_idx ON money.credit_payment (xact);
197 CREATE INDEX money_credit_payment_accepting_usr_idx ON money.credit_payment (accepting_usr);
199 CREATE TABLE money.bnm_desk_payment (
200 cash_drawer TEXT NOT NULL
201 ) INHERITS (money.bnm_payment);
203 CREATE TABLE money.cash_payment () INHERITS (money.bnm_desk_payment);
204 CREATE INDEX money_cash_payment_xact_idx ON money.cash_payment (xact);
205 CREATE INDEX money_cash_payment_accepting_usr_idx ON money.cash_payment (accepting_usr);
207 CREATE TABLE money.check_payment (
208 check_number TEXT NOT NULL
209 ) INHERITS (money.bnm_desk_payment);
210 CREATE INDEX money_check_payment_xact_idx ON money.check_payment (xact);
211 CREATE INDEX money_check_payment_accepting_usr_idx ON money.check_payment (accepting_usr);
213 CREATE TABLE money.credit_card_payment (
214 cc_type TEXT NOT NULL,
215 cc_number TEXT NOT NULL,
216 expire_month INT NOT NULL,
217 expire_year INT NOT NULL,
218 approval_code TEXT NOT NULL
219 ) INHERITS (money.bnm_desk_payment);
220 CREATE INDEX money_credit_card_payment_xact_idx ON money.credit_card_payment (xact);
221 CREATE INDEX money_credit_card_payment_accepting_usr_idx ON money.credit_card_payment (accepting_usr);