From dce1672e7cd0e8cf681c93a9a01206ab3de0f279 Mon Sep 17 00:00:00 2001 From: Michael Hafen Date: Thu, 2 Dec 2021 17:20:39 -0700 Subject: [PATCH] Bug 18855: Don't create duplicate overdue fines if an issue is returned Content-Type: text/plain; charset="utf-8" If cronjobs/fines.pl is running during circulation hours, then an issue may be considered for having it's overdue fine updated after the issue has been returned and it's fine status flipped from 'UNRETURNED' to 'RETURNED'. In this case UpdateFine will create a duplicate fine because it can't find the specific accountline for the overdue and unreturned fine. This changes cronjobs/fines.pl to verify the fine hasn't changed while it was working on other overdues. If the fine is no longer 'UNRETURNED' then don't update it. Test plan: 1. find an overdue fine near the end of the list of overdue fines that cronjobs/fines.pl will be considering. 2. start cronjobs/fines.pl. 3. immediately check in or renew the overdue book. 4. once fines.pl is finished observe that a duplicate overdue fine has been created on the patrons account. 5. apply patch. 6. repeat 1 - 4 and observe that the duplicate fine was not created. --- misc/cronjobs/fines.pl | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/misc/cronjobs/fines.pl b/misc/cronjobs/fines.pl index 3edf6ed736..5fe8e880d8 100755 --- a/misc/cronjobs/fines.pl +++ b/misc/cronjobs/fines.pl @@ -41,6 +41,7 @@ use Try::Tiny qw( catch try ); use Koha::Calendar; use Koha::DateUtils qw( dt_from_string output_pref ); use Koha::Patrons; +use Koha::Database; use C4::Log qw( cronlogaction ); my $help; @@ -100,6 +101,7 @@ my $libname = C4::Context->preference('LibraryName'); my $control = C4::Context->preference('CircControl'); my $mode = C4::Context->preference('finesMode'); my $delim = "\t"; # ? C4::Context->preference('CSVDelimiter') || "\t"; +my $dbh = Koha::Database->dbh; my %is_holiday; my $today = dt_from_string(); @@ -157,16 +159,28 @@ for my $overdue ( @{$overdues} ) { && ( $amount && $amount > 0 ) ) { - UpdateFine( + # Make sure the fine hasn't changed since the script started. + $dbh->do('LOCK TABLES accountlines'); + my $fines = Koha::Account::Lines->search( { - issue_id => $overdue->{issue_id}, - itemnumber => $overdue->{itemnumber}, - borrowernumber => $overdue->{borrowernumber}, - amount => $amount, - due => output_pref($datedue), + issue_id => $overdue->{issue_id}, + debit_type_code => 'OVERDUE', + status => 'UNRETURNED', } ); - $updated++; + if ( $fines->count == 1 ) { + UpdateFine( + { + issue_id => $overdue->{issue_id}, + itemnumber => $overdue->{itemnumber}, + borrowernumber => $overdue->{borrowernumber}, + amount => $amount, + due => output_pref($datedue), + } + ); + $updated++; + } + $dbh->do('UNLOCK TABLES'); } my $borrower = $patron->unblessed; if ($filename) { -- 2.25.1