abd55ad3ced4a8ab7b26bed2febf825558afc7c4
[koha.git] / 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               ('W', 'Writeoff', 0, 1),
41               ('WO', 'Writeoff', 0, 1),
42               ('FORGIVEN', 'Forgiven', 1, 1),
43               ('CREDIT', 'Credit', 1, 1),
44               ('LOST_RETURN', 'Lost item fee refund', 0, 1)
45         }
46     );
47
48     # Adding credit_type_code to accountlines
49     unless ( column_exists('accountlines', 'credit_type_code') ) {
50         $dbh->do(
51             qq{
52                 ALTER IGNORE TABLE accountlines
53                 ADD
54                   credit_type_code varchar(80) DEFAULT NULL
55                 AFTER
56                   accounttype
57               }
58         );
59     }
60
61     # Linking credit_type_code in accountlines to code in account_credit_types
62     unless ( foreign_key_exists( 'accountlines', 'accountlines_ibfk_credit_type' ) ) {
63         $dbh->do(
64             qq{
65             ALTER TABLE accountlines ADD CONSTRAINT `accountlines_ibfk_credit_type` FOREIGN KEY (`credit_type_code`) REFERENCES `account_credit_types` (`code`) ON DELETE SET NULL ON UPDATE CASCADE
66               }
67         );
68     }
69
70     # Dropping the check constraint in accountlines
71     $dbh->do(
72         qq{
73         ALTER TABLE accountlines ADD CONSTRAINT `accountlines_check_type` CHECK (accounttype IS NOT NULL OR credit_type_code IS NOT NULL)
74         }
75     );
76
77     # Update accountype 'C' to 'CREDIT'
78     $dbh->do(
79         qq{
80           UPDATE accountlines SET accounttype = 'CREDIT' WHERE accounttype = 'C' OR accounttype = 'CR'
81         }
82     );
83
84     # Update accountype 'FOR' to 'FORGIVEN'
85     $dbh->do(
86         qq{
87           UPDATE accountlines SET accounttype = 'FORGIVEN' WHERE accounttype = 'FOR' OR accounttype = 'FORW'
88         }
89     );
90
91     # Update accountype 'Pay' to 'PAYMENT'
92     $dbh->do(
93         qq{
94           UPDATE accountlines SET accounttype = 'PAYMENT' WHERE accounttype = 'Pay' OR accounttype = 'PAY'
95         }
96     );
97
98     # Populating credit_type_code
99     $dbh->do(
100         qq{
101         UPDATE accountlines SET credit_type_code = accounttype, accounttype = NULL WHERE accounttype IN (SELECT code from account_credit_types)
102         }
103     );
104
105     # Adding a check constraints to accountlines
106     $dbh->do(
107         qq{
108         ALTER TABLE accountlines ADD CONSTRAINT `accountlines_check_type` CHECK (accounttype IS NOT NULL OR credit_type_code IS NOT NULL)
109         }
110     );
111
112     SetVersion($DBversion);
113     print "Upgrade to $DBversion done (Bug 23049 - Add account debit_credit)\n";
114 }