X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=Koha%2FCalendar.pm;h=a0b642b3cc8bfea5986772e23dea9e87b468c276;hb=de69ee9a91925497ae7598f9d406dca88de32a93;hp=276c7ce39148b27dd0f8971a696a14b86a7f8345;hpb=aca1f5f6a9c5fb58e62ab060c272eadc287bb167;p=srvgit diff --git a/Koha/Calendar.pm b/Koha/Calendar.pm index 276c7ce391..a0b642b3cc 100644 --- a/Koha/Calendar.pm +++ b/Koha/Calendar.pm @@ -7,8 +7,8 @@ use DateTime; use DateTime::Set; use DateTime::Duration; use C4::Context; +use Koha::Cache; use Carp; -use Readonly; sub new { my ( $classname, %options ) = @_; @@ -34,7 +34,6 @@ sub _init { ); $weekly_closed_days_sth->execute( $branch ); $self->{weekly_closed_days} = [ 0, 0, 0, 0, 0, 0, 0 ]; - Readonly::Scalar my $sunday => 7; while ( my $tuple = $weekly_closed_days_sth->fetchrow_hashref ) { $self->{weekly_closed_days}->[ $tuple->{weekday} ] = 1; } @@ -59,7 +58,9 @@ sub _init { # is allowing this with the expectation that prior to release of # 3.16, bug 8089 will be fixed and we can switch the caching over # to Koha::Cache. -our ( $exception_holidays, $single_holidays ); + +our $exception_holidays; + sub exception_holidays { my ( $self ) = @_; my $dbh = C4::Context->dbh; @@ -89,31 +90,58 @@ sub exception_holidays { } sub single_holidays { - my ( $self ) = @_; - my $dbh = C4::Context->dbh; - my $branch = $self->{branchcode}; - if ( $single_holidays ) { - $self->{single_holidays} = $single_holidays; - return $single_holidays; - } - my $single_holidays_sth = $dbh->prepare( + my ( $self, $date ) = @_; + my $branchcode = $self->{branchcode}; + my $cache = Koha::Cache->get_instance(); + my $single_holidays = $cache->get_from_cache('single_holidays'); + + # $single_holidays looks like: + # { + # CPL => [ + # [0] 20131122, + # ... + # ], + # ... + # } + + 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( $branch ); - my $dates = []; - while ( my ( $day, $month, $year ) = $single_holidays_sth->fetchrow ) { - push @{$dates}, - DateTime->new( - day => $day, - month => $month, - year => $year, - time_zone => C4::Context->tz() - )->truncate( to => 'day' ); + ); + $single_holidays_sth->execute($branchcode); + + my @ymd_arr; + while ( my ( $day, $month, $year ) = + $single_holidays_sth->fetchrow ) + { + my $dt = DateTime->new( + day => $day, + month => $month, + year => $year, + time_zone => C4::Context->tz() + )->truncate( to => 'day' ); + push @ymd_arr, $dt->ymd(''); + } + $single_holidays->{$br} = \@ymd_arr; + } # br + $cache->set_in_cache( 'single_holidays', $single_holidays, + 76800 ) #24 hrs ; } - $self->{single_holidays} = DateTime::Set->from_datetimes( dates => $dates ); - $single_holidays = $self->{single_holidays}; - return $single_holidays; + my $holidays = ( $single_holidays->{$branchcode} ); + for my $hols (@$holidays ) { + return 1 if ( $date == $hols ) #match ymds; + } + return 0; } + sub addDate { my ( $self, $startdate, $add_duration, $unit ) = @_; @@ -127,7 +155,7 @@ sub addDate { if ( $unit eq 'hours' ) { # Fixed for legacy support. Should be set as a branch parameter - Readonly::Scalar my $return_by_hour => 10; + my $return_by_hour = 10; $dt = $self->addHours($startdate, $add_duration, $return_by_hour); } else { @@ -167,6 +195,8 @@ sub addDays { my ( $self, $startdate, $days_duration ) = @_; my $base_date = $startdate->clone(); + $self->{days_mode} ||= q{}; + if ( $self->{days_mode} eq 'Calendar' ) { # use the calendar to skip all days the library is closed # when adding @@ -193,6 +223,7 @@ sub addDays { # Datedue, then use the calendar to push # the date to the next open day if holiday if ( $self->is_holiday($base_date) ) { + if ( $days_duration->is_negative() ) { $base_date = $self->prev_open_day($base_date); } else { @@ -207,12 +238,14 @@ sub addDays { sub is_holiday { my ( $self, $dt ) = @_; + my $localdt = $dt->clone(); my $day = $localdt->day; my $month = $localdt->month; $localdt->truncate( to => 'day' ); + if ( $self->exception_holidays->contains($localdt) ) { # exceptions are not holidays return 0; @@ -220,7 +253,8 @@ sub is_holiday { my $dow = $localdt->day_of_week; # Representation fix - # TODO: Shouldn't we shift the rest of the $dow also? + # DateTime object dow (1-7) where Monday is 1 + # Arrays are 0-based where 0 = Sunday, not 7. if ( $dow == 7 ) { $dow = 0; } @@ -233,7 +267,8 @@ sub is_holiday { return 1; } - if ( $self->single_holidays->contains($localdt) ) { + my $ymd = $localdt->ymd('') ; + if ($self->single_holidays( $ymd ) == 1 ) { return 1; } @@ -339,18 +374,6 @@ sub clear_weekly_closed_days { return; } -sub add_holiday { - my $self = shift; - my $new_dt = shift; - my @dt = $self->single_holidays->as_list; - push @dt, $new_dt; - $self->{single_holidays} = - DateTime::Set->from_datetimes( dates => \@dt ); - $single_holidays = $self->{single_holidays}; - - return; -} - 1; __END__ @@ -427,6 +450,13 @@ Currently unit is only used to invoke Staffs return Monday at 10 am rule this parameter will be removed when issuingrules properly cope with that +=head2 single_holidays + +my $rc = $self->single_holidays( $ymd ); + +Passed a $date in Ymd (yyyymmdd) format - returns 1 if date is a single_holiday, or 0 if not. + + =head2 is_holiday $yesno = $calendar->is_holiday($dt);