Lines 15-726
package C4::Calendar;
Link Here
|
15 |
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
15 |
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
16 |
# Suite 330, Boston, MA 02111-1307 USA |
16 |
# Suite 330, Boston, MA 02111-1307 USA |
17 |
|
17 |
|
18 |
use strict; |
18 |
use Modern::Perl; |
19 |
use warnings; |
|
|
20 |
use vars qw($VERSION @EXPORT); |
19 |
use vars qw($VERSION @EXPORT); |
21 |
|
20 |
|
22 |
use Carp; |
21 |
use Carp; |
23 |
use Date::Calc qw( Date_to_Days Today); |
|
|
24 |
|
22 |
|
25 |
use C4::Context; |
23 |
use Koha::Database; |
|
|
24 |
|
25 |
our ( @ISA, @EXPORT ); |
26 |
|
27 |
BEGIN { |
28 |
@ISA = qw( Exporter ); |
29 |
@EXPORT = qw( |
30 |
GetSingleEvents |
31 |
GetWeeklyEvents |
32 |
GetYearlyEvents |
33 |
ModSingleEvent |
34 |
ModRepeatingEvent |
35 |
DelSingleEvent |
36 |
DelRepeatingEvent |
37 |
CopyAllEvents |
38 |
); |
39 |
} |
26 |
|
40 |
|
27 |
use constant ISO_DATE_FORMAT => "%04d-%02d-%02d"; |
41 |
use constant ISO_DATE_FORMAT => "%04d-%02d-%02d"; |
28 |
=head1 NAME |
42 |
=head1 NAME |
29 |
|
43 |
|
30 |
C4::Calendar::Calendar - Koha module dealing with holidays. |
44 |
C4::Calendar - Koha module dealing with holidays. |
31 |
|
45 |
|
32 |
=head1 SYNOPSIS |
46 |
=head1 SYNOPSIS |
33 |
|
47 |
|
34 |
use C4::Calendar::Calendar; |
48 |
use C4::Calendar; |
35 |
|
49 |
|
36 |
=head1 DESCRIPTION |
50 |
=head1 DESCRIPTION |
37 |
|
51 |
|
38 |
This package is used to deal with holidays. Through this package, you can set |
52 |
This package is used to deal with hours and holidays; |
39 |
all kind of holidays for the library. |
|
|
40 |
|
53 |
|
41 |
=head1 FUNCTIONS |
54 |
=head1 FUNCTIONS |
42 |
|
55 |
|
43 |
=head2 new |
56 |
=head2 GetSingleEvents |
44 |
|
|
|
45 |
$calendar = C4::Calendar->new(branchcode => $branchcode); |
46 |
|
47 |
Each library branch has its own Calendar. |
48 |
C<$branchcode> specifies which Calendar you want. |
49 |
|
50 |
=cut |
51 |
|
52 |
sub new { |
53 |
my $classname = shift @_; |
54 |
my %options = @_; |
55 |
my $self = bless({}, $classname); |
56 |
foreach my $optionName (keys %options) { |
57 |
$self->{lc($optionName)} = $options{$optionName}; |
58 |
} |
59 |
defined($self->{branchcode}) or croak "No branchcode argument to new. Should be C4::Calendar->new(branchcode => \$branchcode)"; |
60 |
$self->_init($self->{branchcode}); |
61 |
return $self; |
62 |
} |
63 |
|
64 |
sub _init { |
65 |
my $self = shift @_; |
66 |
my $branch = shift; |
67 |
defined($branch) or die "No branchcode sent to _init"; # must test for defined here and above to allow "" |
68 |
my $dbh = C4::Context->dbh(); |
69 |
my $repeatable = $dbh->prepare( 'SELECT * |
70 |
FROM repeatable_holidays |
71 |
WHERE ( branchcode = ? ) |
72 |
AND (ISNULL(weekday) = ?)' ); |
73 |
$repeatable->execute($branch,0); |
74 |
my %week_days_holidays; |
75 |
while (my $row = $repeatable->fetchrow_hashref) { |
76 |
my $key = $row->{weekday}; |
77 |
$week_days_holidays{$key}{title} = $row->{title}; |
78 |
$week_days_holidays{$key}{description} = $row->{description}; |
79 |
} |
80 |
$self->{'week_days_holidays'} = \%week_days_holidays; |
81 |
|
82 |
$repeatable->execute($branch,1); |
83 |
my %day_month_holidays; |
84 |
while (my $row = $repeatable->fetchrow_hashref) { |
85 |
my $key = $row->{month} . "/" . $row->{day}; |
86 |
$day_month_holidays{$key}{title} = $row->{title}; |
87 |
$day_month_holidays{$key}{description} = $row->{description}; |
88 |
$day_month_holidays{$key}{day} = sprintf("%02d", $row->{day}); |
89 |
$day_month_holidays{$key}{month} = sprintf("%02d", $row->{month}); |
90 |
} |
91 |
$self->{'day_month_holidays'} = \%day_month_holidays; |
92 |
|
93 |
my $special = $dbh->prepare( 'SELECT day, month, year, title, description |
94 |
FROM special_holidays |
95 |
WHERE ( branchcode = ? ) |
96 |
AND (isexception = ?)' ); |
97 |
$special->execute($branch,1); |
98 |
my %exception_holidays; |
99 |
while (my ($day, $month, $year, $title, $description) = $special->fetchrow) { |
100 |
$exception_holidays{"$year/$month/$day"}{title} = $title; |
101 |
$exception_holidays{"$year/$month/$day"}{description} = $description; |
102 |
$exception_holidays{"$year/$month/$day"}{date} = |
103 |
sprintf(ISO_DATE_FORMAT, $year, $month, $day); |
104 |
} |
105 |
$self->{'exception_holidays'} = \%exception_holidays; |
106 |
|
107 |
$special->execute($branch,0); |
108 |
my %single_holidays; |
109 |
while (my ($day, $month, $year, $title, $description) = $special->fetchrow) { |
110 |
$single_holidays{"$year/$month/$day"}{title} = $title; |
111 |
$single_holidays{"$year/$month/$day"}{description} = $description; |
112 |
$single_holidays{"$year/$month/$day"}{date} = |
113 |
sprintf(ISO_DATE_FORMAT, $year, $month, $day); |
114 |
} |
115 |
$self->{'single_holidays'} = \%single_holidays; |
116 |
return $self; |
117 |
} |
118 |
|
119 |
=head2 get_week_days_holidays |
120 |
|
121 |
$week_days_holidays = $calendar->get_week_days_holidays(); |
122 |
|
123 |
Returns a hash reference to week days holidays. |
124 |
|
125 |
=cut |
126 |
|
127 |
sub get_week_days_holidays { |
128 |
my $self = shift @_; |
129 |
my $week_days_holidays = $self->{'week_days_holidays'}; |
130 |
return $week_days_holidays; |
131 |
} |
132 |
|
133 |
=head2 get_day_month_holidays |
134 |
|
135 |
$day_month_holidays = $calendar->get_day_month_holidays(); |
136 |
|
137 |
Returns a hash reference to day month holidays. |
138 |
|
139 |
=cut |
140 |
|
141 |
sub get_day_month_holidays { |
142 |
my $self = shift @_; |
143 |
my $day_month_holidays = $self->{'day_month_holidays'}; |
144 |
return $day_month_holidays; |
145 |
} |
146 |
|
147 |
=head2 get_exception_holidays |
148 |
|
149 |
$exception_holidays = $calendar->exception_holidays(); |
150 |
|
151 |
Returns a hash reference to exception holidays. This kind of days are those |
152 |
which stands for a holiday, but you wanted to make an exception for this particular |
153 |
date. |
154 |
|
155 |
=cut |
156 |
|
157 |
sub get_exception_holidays { |
158 |
my $self = shift @_; |
159 |
my $exception_holidays = $self->{'exception_holidays'}; |
160 |
return $exception_holidays; |
161 |
} |
162 |
|
163 |
=head2 get_single_holidays |
164 |
|
165 |
$single_holidays = $calendar->get_single_holidays(); |
166 |
|
167 |
Returns a hash reference to single holidays. This kind of holidays are those which |
168 |
happend just one time. |
169 |
|
57 |
|
170 |
=cut |
58 |
\@events = GetSingleEvents( $branchcode ) |
171 |
|
|
|
172 |
sub get_single_holidays { |
173 |
my $self = shift @_; |
174 |
my $single_holidays = $self->{'single_holidays'}; |
175 |
return $single_holidays; |
176 |
} |
177 |
|
178 |
=head2 insert_week_day_holiday |
179 |
|
180 |
insert_week_day_holiday(weekday => $weekday, |
181 |
title => $title, |
182 |
description => $description); |
183 |
|
59 |
|
184 |
Inserts a new week day for $self->{branchcode}. |
60 |
Get the non-repeating events for the given library. |
185 |
|
|
|
186 |
C<$day> Is the week day to make holiday. |
187 |
|
188 |
C<$title> Is the title to store for the holiday formed by $year/$month/$day. |
189 |
|
190 |
C<$description> Is the description to store for the holiday formed by $year/$month/$day. |
191 |
|
61 |
|
192 |
=cut |
62 |
=cut |
193 |
|
63 |
|
194 |
sub insert_week_day_holiday { |
64 |
sub GetSingleEvents { |
195 |
my $self = shift @_; |
65 |
my ( $branchcode ) = @_; |
196 |
my %options = @_; |
66 |
|
197 |
|
67 |
return C4::Context->dbh->selectall_arrayref( q{ |
198 |
my $weekday = $options{weekday}; |
68 |
SELECT |
199 |
croak "Invalid weekday $weekday" unless $weekday =~ m/^[0-6]$/; |
69 |
CONCAT(LPAD(year, 4, '0'), '-', LPAD(month, 2, '0'), '-', LPAD(day, 2, '0')) as event_date, |
200 |
|
70 |
0 as open_hour, 0 as open_minute, IF(isexception, 24, 0) as close_hour, |
201 |
my $dbh = C4::Context->dbh(); |
71 |
0 as close_minute, title, description, IF(isexception, 0, 1) as closed |
202 |
my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ( '',?,?,NULL,NULL,?,? )"); |
72 |
FROM special_holidays |
203 |
$insertHoliday->execute( $self->{branchcode}, $weekday, $options{title}, $options{description}); |
73 |
WHERE branchcode = ? |
204 |
$self->{'week_days_holidays'}->{$weekday}{title} = $options{title}; |
74 |
}, { Slice => {} }, $branchcode ); |
205 |
$self->{'week_days_holidays'}->{$weekday}{description} = $options{description}; |
|
|
206 |
return $self; |
207 |
} |
75 |
} |
208 |
|
76 |
|
209 |
=head2 insert_day_month_holiday |
77 |
=head2 GetWeeklyEvents |
210 |
|
78 |
|
211 |
insert_day_month_holiday(day => $day, |
79 |
\@events = GetWeeklyEvents( $branchcode ) |
212 |
month => $month, |
|
|
213 |
title => $title, |
214 |
description => $description); |
215 |
|
80 |
|
216 |
Inserts a new day month holiday for $self->{branchcode}. |
81 |
Get the weekly-repeating events for the given library. |
217 |
|
|
|
218 |
C<$day> Is the day month to make the date to insert. |
219 |
|
220 |
C<$month> Is month to make the date to insert. |
221 |
|
222 |
C<$title> Is the title to store for the holiday formed by $year/$month/$day. |
223 |
|
224 |
C<$description> Is the description to store for the holiday formed by $year/$month/$day. |
225 |
|
82 |
|
226 |
=cut |
83 |
=cut |
227 |
|
84 |
|
228 |
sub insert_day_month_holiday { |
85 |
sub GetWeeklyEvents { |
229 |
my $self = shift @_; |
86 |
my ( $branchcode ) = @_; |
230 |
my %options = @_; |
|
|
231 |
|
232 |
my $dbh = C4::Context->dbh(); |
233 |
my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ('', ?, NULL, ?, ?, ?,? )"); |
234 |
$insertHoliday->execute( $self->{branchcode}, $options{day},$options{month},$options{title}, $options{description}); |
235 |
$self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{title} = $options{title}; |
236 |
$self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{description} = $options{description}; |
237 |
return $self; |
238 |
} |
239 |
|
240 |
=head2 insert_single_holiday |
241 |
|
242 |
insert_single_holiday(day => $day, |
243 |
month => $month, |
244 |
year => $year, |
245 |
title => $title, |
246 |
description => $description); |
247 |
|
248 |
Inserts a new single holiday for $self->{branchcode}. |
249 |
|
87 |
|
250 |
C<$day> Is the day month to make the date to insert. |
88 |
return C4::Context->dbh->selectall_arrayref( q{ |
251 |
|
89 |
SELECT |
252 |
C<$month> Is month to make the date to insert. |
90 |
weekday, 0 as open_hour, 0 as open_minute, 0 as close_hour, |
253 |
|
91 |
0 as close_minute, title, description, 1 as closed |
254 |
C<$year> Is year to make the date to insert. |
92 |
FROM repeatable_holidays |
255 |
|
93 |
WHERE branchcode = ? AND weekday IS NOT NULL |
256 |
C<$title> Is the title to store for the holiday formed by $year/$month/$day. |
94 |
}, { Slice => {} }, $branchcode ); |
257 |
|
|
|
258 |
C<$description> Is the description to store for the holiday formed by $year/$month/$day. |
259 |
|
260 |
=cut |
261 |
|
262 |
sub insert_single_holiday { |
263 |
my $self = shift @_; |
264 |
my %options = @_; |
265 |
|
266 |
@options{qw(year month day)} = ( $options{date} =~ m/(\d+)-(\d+)-(\d+)/o ) |
267 |
if $options{date} && !$options{day}; |
268 |
|
269 |
my $dbh = C4::Context->dbh(); |
270 |
my $isexception = 0; |
271 |
my $insertHoliday = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)"); |
272 |
$insertHoliday->execute( $self->{branchcode}, $options{day},$options{month},$options{year}, $isexception, $options{title}, $options{description}); |
273 |
$self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title}; |
274 |
$self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description}; |
275 |
return $self; |
276 |
} |
95 |
} |
277 |
|
96 |
|
278 |
=head2 insert_exception_holiday |
97 |
=head2 GetYearlyEvents |
279 |
|
|
|
280 |
insert_exception_holiday(day => $day, |
281 |
month => $month, |
282 |
year => $year, |
283 |
title => $title, |
284 |
description => $description); |
285 |
|
286 |
Inserts a new exception holiday for $self->{branchcode}. |
287 |
|
288 |
C<$day> Is the day month to make the date to insert. |
289 |
|
98 |
|
290 |
C<$month> Is month to make the date to insert. |
99 |
\@events = GetYearlyEvents( $branchcode ) |
291 |
|
100 |
|
292 |
C<$year> Is year to make the date to insert. |
101 |
Get the yearly-repeating events for the given library. |
293 |
|
|
|
294 |
C<$title> Is the title to store for the holiday formed by $year/$month/$day. |
295 |
|
296 |
C<$description> Is the description to store for the holiday formed by $year/$month/$day. |
297 |
|
102 |
|
298 |
=cut |
103 |
=cut |
299 |
|
104 |
|
300 |
sub insert_exception_holiday { |
105 |
sub GetYearlyEvents { |
301 |
my $self = shift @_; |
106 |
my ( $branchcode ) = @_; |
302 |
my %options = @_; |
|
|
303 |
|
304 |
@options{qw(year month day)} = ( $options{date} =~ m/(\d+)-(\d+)-(\d+)/o ) |
305 |
if $options{date} && !$options{day}; |
306 |
|
107 |
|
307 |
my $dbh = C4::Context->dbh(); |
108 |
return C4::Context->dbh->selectall_arrayref( q{ |
308 |
my $isexception = 1; |
109 |
SELECT |
309 |
my $insertException = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)"); |
110 |
month, day, 0 as open_hour, 0 as open_minute, 0 as close_hour, |
310 |
$insertException->execute( $self->{branchcode}, $options{day},$options{month},$options{year}, $isexception, $options{title}, $options{description}); |
111 |
0 as close_minute, title, description, 1 as closed |
311 |
$self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title}; |
112 |
FROM repeatable_holidays |
312 |
$self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description}; |
113 |
WHERE branchcode = ? AND weekday IS NULL |
313 |
return $self; |
114 |
}, { Slice => {} }, $branchcode ); |
314 |
} |
115 |
} |
315 |
|
116 |
|
316 |
=head2 ModWeekdayholiday |
117 |
=head2 ModSingleEvent |
317 |
|
|
|
318 |
ModWeekdayholiday(weekday =>$weekday, |
319 |
title => $title, |
320 |
description => $description) |
321 |
|
322 |
Modifies the title and description of a weekday for $self->{branchcode}. |
323 |
|
118 |
|
324 |
C<$weekday> Is the title to update for the holiday. |
119 |
ModSingleEvent( $branchcode, \%info ) |
325 |
|
120 |
|
326 |
C<$description> Is the description to update for the holiday. |
121 |
Creates or updates an event for a single date. $info->{date} should be an |
|
|
122 |
ISO-formatted date string, and \%info should also contain the following keys: |
123 |
open_hour, open_minute, close_hour, close_minute, title and description. |
327 |
|
124 |
|
328 |
=cut |
125 |
=cut |
329 |
|
126 |
|
330 |
sub ModWeekdayholiday { |
127 |
sub ModSingleEvent { |
331 |
my $self = shift @_; |
128 |
my ( $branchcode, $info ) = @_; |
332 |
my %options = @_; |
|
|
333 |
|
334 |
my $dbh = C4::Context->dbh(); |
335 |
my $updateHoliday = $dbh->prepare("UPDATE repeatable_holidays SET title = ?, description = ? WHERE branchcode = ? AND weekday = ?"); |
336 |
$updateHoliday->execute( $options{title},$options{description},$self->{branchcode},$options{weekday}); |
337 |
$self->{'week_days_holidays'}->{$options{weekday}}{title} = $options{title}; |
338 |
$self->{'week_days_holidays'}->{$options{weekday}}{description} = $options{description}; |
339 |
return $self; |
340 |
} |
341 |
|
342 |
=head2 ModDaymonthholiday |
343 |
|
344 |
ModDaymonthholiday(day => $day, |
345 |
month => $month, |
346 |
title => $title, |
347 |
description => $description); |
348 |
|
129 |
|
349 |
Modifies the title and description for a day/month holiday for $self->{branchcode}. |
130 |
my ( $year, $month, $day ) = ( $info->{date} =~ /(\d+)-(\d+)-(\d+)/ ); |
|
|
131 |
return unless ( $year && $month && $day ); |
350 |
|
132 |
|
351 |
C<$day> The day of the month for the update. |
133 |
my $dbh = C4::Context->dbh; |
|
|
134 |
my @args = ( ( map { $info->{$_} } qw(title description) ), $info->{close_hour} != 0, $branchcode, $year, $month, $day ); |
352 |
|
135 |
|
353 |
C<$month> The month to be used for the update. |
136 |
# The code below relies on $dbh->do returning 0 when the update affects no rows |
|
|
137 |
my $affected = $dbh->do( q{ |
138 |
UPDATE special_holidays |
139 |
SET |
140 |
title = ?, description = ?, isexception = ? |
141 |
WHERE branchcode = ? AND year = ? AND month = ? AND day = ? |
142 |
}, {}, @args ); |
354 |
|
143 |
|
355 |
C<$title> The title to be updated for the holiday. |
144 |
$dbh->do( q{ |
356 |
|
145 |
INSERT |
357 |
C<$description> The description to be update for the holiday. |
146 |
INTO special_holidays(title, description, isexception, branchcode, year, month, day) |
358 |
|
147 |
VALUES (?, ?, ?, ?, ?, ?, ?) |
359 |
=cut |
148 |
}, {}, @args ) unless ( $affected > 0 ); |
360 |
|
|
|
361 |
sub ModDaymonthholiday { |
362 |
my $self = shift @_; |
363 |
my %options = @_; |
364 |
|
365 |
my $dbh = C4::Context->dbh(); |
366 |
my $updateHoliday = $dbh->prepare("UPDATE repeatable_holidays SET title = ?, description = ? WHERE month = ? AND day = ? AND branchcode = ?"); |
367 |
$updateHoliday->execute( $options{title},$options{description},$options{month},$options{day},$self->{branchcode}); |
368 |
$self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{title} = $options{title}; |
369 |
$self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{description} = $options{description}; |
370 |
return $self; |
371 |
} |
149 |
} |
372 |
|
150 |
|
373 |
=head2 ModSingleholiday |
151 |
=head2 ModRepeatingEvent |
374 |
|
|
|
375 |
ModSingleholiday(day => $day, |
376 |
month => $month, |
377 |
year => $year, |
378 |
title => $title, |
379 |
description => $description); |
380 |
|
381 |
Modifies the title and description for a single holiday for $self->{branchcode}. |
382 |
|
383 |
C<$day> Is the day of the month to make the update. |
384 |
|
385 |
C<$month> Is the month to make the update. |
386 |
|
387 |
C<$year> Is the year to make the update. |
388 |
|
152 |
|
389 |
C<$title> Is the title to update for the holiday formed by $year/$month/$day. |
153 |
ModRepeatingEvent( $branchcode, \%info ) |
390 |
|
154 |
|
391 |
C<$description> Is the description to update for the holiday formed by $year/$month/$day. |
155 |
Creates or updates a weekly- or yearly-repeating event. Either $info->{weekday}, |
|
|
156 |
or $info->{month} and $info->{day} should be set, for a weekly or yearly event, |
157 |
respectively. |
392 |
|
158 |
|
393 |
=cut |
159 |
=cut |
394 |
|
160 |
|
395 |
sub ModSingleholiday { |
161 |
sub _get_compare { |
396 |
my $self = shift @_; |
162 |
my ( $colname, $value ) = @_; |
397 |
my %options = @_; |
|
|
398 |
|
399 |
my $dbh = C4::Context->dbh(); |
400 |
my $isexception = 0; |
401 |
my $updateHoliday = $dbh->prepare("UPDATE special_holidays SET title = ?, description = ? WHERE day = ? AND month = ? AND year = ? AND branchcode = ? AND isexception = ?"); |
402 |
$updateHoliday->execute($options{title},$options{description},$options{day},$options{month},$options{year},$self->{branchcode},$isexception); |
403 |
$self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title}; |
404 |
$self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description}; |
405 |
return $self; |
406 |
} |
407 |
|
408 |
=head2 ModExceptionholiday |
409 |
|
410 |
ModExceptionholiday(day => $day, |
411 |
month => $month, |
412 |
year => $year, |
413 |
title => $title, |
414 |
description => $description); |
415 |
|
416 |
Modifies the title and description for an exception holiday for $self->{branchcode}. |
417 |
|
418 |
C<$day> Is the day of the month for the holiday. |
419 |
|
420 |
C<$month> Is the month for the holiday. |
421 |
|
422 |
C<$year> Is the year for the holiday. |
423 |
|
424 |
C<$title> Is the title to be modified for the holiday formed by $year/$month/$day. |
425 |
|
426 |
C<$description> Is the description to be modified for the holiday formed by $year/$month/$day. |
427 |
|
163 |
|
428 |
=cut |
164 |
return ' AND ' . $colname . ' ' . ( defined( $value ) ? '=' : 'IS' ) . ' ?'; |
429 |
|
|
|
430 |
sub ModExceptionholiday { |
431 |
my $self = shift @_; |
432 |
my %options = @_; |
433 |
|
434 |
my $dbh = C4::Context->dbh(); |
435 |
my $isexception = 1; |
436 |
my $updateHoliday = $dbh->prepare("UPDATE special_holidays SET title = ?, description = ? WHERE day = ? AND month = ? AND year = ? AND branchcode = ? AND isexception = ?"); |
437 |
$updateHoliday->execute($options{title},$options{description},$options{day},$options{month},$options{year},$self->{branchcode},$isexception); |
438 |
$self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title}; |
439 |
$self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description}; |
440 |
return $self; |
441 |
} |
165 |
} |
442 |
|
166 |
|
443 |
=head2 delete_holiday |
167 |
sub ModRepeatingEvent { |
444 |
|
168 |
my ( $branchcode, $info ) = @_; |
445 |
delete_holiday(weekday => $weekday |
|
|
446 |
day => $day, |
447 |
month => $month, |
448 |
year => $year); |
449 |
|
450 |
Delete a holiday for $self->{branchcode}. |
451 |
|
452 |
C<$weekday> Is the week day to delete. |
453 |
|
169 |
|
454 |
C<$day> Is the day month to make the date to delete. |
170 |
my $dbh = C4::Context->dbh; |
|
|
171 |
my $open = ( $info->{close_hour} != 0 ); |
455 |
|
172 |
|
456 |
C<$month> Is month to make the date to delete. |
173 |
if ($open) { |
457 |
|
174 |
$dbh->do( q{ |
458 |
C<$year> Is year to make the date to delete. |
175 |
DELETE FROM repeatable_holidays |
459 |
|
176 |
WHERE branchcode = ? |
460 |
=cut |
177 |
} . _get_compare( 'weekday', $info->{weekday} ) . _get_compare( 'month', $info->{month} ) . _get_compare( 'day', $info->{day} ), {}, $branchcode, $info->{weekday}, $info->{month}, $info->{day} ); |
461 |
|
|
|
462 |
sub delete_holiday { |
463 |
my $self = shift @_; |
464 |
my %options = @_; |
465 |
|
466 |
# Verify what kind of holiday that day is. For example, if it is |
467 |
# a repeatable holiday, this should check if there are some exception |
468 |
# for that holiday rule. Otherwise, if it is a regular holiday, it´s |
469 |
# ok just deleting it. |
470 |
|
471 |
my $dbh = C4::Context->dbh(); |
472 |
my $isSingleHoliday = $dbh->prepare("SELECT id FROM special_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?) AND (year = ?)"); |
473 |
$isSingleHoliday->execute($self->{branchcode}, $options{day}, $options{month}, $options{year}); |
474 |
if ($isSingleHoliday->rows) { |
475 |
my $id = $isSingleHoliday->fetchrow; |
476 |
$isSingleHoliday->finish; # Close the last query |
477 |
|
478 |
my $deleteHoliday = $dbh->prepare("DELETE FROM special_holidays WHERE id = ?"); |
479 |
$deleteHoliday->execute($id); |
480 |
delete($self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}); |
481 |
} else { |
178 |
} else { |
482 |
$isSingleHoliday->finish; # Close the last query |
179 |
my @args = ( ( map { $info->{$_} } qw(title description) ), $branchcode, $info->{weekday}, $info->{month}, $info->{day} ); |
483 |
|
180 |
|
484 |
my $isWeekdayHoliday = $dbh->prepare("SELECT id FROM repeatable_holidays WHERE branchcode = ? AND weekday = ?"); |
181 |
# The code below relies on $dbh->do returning 0 when the update affects no rows |
485 |
$isWeekdayHoliday->execute($self->{branchcode}, $options{weekday}); |
182 |
my $affected = $dbh->do( q{ |
486 |
if ($isWeekdayHoliday->rows) { |
183 |
UPDATE repeatable_holidays |
487 |
my $id = $isWeekdayHoliday->fetchrow; |
184 |
SET |
488 |
$isWeekdayHoliday->finish; # Close the last query |
185 |
title = ?, description = ? |
489 |
|
186 |
WHERE branchcode = ? |
490 |
my $updateExceptions = $dbh->prepare("UPDATE special_holidays SET isexception = 0 WHERE (WEEKDAY(CONCAT(special_holidays.year,'-',special_holidays.month,'-',special_holidays.day)) = ?) AND (branchcode = ?)"); |
187 |
} . _get_compare( 'weekday', $info->{weekday} ) . _get_compare( 'month', $info->{month} ) . _get_compare( 'day', $info->{day} ), {}, @args ); |
491 |
$updateExceptions->execute($options{weekday}, $self->{branchcode}); |
188 |
|
492 |
$updateExceptions->finish; # Close the last query |
189 |
$dbh->do( q{ |
493 |
|
190 |
INSERT |
494 |
my $deleteHoliday = $dbh->prepare("DELETE FROM repeatable_holidays WHERE id = ?"); |
191 |
INTO repeatable_holidays(title, description, branchcode, weekday, month, day) |
495 |
$deleteHoliday->execute($id); |
192 |
VALUES (?, ?, ?, ?, ?, ?) |
496 |
delete($self->{'week_days_holidays'}->{$options{weekday}}); |
193 |
}, {}, @args ) unless ( $affected > 0 ); |
497 |
} else { |
|
|
498 |
$isWeekdayHoliday->finish; # Close the last query |
499 |
|
500 |
my $isDayMonthHoliday = $dbh->prepare("SELECT id FROM repeatable_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?)"); |
501 |
$isDayMonthHoliday->execute($self->{branchcode}, $options{day}, $options{month}); |
502 |
if ($isDayMonthHoliday->rows) { |
503 |
my $id = $isDayMonthHoliday->fetchrow; |
504 |
$isDayMonthHoliday->finish; |
505 |
my $updateExceptions = $dbh->prepare("UPDATE special_holidays SET isexception = 0 WHERE (special_holidays.branchcode = ?) AND (special_holidays.day = ?) and (special_holidays.month = ?)"); |
506 |
$updateExceptions->execute($self->{branchcode}, $options{day}, $options{month}); |
507 |
$updateExceptions->finish; # Close the last query |
508 |
|
509 |
my $deleteHoliday = $dbh->prepare("DELETE FROM repeatable_holidays WHERE (id = ?)"); |
510 |
$deleteHoliday->execute($id); |
511 |
delete($self->{'day_month_holidays'}->{"$options{month}/$options{day}"}); |
512 |
} |
513 |
} |
514 |
} |
194 |
} |
515 |
return $self; |
|
|
516 |
} |
517 |
=head2 delete_holiday_range |
518 |
|
519 |
delete_holiday_range(day => $day, |
520 |
month => $month, |
521 |
year => $year); |
522 |
|
523 |
Delete a holiday range of dates for $self->{branchcode}. |
524 |
|
525 |
C<$day> Is the day month to make the date to delete. |
526 |
|
527 |
C<$month> Is month to make the date to delete. |
528 |
|
529 |
C<$year> Is year to make the date to delete. |
530 |
|
531 |
=cut |
532 |
|
533 |
sub delete_holiday_range { |
534 |
my $self = shift; |
535 |
my %options = @_; |
536 |
|
537 |
my $dbh = C4::Context->dbh(); |
538 |
my $sth = $dbh->prepare("DELETE FROM special_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?) AND (year = ?)"); |
539 |
$sth->execute($self->{branchcode}, $options{day}, $options{month}, $options{year}); |
540 |
} |
195 |
} |
541 |
|
196 |
|
542 |
=head2 delete_holiday_range_repeatable |
197 |
=head2 DelSingleEvent |
543 |
|
198 |
|
544 |
delete_holiday_range_repeatable(day => $day, |
199 |
DelSingleEvent( $branchcode, \%info ) |
545 |
month => $month); |
|
|
546 |
|
200 |
|
547 |
Delete a holiday for $self->{branchcode}. |
201 |
Deletes an event for a single date. $info->{date} should be an ISO-formatted date string. |
548 |
|
|
|
549 |
C<$day> Is the day month to make the date to delete. |
550 |
|
551 |
C<$month> Is month to make the date to delete. |
552 |
|
202 |
|
553 |
=cut |
203 |
=cut |
554 |
|
204 |
|
555 |
sub delete_holiday_range_repeatable { |
205 |
sub DelSingleEvent { |
556 |
my $self = shift; |
206 |
my ( $branchcode, $info ) = @_; |
557 |
my %options = @_; |
|
|
558 |
|
559 |
my $dbh = C4::Context->dbh(); |
560 |
my $sth = $dbh->prepare("DELETE FROM repeatable_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?)"); |
561 |
$sth->execute($self->{branchcode}, $options{day}, $options{month}); |
562 |
} |
563 |
|
564 |
=head2 delete_exception_holiday_range |
565 |
|
207 |
|
566 |
delete_exception_holiday_range(weekday => $weekday |
208 |
my ( $year, $month, $day ) = ( $info->{date} =~ /(\d+)-(\d+)-(\d+)/ ); |
567 |
day => $day, |
209 |
return unless ( $year && $month && $day ); |
568 |
month => $month, |
|
|
569 |
year => $year); |
570 |
|
210 |
|
571 |
Delete a holiday for $self->{branchcode}. |
211 |
C4::Context->dbh->do( q{ |
572 |
|
212 |
DELETE FROM special_holidays |
573 |
C<$day> Is the day month to make the date to delete. |
213 |
WHERE branchcode = ? AND year = ? AND month = ? AND day = ? |
574 |
|
214 |
}, {}, $branchcode, $year, $month, $day ); |
575 |
C<$month> Is month to make the date to delete. |
|
|
576 |
|
577 |
C<$year> Is year to make the date to delete. |
578 |
|
579 |
=cut |
580 |
|
581 |
sub delete_exception_holiday_range { |
582 |
my $self = shift; |
583 |
my %options = @_; |
584 |
|
585 |
my $dbh = C4::Context->dbh(); |
586 |
my $sth = $dbh->prepare("DELETE FROM special_holidays WHERE (branchcode = ?) AND (isexception = 1) AND (day = ?) AND (month = ?) AND (year = ?)"); |
587 |
$sth->execute($self->{branchcode}, $options{day}, $options{month}, $options{year}); |
588 |
} |
215 |
} |
589 |
|
216 |
|
590 |
=head2 isHoliday |
217 |
=head2 DelRepeatingEvent |
591 |
|
|
|
592 |
$isHoliday = isHoliday($day, $month $year); |
593 |
|
594 |
C<$day> Is the day to check whether if is a holiday or not. |
595 |
|
596 |
C<$month> Is the month to check whether if is a holiday or not. |
597 |
|
598 |
C<$year> Is the year to check whether if is a holiday or not. |
599 |
|
600 |
=cut |
601 |
|
602 |
sub isHoliday { |
603 |
my ($self, $day, $month, $year) = @_; |
604 |
# FIXME - date strings are stored in non-padded metric format. should change to iso. |
605 |
# FIXME - should change arguments to accept C4::Dates object |
606 |
$month=$month+0; |
607 |
$year=$year+0; |
608 |
$day=$day+0; |
609 |
my $weekday = &Date::Calc::Day_of_Week($year, $month, $day) % 7; |
610 |
my $weekDays = $self->get_week_days_holidays(); |
611 |
my $dayMonths = $self->get_day_month_holidays(); |
612 |
my $exceptions = $self->get_exception_holidays(); |
613 |
my $singles = $self->get_single_holidays(); |
614 |
if (defined($exceptions->{"$year/$month/$day"})) { |
615 |
return 0; |
616 |
} else { |
617 |
if ((exists($weekDays->{$weekday})) || |
618 |
(exists($dayMonths->{"$month/$day"})) || |
619 |
(exists($singles->{"$year/$month/$day"}))) { |
620 |
return 1; |
621 |
} else { |
622 |
return 0; |
623 |
} |
624 |
} |
625 |
|
218 |
|
626 |
} |
219 |
DelRepeatingEvent( $branchcode, \%info ) |
627 |
|
220 |
|
628 |
=head2 copy_to_branch |
221 |
Deletes a weekly- or yearly-repeating event. Either $info->{weekday}, or |
629 |
|
222 |
$info->{month} and $info->{day} should be set, for a weekly or yearly event, |
630 |
$calendar->copy_to_branch($target_branch) |
223 |
respectively. |
631 |
|
224 |
|
632 |
=cut |
225 |
=cut |
633 |
|
226 |
|
634 |
sub copy_to_branch { |
227 |
sub DelRepeatingEvent { |
635 |
my ($self, $target_branch) = @_; |
228 |
my ( $branchcode, $info ) = @_; |
636 |
|
|
|
637 |
croak "No target_branch" unless $target_branch; |
638 |
|
229 |
|
639 |
my $target_calendar = C4::Calendar->new(branchcode => $target_branch); |
230 |
C4::Context->dbh->do( q{ |
640 |
|
231 |
DELETE FROM repeatable_holidays |
641 |
my ($y, $m, $d) = Today(); |
232 |
WHERE branchcode = ? |
642 |
my $today = sprintf ISO_DATE_FORMAT, $y,$m,$d; |
233 |
} . _get_compare( 'weekday', $info->{weekday} ) . _get_compare( 'month', $info->{month} ) . _get_compare( 'day', $info->{day} ), {}, $branchcode, $info->{weekday}, $info->{month}, $info->{day} ); |
643 |
|
|
|
644 |
my $wdh = $self->get_week_days_holidays; |
645 |
$target_calendar->insert_week_day_holiday( weekday => $_, %{ $wdh->{$_} } ) |
646 |
foreach keys %$wdh; |
647 |
$target_calendar->insert_day_month_holiday(%$_) |
648 |
foreach values %{ $self->get_day_month_holidays }; |
649 |
$target_calendar->insert_exception_holiday(%$_) |
650 |
foreach grep { $_->{date} gt $today } values %{ $self->get_exception_holidays }; |
651 |
$target_calendar->insert_single_holiday(%$_) |
652 |
foreach grep { $_->{date} gt $today } values %{ $self->get_single_holidays }; |
653 |
|
654 |
return 1; |
655 |
} |
234 |
} |
656 |
|
235 |
|
657 |
=head2 addDate |
236 |
=head2 CopyAllEvents |
658 |
|
237 |
|
659 |
my ($day, $month, $year) = $calendar->addDate($date, $offset) |
238 |
CopyAllEvents( $from_branchcode, $to_branchcode ) |
660 |
|
239 |
|
661 |
C<$date> is a C4::Dates object representing the starting date of the interval. |
240 |
Copies all events from one branch to another. |
662 |
|
|
|
663 |
C<$offset> Is the number of days that this function has to count from $date. |
664 |
|
241 |
|
665 |
=cut |
242 |
=cut |
666 |
|
243 |
|
667 |
sub addDate { |
244 |
sub CopyAllEvents { |
668 |
my ($self, $startdate, $offset) = @_; |
245 |
my ( $from_branchcode, $to_branchcode ) = @_; |
669 |
my ($year,$month,$day) = split("-",$startdate->output('iso')); |
246 |
|
670 |
my $daystep = 1; |
247 |
C4::Context->dbh->do( q{ |
671 |
if ($offset < 0) { # In case $offset is negative |
248 |
INSERT IGNORE INTO special_holidays(branchcode, year, month, day, isexception, title, description) |
672 |
# $offset = $offset*(-1); |
249 |
SELECT ?, year, month, day, isexception, title, description |
673 |
$daystep = -1; |
250 |
FROM special_holidays |
674 |
} |
251 |
WHERE branchcode = ? |
675 |
my $daysMode = C4::Context->preference('useDaysMode'); |
252 |
}, {}, $to_branchcode, $from_branchcode ); |
676 |
if ($daysMode eq 'Datedue') { |
253 |
|
677 |
($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset ); |
254 |
C4::Context->dbh->do( q{ |
678 |
while ($self->isHoliday($day, $month, $year)) { |
255 |
INSERT IGNORE INTO repeatable_holidays(branchcode, weekday, month, day, title, description) |
679 |
($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $daystep); |
256 |
SELECT ?, weekday, month, day, title, description |
680 |
} |
257 |
FROM repeatable_holidays |
681 |
} elsif($daysMode eq 'Calendar') { |
258 |
WHERE branchcode = ? |
682 |
while ($offset != 0) { |
259 |
}, {}, $to_branchcode, $from_branchcode ); |
683 |
($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $daystep); |
|
|
684 |
if (!($self->isHoliday($day, $month, $year))) { |
685 |
$offset = $offset - $daystep; |
686 |
} |
687 |
} |
688 |
} else { ## ($daysMode eq 'Days') |
689 |
($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset ); |
690 |
} |
691 |
return(C4::Dates->new( sprintf(ISO_DATE_FORMAT,$year,$month,$day),'iso')); |
692 |
} |
260 |
} |
693 |
|
261 |
|
694 |
=head2 daysBetween |
|
|
695 |
|
696 |
my $daysBetween = $calendar->daysBetween($startdate, $enddate) |
697 |
|
698 |
C<$startdate> and C<$enddate> are C4::Dates objects that define the interval. |
699 |
|
700 |
Returns the number of non-holiday days in the interval. |
701 |
useDaysMode syspref has no effect here. |
702 |
=cut |
703 |
|
704 |
sub daysBetween { |
705 |
my $self = shift or return; |
706 |
my $startdate = shift or return; |
707 |
my $enddate = shift or return; |
708 |
my ($yearFrom,$monthFrom,$dayFrom) = split("-",$startdate->output('iso')); |
709 |
my ($yearTo, $monthTo, $dayTo ) = split("-", $enddate->output('iso')); |
710 |
if (Date_to_Days($yearFrom,$monthFrom,$dayFrom) > Date_to_Days($yearTo,$monthTo,$dayTo)) { |
711 |
return 0; |
712 |
# we don't go backwards ( FIXME - handle this error better ) |
713 |
} |
714 |
my $count = 0; |
715 |
while (1) { |
716 |
($yearFrom != $yearTo or $monthFrom != $monthTo or $dayFrom != $dayTo) or last; # if they all match, it's the last day |
717 |
unless ($self->isHoliday($dayFrom, $monthFrom, $yearFrom)) { |
718 |
$count++; |
719 |
} |
720 |
($yearFrom, $monthFrom, $dayFrom) = &Date::Calc::Add_Delta_Days($yearFrom, $monthFrom, $dayFrom, 1); |
721 |
} |
722 |
return($count); |
723 |
} |
724 |
|
262 |
|
725 |
1; |
263 |
1; |
726 |
|
264 |
|
Lines 729-733
__END__
Link Here
|
729 |
=head1 AUTHOR |
267 |
=head1 AUTHOR |
730 |
|
268 |
|
731 |
Koha Physics Library UNLP <matias_veleda@hotmail.com> |
269 |
Koha Physics Library UNLP <matias_veleda@hotmail.com> |
|
|
270 |
Jesse Weaver <jweaver@bywatersolutions.com> |
732 |
|
271 |
|
733 |
=cut |
272 |
=cut |