From 859d1f197cb9076e77d86b8006c485af17a72230 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 10 Aug 2020 16:39:08 +0200 Subject: [PATCH] Bug 18501: Prepare the ground This code was duplicated and we are going to need it. --- C4/Circulation.pm | 61 +++++++---------------------------------------- Koha/Item.pm | 39 ++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 53 deletions(-) diff --git a/C4/Circulation.pm b/C4/Circulation.pm index a9cf02ca3d..1b298bcb7f 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -52,7 +52,6 @@ use Koha::Database; use Koha::Libraries; use Koha::Account::Lines; use Koha::Holds; -use Koha::RefundLostItemFeeRules; use Koha::Account::Lines; use Koha::Account::Offsets; use Koha::Config::SysPrefs; @@ -1457,32 +1456,7 @@ sub AddIssue { ## If item was lost, it has now been found, reverse any list item charges if necessary. if ( $item_object->itemlost ) { - my $refund = 1; - my $no_refund_after_days = C4::Context->preference('NoRefundOnLostReturnedItemsAge'); - if ($no_refund_after_days) { - my $today = dt_from_string(); - my $lost_age_in_days = - dt_from_string( $item_object->itemlost_on ) - ->delta_days($today) - ->in_units('days'); - - $refund = 0 unless ( $lost_age_in_days < $no_refund_after_days ); - } - - if ( - $refund - && Koha::RefundLostItemFeeRules->should_refund( - { - current_branch => C4::Context->userenv->{branch}, - item_home_branch => $item_object->homebranch, - item_holding_branch => $item_object->holdingbranch, - } - ) - ) - { - _FixAccountForLostAndFound( $item_object->itemnumber, undef, - $item_object->barcode ); - } + $item_object->set_found; } $item_object->issues( ( $item_object->issues || 0 ) + 1); @@ -2060,33 +2034,14 @@ sub AddReturn { if ( $item->itemlost ) { $messages->{'WasLost'} = 1; unless ( C4::Context->preference("BlockReturnOfLostItems") ) { - my $refund = 1; - my $no_refund_after_days = C4::Context->preference('NoRefundOnLostReturnedItemsAge'); - if ($no_refund_after_days) { - my $today = dt_from_string(); - my $lost_age_in_days = - dt_from_string( $item->itemlost_on ) - ->delta_days($today) - ->in_units('days'); - - $refund = 0 unless ( $lost_age_in_days < $no_refund_after_days ); - } + my $refunded = $item->set_found( + { + holdingbranch => $item_holding_branch, + borrowernumber => $borrowernumber + } + ); - if ( - $refund && - Koha::RefundLostItemFeeRules->should_refund( - { - current_branch => C4::Context->userenv->{branch}, - item_home_branch => $item->homebranch, - item_holding_branch => $item_holding_branch - } - ) - ) - { - _FixAccountForLostAndFound( $item->itemnumber, - $borrowernumber, $barcode ); - $messages->{'LostItemFeeRefunded'} = 1; - } + $messages->{'LostItemFeeRefunded'} = $refunded; } } diff --git a/Koha/Item.pm b/Koha/Item.pm index 29406b551d..9f32beefd9 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -41,6 +41,7 @@ use Koha::Item::Transfers; use Koha::Patrons; use Koha::Plugins; use Koha::Libraries; +use Koha::RefundLostItemFeeRules; use Koha::StockRotationItem; use Koha::StockRotationRotas; @@ -762,6 +763,44 @@ sub renewal_branchcode { return $branchcode; } +sub set_found { + my ($self, $params) = @_; + + my $holdingbranch = $params->{holdingbranch} || $self->holdingbranch; + my $borrowernumber = $params->{borrowernumber} || undef; + + ## If item was lost, it has now been found, reverse any list item charges if necessary. + my $refund = 1; + my $no_refund_after_days = + C4::Context->preference('NoRefundOnLostReturnedItemsAge'); + if ($no_refund_after_days) { + my $today = dt_from_string(); + my $lost_age_in_days = + dt_from_string( $self->itemlost_on )->delta_days($today) + ->in_units('days'); + + $refund = 0 unless ( $lost_age_in_days < $no_refund_after_days ); + } + + my $refunded; + if ( + $refund + && Koha::RefundLostItemFeeRules->should_refund( + { + current_branch => C4::Context->userenv->{branch}, + item_home_branch => $self->homebranch, + item_holding_branch => $holdingbranch, + } + ) + ) + { + _FixAccountForLostAndFound( $self->itemnumber, borrowernumber, $self->barcode ); + $refunded = 1; + } + + return $refunded; +} + =head3 to_api_mapping This method returns the mapping for representing a Koha::Item object -- 2.20.1