Lines 4-10
use Modern::Perl;
Link Here
|
4 |
|
4 |
|
5 |
use Carp; |
5 |
use Carp; |
6 |
use DateTime; |
6 |
use DateTime; |
7 |
use DateTime::Set; |
|
|
8 |
use DateTime::Duration; |
7 |
use DateTime::Duration; |
9 |
use C4::Context; |
8 |
use C4::Context; |
10 |
use Koha::Caches; |
9 |
use Koha::Caches; |
Lines 54-83
sub _init {
Link Here
|
54 |
sub exception_holidays { |
53 |
sub exception_holidays { |
55 |
my ( $self ) = @_; |
54 |
my ( $self ) = @_; |
56 |
|
55 |
|
57 |
my $cache = Koha::Caches->get_instance(); |
56 |
my $cache = Koha::Caches->get_instance(); |
58 |
my $cached = $cache->get_from_cache('exception_holidays'); |
57 |
my $exception_holidays = $cache->get_from_cache('exception_holidays'); |
59 |
return $cached if $cached; |
|
|
60 |
|
58 |
|
61 |
my $dbh = C4::Context->dbh; |
59 |
# Populate the cache is necessary |
62 |
my $branch = $self->{branchcode}; |
60 |
unless ($exception_holidays) { |
63 |
my $exception_holidays_sth = $dbh->prepare( |
61 |
my $dbh = C4::Context->dbh; |
64 |
'SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 1' |
62 |
$exception_holidays = {}; |
65 |
); |
63 |
|
66 |
$exception_holidays_sth->execute( $branch ); |
64 |
# Push holidays for each branch |
67 |
my $dates = []; |
65 |
my $exception_holidays_sth = $dbh->prepare( |
68 |
while ( my ( $day, $month, $year ) = $exception_holidays_sth->fetchrow ) { |
66 |
'SELECT day, month, year, branchcode FROM special_holidays WHERE isexception = 1' |
69 |
push @{$dates}, |
67 |
); |
70 |
DateTime->new( |
68 |
$exception_holidays_sth->execute(); |
71 |
day => $day, |
69 |
my $dates = []; |
72 |
month => $month, |
70 |
while ( my ( $day, $month, $year, $branch ) = |
73 |
year => $year, |
71 |
$exception_holidays_sth->fetchrow ) |
74 |
time_zone => "floating", |
72 |
{ |
75 |
)->truncate( to => 'day' ); |
73 |
my $datestring = |
|
|
74 |
sprintf( "%04d", $year ) |
75 |
. sprintf( "%02d", $month ) |
76 |
. sprintf( "%02d", $day ); |
77 |
|
78 |
$exception_holidays->{$branch}->{$datestring} = 1; |
79 |
} |
80 |
$cache->set_in_cache( 'exception_holidays', $exception_holidays, |
81 |
{ expiry => 76800 } ); |
76 |
} |
82 |
} |
77 |
$self->{exception_holidays} = |
83 |
|
78 |
DateTime::Set->from_datetimes( dates => $dates ); |
84 |
return $exception_holidays->{$self->{branchcode}} // {}; |
79 |
$cache->set_in_cache( 'exception_holidays', $self->{exception_holidays} ); |
85 |
} |
80 |
return $self->{exception_holidays}; |
86 |
|
|
|
87 |
sub is_exception_holiday { |
88 |
my ( $self, $date ) = @_; |
89 |
|
90 |
return 1 if ( $self->exception_holidays->{$date} ); |
91 |
return 0; |
81 |
} |
92 |
} |
82 |
|
93 |
|
83 |
sub single_holidays { |
94 |
sub single_holidays { |
Lines 126-131
sub single_holidays {
Link Here
|
126 |
$cache->set_in_cache( 'single_holidays', $single_holidays, |
137 |
$cache->set_in_cache( 'single_holidays', $single_holidays, |
127 |
{ expiry => 76800 } ) #24 hrs ; |
138 |
{ expiry => 76800 } ) #24 hrs ; |
128 |
} |
139 |
} |
|
|
140 |
|
129 |
my $holidays = ( $single_holidays->{$branchcode} ); |
141 |
my $holidays = ( $single_holidays->{$branchcode} ); |
130 |
for my $hols (@$holidays ) { |
142 |
for my $hols (@$holidays ) { |
131 |
return 1 if ( $date == $hols ) #match ymds; |
143 |
return 1 if ( $date == $hols ) #match ymds; |
Lines 267-279
sub is_holiday {
Link Here
|
267 |
my $localdt = $dt->clone(); |
279 |
my $localdt = $dt->clone(); |
268 |
my $day = $localdt->day; |
280 |
my $day = $localdt->day; |
269 |
my $month = $localdt->month; |
281 |
my $month = $localdt->month; |
|
|
282 |
my $ymd = $localdt->ymd('') ; |
270 |
|
283 |
|
271 |
#Change timezone to "floating" before doing any calculations or comparisons |
284 |
#Change timezone to "floating" before doing any calculations or comparisons |
272 |
$localdt->set_time_zone("floating"); |
285 |
$localdt->set_time_zone("floating"); |
273 |
$localdt->truncate( to => 'day' ); |
286 |
$localdt->truncate( to => 'day' ); |
274 |
|
287 |
|
275 |
|
288 |
|
276 |
if ( $self->exception_holidays->contains($localdt) ) { |
289 |
if ( $self->is_exception_holiday( $ymd ) == 1 ) { |
277 |
# exceptions are not holidays |
290 |
# exceptions are not holidays |
278 |
return 0; |
291 |
return 0; |
279 |
} |
292 |
} |
Lines 294-300
sub is_holiday {
Link Here
|
294 |
return 1; |
307 |
return 1; |
295 |
} |
308 |
} |
296 |
|
309 |
|
297 |
my $ymd = $localdt->ymd('') ; |
|
|
298 |
if ($self->single_holidays( $ymd ) == 1 ) { |
310 |
if ($self->single_holidays( $ymd ) == 1 ) { |
299 |
return 1; |
311 |
return 1; |
300 |
} |
312 |
} |
Lines 512-517
my $rc = $self->single_holidays( $ymd );
Link Here
|
512 |
|
524 |
|
513 |
Passed a $date in Ymd (yyyymmdd) format - returns 1 if date is a single_holiday, or 0 if not. |
525 |
Passed a $date in Ymd (yyyymmdd) format - returns 1 if date is a single_holiday, or 0 if not. |
514 |
|
526 |
|
|
|
527 |
=head2 exception_holidays |
528 |
|
529 |
my $exceptions = $self->exception_holidays; |
530 |
|
531 |
Returns a hashref of exception holidays for the branch |
532 |
|
533 |
=head2 is_exception_holiday |
534 |
|
535 |
my $rc = $self->is_exception_holiday( $ymd ); |
536 |
|
537 |
Passed a $date in Ymd (yyyymmdd) format - returns 1 if the date is an exception_holiday, or 0 if not. |
515 |
|
538 |
|
516 |
=head2 is_holiday |
539 |
=head2 is_holiday |
517 |
|
540 |
|
518 |
- |
|
|