|
Lines 4-10
use warnings;
Link Here
|
| 4 |
use 5.010; |
4 |
use 5.010; |
| 5 |
|
5 |
|
| 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 55-84
sub _init {
Link Here
|
| 55 |
sub exception_holidays { |
54 |
sub exception_holidays { |
| 56 |
my ( $self ) = @_; |
55 |
my ( $self ) = @_; |
| 57 |
|
56 |
|
| 58 |
my $cache = Koha::Caches->get_instance(); |
57 |
my $cache = Koha::Caches->get_instance(); |
| 59 |
my $cached = $cache->get_from_cache('exception_holidays'); |
58 |
my $exception_holidays = $cache->get_from_cache('exception_holidays'); |
| 60 |
return $cached if $cached; |
|
|
| 61 |
|
59 |
|
| 62 |
my $dbh = C4::Context->dbh; |
60 |
# Populate the cache is necessary |
| 63 |
my $branch = $self->{branchcode}; |
61 |
unless ($exception_holidays) { |
| 64 |
my $exception_holidays_sth = $dbh->prepare( |
62 |
my $dbh = C4::Context->dbh; |
| 65 |
'SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 1' |
63 |
$exception_holidays = {}; |
| 66 |
); |
64 |
|
| 67 |
$exception_holidays_sth->execute( $branch ); |
65 |
# Push holidays for each branch |
| 68 |
my $dates = []; |
66 |
my $exception_holidays_sth = $dbh->prepare( |
| 69 |
while ( my ( $day, $month, $year ) = $exception_holidays_sth->fetchrow ) { |
67 |
'SELECT day, month, year, branchcode FROM special_holidays WHERE isexception = 1' |
| 70 |
push @{$dates}, |
68 |
); |
| 71 |
DateTime->new( |
69 |
$exception_holidays_sth->execute(); |
| 72 |
day => $day, |
70 |
my $dates = []; |
| 73 |
month => $month, |
71 |
while ( my ( $day, $month, $year, $branch ) = |
| 74 |
year => $year, |
72 |
$exception_holidays_sth->fetchrow ) |
| 75 |
time_zone => "floating", |
73 |
{ |
| 76 |
)->truncate( to => 'day' ); |
74 |
my $datestring = |
|
|
75 |
sprintf( "%04d", $year ) |
| 76 |
. sprintf( "%02d", $month ) |
| 77 |
. sprintf( "%02d", $day ); |
| 78 |
|
| 79 |
$exception_holidays->{$branch}->{$datestring} = 1; |
| 80 |
} |
| 81 |
$cache->set_in_cache( 'exception_holidays', $exception_holidays, |
| 82 |
{ expiry => 76800 } ); |
| 77 |
} |
83 |
} |
| 78 |
$self->{exception_holidays} = |
84 |
|
| 79 |
DateTime::Set->from_datetimes( dates => $dates ); |
85 |
return $exception_holidays->{$self->{branchcode}} // {}; |
| 80 |
$cache->set_in_cache( 'exception_holidays', $self->{exception_holidays} ); |
86 |
} |
| 81 |
return $self->{exception_holidays}; |
87 |
|
|
|
88 |
sub is_exception_holiday { |
| 89 |
my ( $self, $date ) = @_; |
| 90 |
|
| 91 |
return 1 if ( $self->exception_holidays->{$date} ); |
| 92 |
return 0; |
| 82 |
} |
93 |
} |
| 83 |
|
94 |
|
| 84 |
sub single_holidays { |
95 |
sub single_holidays { |
|
Lines 127-132
sub single_holidays {
Link Here
|
| 127 |
$cache->set_in_cache( 'single_holidays', $single_holidays, |
138 |
$cache->set_in_cache( 'single_holidays', $single_holidays, |
| 128 |
{ expiry => 76800 } ) #24 hrs ; |
139 |
{ expiry => 76800 } ) #24 hrs ; |
| 129 |
} |
140 |
} |
|
|
141 |
|
| 130 |
my $holidays = ( $single_holidays->{$branchcode} ); |
142 |
my $holidays = ( $single_holidays->{$branchcode} ); |
| 131 |
for my $hols (@$holidays ) { |
143 |
for my $hols (@$holidays ) { |
| 132 |
return 1 if ( $date == $hols ) #match ymds; |
144 |
return 1 if ( $date == $hols ) #match ymds; |
|
Lines 250-262
sub is_holiday {
Link Here
|
| 250 |
my $localdt = $dt->clone(); |
262 |
my $localdt = $dt->clone(); |
| 251 |
my $day = $localdt->day; |
263 |
my $day = $localdt->day; |
| 252 |
my $month = $localdt->month; |
264 |
my $month = $localdt->month; |
|
|
265 |
my $ymd = $localdt->ymd('') ; |
| 253 |
|
266 |
|
| 254 |
#Change timezone to "floating" before doing any calculations or comparisons |
267 |
#Change timezone to "floating" before doing any calculations or comparisons |
| 255 |
$localdt->set_time_zone("floating"); |
268 |
$localdt->set_time_zone("floating"); |
| 256 |
$localdt->truncate( to => 'day' ); |
269 |
$localdt->truncate( to => 'day' ); |
| 257 |
|
270 |
|
| 258 |
|
271 |
|
| 259 |
if ( $self->exception_holidays->contains($localdt) ) { |
272 |
if ( $self->is_exception_holiday( $ymd ) == 1 ) { |
| 260 |
# exceptions are not holidays |
273 |
# exceptions are not holidays |
| 261 |
return 0; |
274 |
return 0; |
| 262 |
} |
275 |
} |
|
Lines 277-283
sub is_holiday {
Link Here
|
| 277 |
return 1; |
290 |
return 1; |
| 278 |
} |
291 |
} |
| 279 |
|
292 |
|
| 280 |
my $ymd = $localdt->ymd('') ; |
|
|
| 281 |
if ($self->single_holidays( $ymd ) == 1 ) { |
293 |
if ($self->single_holidays( $ymd ) == 1 ) { |
| 282 |
return 1; |
294 |
return 1; |
| 283 |
} |
295 |
} |
|
Lines 484-489
my $rc = $self->single_holidays( $ymd );
Link Here
|
| 484 |
|
496 |
|
| 485 |
Passed a $date in Ymd (yyyymmdd) format - returns 1 if date is a single_holiday, or 0 if not. |
497 |
Passed a $date in Ymd (yyyymmdd) format - returns 1 if date is a single_holiday, or 0 if not. |
| 486 |
|
498 |
|
|
|
499 |
=head2 exception_holidays |
| 500 |
|
| 501 |
my $exceptions = $self->exception_holidays; |
| 502 |
|
| 503 |
Returns a hashref of exception holidays for the branch |
| 504 |
|
| 505 |
=head2 is_exception_holiday |
| 506 |
|
| 507 |
my $rc = $self->is_exception_holiday( $ymd ); |
| 508 |
|
| 509 |
Passed a $date in Ymd (yyyymmdd) format - returns 1 if the date is an exception_holiday, or 0 if not. |
| 487 |
|
510 |
|
| 488 |
=head2 is_holiday |
511 |
=head2 is_holiday |
| 489 |
|
512 |
|
| 490 |
- |
|
|