|
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 260-272
sub is_holiday {
Link Here
|
| 260 |
my $localdt = $dt->clone(); |
272 |
my $localdt = $dt->clone(); |
| 261 |
my $day = $localdt->day; |
273 |
my $day = $localdt->day; |
| 262 |
my $month = $localdt->month; |
274 |
my $month = $localdt->month; |
|
|
275 |
my $ymd = $localdt->ymd('') ; |
| 263 |
|
276 |
|
| 264 |
#Change timezone to "floating" before doing any calculations or comparisons |
277 |
#Change timezone to "floating" before doing any calculations or comparisons |
| 265 |
$localdt->set_time_zone("floating"); |
278 |
$localdt->set_time_zone("floating"); |
| 266 |
$localdt->truncate( to => 'day' ); |
279 |
$localdt->truncate( to => 'day' ); |
| 267 |
|
280 |
|
| 268 |
|
281 |
|
| 269 |
if ( $self->exception_holidays->contains($localdt) ) { |
282 |
if ( $self->is_exception_holiday( $ymd ) == 1 ) { |
| 270 |
# exceptions are not holidays |
283 |
# exceptions are not holidays |
| 271 |
return 0; |
284 |
return 0; |
| 272 |
} |
285 |
} |
|
Lines 287-293
sub is_holiday {
Link Here
|
| 287 |
return 1; |
300 |
return 1; |
| 288 |
} |
301 |
} |
| 289 |
|
302 |
|
| 290 |
my $ymd = $localdt->ymd('') ; |
|
|
| 291 |
if ($self->single_holidays( $ymd ) == 1 ) { |
303 |
if ($self->single_holidays( $ymd ) == 1 ) { |
| 292 |
return 1; |
304 |
return 1; |
| 293 |
} |
305 |
} |
|
Lines 505-510
my $rc = $self->single_holidays( $ymd );
Link Here
|
| 505 |
|
517 |
|
| 506 |
Passed a $date in Ymd (yyyymmdd) format - returns 1 if date is a single_holiday, or 0 if not. |
518 |
Passed a $date in Ymd (yyyymmdd) format - returns 1 if date is a single_holiday, or 0 if not. |
| 507 |
|
519 |
|
|
|
520 |
=head2 exception_holidays |
| 521 |
|
| 522 |
my $exceptions = $self->exception_holidays; |
| 523 |
|
| 524 |
Returns a hashref of exception holidays for the branch |
| 525 |
|
| 526 |
=head2 is_exception_holiday |
| 527 |
|
| 528 |
my $rc = $self->is_exception_holiday( $ymd ); |
| 529 |
|
| 530 |
Passed a $date in Ymd (yyyymmdd) format - returns 1 if the date is an exception_holiday, or 0 if not. |
| 508 |
|
531 |
|
| 509 |
=head2 is_holiday |
532 |
=head2 is_holiday |
| 510 |
|
533 |
|
| 511 |
- |
|
|