From a1641914f63532a489476e0fd393ecef7d94982e Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 4 Jun 2020 10:53:05 +0100 Subject: [PATCH] Bug 23091: Add handling for restore case This patch adds handling for the 'restore' case of lostreturn policy. --- C4/Circulation.pm | 70 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 4 deletions(-) diff --git a/C4/Circulation.pm b/C4/Circulation.pm index aadebf9adf..c0bdbd1b23 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -2071,6 +2071,8 @@ sub AddReturn { $messages->{'LostItemFeeCharged'} = 1; } elsif ( $lostreturn_policy eq 'restore' ) { + _RestoreOverdueForLostAndFound( $item->itemnumber, + $borrowernumber ); $messages->{'LostItemFeeRestored'} = 1; } } @@ -2477,15 +2479,12 @@ sub _FixOverduesOnReturn { $credit->apply({ debits => [ $accountline ], offset_type => 'Forgiven' }); - $accountline->status('FORGIVEN'); - if (C4::Context->preference("FinesLog")) { &logaction("FINES", 'MODIFY',$borrowernumber,"Overdue forgiven: item $item"); } - } else { - $accountline->status($status); } + $accountline->status($status); return $accountline->store(); } ); @@ -2582,6 +2581,69 @@ sub _FixAccountForLostAndFound { return ($credit) ? $credit->id : undef; } +=head2 _RestoreOverdueForLostAndFound + + &_RestoreOverdueForLostAndFound($borrowernumber, $itemnumber ); + +C<$borrowernumber> borrowernumber + +C<$itemnumber> itemnumber + +Internal function + +=cut + +sub _RestoreOverdueForLostAndFound { + my ( $borrowernumber, $item ) = @_; + + unless( $borrowernumber ) { + warn "_RestoreOverdueForLostAndFound() not supplied valid borrowernumber"; + return; + } + unless( $item ) { + warn "_RestoreOverdueForLostAndFound() not supplied valid itemnumber"; + return; + } + + my $schema = Koha::Database->schema; + + my $result = $schema->txn_do( + sub { + # check for lost overdue fine + my $accountlines = Koha::Account::Lines->search( + { + borrowernumber => $borrowernumber, + itemnumber => $item, + debit_type_code => 'OVERDUE', + status => 'LOST' + }, + { + order_by => { '-desc' => 'date' }, + rows => 1 + } + ); + return 0 unless $accountlines->count; # no warning, there's just nothing to fix + + # Find related forgive credit + my $overdue = $accountlines->next; + my $refunds = $overdue->credits( + { + credit_type_code => 'FORGIVEN', + itemnumber => $item, + status => { '!=' => 'VOID' } + }, + { order_by => { '-desc' => 'date' }, rows => 1 } + ); + return 0 unless $refunds->count; # no warning, there's just nothing to fix + + # Revert the forgive credit + my $refund = $refunds->next; + return $refund->void(); + } + ); + + return $result; +} =head2 _GetCircControlBranch my $circ_control_branch = _GetCircControlBranch($iteminfos, $borrower); -- 2.20.1