608c6aaa7b69be1c35ba447f36d1e9c483d3e8d8
[koha.git] / misc / maintenance / fix_accountlines_rmdupfines_bug8253.pl
1 #!/usr/bin/perl
2 #
3 # Copyright (C) 2012 ByWater Solutions
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use strict;
21 use warnings;
22
23 use Koha::Script;
24 use C4::Context;
25
26 use Getopt::Long qw( GetOptions );
27 use Data::Dumper;
28
29 sub print_usage {
30     print <<_USAGE_
31 $0: Remove duplicate fines
32
33 Due to bug 8253, upgrading from Koha 3.6 to 3.8 may introduce duplicate fines.
34 This script will remove these duplicate fines. To use, repeatably run this
35 script until there are no more duplicates in the database.
36
37 Parameters:
38   --confirm or -c     Confirm you want to run the script.
39   --help or -h        Print out this help message.
40 _USAGE_
41 }
42
43 my $help;
44 my $confirm;
45 my $result = GetOptions(
46     'confirm|c' => \$confirm,
47     'help|h'   => \$help,
48 );
49 if ( $help || !$confirm ) {
50     print_usage();
51     exit 0;
52 }
53
54
55 my $dbh = C4::Context->dbh;
56
57 my $query = "
58     SELECT * FROM accountlines
59     WHERE ( accounttype =  'FU' OR accounttype =  'F' )
60     AND description like '%23:59%'
61     ORDER BY borrowernumber, itemnumber, accountlines_id, description
62 ";
63 my $sth = $dbh->prepare($query);
64 $sth->execute();
65 my $results = $sth->fetchall_arrayref( {} );
66
67 $query =
68 "SELECT * FROM accountlines WHERE description LIKE ? AND description NOT LIKE ?";
69 $sth = $dbh->prepare($query);
70
71 foreach my $keeper (@$results) {
72
73     warn "WORKING ON KEEPER: " . Data::Dumper::Dumper( $keeper );
74     my ($description_to_match) = split( / 23:59/, $keeper->{'description'} );
75     $description_to_match .= '%';
76
77     warn "DESCRIPTION TO MATCH: " . $description_to_match;
78
79     $sth->execute( $description_to_match, $keeper->{'description'} );
80
81     my $has_changed = 0;
82
83     while ( my $f = $sth->fetchrow_hashref() ) {
84
85         warn "DELETING: " . Data::Dumper::Dumper( $f );
86
87         if ( $f->{'amountoutstanding'} < $keeper->{'amountoutstanding'} ) {
88             $keeper->{'amountoutstanding'} = $f->{'amountoutstanding'};
89             $has_changed = 1;
90         }
91
92         my $sql =
93             "DELETE FROM accountlines WHERE accountlines_id = ?";
94         $dbh->do( $sql, undef, $f->{'accountlines_id'} );
95     }
96
97     if ($has_changed) {
98         my $sql =
99             "UPDATE accountlines SET amountoutstanding = ? WHERE accountlines_id = ?";
100         $dbh->do(
101             $sql,                           undef,
102             $keeper->{'amountoutstanding'}, $keeper->{'accountlines_id'}
103         );
104     }
105 }
106
107 exit;