"Fossies" - the Fresh Open Source Software Archive

Member "koha-19.11.15/misc/maintenance/fix_accountlines_rmdupfines_bug8253.pl" (23 Feb 2021, 3131 Bytes) of package /linux/misc/koha-19.11.15.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "fix_accountlines_rmdupfines_bug8253.pl" see the Fossies "Dox" file reference documentation.

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