Bug 23049: Update existing code to use debit_type
[koha.git] / installer / data / mysql / atomicupdate / bug_23049_debit.perl
1 $DBversion = 'XXX';    # will be replaced by the RM
2 if ( CheckVersion($DBversion) ) {
3
4     # Adding account_debit_types
5     $dbh->do(
6         qq{
7             CREATE TABLE IF NOT EXISTS account_debit_types (
8               code varchar(64) NOT NULL,
9               description varchar(200) NULL,
10               can_be_added_manually tinyint(4) NOT NULL DEFAULT 1,
11               default_amount decimal(28, 6) NULL,
12               is_system tinyint(1) NOT NULL DEFAULT 0,
13               PRIMARY KEY (code)
14             ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci
15           }
16     );
17
18     # Adding ac_debit_types_branches
19     $dbh->do(
20         qq{
21             CREATE TABLE IF NOT EXISTS ac_debit_types_branches (
22                 debit_type_code VARCHAR(64),
23                 branchcode VARCHAR(10),
24                 FOREIGN KEY (debit_type_code) REFERENCES account_debit_types(code) ON DELETE CASCADE,
25                 FOREIGN KEY (branchcode) REFERENCES branches(branchcode) ON DELETE CASCADE
26             ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
27         }
28     );
29
30     # Populating account_debit_types
31     $dbh->do(
32         qq{
33             INSERT IGNORE INTO account_debit_types (
34               code,
35               description,
36               can_be_added_manually,
37               default_amount,
38               is_system
39             )
40             VALUES
41               ('ACCOUNT', 'Account creation fee', 0, NULL, 1),
42               ('ACCOUNT_RENEW', 'Account renewal fee', 0, NULL, 1),
43               ('HE', 'Hold waiting too long', 0, NULL, 1),
44               ('LOST', 'Lost item', 1, NULL, 1),
45               ('M', 'Manual fee', 1, NULL, 0),
46               ('N', 'New card fee', 1, NULL, 1),
47               ('OVERDUE', 'Overdue fine', 0, NULL, 1),
48               ('PF', 'Lost item processing fee', 0, NULL, 1),
49               ('RENT', 'Rental fee', 0, NULL, 1),
50               ('RENT_DAILY', 'Daily rental fee', 0, NULL, 1),
51               ('RENT_RENEW', 'Renewal of rental item', 0, NULL, 1),
52               ('RENT_DAILY_RENEW', 'Rewewal of daily rental item', 0, NULL, 1),
53               ('Res', 'Hold fee', 0, NULL, 1)
54         }
55     );
56
57     # Moving MANUAL_INV to account_debit_types
58     $dbh->do(
59         qq{
60             INSERT IGNORE INTO account_debit_types (
61               code,
62               default_amount,
63               description,
64               can_be_added_manually,
65               is_system
66             )
67             SELECT
68               SUBSTR(authorised_value, 1, 64),
69               lib,
70               authorised_value,
71               1,
72               0
73             FROM
74               authorised_values
75             WHERE
76               category = 'MANUAL_INV'
77           }
78     );
79
80     # Adding debit_type_code to accountlines
81     unless ( column_exists('accountlines', 'debit_type_code') ) {
82         $dbh->do(
83             qq{
84                 ALTER IGNORE TABLE accountlines
85                 ADD
86                   debit_type_code varchar(64) DEFAULT NULL
87                 AFTER
88                   accounttype
89               }
90         );
91     }
92
93     # Linking debit_type_code in accountlines to code in account_debit_types
94     unless ( foreign_key_exists( 'accountlines', 'accountlines_ibfk_debit_type' ) ) {
95         $dbh->do(
96             qq{
97             ALTER TABLE accountlines ADD CONSTRAINT `accountlines_ibfk_debit_type` FOREIGN KEY (`debit_type_code`) REFERENCES `account_debit_types` (`code`) ON DELETE RESTRICT ON UPDATE CASCADE
98               }
99         );
100     }
101
102     # Adding a check constraints to accountlines
103     $dbh->do(
104         qq{
105         ALTER TABLE accountlines ADD CONSTRAINT `accountlines_check_type` CHECK (accounttype IS NOT NULL OR debit_type_code IS NOT NULL)
106         }
107     );
108
109     # Populating debit_type_code
110     $dbh->do(
111         qq{
112         UPDATE accountlines SET debit_type_code = accounttype, accounttype = NULL WHERE accounttype IN (SELECT code from account_debit_types)
113         }
114     );
115
116     # Remove MANUAL_INV
117     $dbh->do(
118         qq{
119         DELETE FROM authorised_values WHERE category = 'MANUAL_INV'
120         }
121     );
122     $dbh->do(
123         qq{
124         DELETE FROM authorised_value_categories WHERE category_name = 'MANUAL_INV'
125         }
126     );
127
128     # Add new permission
129     $dbh->do(
130         q{
131             INSERT IGNORE INTO permissions (module_bit, code, description)
132             VALUES
133               (
134                 3,
135                 'manage_accounts',
136                 'Manage Account Debit and Credit Types'
137               )
138         }
139     );
140
141     SetVersion($DBversion);
142     print "Upgrade to $DBversion done (Bug 23049 - Add account debit_types)\n";
143 }