]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0061.schema.acqfa_no_percent.sql
Add fund column to acq.invoice_item.
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0061.schema.acqfa_no_percent.sql
1 BEGIN;
2
3 -- Script to eliminate acq.fund_allocation.percent, which has been moved to the
4 -- acq.fund_allocation_percent table.
5
6 INSERT INTO config.upgrade_log (version) VALUES ('0061');  -- Scott McKellar
7
8 -- If the following step fails, it's probably because there are still some non-null percent values in
9 -- acq.fund_allocation.  They should have all been converted to amounts, and then set to null, by a
10 -- previous upgrade script, 0049.schema.acq_funding_allocation_percent.sql.  If there are any non-null
11 -- values, then either that script didn't run, or it didn't work, or some non-null values slipped in
12 -- afterwards.
13
14 -- To convert any remaining percents to amounts: create, run, and then drop the temporary stored
15 -- procedure acq.fund_alloc_percent_val as defined in 0049.schema.acq_funding_allocation_percent.sql.
16
17 ALTER TABLE acq.fund_allocation
18 ALTER COLUMN amount SET NOT NULL;
19
20 CREATE OR REPLACE VIEW acq.fund_allocation_total AS
21     SELECT  fund,
22             SUM(a.amount * acq.exchange_ratio(s.currency_type, f.currency_type))::NUMERIC(100,2) AS amount
23     FROM acq.fund_allocation a
24          JOIN acq.fund f ON (a.fund = f.id)
25          JOIN acq.funding_source s ON (a.funding_source = s.id)
26     GROUP BY 1;
27
28 CREATE OR REPLACE VIEW acq.funding_source_allocation_total AS
29     SELECT  funding_source,
30             SUM(a.amount)::NUMERIC(100,2) AS amount
31     FROM  acq.fund_allocation a
32     GROUP BY 1;
33
34 ALTER TABLE acq.fund_allocation
35 DROP COLUMN percent;
36
37 COMMIT;