Bug 22563: (follow-up) Fix tests in Koha/Account.t
[koha-equinox.git] / installer / data / mysql / atomicupdate / bug_22563.perl
1 $DBversion = 'XXX';    # will be replaced by the RM
2 if ( CheckVersion($DBversion) ) {
3
4     # Find and correct pathological cases of LR becoming a credit
5     my $sth = $dbh->prepare( "SELECT accountlines_id, issue_id, borrowernumber, itemnumber, amount, manager_id FROM accountlines WHERE accounttype = 'LR' AND amount < 0" );
6     $sth->execute();
7     while ( my $row = $sth->fetchrow_hashref ) {
8         $dbh->do(
9             "INSERT INTO accountlines (accounttype, issue_id, borrowernumber, itemnumber, amount, manager_id) VALUES ( ?, ?, ?, ?, ?, ? );",
10             {},
11             (
12                 'CR',                   $row->{issue_id},
13                 $row->{borrowernumber}, $row->{itemnumber},
14                 $row->{amount},         $row->{manager_id}
15             )
16         );
17         my $credit_id = $dbh->last_insert_id();
18         my $amount = $row->{amount} * -1;
19         $dbh->do("INSERT INTO account_offsets (credit_id, debit_id, type, amount) VALUES (?,?,?,?);",{},($credit_id, $row->{accountlines_id}, 'Lost Item', $amount));
20         $dbh->do("UPDATE accountlines SET amount = '$amount' WHERE accountlines_id = '$row->{accountlines_id}';");
21     }
22
23     $dbh->do(qq{
24         UPDATE
25           accountlines
26         SET
27           accounttype = 'LOST',
28           status = 'RETURNED'
29         WHERE
30           accounttype = 'LR';
31     });
32
33     # Find and correct pathalogical cases of L having been converted to W
34     $sth = $dbh->prepare( "SELECT accountlines_id, issue_id, borrowernumber, itemnumber, amount, manager_id FROM accountlines WHERE accounttype = 'W' AND itemnumber IS NOT NULL" );
35     $sth->execute();
36     while ( my $row = $sth->fetchrow_hashref ) {
37         my $amount = $row->{amount} * -1;
38         $dbh->do(
39             "INSERT INTO accountlines (accounttype, issue_id, borrowernumber, itemnumber, amount, manager_id) VALUES ( ?, ?, ?, ?, ?, ? );",
40             {},
41             (
42                 'LOST', $row->{issue_id}, $row->{borrowernumber},
43                 $row->{itemnumber}, $amount, $row->{manager_id}
44             )
45         );
46         my $debit_id = $dbh->last_insert_id();
47         $dbh->do("INSERT INTO account_offsets (credit_id, debit_id, type, amount) VALUES (?,?,?,?);",{},($row->{accountlines_id}, $debit_id, 'Lost Item Returned', $amount));
48     }
49
50     $dbh->do(qq{
51         UPDATE
52           accountlines
53         SET
54           accounttype = 'LOST'
55         WHERE
56           accounttype = 'L';
57     });
58
59     $dbh->do(qq{
60         UPDATE
61           accountlines
62         SET
63           accounttype = 'LOST_RETURNED'
64         WHERE
65           accounttype = 'CR';
66     });
67
68     SetVersion($DBversion);
69     print "Upgrade to $DBversion done (Bug 22563 - Fix accounttypes for 'L', 'LR' and 'CR')\n";
70 }