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.grocery ( -- Catchall table for local billing
16 billing_location INT NOT NULL, -- library creating transaction
18 ) INHERITS (money.billable_xact);
20 CREATE TABLE money.billing (
21 id BIGSERIAL PRIMARY KEY,
22 xact BIGINT NOT NULL, -- money.billable_xact.id
23 billing_ts TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
24 voided BOOL NOT NULL DEFAULT FALSE,
26 void_time TIMESTAMP WITH TIME ZONE,
27 amount NUMERIC(6,2) NOT NULL,
28 billing_type TEXT NOT NULL,
31 CREATE INDEX m_b_xact_idx ON money.billing (xact);
33 CREATE TABLE money.payment (
34 id BIGSERIAL PRIMARY KEY,
35 xact BIGINT NOT NULL, -- money.billable_xact.id
36 payment_ts TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
37 voided BOOL NOT NULL DEFAULT FALSE,
38 amount NUMERIC(6,2) NOT NULL,
41 CREATE INDEX m_p_xact_idx ON money.payment (xact);
43 CREATE OR REPLACE VIEW money.payment_view AS
44 SELECT p.*,c.relname AS payment_type
46 JOIN pg_class c ON (p.tableoid = c.oid);
48 CREATE OR REPLACE VIEW money.transaction_billing_type_summary AS
50 billing_type AS last_billing_type,
51 LAST(note) AS last_billing_note,
52 MAX(billing_ts) AS last_billing_ts,
53 SUM(COALESCE(amount,0)) AS total_owed
56 GROUP BY xact,billing_type
57 ORDER BY MAX(billing_ts);
59 CREATE OR REPLACE VIEW money.transaction_billing_summary AS
61 LAST(billing_type) AS last_billing_type,
62 LAST(note) AS last_billing_note,
63 MAX(billing_ts) AS last_billing_ts,
64 SUM(COALESCE(amount,0)) AS total_owed
68 ORDER BY MAX(billing_ts);
70 CREATE OR REPLACE VIEW money.transaction_payment_summary AS
72 LAST(payment_type) AS last_payment_type,
73 LAST(note) AS last_payment_note,
74 MAX(payment_ts) as last_payment_ts,
75 SUM(COALESCE(amount,0)) AS total_paid
76 FROM money.payment_view
79 ORDER BY MAX(payment_ts);
81 CREATE OR REPLACE VIEW money.open_transaction_billing_type_summary AS
83 billing_type AS last_billing_type,
84 LAST(note) AS last_billing_note,
85 MAX(billing_ts) AS last_billing_ts,
86 SUM(COALESCE(amount,0)) AS total_owed
89 GROUP BY xact,billing_type
90 ORDER BY MAX(billing_ts);
92 CREATE OR REPLACE VIEW money.open_transaction_billing_summary AS
94 LAST(billing_type) AS last_billing_type,
95 LAST(note) AS last_billing_note,
96 MAX(billing_ts) AS last_billing_ts,
97 SUM(COALESCE(amount,0)) AS total_owed
101 ORDER BY MAX(billing_ts);
103 CREATE OR REPLACE VIEW money.open_transaction_payment_summary AS
105 LAST(payment_type) AS last_payment_type,
106 LAST(note) AS last_payment_note,
107 MAX(payment_ts) as last_payment_ts,
108 SUM(COALESCE(amount,0)) AS total_paid
109 FROM money.payment_view
110 WHERE voided IS FALSE
112 ORDER BY MAX(payment_ts);
114 CREATE OR REPLACE VIEW money.billable_xact_summary AS
115 SELECT xact.id AS id,
117 xact.xact_start AS xact_start,
118 xact.xact_finish AS xact_finish,
120 credit.last_payment_ts,
121 credit.last_payment_note,
122 credit.last_payment_type,
124 debit.last_billing_ts,
125 debit.last_billing_note,
126 debit.last_billing_type,
127 COALESCE(debit.total_owed,0) - COALESCE(credit.total_paid,0) AS balance_owed,
128 p.relname AS xact_type
129 FROM money.billable_xact xact
130 JOIN pg_class p ON (xact.tableoid = p.oid)
131 LEFT JOIN money.transaction_billing_summary debit ON (xact.id = debit.xact)
132 LEFT JOIN money.transaction_payment_summary credit ON (xact.id = credit.xact);
134 CREATE OR REPLACE VIEW money.open_billable_xact_summary AS
135 SELECT xact.id AS id,
137 xact.xact_start AS xact_start,
138 xact.xact_finish AS xact_finish,
140 credit.last_payment_ts,
141 credit.last_payment_note,
142 credit.last_payment_type,
144 debit.last_billing_ts,
145 debit.last_billing_note,
146 debit.last_billing_type,
147 COALESCE(debit.total_owed,0) - COALESCE(credit.total_paid,0) AS balance_owed,
148 p.relname AS xact_type
149 FROM money.billable_xact xact
150 JOIN pg_class p ON (xact.tableoid = p.oid)
151 LEFT JOIN money.transaction_billing_summary debit ON (xact.id = debit.xact)
152 LEFT JOIN money.transaction_payment_summary credit ON (xact.id = credit.xact)
153 WHERE xact.xact_finish IS NULL;
155 CREATE OR REPLACE VIEW money.open_usr_summary AS
157 SUM(total_paid) AS total_paid,
158 SUM(total_owed) AS total_owed,
159 SUM(balance_owed) AS balance_owed
160 FROM money.open_billable_xact_summary
163 CREATE OR REPLACE VIEW money.open_usr_circulation_summary AS
165 SUM(total_paid) AS total_paid,
166 SUM(total_owed) AS total_owed,
167 SUM(balance_owed) AS balance_owed
168 FROM money.open_billable_xact_summary
169 WHERE xact_type = 'circulation'
172 CREATE OR REPLACE VIEW money.usr_summary AS
174 SUM(total_paid) AS total_paid,
175 SUM(total_owed) AS total_owed,
176 SUM(balance_owed) AS balance_owed
177 FROM money.billable_xact_summary
180 CREATE OR REPLACE VIEW money.usr_circulation_summary AS
182 SUM(total_paid) AS total_paid,
183 SUM(total_owed) AS total_owed,
184 SUM(balance_owed) AS balance_owed
185 FROM money.billable_xact_summary
186 WHERE xact_type = 'circulation'
189 CREATE TABLE money.bnm_payment (
190 amount_collected NUMERIC(6,2) NOT NULL,
191 accepting_usr INT NOT NULL
192 ) INHERITS (money.payment);
194 CREATE TABLE money.forgive_payment () INHERITS (money.bnm_payment);
195 CREATE INDEX money_forgive_payment_xact_idx ON money.forgive_payment (xact);
196 CREATE INDEX money_forgive_payment_accepting_usr_idx ON money.forgive_payment (accepting_usr);
198 CREATE TABLE money.work_payment () INHERITS (money.bnm_payment);
199 CREATE INDEX money_work_payment_xact_idx ON money.work_payment (xact);
200 CREATE INDEX money_work_payment_accepting_usr_idx ON money.work_payment (accepting_usr);
202 CREATE TABLE money.credit_payment () INHERITS (money.bnm_payment);
203 CREATE INDEX money_credit_payment_xact_idx ON money.credit_payment (xact);
204 CREATE INDEX money_credit_payment_accepting_usr_idx ON money.credit_payment (accepting_usr);
206 CREATE TABLE money.bnm_desk_payment (
207 cash_drawer TEXT NOT NULL
208 ) INHERITS (money.bnm_payment);
210 CREATE TABLE money.cash_payment () INHERITS (money.bnm_desk_payment);
211 CREATE INDEX money_cash_payment_xact_idx ON money.cash_payment (xact);
212 CREATE INDEX money_cash_payment_accepting_usr_idx ON money.cash_payment (accepting_usr);
214 CREATE TABLE money.check_payment (
215 check_number TEXT NOT NULL
216 ) INHERITS (money.bnm_desk_payment);
217 CREATE INDEX money_check_payment_xact_idx ON money.check_payment (xact);
218 CREATE INDEX money_check_payment_accepting_usr_idx ON money.check_payment (accepting_usr);
220 CREATE TABLE money.credit_card_payment (
221 cc_type TEXT NOT NULL,
222 cc_number TEXT NOT NULL,
223 expire_month INT NOT NULL,
224 expire_year INT NOT NULL,
225 approval_code TEXT NOT NULL
226 ) INHERITS (money.bnm_desk_payment);
227 CREATE INDEX money_credit_card_payment_xact_idx ON money.credit_card_payment (xact);
228 CREATE INDEX money_credit_card_payment_accepting_usr_idx ON money.credit_card_payment (accepting_usr);