From cf0fffd66bb4e3de0a69409c094cf4e5a90cb435 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Mon, 30 Dec 2019 19:27:38 +0000 Subject: [PATCH] Bug 19014: Return auto_too_soon before on_reserve When calling CanBookBeRenewed we do check for 'too_soon', however, a reserve makes the return 'on_reserve' before other statuses. It seems the patron specific reasons should be returned first. This patch places the 'on_reserve' check after too_soon and others To test: 1 - Checkout an item to a patron with a due date 2 days in the future and 'No renewal before' unset 2 - Place a hold on that item 3 - Define an AUTO_RENEWALS circulation notice with below at a minimum [% checkout.auto_renew_error %] 4 - perl misc/cronjobs/automatic_renewals.pl --send-notices 5 - Patron is not notified 6 - Set 'No renewal before' to 1 7 - run cronjob 8 - Patron is notified with error 'on_reserve' 9 - Set 'No renewal before' to 3 10 - UPDATE issues SET auto_renew_error = NULL; 11 - run cronjob 12 - Patron is notified with error 'on_reserve' 13 - APPLY PATCH 14 - Unset 'No renewal before' 15 - UPDATE issues SET auto_renew_error = NULL; 16 - run cronjob 17 - Patron is not notified 18 - UPDATE issues SET auto_renew_error = NULL; 19 - Set 'No renewal before' to 1 20 - run cronjob 21 - Patron is notified with error 'on_reserve' 22 - Set 'No renewal before' to 3 23 - UPDATE issues SET auto_renew_error = NULL; 24 - run cronjob 25 - Patron is not notified Signed-off-by: Andrew Fuerste-Henry --- C4/Circulation.pm | 197 +++++++++++++++++++++++++++--------------------------- 1 file changed, 99 insertions(+), 98 deletions(-) diff --git a/C4/Circulation.pm b/C4/Circulation.pm index c3629c2472..8978ba016d 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -2676,6 +2676,105 @@ sub CanBookBeRenewed { my $patron = $issue->patron or return; + # override_limit will override anything else except on_reserve + unless ( $override_limit ){ + my $branchcode = _GetCircControlBranch( $item->unblessed, $patron->unblessed ); + my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule( + { categorycode => $patron->categorycode, + itemtype => $item->effective_itemtype, + branchcode => $branchcode + } + ); + + return ( 0, "too_many" ) + if not $issuing_rule or $issuing_rule->renewalsallowed <= $issue->renewals; + + my $overduesblockrenewing = C4::Context->preference('OverduesBlockRenewing'); + my $restrictionblockrenewing = C4::Context->preference('RestrictionBlockRenewing'); + $patron = Koha::Patrons->find($borrowernumber); # FIXME Is this really useful? + my $restricted = $patron->is_debarred; + my $hasoverdues = $patron->has_overdues; + + if ( $restricted and $restrictionblockrenewing ) { + return ( 0, 'restriction'); + } elsif ( ($hasoverdues and $overduesblockrenewing eq 'block') || ($issue->is_overdue and $overduesblockrenewing eq 'blockitem') ) { + return ( 0, 'overdue'); + } + + if ( $issue->auto_renew ) { + + if ( $patron->category->effective_BlockExpiredPatronOpacActions and $patron->is_expired ) { + return ( 0, 'auto_account_expired' ); + } + + if ( defined $issuing_rule->no_auto_renewal_after + and $issuing_rule->no_auto_renewal_after ne "" ) { + # Get issue_date and add no_auto_renewal_after + # If this is greater than today, it's too late for renewal. + my $maximum_renewal_date = dt_from_string($issue->issuedate, 'sql'); + $maximum_renewal_date->add( + $issuing_rule->lengthunit => $issuing_rule->no_auto_renewal_after + ); + my $now = dt_from_string; + if ( $now >= $maximum_renewal_date ) { + return ( 0, "auto_too_late" ); + } + } + if ( defined $issuing_rule->no_auto_renewal_after_hard_limit + and $issuing_rule->no_auto_renewal_after_hard_limit ne "" ) { + # If no_auto_renewal_after_hard_limit is >= today, it's also too late for renewal + if ( dt_from_string >= dt_from_string( $issuing_rule->no_auto_renewal_after_hard_limit ) ) { + return ( 0, "auto_too_late" ); + } + } + + if ( C4::Context->preference('OPACFineNoRenewalsBlockAutoRenew') ) { + my $fine_no_renewals = C4::Context->preference("OPACFineNoRenewals"); + my $amountoutstanding = + C4::Context->preference("OPACFineNoRenewalsIncludeCredit") + ? $patron->account->balance + : $patron->account->outstanding_debits->total_outstanding; + if ( $amountoutstanding and $amountoutstanding > $fine_no_renewals ) { + return ( 0, "auto_too_much_oweing" ); + } + } + } + + 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 ); + + # 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 ( $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" ); + } + } + + # 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" ); + } + } + my ( $resfound, $resrec, undef ) = C4::Reserves::CheckReserves($itemnumber); # This item can fill one or more unfilled reserve, can those unfilled reserves @@ -2743,104 +2842,6 @@ sub CanBookBeRenewed { } return ( 0, "on_reserve" ) if $resfound; # '' when no hold was found - return ( 1, undef ) if $override_limit; - - my $branchcode = _GetCircControlBranch( $item->unblessed, $patron->unblessed ); - my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule( - { categorycode => $patron->categorycode, - itemtype => $item->effective_itemtype, - branchcode => $branchcode - } - ); - - return ( 0, "too_many" ) - if not $issuing_rule or $issuing_rule->renewalsallowed <= $issue->renewals; - - my $overduesblockrenewing = C4::Context->preference('OverduesBlockRenewing'); - my $restrictionblockrenewing = C4::Context->preference('RestrictionBlockRenewing'); - $patron = Koha::Patrons->find($borrowernumber); # FIXME Is this really useful? - my $restricted = $patron->is_debarred; - my $hasoverdues = $patron->has_overdues; - - if ( $restricted and $restrictionblockrenewing ) { - return ( 0, 'restriction'); - } elsif ( ($hasoverdues and $overduesblockrenewing eq 'block') || ($issue->is_overdue and $overduesblockrenewing eq 'blockitem') ) { - return ( 0, 'overdue'); - } - - if ( $issue->auto_renew ) { - - if ( $patron->category->effective_BlockExpiredPatronOpacActions and $patron->is_expired ) { - return ( 0, 'auto_account_expired' ); - } - - if ( defined $issuing_rule->no_auto_renewal_after - and $issuing_rule->no_auto_renewal_after ne "" ) { - # Get issue_date and add no_auto_renewal_after - # If this is greater than today, it's too late for renewal. - my $maximum_renewal_date = dt_from_string($issue->issuedate, 'sql'); - $maximum_renewal_date->add( - $issuing_rule->lengthunit => $issuing_rule->no_auto_renewal_after - ); - my $now = dt_from_string; - if ( $now >= $maximum_renewal_date ) { - return ( 0, "auto_too_late" ); - } - } - if ( defined $issuing_rule->no_auto_renewal_after_hard_limit - and $issuing_rule->no_auto_renewal_after_hard_limit ne "" ) { - # If no_auto_renewal_after_hard_limit is >= today, it's also too late for renewal - if ( dt_from_string >= dt_from_string( $issuing_rule->no_auto_renewal_after_hard_limit ) ) { - return ( 0, "auto_too_late" ); - } - } - - if ( C4::Context->preference('OPACFineNoRenewalsBlockAutoRenew') ) { - my $fine_no_renewals = C4::Context->preference("OPACFineNoRenewals"); - my $amountoutstanding = - C4::Context->preference("OPACFineNoRenewalsIncludeCredit") - ? $patron->account->balance - : $patron->account->outstanding_debits->total_outstanding; - if ( $amountoutstanding and $amountoutstanding > $fine_no_renewals ) { - return ( 0, "auto_too_much_oweing" ); - } - } - } - - 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 ); - - # 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 ( $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" ); - } - } - - # 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" ); - } - return ( 1, undef ); } -- 2.11.0