Bug 15260: Modify next/prev_open_day
authorAndrew Isherwood <andrew.isherwood@ptfs-europe.com>
Fri, 1 Feb 2019 12:24:54 +0000 (12:24 +0000)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Thu, 31 Oct 2019 11:52:26 +0000 (11:52 +0000)
We require next_open_day & prev_open_day to be more flexible. We could
create a separate sub, but that's not very DRY given that they'll do
pretty much the same thing.

So next_open_day becomes next_open_days and prev_open_day becomes
prev_open_days and both functions accept an additional parameter which
determines how many days they add or subtract.

All calls of these two functions have been modified accordingly.

Sponsored-by: Cheshire West and Chester Council
Sponsored-by: Cheshire East Council
Sponsored-by: Newcastle City Council
Sponsored-by: Sefton Council
Signed-off-by: Liz Rea <wizzyrea@gmail.com>
Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

C4/Circulation.pm
Koha/Calendar.pm
misc/cronjobs/thirdparty/TalkingTech_itiva_outbound.pl

index 5880abe..8db38ca 100644 (file)
@@ -3581,7 +3581,7 @@ sub CalcDateDue {
           my $calendar = Koha::Calendar->new( branchcode => $branch );
           if ( $calendar->is_holiday($datedue) ) {
               # Don't return on a closed day
-              $datedue = $calendar->prev_open_day( $datedue );
+              $datedue = $calendar->prev_open_days( $datedue, 1 );
           }
         }
     }
index 07faad5..e138ff3 100644 (file)
@@ -171,9 +171,9 @@ sub addHours {
           $self->is_holiday($base_date) ) {
 
         if ( $hours_duration->is_negative() ) {
-            $base_date = $self->prev_open_day($base_date);
+            $base_date = $self->prev_open_days($base_date, 1);
         } else {
-            $base_date = $self->next_open_day($base_date);
+            $base_date = $self->next_open_days($base_date, 1);
         }
 
         $base_date->set_hour($return_by_hour);
@@ -196,12 +196,12 @@ sub addDays {
 
         if ( $days_duration->is_negative() ) {
             while ($days) {
-                $base_date = $self->prev_open_day($base_date);
+                $base_date = $self->prev_open_days($base_date, 1);
                 --$days;
             }
         } else {
             while ($days) {
-                $base_date = $self->next_open_day($base_date);
+                $base_date = $self->next_open_days($base_date, 1);
                 --$days;
             }
         }
@@ -270,27 +270,31 @@ sub is_holiday {
     return 0;
 }
 
-sub next_open_day {
-    my ( $self, $dt ) = @_;
+sub next_open_days {
+    my ( $self, $dt, $to_add ) = @_;
     my $base_date = $dt->clone();
 
-    $base_date->add(days => 1);
+    $base_date->add(days => $to_add);
 
     while ($self->is_holiday($base_date)) {
-        $base_date->add(days => 1);
+        $base_date->add(days => $to_add);
     }
 
     return $base_date;
 }
 
-sub prev_open_day {
-    my ( $self, $dt ) = @_;
+sub prev_open_days {
+    my ( $self, $dt, $to_sub ) = @_;
     my $base_date = $dt->clone();
 
-    $base_date->add(days => -1);
+    # It feels logical to be passed a positive number, though we're
+    # subtracting, so do the right thing
+    $to_sub = $to_sub > 0 ? 0 - $to_sub : $to_sub;
+
+    $base_date->add(days => $to_sub);
 
     while ($self->is_holiday($base_date)) {
-        $base_date->add(days => -1);
+        $base_date->add(days => $to_sub);
     }
 
     return $base_date;
@@ -306,7 +310,7 @@ sub days_forward {
     my $base_dt = $start_dt->clone();
 
     while ($num_days--) {
-        $base_dt = $self->next_open_day($base_dt);
+        $base_dt = $self->next_open_days($base_dt, 1);
     }
 
     return $base_dt;
@@ -468,21 +472,23 @@ Passed two dates returns a DateTime::Duration object measuring the length betwee
 ignoring closed days. Always returns a positive number irrespective of the
 relative order of the parameters
 
-=head2 next_open_day
+=head2 next_open_days
 
-$datetime = $calendar->next_open_day($duedate_dt)
+$datetime = $calendar->next_open_days($duedate_dt, $to_add)
 
-Passed a Datetime returns another Datetime representing the next open day. It is
-intended for use to calculate the due date when useDaysMode syspref is set to either
-'Datedue' or 'Calendar'.
+Passed a Datetime and number of days,  returns another Datetime representing
+the next open day after adding the passed number of days. It is intended for
+use to calculate the due date when useDaysMode syspref is set to either
+'Datedue', 'Calendar' or 'Dayweek'.
 
-=head2 prev_open_day
+=head2 prev_open_days
 
-$datetime = $calendar->prev_open_day($duedate_dt)
+$datetime = $calendar->prev_open_days($duedate_dt, $to_sub)
 
-Passed a Datetime returns another Datetime representing the previous open day. It is
-intended for use to calculate the due date when useDaysMode syspref is set to either
-'Datedue' or 'Calendar'.
+Passed a Datetime and a number of days, returns another Datetime
+representing the previous open day after subtracting the number of passed
+days. It is intended for use to calculate the due date when useDaysMode
+syspref is set to either 'Datedue', 'Calendar' or 'Dayweek'.
 
 =head2 set_daysmode
 
index 2207934..1131da6 100755 (executable)
@@ -337,7 +337,7 @@ sub GetWaitingHolds {
         my $waiting_date = dt_from_string( $issue->{waitingdate}, 'sql' );
         my $pickup_date = $waiting_date->clone->add( days => $pickupdelay );
         if ( $calendar->is_holiday($pickup_date) ) {
-            $pickup_date = $calendar->next_open_day( $pickup_date );
+            $pickup_date = $calendar->next_open_days( $pickup_date, 1 );
         }
 
         $issue->{'date_due'} = output_pref({dt => $pickup_date, dateformat => 'iso' });
@@ -345,7 +345,7 @@ sub GetWaitingHolds {
 
         my $days_to_subtract = 0;
         if ( $calendar->is_holiday($waiting_date) ) {
-            my $next_open_day = $calendar->next_open_day( $waiting_date );
+            my $next_open_day = $calendar->next_open_days( $waiting_date, 1 );
             $days_to_subtract = $calendar->days_between($waiting_date, $next_open_day)->days;
         }