From de1167a7608abf8ddda3e5a99278b1c7cc5a9cb9 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Thu, 16 Jul 2020 19:30:58 +0000 Subject: [PATCH] Bug 26000: Cache all branches for exception_holidays To test: 0 - Have an itemtype rule shared for branch A and B, or same period 1 - Set some holidays for branch A within circ period for an itemtype 2 - Set same holidays, but make some an exception, for branch b 3 - Set useDaysMode to 'Use the calendar to skip days the library is closed' 4 - Checkout an item for branch A, date is pushed forward 5 - Checkout an item for branch B, date is pushed forward? It hsould not be the same as A because fo holiday exception 6 - Apply patch 7 - Restart all 8 - Checkout an item for branch A, date is pushed forward 9 - Checkout an item for branch B, date is not pushed forward --- Koha/Calendar.pm | 95 ++++++++++++++++++++++++-------------------------------- 1 file changed, 41 insertions(+), 54 deletions(-) diff --git a/Koha/Calendar.pm b/Koha/Calendar.pm index 0b5ac1862d..5d0811a19c 100644 --- a/Koha/Calendar.pm +++ b/Koha/Calendar.pm @@ -56,33 +56,14 @@ sub exception_holidays { my $cache = Koha::Caches->get_instance(); my $cached = $cache->get_from_cache('exception_holidays'); - return $cached if $cached; + $cached = $self->_cache_and_get_holidays( $cache, 'exception_holidays' ) + unless ($cached && $cached->{ $self->{branchcode} }); + return $cached->{ $self->{branchcode} }; - my $dbh = C4::Context->dbh; - my $branch = $self->{branchcode}; - my $exception_holidays_sth = $dbh->prepare( -'SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 1' - ); - $exception_holidays_sth->execute( $branch ); - my $dates = []; - while ( my ( $day, $month, $year ) = $exception_holidays_sth->fetchrow ) { - push @{$dates}, - DateTime->new( - day => $day, - month => $month, - year => $year, - time_zone => "floating", - )->truncate( to => 'day' ); - } - $self->{exception_holidays} = - DateTime::Set->from_datetimes( dates => $dates ); - $cache->set_in_cache( 'exception_holidays', $self->{exception_holidays} ); - return $self->{exception_holidays}; } sub single_holidays { my ( $self, $date ) = @_; - my $branchcode = $self->{branchcode}; my $cache = Koha::Caches->get_instance(); my $single_holidays = $cache->get_from_cache('single_holidays'); @@ -95,44 +76,50 @@ sub single_holidays { # ... # } - unless ($single_holidays) { - my $dbh = C4::Context->dbh; - $single_holidays = {}; - - # push holidays for each branch - my $branches_sth = - $dbh->prepare('SELECT distinct(branchcode) FROM special_holidays'); - $branches_sth->execute(); - while ( my $br = $branches_sth->fetchrow ) { - my $single_holidays_sth = $dbh->prepare( -'SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 0' - ); - $single_holidays_sth->execute($br); - - my @ymd_arr; - while ( my ( $day, $month, $year ) = - $single_holidays_sth->fetchrow ) - { - my $dt = DateTime->new( - day => $day, - month => $month, - year => $year, - time_zone => 'floating', - )->truncate( to => 'day' ); - push @ymd_arr, $dt->ymd(''); - } - $single_holidays->{$br} = \@ymd_arr; - } # br - $cache->set_in_cache( 'single_holidays', $single_holidays, - { expiry => 76800 } ) #24 hrs ; - } - my $holidays = ( $single_holidays->{$branchcode} ); + $single_holidays = $self->_cache_and_get_holidays( $cache, 'single_holidays' ) unless ($single_holidays); + my $holidays = $single_holidays->{ $self->{branchcode} }; for my $hols (@$holidays ) { return 1 if ( $date == $hols ) #match ymds; } return 0; } +sub _cache_and_get_holidays { + my ( $self, $cache, $holiday_type ) = @_; + my $dbh = C4::Context->dbh; + my $holidays = {}; + my $exceptions = $holiday_type eq 'exception_holidays' ? 1 : 0; + + # push holidays for each branch + my $branches_sth = + $dbh->prepare('SELECT distinct(branchcode) FROM branches' ); + $branches_sth->execute(); + while ( my $br = $branches_sth->fetchrow ) { + my $holidays_sth = $dbh->prepare( +'SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = ' . $exceptions + ); + $holidays_sth->execute($br); + + my @ymd_arr; + while ( my ( $day, $month, $year ) = + $holidays_sth->fetchrow ) + { + my $dt = DateTime->new( + day => $day, + month => $month, + year => $year, + time_zone => 'floating', + )->truncate( to => 'day' ); + push @ymd_arr, $exceptions ? $dt : $dt->ymd(''); + } + $holidays->{$br} = $exceptions ? DateTime::Set->from_datetimes( dates => \@ymd_arr ) : \@ymd_arr; + } # br + $cache->set_in_cache( $holiday_type, $holidays, + { expiry => 76800 } ); #24 hrs + return $holidays; +} + + sub addDate { my ( $self, $startdate, $add_duration, $unit ) = @_; -- 2.11.0