]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/xxxx.schema.acq_lp800477
Addresses LP#800477 where some acq views calculate the totals incorrectly or in unexp...
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / xxxx.schema.acq_lp800477
1 BEGIN;
2
3 INSERT INTO config.upgrade_log (version, install_date) VALUES ('XXXXXXX',now());
4
5 -- acq.fund_combined_balance is unchanged however we need to drop it to recreate the other views
6 -- we need to drop all our views because we change the number of columns
7 -- for example, debit_total does not need an encumberance column when we have a sepearate total for that
8
9 DROP VIEW acq.fund_combined_balance;
10 DROP VIEW acq.fund_encumbrance_total;
11 DROP VIEW acq.fund_spent_total;
12 DROP VIEW acq.fund_debit_total;
13
14 CREATE OR REPLACE VIEW acq.fund_debit_total AS
15  SELECT fund.id AS fund, sum(COALESCE(fund_debit.amount, 0::numeric)) AS amount
16    FROM acq.fund fund
17    LEFT JOIN acq.fund_debit fund_debit ON fund.id = fund_debit.fund
18   GROUP BY fund.id;
19
20 CREATE OR REPLACE VIEW acq.fund_encumbrance_total AS
21 SELECT fund.id AS fund, sum(COALESCE(fund_debit.amount, 0::numeric)) AS amount FROM acq.fund fund
22    LEFT JOIN acq.fund_debit fund_debit ON fund.id = fund_debit.fund WHERE fund_debit.encumbrance GROUP BY fund.id;
23
24 CREATE OR REPLACE VIEW acq.fund_spent_total AS
25 SELECT fund.id AS fund, sum(COALESCE(fund_debit.amount, 0::numeric)) AS amount FROM acq.fund fund
26    LEFT JOIN acq.fund_debit fund_debit ON fund.id = fund_debit.fund WHERE NOT fund_debit.encumbrance GROUP BY fund.id;
27
28 CREATE OR REPLACE VIEW acq.fund_combined_balance AS
29  SELECT c.fund, c.amount - COALESCE(d.amount, 0.0) AS amount
30    FROM acq.fund_allocation_total c
31    LEFT JOIN acq.fund_debit_total d USING (fund);
32
33
34 COMMIT;