@@ -, +, @@ --- installer/data/mysql/updatedatabase.pl | 53 ++++++++++++++++++++++++++++++++ 1 files changed, 53 insertions(+), 0 deletions(-) --- a/installer/data/mysql/updatedatabase.pl +++ a/installer/data/mysql/updatedatabase.pl @@ -5369,6 +5369,59 @@ if ( C4::Context->preference("Version") < TransformToNum($DBversion) ) { SetVersion($DBversion); } +$DBversion ="3.09.00.XXX"; +if ( C4::Context->preference("Version") < TransformToNum($DBversion) ) { + my $query = " +SELECT a.* FROM accountlines a +INNER JOIN accountlines b ON ( + a.borrowernumber = b.borrowernumber + AND a.itemnumber = b.itemnumber + AND a.accounttype = b.accounttype +) +WHERE ( + a.accounttype = 'FU' + OR a.accounttype = 'F' +) +AND a.accountno <> b.accountno +GROUP BY a.borrowernumber, a.itemnumber, a.accountno, a.description +ORDER BY a.description ASC +"; + my $sth = $dbh->prepare( $query ); + $sth->execute(); + my $results = $sth->fetchall_arrayref({}); + + my @fines; + while ( $fines[0] = shift( @$results ) ) { + my $desc_to_match = $fines[0]->{'description'}; + + while ( $results->[0]->{'description'} =~ m|^$desc_to_match| ) { + push( @fines, shift( @$results ) ); + } + + ## The last should always be the keeper + my $keeper = pop( @fines ); + my $has_changed = 0; + + foreach my $f ( @fines ) { + if ( $f->{'amountoutstanding'} < $keeper->{'amountoutstanding'} ) { + $keeper->{'amountoutstanding'} = $f->{'amountoutstanding'}; + $has_changed = 1; + } + + my $sql = "DELETE FROM accountlines WHERE borrowernumber = ? AND accountno = ? AND itemnumber = ? AND date = ? AND description = ?"; + $dbh->do($sql,undef,$f->{'borrowernumber'},$f->{'accountno'},$f->{'itemnumber'}, $f->{'date'}, $f->{'description'}); + } + + if ( $has_changed ) { + my $sql = "UPDATE accountlines SET amountoutstanding = ? WHERE borrowernumber = ? AND accountno = ? AND itemnumber = ? AND date = ? AND description = ?"; + $dbh->do($sql,undef,$keeper->{'amountoutstanding'},$keeper->{'borrowernumber'},$keeper->{'accountno'},$keeper->{'itemnumber'}, $keeper->{'date'}, $keeper->{'description'}); + } + @fines = (); + } + + print "Upgrade to $DBversion done (Remove duplicate fines))\n"; + SetVersion($DBversion); +} =head1 FUNCTIONS --