1 DROP SCHEMA money CASCADE;
7 CREATE TABLE money.collections_tracker (
8 id BIGSERIAL PRIMARY KEY,
9 usr INT NOT NULL REFERENCES actor.usr (id), -- actor.usr.id
10 collector INT NOT NULL REFERENCES actor.usr (id),
11 location INT NOT NULL REFERENCES actor.org_unit (id),
12 enter_time TIMESTAMP WITH TIME ZONE
14 CREATE UNIQUE INDEX m_c_t_usr_collector_location_once_idx ON money.collections_tracker (usr, collector, location);
16 CREATE TABLE money.billable_xact (
17 id BIGSERIAL PRIMARY KEY,
18 usr INT NOT NULL, -- actor.usr.id
19 xact_start TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
20 xact_finish TIMESTAMP WITH TIME ZONE
22 CREATE INDEX m_b_x_open_xacts_idx ON money.billable_xact (usr);
24 CREATE TABLE money.grocery ( -- Catchall table for local billing
25 billing_location INT NOT NULL, -- library creating transaction
27 ) INHERITS (money.billable_xact);
29 CREATE TABLE money.billing (
30 id BIGSERIAL PRIMARY KEY,
31 xact BIGINT NOT NULL, -- money.billable_xact.id
32 billing_ts TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
33 voided BOOL NOT NULL DEFAULT FALSE,
35 void_time TIMESTAMP WITH TIME ZONE,
36 amount NUMERIC(6,2) NOT NULL,
37 billing_type TEXT NOT NULL,
40 CREATE INDEX m_b_xact_idx ON money.billing (xact);
41 CREATE INDEX m_b_time_idx ON money.billing (billing_ts);
43 CREATE TABLE money.payment (
44 id BIGSERIAL PRIMARY KEY,
45 xact BIGINT NOT NULL, -- money.billable_xact.id
46 payment_ts TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
47 voided BOOL NOT NULL DEFAULT FALSE,
48 amount NUMERIC(6,2) NOT NULL,
51 CREATE INDEX m_p_xact_idx ON money.payment (xact);
52 CREATE INDEX m_p_time_idx ON money.payment (payment_ts);
54 CREATE OR REPLACE VIEW money.payment_view AS
55 SELECT p.*,c.relname AS payment_type
57 JOIN pg_class c ON (p.tableoid = c.oid);
59 CREATE OR REPLACE VIEW money.transaction_billing_type_summary AS
61 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
67 GROUP BY xact,billing_type
68 ORDER BY MAX(billing_ts);
70 CREATE OR REPLACE VIEW money.transaction_billing_summary AS
72 LAST(billing_type) AS last_billing_type,
73 LAST(note) AS last_billing_note,
74 MAX(billing_ts) AS last_billing_ts,
75 SUM(COALESCE(amount,0)) AS total_owed
79 ORDER BY MAX(billing_ts);
81 CREATE OR REPLACE VIEW money.transaction_payment_summary AS
83 LAST(payment_type) AS last_payment_type,
84 LAST(note) AS last_payment_note,
85 MAX(payment_ts) as last_payment_ts,
86 SUM(COALESCE(amount,0)) AS total_paid
87 FROM money.payment_view
90 ORDER BY MAX(payment_ts);
92 CREATE OR REPLACE VIEW money.transaction_billing_with_void_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(CASE WHEN voided THEN 0 ELSE COALESCE(amount,0) END) AS total_owed
100 ORDER BY MAX(billing_ts);
102 CREATE OR REPLACE VIEW money.transaction_payment_with_void_summary AS
104 LAST(payment_type) AS last_payment_type,
105 LAST(note) AS last_payment_note,
106 MAX(payment_ts) as last_payment_ts,
107 SUM(CASE WHEN voided THEN 0 ELSE COALESCE(amount,0) END) AS total_paid
108 FROM money.payment_view
110 ORDER BY MAX(payment_ts);
112 CREATE OR REPLACE VIEW money.open_transaction_billing_type_summary AS
114 billing_type AS last_billing_type,
115 LAST(note) AS last_billing_note,
116 MAX(billing_ts) AS last_billing_ts,
117 SUM(COALESCE(amount,0)) AS total_owed
119 WHERE voided IS FALSE
120 GROUP BY xact,billing_type
121 ORDER BY MAX(billing_ts);
123 CREATE OR REPLACE VIEW money.open_transaction_billing_summary AS
125 LAST(billing_type) AS last_billing_type,
126 LAST(note) AS last_billing_note,
127 MAX(billing_ts) AS last_billing_ts,
128 SUM(COALESCE(amount,0)) AS total_owed
130 WHERE voided IS FALSE
132 ORDER BY MAX(billing_ts);
134 CREATE OR REPLACE VIEW money.open_transaction_payment_summary AS
136 LAST(payment_type) AS last_payment_type,
137 LAST(note) AS last_payment_note,
138 MAX(payment_ts) as last_payment_ts,
139 SUM(COALESCE(amount,0)) AS total_paid
140 FROM money.payment_view
141 WHERE voided IS FALSE
143 ORDER BY MAX(payment_ts);
145 CREATE OR REPLACE VIEW money.billable_xact_with_void_summary AS
146 SELECT xact.id AS id,
148 xact.xact_start AS xact_start,
149 xact.xact_finish AS xact_finish,
151 credit.last_payment_ts,
152 credit.last_payment_note,
153 credit.last_payment_type,
155 debit.last_billing_ts,
156 debit.last_billing_note,
157 debit.last_billing_type,
158 COALESCE(debit.total_owed,0) - COALESCE(credit.total_paid,0) AS balance_owed,
159 p.relname AS xact_type
160 FROM money.billable_xact xact
161 JOIN pg_class p ON (xact.tableoid = p.oid)
162 LEFT JOIN money.transaction_billing_with_void_summary debit ON (xact.id = debit.xact)
163 LEFT JOIN money.transaction_payment_with_void_summary credit ON (xact.id = credit.xact);
165 CREATE OR REPLACE VIEW money.billable_xact_summary AS
166 SELECT xact.id AS id,
168 xact.xact_start AS xact_start,
169 xact.xact_finish AS xact_finish,
171 credit.last_payment_ts,
172 credit.last_payment_note,
173 credit.last_payment_type,
175 debit.last_billing_ts,
176 debit.last_billing_note,
177 debit.last_billing_type,
178 COALESCE(debit.total_owed,0) - COALESCE(credit.total_paid,0) AS balance_owed,
179 p.relname AS xact_type
180 FROM money.billable_xact xact
181 JOIN pg_class p ON (xact.tableoid = p.oid)
182 LEFT JOIN money.transaction_billing_summary debit ON (xact.id = debit.xact)
183 LEFT JOIN money.transaction_payment_summary credit ON (xact.id = credit.xact);
185 CREATE OR REPLACE VIEW money.open_billable_xact_summary AS
186 SELECT xact.id AS id,
188 xact.xact_start AS xact_start,
189 xact.xact_finish AS xact_finish,
191 credit.last_payment_ts,
192 credit.last_payment_note,
193 credit.last_payment_type,
195 debit.last_billing_ts,
196 debit.last_billing_note,
197 debit.last_billing_type,
198 COALESCE(debit.total_owed,0) - COALESCE(credit.total_paid,0) AS balance_owed,
199 p.relname AS xact_type
200 FROM money.billable_xact xact
201 JOIN pg_class p ON (xact.tableoid = p.oid)
202 LEFT JOIN money.transaction_billing_summary debit ON (xact.id = debit.xact)
203 LEFT JOIN money.transaction_payment_summary credit ON (xact.id = credit.xact)
204 WHERE xact.xact_finish IS NULL;
206 CREATE OR REPLACE VIEW money.open_usr_summary AS
208 SUM(total_paid) AS total_paid,
209 SUM(total_owed) AS total_owed,
210 SUM(balance_owed) AS balance_owed
211 FROM money.open_billable_xact_summary
214 CREATE OR REPLACE VIEW money.open_usr_circulation_summary AS
216 SUM(total_paid) AS total_paid,
217 SUM(total_owed) AS total_owed,
218 SUM(balance_owed) AS balance_owed
219 FROM money.open_billable_xact_summary
220 WHERE xact_type = 'circulation'
223 CREATE OR REPLACE VIEW money.usr_summary AS
225 SUM(total_paid) AS total_paid,
226 SUM(total_owed) AS total_owed,
227 SUM(balance_owed) AS balance_owed
228 FROM money.billable_xact_summary
231 CREATE OR REPLACE VIEW money.usr_circulation_summary AS
233 SUM(total_paid) AS total_paid,
234 SUM(total_owed) AS total_owed,
235 SUM(balance_owed) AS balance_owed
236 FROM money.billable_xact_summary
237 WHERE xact_type = 'circulation'
240 CREATE TABLE money.bnm_payment (
241 amount_collected NUMERIC(6,2) NOT NULL,
242 accepting_usr INT NOT NULL
243 ) INHERITS (money.payment);
245 CREATE TABLE money.forgive_payment () INHERITS (money.bnm_payment);
246 CREATE INDEX money_forgive_id_idx ON money.forgive_payment (id);
247 CREATE INDEX money_forgive_payment_xact_idx ON money.forgive_payment (xact);
248 CREATE INDEX money_forgive_payment_payment_ts_idx ON money.forgive_payment (payment_ts);
249 CREATE INDEX money_forgive_payment_accepting_usr_idx ON money.forgive_payment (accepting_usr);
251 CREATE TABLE money.work_payment () INHERITS (money.bnm_payment);
252 CREATE INDEX money_work_id_idx ON money.work_payment (id);
253 CREATE INDEX money_work_payment_xact_idx ON money.work_payment (xact);
254 CREATE INDEX money_work_payment_payment_ts_idx ON money.work_payment (payment_ts);
255 CREATE INDEX money_work_payment_accepting_usr_idx ON money.work_payment (accepting_usr);
257 CREATE TABLE money.credit_payment () INHERITS (money.bnm_payment);
258 CREATE INDEX money_credit_id_idx ON money.credit_payment (id);
259 CREATE INDEX money_credit_payment_xact_idx ON money.credit_payment (xact);
260 CREATE INDEX money_credit_payment_payment_ts_idx ON money.credit_payment (payment_ts);
261 CREATE INDEX money_credit_payment_accepting_usr_idx ON money.credit_payment (accepting_usr);
263 CREATE TABLE money.bnm_desk_payment (
264 cash_drawer INT REFERENCES actor.workstation (id)
265 ) INHERITS (money.bnm_payment);
267 CREATE TABLE money.cash_payment () INHERITS (money.bnm_desk_payment);
268 CREATE INDEX money_cash_id_idx ON money.cash_payment (id);
269 CREATE INDEX money_cash_payment_xact_idx ON money.cash_payment (xact);
270 CREATE INDEX money_cash_payment_ts_idx ON money.cash_payment (payment_ts);
271 CREATE INDEX money_cash_payment_accepting_usr_idx ON money.cash_payment (accepting_usr);
272 CREATE INDEX money_cash_payment_cash_drawer_idx ON money.cash_payment (cash_drawer);
274 CREATE TABLE money.check_payment (
275 check_number TEXT NOT NULL
276 ) INHERITS (money.bnm_desk_payment);
277 CREATE INDEX money_check_payment_xact_idx ON money.check_payment (xact);
278 CREATE INDEX money_check_id_idx ON money.check_payment (id);
279 CREATE INDEX money_check_payment_ts_idx ON money.check_payment (payment_ts);
280 CREATE INDEX money_check_payment_accepting_usr_idx ON money.check_payment (accepting_usr);
281 CREATE INDEX money_check_payment_cash_drawer_idx ON money.check_payment (cash_drawer);
283 CREATE TABLE money.credit_card_payment (
284 cc_type TEXT NOT NULL,
285 cc_number TEXT NOT NULL,
286 expire_month INT NOT NULL,
287 expire_year INT NOT NULL,
288 approval_code TEXT NOT NULL
289 ) INHERITS (money.bnm_desk_payment);
290 CREATE INDEX money_credit_card_payment_xact_idx ON money.credit_card_payment (xact);
291 CREATE INDEX money_credit_card_id_idx ON money.credit_card_payment (id);
292 CREATE INDEX money_credit_card_payment_ts_idx ON money.credit_card_payment (payment_ts);
293 CREATE INDEX money_credit_card_payment_accepting_usr_idx ON money.credit_card_payment (accepting_usr);
294 CREATE INDEX money_credit_card_payment_cash_drawer_idx ON money.credit_card_payment (cash_drawer);
296 CREATE OR REPLACE VIEW money.cashdrawer_payment_view AS
297 SELECT ou.id AS org_unit,
299 t.payment_type AS payment_type,
300 p.payment_ts AS payment_ts,
304 FROM actor.org_unit ou
305 JOIN actor.workstation ws ON (ou.id = ws.owning_lib)
306 LEFT JOIN money.bnm_desk_payment p ON (ws.id = p.cash_drawer)
307 LEFT JOIN money.payment_view t ON (p.id = t.id);