Lines 56-88
sub exception_holidays {
Link Here
|
56 |
|
56 |
|
57 |
my $cache = Koha::Caches->get_instance(); |
57 |
my $cache = Koha::Caches->get_instance(); |
58 |
my $cached = $cache->get_from_cache('exception_holidays'); |
58 |
my $cached = $cache->get_from_cache('exception_holidays'); |
59 |
return $cached if $cached; |
59 |
$cached = $self->_cache_and_get_holidays( $cache, 'exception_holidays' ) |
|
|
60 |
unless ($cached && $cached->{ $self->{branchcode} }); |
61 |
return $cached->{ $self->{branchcode} }; |
60 |
|
62 |
|
61 |
my $dbh = C4::Context->dbh; |
|
|
62 |
my $branch = $self->{branchcode}; |
63 |
my $exception_holidays_sth = $dbh->prepare( |
64 |
'SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 1' |
65 |
); |
66 |
$exception_holidays_sth->execute( $branch ); |
67 |
my $dates = []; |
68 |
while ( my ( $day, $month, $year ) = $exception_holidays_sth->fetchrow ) { |
69 |
push @{$dates}, |
70 |
DateTime->new( |
71 |
day => $day, |
72 |
month => $month, |
73 |
year => $year, |
74 |
time_zone => "floating", |
75 |
)->truncate( to => 'day' ); |
76 |
} |
77 |
$self->{exception_holidays} = |
78 |
DateTime::Set->from_datetimes( dates => $dates ); |
79 |
$cache->set_in_cache( 'exception_holidays', $self->{exception_holidays} ); |
80 |
return $self->{exception_holidays}; |
81 |
} |
63 |
} |
82 |
|
64 |
|
83 |
sub single_holidays { |
65 |
sub single_holidays { |
84 |
my ( $self, $date ) = @_; |
66 |
my ( $self, $date ) = @_; |
85 |
my $branchcode = $self->{branchcode}; |
|
|
86 |
my $cache = Koha::Caches->get_instance(); |
67 |
my $cache = Koha::Caches->get_instance(); |
87 |
my $single_holidays = $cache->get_from_cache('single_holidays'); |
68 |
my $single_holidays = $cache->get_from_cache('single_holidays'); |
88 |
|
69 |
|
Lines 95-138
sub single_holidays {
Link Here
|
95 |
# ... |
76 |
# ... |
96 |
# } |
77 |
# } |
97 |
|
78 |
|
98 |
unless ($single_holidays) { |
79 |
$single_holidays = $self->_cache_and_get_holidays( $cache, 'single_holidays' ) unless ($single_holidays); |
99 |
my $dbh = C4::Context->dbh; |
80 |
my $holidays = $single_holidays->{ $self->{branchcode} }; |
100 |
$single_holidays = {}; |
|
|
101 |
|
102 |
# push holidays for each branch |
103 |
my $branches_sth = |
104 |
$dbh->prepare('SELECT distinct(branchcode) FROM special_holidays'); |
105 |
$branches_sth->execute(); |
106 |
while ( my $br = $branches_sth->fetchrow ) { |
107 |
my $single_holidays_sth = $dbh->prepare( |
108 |
'SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 0' |
109 |
); |
110 |
$single_holidays_sth->execute($br); |
111 |
|
112 |
my @ymd_arr; |
113 |
while ( my ( $day, $month, $year ) = |
114 |
$single_holidays_sth->fetchrow ) |
115 |
{ |
116 |
my $dt = DateTime->new( |
117 |
day => $day, |
118 |
month => $month, |
119 |
year => $year, |
120 |
time_zone => 'floating', |
121 |
)->truncate( to => 'day' ); |
122 |
push @ymd_arr, $dt->ymd(''); |
123 |
} |
124 |
$single_holidays->{$br} = \@ymd_arr; |
125 |
} # br |
126 |
$cache->set_in_cache( 'single_holidays', $single_holidays, |
127 |
{ expiry => 76800 } ) #24 hrs ; |
128 |
} |
129 |
my $holidays = ( $single_holidays->{$branchcode} ); |
130 |
for my $hols (@$holidays ) { |
81 |
for my $hols (@$holidays ) { |
131 |
return 1 if ( $date == $hols ) #match ymds; |
82 |
return 1 if ( $date == $hols ) #match ymds; |
132 |
} |
83 |
} |
133 |
return 0; |
84 |
return 0; |
134 |
} |
85 |
} |
135 |
|
86 |
|
|
|
87 |
sub _cache_and_get_holidays { |
88 |
my ( $self, $cache, $holiday_type ) = @_; |
89 |
my $dbh = C4::Context->dbh; |
90 |
my $holidays = {}; |
91 |
my $exceptions = $holiday_type eq 'exception_holidays' ? 1 : 0; |
92 |
|
93 |
# push holidays for each branch |
94 |
my $branches_sth = |
95 |
$dbh->prepare('SELECT distinct(branchcode) FROM branches' ); |
96 |
$branches_sth->execute(); |
97 |
while ( my $br = $branches_sth->fetchrow ) { |
98 |
my $holidays_sth = $dbh->prepare( |
99 |
'SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = ' . $exceptions |
100 |
); |
101 |
$holidays_sth->execute($br); |
102 |
|
103 |
my @ymd_arr; |
104 |
while ( my ( $day, $month, $year ) = |
105 |
$holidays_sth->fetchrow ) |
106 |
{ |
107 |
my $dt = DateTime->new( |
108 |
day => $day, |
109 |
month => $month, |
110 |
year => $year, |
111 |
time_zone => 'floating', |
112 |
)->truncate( to => 'day' ); |
113 |
push @ymd_arr, $exceptions ? $dt : $dt->ymd(''); |
114 |
} |
115 |
$holidays->{$br} = $exceptions ? DateTime::Set->from_datetimes( dates => \@ymd_arr ) : \@ymd_arr; |
116 |
} # br |
117 |
$cache->set_in_cache( $holiday_type, $holidays, |
118 |
{ expiry => 76800 } ); #24 hrs |
119 |
return $holidays; |
120 |
} |
121 |
|
122 |
|
136 |
sub addDate { |
123 |
sub addDate { |
137 |
my ( $self, $startdate, $add_duration, $unit ) = @_; |
124 |
my ( $self, $startdate, $add_duration, $unit ) = @_; |
138 |
|
125 |
|
139 |
- |
|
|