Bug 14101: Automatic renewals exactly on due date
authorHolger Meißner <h.meissner.82@web.de>
Tue, 2 Jun 2015 13:39:21 +0000 (15:39 +0200)
committerTomas Cohen Arazi <tomascohen@theke.io>
Fri, 20 Nov 2015 13:13:47 +0000 (10:13 -0300)
If no value for 'no renewal before' is specified, automatic renewal now
falls back on the due date. Also 'no renewal before' can now be zero, so
both automatic and manual renewals can be delayed until due date.

Test plan:

1) Create some circulation rules with different settings for 'No renewal
   before' and 'Automatic renewal'. Both daily and hourly loans should
   work.

2) Try to renew both manually and automatically before and after a renewal
   should be possible. You can run misc/cronjobs/automatic_renewals.pl for
   automatic renewal.

3) Confirm that:
   * Both automatic and manual renewal with 'No renewal before' set
     to 0 do not happen before the due date (exact DateTime).
   * Manual renewal with 'No renewal before' set to undef (enter empty
     string) is unrestricted.
   * Automatic renewal with 'No renewal before' set to undef does not
     happen before the due date.

Sponsored-by: Hochschule für Gesundheit (hsg), Germany
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
C4/Circulation.pm
admin/smart-rules.pl

index e75ec5e..35651f2 100644 (file)
@@ -2817,12 +2817,15 @@ sub CanBookBeRenewed {
         return ( 0, 'overdue');
     }
 
-    if ( $issuingrule->{norenewalbefore} ) {
+    if ( defined $issuingrule->{norenewalbefore}
+        and $issuingrule->{norenewalbefore} ne "" )
+    {
 
         # Get current time and add norenewalbefore.
         # If this is smaller than date_due, it's too soon for renewal.
+        my $now = dt_from_string;
         if (
-            DateTime->now( time_zone => C4::Context->tz() )->add(
+            $now->add(
                 $issuingrule->{lengthunit} => $issuingrule->{norenewalbefore}
             ) < $itemissue->{date_due}
           )
@@ -2830,9 +2833,20 @@ sub CanBookBeRenewed {
             return ( 0, "auto_too_soon" ) if $itemissue->{auto_renew};
             return ( 0, "too_soon" );
         }
+        elsif ( $itemissue->{auto_renew} ) {
+            return ( 0, "auto_renew" );
+        }
+    }
+
+    # Fallback for automatic renewals:
+    # If norenewalbefore is undef, don't renew before due date.
+    elsif ( $itemissue->{auto_renew} ) {
+        my $now = dt_from_string;
+        return ( 0, "auto_renew" )
+          if $now >= $itemissue->{date_due};
+        return ( 0, "auto_too_soon" );
     }
 
-    return ( 0, "auto_renew" ) if $itemissue->{auto_renew};
     return ( 1, undef );
 }
 
@@ -3044,9 +3058,11 @@ sub GetSoonestRenewDate {
     my $issuingrule =
       GetIssuingRule( $borrower->{categorycode}, $item->{itype}, $branchcode );
 
-    my $now = DateTime->now( time_zone => C4::Context->tz() );
+    my $now = dt_from_string;
 
-    if ( $issuingrule->{norenewalbefore} ) {
+    if ( defined $issuingrule->{norenewalbefore}
+        and $issuingrule->{norenewalbefore} ne "" )
+    {
         my $soonestrenewal =
           $itemissue->{date_due}->subtract(
             $issuingrule->{lengthunit} => $issuingrule->{norenewalbefore} );
index d43c686..253b08a 100755 (executable)
@@ -117,7 +117,7 @@ elsif ($op eq 'add') {
     my $renewalsallowed  = $input->param('renewalsallowed');
     my $renewalperiod    = $input->param('renewalperiod');
     my $norenewalbefore  = $input->param('norenewalbefore');
-    $norenewalbefore = undef if $norenewalbefore eq '0' or $norenewalbefore =~ /^\s*$/;
+    $norenewalbefore = undef if $norenewalbefore =~ /^\s*$/;
     my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
     my $reservesallowed  = $input->param('reservesallowed');
     my $onshelfholds     = $input->param('onshelfholds') || 0;