Bug 14957: Merge rules system for merging of MARC records
[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 BEGIN {
24     # find Koha's Perl modules
25     # test carefully before changing this
26     use FindBin ();
27     eval { require "$FindBin::Bin/../kohalib.pl" };
28 }
29
30 use Koha::Script;
31 use C4::Context;
32
33 use Getopt::Long qw( GetOptions );
34 use Data::Dumper;
35
36 sub print_usage {
37     print <<_USAGE_
38 $0: Remove duplicate fines
39
40 Due to bug 8253, upgrading from Koha 3.6 to 3.8 may introduce duplicate fines.
41 This script will remove these duplicate fines. To use, repeatably run this
42 script until there are no more duplicates in the database.
43
44 Parameters:
45   --confirm or -c     Confirm you want to run the script.
46   --help or -h        Print out this help message.
47 _USAGE_
48 }
49
50 my $help;
51 my $confirm;
52 my $result = GetOptions(
53     'confirm|c' => \$confirm,
54     'help|h'   => \$help,
55 );
56 if ( $help || !$confirm ) {
57     print_usage();
58     exit 0;
59 }
60
61
62 my $dbh = C4::Context->dbh;
63
64 my $query = "
65     SELECT * FROM accountlines
66     WHERE ( accounttype =  'FU' OR accounttype =  'F' )
67     AND description like '%23:59%'
68     ORDER BY borrowernumber, itemnumber, accountlines_id, description
69 ";
70 my $sth = $dbh->prepare($query);
71 $sth->execute();
72 my $results = $sth->fetchall_arrayref( {} );
73
74 $query =
75 "SELECT * FROM accountlines WHERE description LIKE ? AND description NOT LIKE ?";
76 $sth = $dbh->prepare($query);
77
78 foreach my $keeper (@$results) {
79
80     warn "WORKING ON KEEPER: " . Data::Dumper::Dumper( $keeper );
81     my ($description_to_match) = split( / 23:59/, $keeper->{'description'} );
82     $description_to_match .= '%';
83
84     warn "DESCRIPTION TO MATCH: " . $description_to_match;
85
86     $sth->execute( $description_to_match, $keeper->{'description'} );
87
88     my $has_changed = 0;
89
90     while ( my $f = $sth->fetchrow_hashref() ) {
91
92         warn "DELETING: " . Data::Dumper::Dumper( $f );
93
94         if ( $f->{'amountoutstanding'} < $keeper->{'amountoutstanding'} ) {
95             $keeper->{'amountoutstanding'} = $f->{'amountoutstanding'};
96             $has_changed = 1;
97         }
98
99         my $sql =
100             "DELETE FROM accountlines WHERE accountlines_id = ?";
101         $dbh->do( $sql, undef, $f->{'accountlines_id'} );
102     }
103
104     if ($has_changed) {
105         my $sql =
106             "UPDATE accountlines SET amountoutstanding = ? WHERE accountlines_id = ?";
107         $dbh->do(
108             $sql,                           undef,
109             $keeper->{'amountoutstanding'}, $keeper->{'accountlines_id'}
110         );
111     }
112 }
113
114 exit;