Bug 22610: (QA follow-up) Make upgrade query more accurate
[koha.git] / installer / data / mysql / atomicupdate / bug_22610.perl
1 $DBversion = 'XXX';    # will be replaced by the RM
2 if ( CheckVersion($DBversion) ) {
3
4     $dbh->do(qq{
5         INSERT INTO
6           authorised_values (category,authorised_value,lib)
7         VALUES
8           ('PAYMENT_TYPE','SIP00','Cash via SIP2'),
9           ('PAYMENT_TYPE','SIP01','VISA via SIP2'),
10           ('PAYMENT_TYPE','SIP02','Creditcard via SIP2')
11     });
12
13     $dbh->do(qq{
14         UPDATE
15           accountlines
16         SET
17           accounttype  = 'Pay',
18           payment_type = 'SIP00'
19         WHERE
20           accounttype = 'Pay00';
21     });
22
23     $dbh->do(qq{
24         UPDATE
25           accountlines
26         SET
27           accounttype  = 'Pay',
28           payment_type = 'SIP01'
29         WHERE
30           accounttype = 'Pay01';
31     });
32
33     $dbh->do(qq{
34         UPDATE
35           accountlines
36         SET
37           accounttype  = 'Pay',
38           payment_type = 'SIP02'
39         WHERE
40           accounttype = 'Pay02';
41     });
42
43     my $sth = $dbh->prepare( qq{SELECT * FROM accountlines WHERE accounttype REGEXP '^Pay[[:digit:]]{2}$' } );
44     $sth->execute();
45     my $seen = {};
46     while (my $row = $sth->fetchrow_hashref) {
47         my $type = $row->{accounttype};
48         my $sipcode = $type;
49         $sipcode =~ s/Pay/SIP/g;
50         unless ($seen->{$sipcode}) {
51             $dbh->do(qq{
52                 INSERT INTO
53                   authorised_values (category,authorised_value,lib)
54                 VALUES
55                   ('PAYMENT_TYPE',"$sipcode",'Unrecognised SIP2 payment type')
56             });
57
58              $dbh->do(qq{
59                 UPDATE
60                   accountlines
61                 SET
62                   accounttype  = 'Pay',
63                   payment_type = "$sipcode"
64                 WHERE
65                   accounttype = "$type";
66             });
67
68             $seen->{$sipcode} = 1;
69         }
70     }
71
72     SetVersion($DBversion);
73     print "Upgrade to $DBversion done (Bug 22610 - Fix accounttypes for SIP2 payments)\n";
74 }