]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/XXXX.schema.aged-billing-payment.sql
LP#1793802 Add money.all_[payments|billings] views
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / XXXX.schema.aged-billing-payment.sql
1
2 BEGIN;
3
4 --SELECT evergreen.upgrade_deps_block_check('XXXX', :eg_version);
5
6 \qecho Migrating aged billing and payment data.  This might take a while.
7
8 CREATE TABLE money.aged_payment (LIKE money.payment INCLUDING INDEXES);
9 ALTER TABLE money.aged_payment ADD COLUMN payment_type TEXT NOT NULL;
10
11 CREATE TABLE money.aged_billing (LIKE money.billing INCLUDING INDEXES);
12
13 INSERT INTO money.aged_payment 
14     SELECT  mp.* FROM money.payment_view mp
15     JOIN action.aged_circulation circ ON (circ.id = mp.xact);
16
17 INSERT INTO money.aged_billing
18     SELECT mb.* FROM money.billing mb
19     JOIN action.aged_circulation circ ON (circ.id = mb.xact);
20
21 DELETE FROM money.payment WHERE id IN (
22     SELECT mp.id FROM money.payment mp
23     JOIN action.aged_circulation circ ON (circ.id = mp.xact)
24 );
25
26 DELETE FROM money.billing WHERE id IN (
27     SELECT mb.id FROM money.billing mb
28     JOIN action.aged_circulation circ ON (circ.id = mb.xact)
29 );
30
31 CREATE OR REPLACE VIEW money.all_payments AS
32     SELECT * FROM money.payment_view 
33     UNION ALL
34     SELECT * FROM money.aged_payment;
35
36 CREATE OR REPLACE VIEW money.all_billings AS
37     SELECT * FROM money.billing
38     UNION ALL
39     SELECT * FROM money.aged_billing;
40
41 CREATE OR REPLACE FUNCTION action.age_circ_on_delete () RETURNS TRIGGER AS $$
42 DECLARE
43 found char := 'N';
44 BEGIN
45
46     -- If there are any renewals for this circulation, don't archive or delete
47     -- it yet.   We'll do so later, when we archive and delete the renewals.
48
49     SELECT 'Y' INTO found
50     FROM action.circulation
51     WHERE parent_circ = OLD.id
52     LIMIT 1;
53
54     IF found = 'Y' THEN
55         RETURN NULL;  -- don't delete
56         END IF;
57
58     -- Archive a copy of the old row to action.aged_circulation
59
60     INSERT INTO action.aged_circulation
61         (id,usr_post_code, usr_home_ou, usr_profile, usr_birth_year, copy_call_number, copy_location,
62         copy_owning_lib, copy_circ_lib, copy_bib_record, xact_start, xact_finish, target_copy,
63         circ_lib, circ_staff, checkin_staff, checkin_lib, renewal_remaining, grace_period, due_date,
64         stop_fines_time, checkin_time, create_time, duration, fine_interval, recurring_fine,
65         max_fine, phone_renewal, desk_renewal, opac_renewal, duration_rule, recurring_fine_rule,
66         max_fine_rule, stop_fines, workstation, checkin_workstation, checkin_scan_time, parent_circ)
67       SELECT
68         id,usr_post_code, usr_home_ou, usr_profile, usr_birth_year, copy_call_number, copy_location,
69         copy_owning_lib, copy_circ_lib, copy_bib_record, xact_start, xact_finish, target_copy,
70         circ_lib, circ_staff, checkin_staff, checkin_lib, renewal_remaining, grace_period, due_date,
71         stop_fines_time, checkin_time, create_time, duration, fine_interval, recurring_fine,
72         max_fine, phone_renewal, desk_renewal, opac_renewal, duration_rule, recurring_fine_rule,
73         max_fine_rule, stop_fines, workstation, checkin_workstation, checkin_scan_time, parent_circ
74         FROM action.all_circulation WHERE id = OLD.id;
75
76     -- Migrate billings and payments to aged tables
77
78     INSERT INTO money.aged_billing
79         SELECT * FROM money.billing WHERE xact = OLD.id;
80
81     INSERT INTO money.aged_payment 
82         SELECT * FROM money.payment_view WHERE xact = OLD.id;
83
84     DELETE FROM money.billing WHERE xact = OLD.id;
85     DELETE FROM money.payment WHERE xact = OLD.id;
86
87     RETURN OLD;
88 END;
89 $$ LANGUAGE 'plpgsql';
90
91 COMMIT;
92