e7c84793b20e53747e5d4672beacc1792fc8b634
[srvgit] / installer / data / mysql / atomicupdate / bug_23805_credit.perl
1 $DBversion = 'XXX';    # will be replaced by the RM
2 if ( CheckVersion($DBversion) ) {
3
4     # Adding account_credit_types
5     $dbh->do(
6         qq{
7             CREATE TABLE IF NOT EXISTS account_credit_types (
8               code varchar(80) NOT NULL,
9               description varchar(200) NULL,
10               can_be_added_manually tinyint(4) NOT NULL DEFAULT 1,
11               is_system tinyint(1) NOT NULL DEFAULT 0,
12               PRIMARY KEY (code)
13             ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci
14           }
15     );
16
17     # Adding account_credit_types_branches
18     $dbh->do(
19         qq{
20             CREATE TABLE IF NOT EXISTS account_credit_types_branches (
21                 credit_type_code VARCHAR(80),
22                 branchcode VARCHAR(10),
23                 FOREIGN KEY (credit_type_code) REFERENCES account_credit_types(code) ON DELETE CASCADE,
24                 FOREIGN KEY (branchcode) REFERENCES branches(branchcode) ON DELETE CASCADE
25             ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
26         }
27     );
28
29     # Populating account_credit_types
30     $dbh->do(
31         qq{
32             INSERT IGNORE INTO account_credit_types (
33               code,
34               description,
35               can_be_added_manually,
36               is_system
37             )
38             VALUES
39               ('PAYMENT', 'Payment', 0, 1),
40               ('WRITEOFF', 'Writeoff', 0, 1),
41               ('FORGIVEN', 'Forgiven', 1, 1),
42               ('CREDIT', 'Credit', 1, 1),
43               ('LOST_RETURN', 'Lost item fee refund', 0, 1)
44         }
45     );
46
47     # Adding credit_type_code to accountlines
48     unless ( column_exists('accountlines', 'credit_type_code') ) {
49         $dbh->do(
50             qq{
51                 ALTER IGNORE TABLE accountlines
52                 ADD
53                   credit_type_code varchar(80) DEFAULT NULL
54                 AFTER
55                   accounttype
56               }
57         );
58     }
59
60     # Linking credit_type_code in accountlines to code in account_credit_types
61     unless ( foreign_key_exists( 'accountlines', 'accountlines_ibfk_credit_type' ) ) {
62         $dbh->do(
63             qq{
64                 ALTER TABLE accountlines
65                 ADD CONSTRAINT
66                   `accountlines_ibfk_credit_type`
67                 FOREIGN KEY (`credit_type_code`) REFERENCES `account_credit_types` (`code`)
68                 ON DELETE RESTRICT
69                 ON UPDATE CASCADE
70               }
71         );
72     }
73
74     # Dropping the check constraint in accountlines
75     $dbh->do(
76         qq{
77           ALTER TABLE
78             accountlines
79           ADD CONSTRAINT
80             `accountlines_check_type`
81           CHECK (credit_type_code IS NOT NULL OR debit_type_code IS NOT NULL)
82         }
83     );
84
85     # Update accountype 'C' to 'CREDIT'
86     $dbh->do(
87         qq{
88           UPDATE accountlines SET accounttype = 'CREDIT' WHERE accounttype = 'C' OR accounttype = 'CR'
89         }
90     );
91
92     # Update accountype 'FOR' to 'FORGIVEN'
93     $dbh->do(
94         qq{
95           UPDATE accountlines SET accounttype = 'FORGIVEN' WHERE accounttype = 'FOR' OR accounttype = 'FORW'
96         }
97     );
98
99     # Update accountype 'Pay' to 'PAYMENT'
100     $dbh->do(
101         qq{
102           UPDATE accountlines SET accounttype = 'PAYMENT' WHERE accounttype = 'Pay' OR accounttype = 'PAY'
103         }
104     );
105
106     # Update accountype 'W' to 'WRITEOFF'
107     $dbh->do(
108         qq{
109           UPDATE accountlines SET accounttype = 'WRITEOFF' WHERE accounttype = 'W' OR accounttype = 'WO'
110         }
111     );
112
113     # Add any unexpected accounttype codes to credit_types as appropriate
114     $dbh->do(
115         qq{
116           INSERT IGNORE INTO account_credit_types (
117             code,
118             description,
119             can_be_added_manually,
120             is_system
121           )
122           SELECT
123             DISTINCT(accounttype),
124             "Unexpected type found during upgrade",
125             1,
126             0
127           FROM
128             accountlines
129           WHERE
130             amount < 0
131         }
132     );
133
134     # Populating credit_type_code
135     $dbh->do(
136         qq{
137         UPDATE accountlines SET credit_type_code = accounttype, accounttype = NULL WHERE accounttype IN (SELECT code from account_credit_types)
138         }
139     );
140
141     # Adding a check constraints to accountlines
142     $dbh->do(
143         qq{
144         ALTER TABLE accountlines ADD CONSTRAINT `accountlines_check_type` CHECK (accounttype IS NOT NULL OR credit_type_code IS NOT NULL)
145         }
146     );
147
148     SetVersion($DBversion);
149     print "Upgrade to $DBversion done (Bug 23049 - Add account debit_credit)\n";
150 }