From d9e4a410934673c1cd6cfb09ded7c7522584316a Mon Sep 17 00:00:00 2001 From: Hammat Wele Date: Mon, 23 Feb 2026 17:40:25 +0000 Subject: [PATCH] Bug 17015: (follow-up) add has_business_days_between() method to DiscreteCalendar.pm --- Koha/DiscreteCalendar.pm | 22 +++++++++ t/db_dependent/DiscreteCalendar.t | 77 ++++++++++++++++++++++++++++++- 2 files changed, 98 insertions(+), 1 deletion(-) diff --git a/Koha/DiscreteCalendar.pm b/Koha/DiscreteCalendar.pm index 178e2b1dfe5..d2e32ef5254 100644 --- a/Koha/DiscreteCalendar.pm +++ b/Koha/DiscreteCalendar.pm @@ -525,6 +525,28 @@ sub get_week_days_holidays { return @week_days; } +sub has_business_days_between { + my ( $self, $start_dt, $end_dt ) = @_; + + # Clone dates to avoid modifying originals + my $current_date = $start_dt->clone->set_time_zone('floating'); + my $end_date = $end_dt->clone->set_time_zone('floating'); + + # Ensure start is before end + if ( $current_date->compare($end_date) >= 0 ) { + return 0; # Same date or start after end + } + + # Check each day between start and end (exclusive) + $current_date->add( days => 1 ); + while ( $current_date->compare($end_date) < 0 ) { + return 1 unless $self->is_holiday($current_date); + $current_date->add( days => 1 ); + } + + return 0; # No business days found +} + =head2 edit_holiday Modifies a date or a range of dates diff --git a/t/db_dependent/DiscreteCalendar.t b/t/db_dependent/DiscreteCalendar.t index 7c717c1cb4b..3b0b0ec7231 100755 --- a/t/db_dependent/DiscreteCalendar.t +++ b/t/db_dependent/DiscreteCalendar.t @@ -18,7 +18,7 @@ # along with Koha; if not, see . use Modern::Perl; -use Test::More tests => 50; +use Test::More tests => 56; use Test::MockModule; use t::lib::TestBuilder; @@ -459,6 +459,81 @@ $calendar->copy_to_branch($branch2); #Check if branch2 is also closed tomorrow after copying from branch1 is($calendar2->is_opened($tomorrow), 0, "$branch2 close tomorrow after copying from $branch1"); +my $calendar3 = Koha::DiscreteCalendar->new( { branchcode => $branch1 } ); + +# Base date +my $monday = dt_from_string('2026-06-01'); # Monday +my $tuesday = dt_from_string('2026-06-02'); # Tuesday +my $wednesday = dt_from_string('2026-06-03'); # Wednesday +my $thursday = dt_from_string('2026-06-04'); # Thursday +my $friday = dt_from_string('2026-06-05'); # Friday +my $saturday = dt_from_string('2026-06-06'); # Saturday +$sunday = dt_from_string('2026-06-07'); # Sunday + +# Add holidays (exceptions = closed days) +$calendar3->edit_holiday( + { + title => "Holiday", + holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION}, + start_date => $wednesday, + end_date => $wednesday, + } +); + +$calendar3->edit_holiday( + { + title => "Holiday", + holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION}, + start_date => $saturday, + end_date => $saturday, + } +); + +$calendar3->edit_holiday( + { + title => "Holiday ", + holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION}, + start_date => $sunday, + end_date => $sunday, + } +); + +# Test 1: Business day between two business days +is( + $calendar->has_business_days_between( $monday, $wednesday ), 1, + 'Should find business day (Tuesday) between Monday and Wednesday' +); + +# Test 2: No business days between consecutive business days +is( + $calendar->has_business_days_between( $monday, $tuesday ), 0, + 'Should find no business days between consecutive days' +); + +# Test 3: Holiday between two business days +is( + $calendar->has_business_days_between( $tuesday, $thursday ), 0, + 'Should find no business days when only holiday (Wednesday) is between' +); + +# Test 4: Multiple days with business days +is( + $calendar->has_business_days_between( $monday, $friday ), 1, + 'Should find business days between Monday and Friday' +); + +# Test 5: Only holidays between dates +is( + $calendar->has_business_days_between( $friday, $sunday ), 0, + 'Should find no business days between Friday and Sunday (Saturday is holiday)' +); + +# Test 6: Same date +is( + $calendar->has_business_days_between( $monday, $monday ), 0, + 'Should find no business days between same date' +); + $schema->storage->txn_rollback; 1; -- 2.34.1