]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/080.schema.money.sql
adding "open only" views
[Evergreen.git] / Open-ILS / src / sql / Pg / 080.schema.money.sql
1 DROP SCHEMA money CASCADE;
2
3 BEGIN;
4
5 CREATE SCHEMA money;
6
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
12 );
13 CREATE INDEX m_b_x_open_xacts_idx ON money.billable_xact (usr) WHERE xact_finish IS NULL;
14
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,
22         note            TEXT
23 );
24 CREATE INDEX m_b_xact_idx ON money.billing (xact);
25
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,
32         note            TEXT
33 );
34 CREATE INDEX m_p_xact_idx ON money.payment (xact);
35
36 CREATE OR REPLACE VIEW money.payment_view AS
37         SELECT  p.*,c.relname AS payment_type
38           FROM  money.payment p
39                 JOIN pg_class c ON (p.tableoid = c.oid);
40
41 CREATE OR REPLACE VIEW money.transaction_billing_type_summary AS
42         SELECT  xact,
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
47           FROM  money.billing
48           WHERE voided IS FALSE
49           GROUP BY xact,billing_type
50           ORDER BY MAX(billing_ts);
51
52 CREATE OR REPLACE VIEW money.transaction_billing_summary AS
53         SELECT  xact,
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
58           FROM  money.billing
59           WHERE voided IS FALSE
60           GROUP BY xact
61           ORDER BY MAX(billing_ts);
62
63 CREATE OR REPLACE VIEW money.transaction_payment_summary AS
64         SELECT  xact,
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
70           WHERE voided IS FALSE
71           GROUP BY xact
72           ORDER BY MAX(payment_ts);
73
74 CREATE OR REPLACE VIEW money.open_transaction_billing_type_summary AS
75         SELECT  xact,
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
80           FROM  money.billing
81           WHERE voided IS FALSE
82           GROUP BY xact,billing_type
83           ORDER BY MAX(billing_ts);
84
85 CREATE OR REPLACE VIEW money.open_transaction_billing_summary AS
86         SELECT  xact,
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
91           FROM  money.billing
92           WHERE voided IS FALSE
93           GROUP BY xact
94           ORDER BY MAX(billing_ts);
95
96 CREATE OR REPLACE VIEW money.open_transaction_payment_summary AS
97         SELECT  xact,
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
104           GROUP BY xact
105           ORDER BY MAX(payment_ts);
106
107 CREATE OR REPLACE VIEW money.billable_xact_summary AS
108         SELECT  xact.id AS id,
109                 xact.usr AS usr,
110                 xact.xact_start AS xact_start,
111                 xact.xact_finish AS xact_finish,
112                 credit.total_paid,
113                 credit.last_payment_ts,
114                 credit.last_payment_note,
115                 credit.last_payment_type,
116                 debit.total_owed,
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);
126
127 CREATE OR REPLACE VIEW money.open_billable_xact_summary AS
128         SELECT  xact.id AS id,
129                 xact.usr AS usr,
130                 xact.xact_start AS xact_start,
131                 xact.xact_finish AS xact_finish,
132                 credit.total_paid,
133                 credit.last_payment_ts,
134                 credit.last_payment_note,
135                 credit.last_payment_type,
136                 debit.total_owed,
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;
147
148 CREATE OR REPLACE VIEW money.open_usr_summary AS
149         SELECT  usr,
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
154           GROUP BY 1;
155
156 CREATE OR REPLACE VIEW money.open_usr_circulation_summary AS
157         SELECT  usr,
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'
163           GROUP BY 1;
164
165 CREATE OR REPLACE VIEW money.usr_summary AS
166         SELECT  usr,
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
171           GROUP BY 1;
172
173 CREATE OR REPLACE VIEW money.usr_circulation_summary AS
174         SELECT  usr,
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'
180           GROUP BY 1;
181
182 CREATE TABLE money.bnm_payment (
183         amount_collected        NUMERIC(6,2)    NOT NULL,
184         accepting_usr           INT             NOT NULL
185 ) INHERITS (money.payment);
186
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);
190
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);
194
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);
198
199 CREATE TABLE money.bnm_desk_payment (
200         cash_drawer     TEXT    NOT NULL
201 ) INHERITS (money.bnm_payment);
202
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);
206
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);
212
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);
222
223
224 COMMIT;
225