From b01735b68300683aab560b99908ac7d94b06e746 Mon Sep 17 00:00:00 2001 From: Andrew Isherwood Date: Fri, 1 Feb 2019 12:24:54 +0000 Subject: [PATCH] Bug 15260: Modify next/prev_open_day 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 and Cheshire East Councils Sponsored-by: Newcastle City Council Sponsored-by: Sefton Council Signed-off-by: Liz Rea --- C4/Circulation.pm | 2 +- Koha/Calendar.pm | 52 ++++++++++++---------- .../thirdparty/TalkingTech_itiva_outbound.pl | 4 +- 3 files changed, 32 insertions(+), 26 deletions(-) diff --git a/C4/Circulation.pm b/C4/Circulation.pm index b9ff0f2824..c702688dbc 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -3566,7 +3566,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 ); } } } diff --git a/Koha/Calendar.pm b/Koha/Calendar.pm index fc63572b28..b6ffbbc161 100644 --- a/Koha/Calendar.pm +++ b/Koha/Calendar.pm @@ -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; @@ -472,21 +476,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 diff --git a/misc/cronjobs/thirdparty/TalkingTech_itiva_outbound.pl b/misc/cronjobs/thirdparty/TalkingTech_itiva_outbound.pl index 98a3d1007f..98dc338e7c 100755 --- a/misc/cronjobs/thirdparty/TalkingTech_itiva_outbound.pl +++ b/misc/cronjobs/thirdparty/TalkingTech_itiva_outbound.pl @@ -307,7 +307,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' }); @@ -315,7 +315,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; } -- 2.11.0