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