From ce4ad972e52fe45f75589fa13d9820e3ee1e563d Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Wed, 23 Feb 2022 16:25:21 +0000 Subject: [PATCH] Bug 30168: Use checkout object in GetSoonestRenewDate Signed-off-by: David Nind Signed-off-by: Katrin Fischer Signed-off-by: Tomas Cohen Arazi --- C4/Circulation.pm | 29 ++++++++++------------------- t/db_dependent/Circulation.t | 10 +++++----- 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/C4/Circulation.pm b/C4/Circulation.pm index 39ce5f252d..f4f772cbee 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -2982,7 +2982,7 @@ sub CanBookBeRenewed { } return ( 0, $auto_renew, { soonest_renew_date => $soonest } ) if $auto_renew =~ 'too_soon';#$auto_renew ne "no" && $auto_renew ne "ok"; - $soonest = GetSoonestRenewDate($borrowernumber, $itemnumber); + $soonest = GetSoonestRenewDate($issue); if ( $soonest > dt_from_string() ){ return (0, "too_soon", { soonest_renew_date => $soonest } ) unless $override_limit; } @@ -3272,14 +3272,11 @@ sub GetRenewCount { =head2 GetSoonestRenewDate - $NoRenewalBeforeThisDate = &GetSoonestRenewDate($borrowernumber, $itemnumber); + $NoRenewalBeforeThisDate = &GetSoonestRenewDate($issue); Find out the soonest possible renew date of a borrowed item. -C<$borrowernumber> is the borrower number of the patron who currently -has the item on loan. - -C<$itemnumber> is the number of the item to renew. +C<$checkout> is the checkout object to renew. C<$GetSoonestRenewDate> returns the DateTime of the soonest possible renew date, based on the value "No renewal before" of the applicable @@ -3290,16 +3287,10 @@ cannot be found. =cut sub GetSoonestRenewDate { - my ( $borrowernumber, $itemnumber ) = @_; + my ( $checkout ) = @_; - my $dbh = C4::Context->dbh; - - my $item = Koha::Items->find($itemnumber) or return; - my $itemissue = $item->checkout or return; - - $borrowernumber ||= $itemissue->borrowernumber; - my $patron = Koha::Patrons->find( $borrowernumber ) - or return; + my $item = $checkout->item or return; + my $patron = $checkout->patron or return; my $branchcode = _GetCircControlBranch( $item->unblessed, $patron->unblessed ); my $issuing_rule = Koha::CirculationRules->get_effective_rules( @@ -3319,7 +3310,7 @@ sub GetSoonestRenewDate { and $issuing_rule->{norenewalbefore} ne "" ) { my $soonestrenewal = - dt_from_string( $itemissue->date_due )->subtract( + dt_from_string( $checkout->date_due )->subtract( $issuing_rule->{lengthunit} => $issuing_rule->{norenewalbefore} ); if ( C4::Context->preference('NoRenewalBeforePrecision') eq 'date' @@ -3328,9 +3319,9 @@ sub GetSoonestRenewDate { $soonestrenewal->truncate( to => 'day' ); } return $soonestrenewal if $now < $soonestrenewal; - } elsif ( $itemissue->auto_renew && $patron->autorenew_checkouts ) { + } elsif ( $checkout->auto_renew && $patron->autorenew_checkouts ) { # Checkouts with auto-renewing fall back to due date - my $soonestrenewal = dt_from_string( $itemissue->date_due ); + my $soonestrenewal = dt_from_string( $checkout->date_due ); if ( C4::Context->preference('NoRenewalBeforePrecision') eq 'date' and $issuing_rule->{lengthunit} eq 'days' ) { @@ -4423,7 +4414,7 @@ sub _CanBookBeAutoRenewed { } } - my $soonest = GetSoonestRenewDate($patron->id, $item->id); + my $soonest = GetSoonestRenewDate($issue); if ( $soonest > dt_from_string() ) { return ( "auto_too_soon", $soonest ); diff --git a/t/db_dependent/Circulation.t b/t/db_dependent/Circulation.t index 53fb268024..b12e1b27f0 100755 --- a/t/db_dependent/Circulation.t +++ b/t/db_dependent/Circulation.t @@ -5620,7 +5620,7 @@ subtest "GetSoonestRenewDate tests" => sub { # Test 'exact time' setting for syspref NoRenewalBeforePrecision t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'exact_time' ); is( - GetSoonestRenewDate( $patron->id, $item->itemnumber ), + GetSoonestRenewDate( $issue ), $datedue->clone->add( days => -7 ), 'Bug 14395: Renewals permitted 7 days before due date, as expected' ); @@ -5629,7 +5629,7 @@ subtest "GetSoonestRenewDate tests" => sub { # Test 'date' setting for syspref NoRenewalBeforePrecision t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'date' ); is( - GetSoonestRenewDate( $patron->id, $item->itemnumber ), + GetSoonestRenewDate( $issue ), $datedue->clone->add( days => -7 )->truncate( to => 'day' ), 'Bug 14395: Renewals permitted 7 days before due date, as expected' ); @@ -5646,7 +5646,7 @@ subtest "GetSoonestRenewDate tests" => sub { ); is( - GetSoonestRenewDate( $patron->id, $item->itemnumber ), + GetSoonestRenewDate( $issue ), dt_from_string, 'Checkouts without auto-renewal can be renewed immediately if no norenewalbefore' ); @@ -5654,13 +5654,13 @@ subtest "GetSoonestRenewDate tests" => sub { t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'date' ); $issue->auto_renew(1)->store; is( - GetSoonestRenewDate( $patron->id, $item->itemnumber ), + GetSoonestRenewDate( $issue ), $datedue->clone->truncate( to => 'day' ), 'Checkouts with auto-renewal can be renewed earliest on due date if no renewalbefore' ); t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'exact' ); is( - GetSoonestRenewDate( $patron->id, $item->itemnumber ), + GetSoonestRenewDate( $issue ), $datedue, 'Checkouts with auto-renewal can be renewed earliest on due date if no renewalbefore' ); -- 2.39.5