From 6c2d2d13382acbb6aac1ab39e6aecfa16c43954e Mon Sep 17 00:00:00 2001 From: David Cook Date: Thu, 14 Feb 2019 16:43:47 +1100 Subject: [PATCH] Bug 22333: Replace duplicated code in CanBookBeRenewed with GetSoonestRenewDate This patch replaces some duplicated code in CanBookBeRenewed by using the GetSoonestRenewDate function. This patch also refactors some of the auto_renew logic in CanBookBeRenewed, so that it's more readable/maintainable. --- C4/Circulation.pm | 46 ++++++++++++++++++---------------------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/C4/Circulation.pm b/C4/Circulation.pm index fa082104a1..1c8e0b9e99 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -2750,38 +2750,28 @@ sub CanBookBeRenewed { } } - if ( defined $issuing_rule->norenewalbefore - and $issuing_rule->norenewalbefore ne "" ) - { - - # Calculate soonest renewal by subtracting 'No renewal before' from due date - my $soonestrenewal = dt_from_string( $issue->date_due, 'sql' )->subtract( - $issuing_rule->lengthunit => $issuing_rule->norenewalbefore ); + #NOTE: If norenewalbefore is undef, $soonestrenewal will equal $now + my $soonestrenewal = GetSoonestRenewDate($borrowernumber, $itemnumber); + my $now = dt_from_string; - # Depending on syspref reset the exact time, only check the date - if ( C4::Context->preference('NoRenewalBeforePrecision') eq 'date' - and $issuing_rule->lengthunit eq 'days' ) - { - $soonestrenewal->truncate( to => 'day' ); + if ( $issue->auto_renew ){ + if ( $soonestrenewal > $now ){ + return ( 0, "auto_too_soon" ); } - - if ( $soonestrenewal > DateTime->now( time_zone => C4::Context->tz() ) ) - { - return ( 0, "auto_too_soon" ) if $issue->auto_renew; - return ( 0, "too_soon" ); - } - elsif ( $issue->auto_renew ) { - return ( 0, "auto_renew" ); + else { + #Fallback: if norenewalbefore is undef, don't renew before due date + if ( dt_from_string( $issue->date_due, 'sql' ) > $now ){ + return ( 0, "auto_too_soon" ); + } + else { + return ( 0, "auto_renew" ); + } } } - - # Fallback for automatic renewals: - # If norenewalbefore is undef, don't renew before due date. - if ( $issue->auto_renew ) { - my $now = dt_from_string; - return ( 0, "auto_renew" ) - if $now >= dt_from_string( $issue->date_due, 'sql' ); - return ( 0, "auto_too_soon" ); + else { + if ( $soonestrenewal > $now ){ + return ( 0, "too_soon" ); + } } return ( 1, undef ); -- 2.16.4