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