Line 0
Link Here
|
|
|
1 |
package Koha::DiscreteCalendar; |
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Koha is free software; you can redistribute it and/or modify it |
6 |
# under the terms of the GNU General Public License as published by |
7 |
# the Free Software Foundation; either version 3 of the License, or |
8 |
# (at your option) any later version. |
9 |
# |
10 |
# Koha is distributed in the hope that it will be useful, but |
11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
# GNU General Public License for more details. |
14 |
# |
15 |
# You should have received a copy of the GNU General Public License |
16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
17 |
|
18 |
#####Sets holiday periods for each branch. Datedues will be extended if branch is closed -TG |
19 |
use Modern::Perl; |
20 |
|
21 |
use CGI qw ( -utf8 ); |
22 |
use Carp; |
23 |
use DateTime; |
24 |
use DateTime::Format::Strptime; |
25 |
use Data::Dumper; |
26 |
|
27 |
use C4::Context; |
28 |
use C4::Output; |
29 |
use Koha::Database; |
30 |
use Koha::DateUtils; |
31 |
|
32 |
# Global variables to make code more readable |
33 |
our $HOLIDAYS = { |
34 |
EXCEPTION => 'E', |
35 |
REPEATABLE => 'R', |
36 |
SINGLE => 'S', |
37 |
NEED_VALIDATION => 'N', |
38 |
FLOAT => 'F', |
39 |
WEEKLY => 'W', |
40 |
NONE => 'none' |
41 |
}; |
42 |
|
43 |
=head1 NAME |
44 |
|
45 |
Koha::DiscreteCalendar - Object containing a branches calendar, working with the SQL database |
46 |
|
47 |
=head1 SYNOPSIS |
48 |
|
49 |
use Koha::DiscreteCalendar |
50 |
|
51 |
my $c = Koha::DiscreteCalendar->new({ branchcode => 'MAIN' }); |
52 |
my $dt = dt_from_string(); |
53 |
|
54 |
# are we open |
55 |
$open = $c->is_holiday($dt); |
56 |
# when will item be due if loan period = $dur (a DateTime::Duration object) |
57 |
$duedate = $c->addDuration($dt, $dur, 'days'); |
58 |
|
59 |
|
60 |
=head1 DESCRIPTION |
61 |
|
62 |
Implements a new Calendar object, but uses the SQL database to keep track of days and holidays. |
63 |
This results in a performance gain since the optimization is done by the MySQL database/team. |
64 |
|
65 |
=head1 METHODS |
66 |
|
67 |
=head2 new : Create a (discrete) calendar object |
68 |
|
69 |
my $calendar = Koha::DiscreteCalendar->new({ branchcode => 'MAIN' }); |
70 |
|
71 |
The option branchcode is required |
72 |
|
73 |
=cut |
74 |
|
75 |
sub new { |
76 |
my ( $classname, $options ) = @_; |
77 |
my $self = {}; |
78 |
bless $self, $classname; |
79 |
for my $o_name ( keys %{ $options } ) { |
80 |
my $o = lc $o_name; |
81 |
$self->{$o} = $options->{$o_name}; |
82 |
} |
83 |
if ( !defined $self->{branchcode} ) { |
84 |
croak 'No branchcode argument passed to Koha::DiscreteCalendar->new'; |
85 |
} |
86 |
if ( ref $self->{branchcode} eq 'Koha::Library' ) { |
87 |
$self->{branchcode} = $self->{branchcode}->branchcode; |
88 |
} |
89 |
$self->_init(); |
90 |
|
91 |
return $self; |
92 |
} |
93 |
|
94 |
sub _init { |
95 |
my $self = shift; |
96 |
$self->{days_mode} ||= C4::Context->preference('useDaysMode'); |
97 |
#If the branchcode doesn't exist we use the default calendar. |
98 |
my $schema = Koha::Database->new->schema; |
99 |
my $branchcode = $self->{branchcode}; |
100 |
my $dtf = $schema->storage->datetime_parser; |
101 |
my $today = $dtf->format_datetime(DateTime->today); |
102 |
my $rs = $schema->resultset('DiscreteCalendar')->single( |
103 |
{ |
104 |
branchcode => $branchcode, |
105 |
date => $today |
106 |
} |
107 |
); |
108 |
#use default if no calendar is found |
109 |
if (!$rs) { |
110 |
$self->{branchcode} = undef; |
111 |
$self->{no_branch_selected} = 1; |
112 |
} |
113 |
|
114 |
} |
115 |
|
116 |
=head2 get_dates_info |
117 |
|
118 |
my @dates = $calendar->get_dates_info(); |
119 |
|
120 |
Returns an array of hashes representing the dates in this calendar. The hash |
121 |
contains the fields C<$date>, C<$outputdate>, C<$holiday_type>, C<$open_hour>, |
122 |
C<$close_hour> and C<$note>. |
123 |
|
124 |
=cut |
125 |
|
126 |
sub get_dates_info { |
127 |
my $self = shift; |
128 |
my $branchcode = $self->{branchcode}; |
129 |
my @datesInfos =(); |
130 |
my $schema = Koha::Database->new->schema; |
131 |
|
132 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
133 |
{ |
134 |
branchcode => $branchcode |
135 |
}, |
136 |
{ |
137 |
select => [ 'date', { DATE => 'date' } ], |
138 |
as => [qw/ date date /], |
139 |
columns =>[ qw/ holiday_type open_hour close_hour note/] |
140 |
}, |
141 |
); |
142 |
|
143 |
while (my $date = $rs->next()) { |
144 |
my $outputdate = dt_from_string( $date->date(), 'iso'); |
145 |
$outputdate = output_pref( { dt => $outputdate, dateonly => 1 } ); |
146 |
push @datesInfos, { |
147 |
date => $date->date(), |
148 |
outputdate => $outputdate, |
149 |
holiday_type => $date->holiday_type() , |
150 |
open_hour => $date->open_hour(), |
151 |
close_hour => $date->close_hour(), |
152 |
note => $date->note() |
153 |
}; |
154 |
} |
155 |
|
156 |
return @datesInfos; |
157 |
} |
158 |
|
159 |
=head2 add_new_branch |
160 |
|
161 |
Koha::DiscreteCalendar->add_new_branch($copyBranch, $newBranch) |
162 |
|
163 |
This method will copy everything from a given branch to a new branch |
164 |
C<$copyBranch> is the branch to copy from |
165 |
C<$newBranch> is the branch to be created, and copy into |
166 |
|
167 |
=cut |
168 |
|
169 |
sub add_new_branch { |
170 |
my ( $classname, $copyBranch, $newBranch) = @_; |
171 |
|
172 |
my $schema = Koha::Database->new->schema; |
173 |
|
174 |
my $branch_rs = $schema->resultset('DiscreteCalendar')->search({ |
175 |
branchcode => $copyBranch |
176 |
}); |
177 |
|
178 |
while (my $row = $branch_rs->next()) { |
179 |
$schema->resultset('DiscreteCalendar')->create({ |
180 |
date => $row->date(), |
181 |
branchcode => $newBranch, |
182 |
is_opened => $row->is_opened(), |
183 |
holiday_type => $row->holiday_type(), |
184 |
open_hour => $row->open_hour(), |
185 |
close_hour => $row->close_hour(), |
186 |
}); |
187 |
} |
188 |
|
189 |
} |
190 |
|
191 |
=head2 get_date_info |
192 |
|
193 |
my $date = $calendar->get_date_info; |
194 |
|
195 |
Returns a reference-to-hash representing a DiscreteCalendar date data object. |
196 |
The hash contains the fields C<$date>, C<$outputdate>, C<$holiday_type>, |
197 |
C<$open_hour>, C<$close_hour> and C<$note>. |
198 |
|
199 |
=cut |
200 |
|
201 |
sub get_date_info { |
202 |
my ($self, $date) = @_; |
203 |
my $branchcode = $self->{branchcode}; |
204 |
my $schema = Koha::Database->new->schema; |
205 |
my $dtf = $schema->storage->datetime_parser; |
206 |
#String dates for Database usage |
207 |
my $date_string = $dtf->format_datetime($date); |
208 |
|
209 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
210 |
{ |
211 |
branchcode => $branchcode, |
212 |
}, |
213 |
{ |
214 |
select => [ 'date', { DATE => 'date' } ], |
215 |
as => [qw/ date date /], |
216 |
where => \['DATE(?) = date', $date_string ], |
217 |
columns =>[ qw/ branchcode holiday_type open_hour close_hour note/] |
218 |
}, |
219 |
); |
220 |
my $dateDTO; |
221 |
while (my $date = $rs->next()) { |
222 |
$dateDTO = { |
223 |
date => $date->date(), |
224 |
branchcode => $date->branchcode(), |
225 |
holiday_type => $date->holiday_type() , |
226 |
open_hour => $date->open_hour(), |
227 |
close_hour => $date->close_hour(), |
228 |
note => $date->note() |
229 |
}; |
230 |
} |
231 |
|
232 |
return $dateDTO; |
233 |
} |
234 |
|
235 |
=head2 get_max_date |
236 |
|
237 |
my $maxDate = $calendar->get_max_date(); |
238 |
|
239 |
Returns the furthest date available in the databse of current branch. |
240 |
|
241 |
=cut |
242 |
|
243 |
sub get_max_date { |
244 |
my $self = shift; |
245 |
my $branchcode = $self->{branchcode}; |
246 |
my $schema = Koha::Database->new->schema; |
247 |
|
248 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
249 |
{ |
250 |
branchcode => $branchcode |
251 |
}, |
252 |
{ |
253 |
select => [{ MAX => 'date' } ], |
254 |
as => [qw/ max /], |
255 |
} |
256 |
); |
257 |
|
258 |
return $rs->next()->get_column('max'); |
259 |
} |
260 |
|
261 |
=head2 get_min_date |
262 |
|
263 |
my $minDate = $calendar->get_min_date(); |
264 |
|
265 |
Returns the oldest date available in the databse of current branch. |
266 |
|
267 |
=cut |
268 |
|
269 |
sub get_min_date { |
270 |
my $self = shift; |
271 |
my $branchcode = $self->{branchcode}; |
272 |
my $schema = Koha::Database->new->schema; |
273 |
|
274 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
275 |
{ |
276 |
branchcode => $branchcode |
277 |
}, |
278 |
{ |
279 |
select => [{ MIN => 'date' } ], |
280 |
as => [qw/ min /], |
281 |
} |
282 |
); |
283 |
|
284 |
return $rs->next()->get_column('min'); |
285 |
} |
286 |
|
287 |
=head2 get_unique_holidays |
288 |
|
289 |
my @unique_holidays = $calendar->get_unique_holidays(); |
290 |
|
291 |
Returns an array of all the date objects that are unique holidays. |
292 |
|
293 |
=cut |
294 |
|
295 |
sub get_unique_holidays { |
296 |
my $self = shift; |
297 |
my $exclude_past = shift || 1; |
298 |
my $branchcode = $self->{branchcode}; |
299 |
my @unique_holidays; |
300 |
my $schema = Koha::Database->new->schema; |
301 |
|
302 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
303 |
{ |
304 |
branchcode => $branchcode, |
305 |
holiday_type => $HOLIDAYS->{EXCEPTION} |
306 |
}, |
307 |
{ |
308 |
select => [{ DATE => 'date' }, 'note' ], |
309 |
as => [qw/ date note/], |
310 |
where => ($exclude_past ? \[' date >= CURRENT_DATE()'] : {} ), |
311 |
} |
312 |
); |
313 |
while (my $date = $rs->next()) { |
314 |
my $outputdate = dt_from_string($date->date(), 'iso'); |
315 |
$outputdate = output_pref( { dt => $outputdate, dateonly => 1 } ); |
316 |
push @unique_holidays, { |
317 |
date => $date->date(), |
318 |
outputdate => $outputdate, |
319 |
note => $date->note() |
320 |
} |
321 |
} |
322 |
|
323 |
return @unique_holidays; |
324 |
} |
325 |
|
326 |
=head2 get_float_holidays |
327 |
|
328 |
my @float_holidays = $calendar->get_float_holidays(); |
329 |
|
330 |
Returns an array of all the date objects that are float holidays. |
331 |
|
332 |
=cut |
333 |
|
334 |
sub get_float_holidays { |
335 |
my $self = shift; |
336 |
my $exclude_past = shift || 1; |
337 |
my $branchcode = $self->{branchcode}; |
338 |
my @float_holidays; |
339 |
my $schema = Koha::Database->new->schema; |
340 |
|
341 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
342 |
{ |
343 |
branchcode => $branchcode, |
344 |
holiday_type => $HOLIDAYS->{FLOAT} |
345 |
}, |
346 |
{ |
347 |
select => [{ DATE => 'date' }, 'note' ], |
348 |
as => [qw/ date note/], |
349 |
where => ($exclude_past ? \[' date >= CURRENT_DATE()'] : {} ), |
350 |
} |
351 |
); |
352 |
while (my $date = $rs->next()) { |
353 |
my $outputdate = dt_from_string($date->date(), 'iso'); |
354 |
$outputdate = output_pref( { dt => $outputdate, dateonly => 1 } ); |
355 |
push @float_holidays, { |
356 |
date => $date->date(), |
357 |
outputdate => $outputdate, |
358 |
note => $date->note() |
359 |
} |
360 |
} |
361 |
|
362 |
return @float_holidays; |
363 |
} |
364 |
|
365 |
=head2 get_need_validation_holidays |
366 |
|
367 |
my @need_validation_holidays = $calendar->get_need_validation_holidays(); |
368 |
|
369 |
Returns an array of all the date objects that are float holidays in need of validation. |
370 |
|
371 |
=cut |
372 |
|
373 |
sub get_need_validation_holidays { |
374 |
my $self = shift; |
375 |
my $exclude_past = shift || 1; |
376 |
my $branchcode = $self->{branchcode}; |
377 |
my @need_validation_holidays; |
378 |
my $schema = Koha::Database->new->schema; |
379 |
|
380 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
381 |
{ |
382 |
branchcode => $branchcode, |
383 |
holiday_type => $HOLIDAYS->{NEED_VALIDATION} |
384 |
}, |
385 |
{ |
386 |
select => [{ DATE => 'date' }, 'note' ], |
387 |
as => [qw/ date note/], |
388 |
where => ($exclude_past ? \[' date >= CURRENT_DATE()'] : {} ), |
389 |
} |
390 |
); |
391 |
while (my $date = $rs->next()) { |
392 |
my $outputdate = dt_from_string($date->date(), 'iso'); |
393 |
$outputdate = output_pref( { dt => $outputdate, dateonly => 1 } ); |
394 |
push @need_validation_holidays, { |
395 |
date => $date->date(), |
396 |
outputdate => $outputdate, |
397 |
note => $date->note() |
398 |
} |
399 |
} |
400 |
|
401 |
return @need_validation_holidays; |
402 |
} |
403 |
|
404 |
=head2 get_repeatable_holidays |
405 |
|
406 |
my @repeatable_holidays = $calendar->get_repeatable_holidays(); |
407 |
|
408 |
Returns an array of all the date objects that are repeatable holidays. |
409 |
|
410 |
=cut |
411 |
|
412 |
sub get_repeatable_holidays { |
413 |
my $self = shift; |
414 |
my $exclude_past = shift || 1; |
415 |
my $branchcode = $self->{branchcode}; |
416 |
my @repeatable_holidays; |
417 |
my $schema = Koha::Database->new->schema; |
418 |
|
419 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
420 |
{ |
421 |
branchcode => $branchcode, |
422 |
holiday_type => $HOLIDAYS->{'REPEATABLE'}, |
423 |
|
424 |
}, |
425 |
{ |
426 |
select => \[ 'distinct DAY(date), MONTH(date), note'], |
427 |
as => [qw/ day month note/], |
428 |
where => ($exclude_past ? \[' date >= CURRENT_DATE()'] : {} ), |
429 |
} |
430 |
); |
431 |
|
432 |
while (my $date = $rs->next()) { |
433 |
push @repeatable_holidays, { |
434 |
day => $date->get_column('day'), |
435 |
month => $date->get_column('month'), |
436 |
note => $date->note() |
437 |
}; |
438 |
} |
439 |
|
440 |
return @repeatable_holidays; |
441 |
} |
442 |
|
443 |
=head2 get_week_days_holidays |
444 |
|
445 |
my @week_days_holidays = $calendar->get_week_days_holidays; |
446 |
|
447 |
Returns an array of all the date objects that are weekly holidays. |
448 |
|
449 |
=cut |
450 |
|
451 |
sub get_week_days_holidays { |
452 |
my $self = shift; |
453 |
my $exclude_past = shift || 1; |
454 |
my $branchcode = $self->{branchcode}; |
455 |
my @week_days; |
456 |
my $schema = Koha::Database->new->schema; |
457 |
|
458 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
459 |
{ |
460 |
holiday_type => $HOLIDAYS->{WEEKLY}, |
461 |
branchcode => $branchcode, |
462 |
}, |
463 |
{ |
464 |
select => [{ DAYOFWEEK => 'date'}, 'note'], |
465 |
as => [qw/ weekday note /], |
466 |
distinct => 1, |
467 |
where => ($exclude_past ? \[' date >= CURRENT_DATE()'] : {} ), |
468 |
} |
469 |
); |
470 |
|
471 |
while (my $date = $rs->next()) { |
472 |
push @week_days, { |
473 |
weekday => ($date->get_column('weekday') -1), |
474 |
note => $date->note() |
475 |
}; |
476 |
} |
477 |
|
478 |
return @week_days; |
479 |
} |
480 |
|
481 |
=head2 edit_holiday |
482 |
|
483 |
Modifies a date or a range of dates |
484 |
|
485 |
C<$title> Is the title to be modified for the holiday formed by $year/$month/$day. |
486 |
|
487 |
C<$weekday> Is the day of week for the holiday or the value everyday when it's for the whole range. |
488 |
|
489 |
C<$holiday_type> Is the type of the holiday : |
490 |
E : Exception holiday, single day. |
491 |
F : Floating holiday, different day each year. |
492 |
N : Needs validation, copied float holiday from the past |
493 |
R : Repeatable holiday, repeated on same date. |
494 |
W : Weekly holiday, same day of the week. |
495 |
|
496 |
C<$open_hour> Is the opening hour. |
497 |
C<$close_hour> Is the closing hour. |
498 |
C<$start_date> Is the start of the range of dates. |
499 |
C<$end_date> Is the end of the range of dates. |
500 |
C<$delete_type> Delete all |
501 |
C<$today> Today based on the local date, using JavaScript. |
502 |
|
503 |
=cut |
504 |
|
505 |
sub edit_holiday { |
506 |
my $self = shift; |
507 |
my ($params) = @_; |
508 |
|
509 |
my $title = $params->{title}; |
510 |
my $weekday = $params->{weekday} || ''; |
511 |
my $holiday_type = $params->{holiday_type}; |
512 |
|
513 |
my $start_date = $params->{start_date}; |
514 |
my $end_date = $params->{end_date}; |
515 |
|
516 |
my $open_hour = $params->{open_hour} || ''; |
517 |
my $close_hour = $params->{close_hour} || ''; |
518 |
|
519 |
my $delete_type = $params->{delete_type} || undef; |
520 |
my $today = $params->{today} || dt_from_string()->truncate( to => 'day' ); |
521 |
|
522 |
my $branchcode = $self->{branchcode}; |
523 |
|
524 |
# When override param is set, this function will allow past dates to be set as holidays, |
525 |
# otherwise it will not. This is meant to only be used for testing. |
526 |
my $override = $params->{override} || 0; |
527 |
|
528 |
my $schema = Koha::Database->new->schema; |
529 |
$schema->{AutoCommit} = 0; |
530 |
$schema->storage->txn_begin; |
531 |
my $dtf = DateTime::Format::Strptime->new(pattern => "%F %T"); |
532 |
|
533 |
#String dates for Database usage |
534 |
my $start_date_string = $dtf->format_datetime($start_date->clone->truncate(to => 'day')); |
535 |
my $end_date_string = $dtf->format_datetime($end_date->clone->truncate(to => 'day')); |
536 |
$today = $dtf->format_datetime($today->clone->truncate(to => 'day')); |
537 |
my %updateValues = ( |
538 |
is_opened => 0, |
539 |
note => $title, |
540 |
holiday_type => $holiday_type, |
541 |
); |
542 |
$updateValues{open_hour} = $open_hour if $open_hour ne ''; |
543 |
$updateValues{close_hour} = $close_hour if $close_hour ne ''; |
544 |
|
545 |
if ($holiday_type eq $HOLIDAYS->{WEEKLY}) { |
546 |
#Update weekly holidays |
547 |
if ($start_date_string eq $end_date_string) { |
548 |
$end_date_string = $self->get_max_date(); |
549 |
} |
550 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
551 |
{ |
552 |
branchcode => $branchcode, |
553 |
}, |
554 |
{ |
555 |
where => \[ 'DAYOFWEEK(date) = ? AND date >= DATE(?) AND date <= DATE(?)', $weekday, $start_date_string, $end_date_string], |
556 |
} |
557 |
); |
558 |
|
559 |
while (my $date = $rs->next()) { |
560 |
$date->update(\%updateValues); |
561 |
} |
562 |
} elsif ($holiday_type eq $HOLIDAYS->{EXCEPTION} || $holiday_type eq $HOLIDAYS->{FLOAT} || $holiday_type eq $HOLIDAYS->{NEED_VALIDATION}) { |
563 |
#Update Exception Float and Needs Validation holidays |
564 |
my $where = { date => { -between => [$start_date_string, $end_date_string]}}; |
565 |
if ($start_date_string ne $end_date_string && $weekday && $weekday ne 'everyday') { |
566 |
$where = {-and => [ \["DAYOFWEEK(date) = ?", $weekday], date => { -between => [$start_date_string, $end_date_string]}]}; |
567 |
} |
568 |
$where->{date}{'>='} = $today unless $override; |
569 |
|
570 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
571 |
{ |
572 |
branchcode => $branchcode, |
573 |
}, |
574 |
{ |
575 |
where => $where, |
576 |
} |
577 |
); |
578 |
while (my $date = $rs->next()) { |
579 |
$date->update(\%updateValues); |
580 |
} |
581 |
|
582 |
} elsif ($holiday_type eq $HOLIDAYS->{REPEATABLE}) { |
583 |
#Update repeatable holidays |
584 |
my $parser = DateTime::Format::Strptime->new( |
585 |
pattern => '%m-%d', |
586 |
on_error => 'croak', |
587 |
); |
588 |
#Format the dates to have only month-day ex: 01-04 for January 4th |
589 |
$start_date = $parser->format_datetime($start_date); |
590 |
$end_date = $parser->format_datetime($end_date); |
591 |
my $where = { -and => [ \["(DATE_FORMAT(date, '\%m-\%d') BETWEEN ? AND ?)", $start_date, $end_date] ] }; |
592 |
push @{$where->{'-and'}}, { 'date' => { '>=' => $today } } unless $override; |
593 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
594 |
{ |
595 |
branchcode => $branchcode, |
596 |
}, |
597 |
{ |
598 |
where => $where, |
599 |
} |
600 |
); |
601 |
while (my $date = $rs->next()) { |
602 |
$date->update(\%updateValues); |
603 |
} |
604 |
|
605 |
} else { |
606 |
#Update date(s)/Remove holidays |
607 |
my $where = { date => { -between => [$start_date_string, $end_date_string]}}; |
608 |
if ($start_date_string ne $end_date_string && $weekday && $weekday ne 'everyday') { |
609 |
$where = {-and => [ \["DAYOFWEEK(date) = ?", $weekday], date => { -between => [$start_date_string, $end_date_string]}]}; |
610 |
} |
611 |
$where->{date}{'>='} = $today unless $override; |
612 |
|
613 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
614 |
{ |
615 |
branchcode => $branchcode, |
616 |
}, |
617 |
{ |
618 |
where => $where, |
619 |
} |
620 |
); |
621 |
#If none, the date(s) will be normal days, else, |
622 |
if ($holiday_type eq 'none') { |
623 |
$updateValues{holiday_type} =''; |
624 |
$updateValues{is_opened} =1; |
625 |
} else { |
626 |
delete $updateValues{holiday_type}; |
627 |
delete $updateValues{is_opened}; |
628 |
} |
629 |
|
630 |
while (my $date = $rs->next()) { |
631 |
if ($delete_type) { |
632 |
if ($date->holiday_type() eq $HOLIDAYS->{WEEKLY}) { |
633 |
$self->remove_weekly_holidays($weekday, \%updateValues, $today); |
634 |
} elsif ($date->holiday_type() eq $HOLIDAYS->{REPEATABLE}) { |
635 |
$self->remove_repeatable_holidays($start_date, $end_date, \%updateValues, $today); |
636 |
} |
637 |
} else { |
638 |
$date->update(\%updateValues); |
639 |
} |
640 |
} |
641 |
} |
642 |
$schema->storage->txn_commit; |
643 |
$schema->{AutoCommit} = 1; |
644 |
} |
645 |
|
646 |
=head2 remove_weekly_holidays |
647 |
|
648 |
$calendar->remove_weekly_holidays($weekday, $updateValues, $today); |
649 |
|
650 |
Removes a weekly holiday and updates the days' parameters |
651 |
C<$weekday> is the weekday to un-holiday |
652 |
C<$updatevalues> is hashref containing the new parameters |
653 |
C<$today> is today's date |
654 |
|
655 |
=cut |
656 |
|
657 |
sub remove_weekly_holidays { |
658 |
my ($self, $weekday, $updateValues, $today) = @_; |
659 |
my $branchcode = $self->{branchcode}; |
660 |
my $schema = Koha::Database->new->schema; |
661 |
|
662 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
663 |
{ |
664 |
branchcode => $branchcode, |
665 |
is_opened => 0, |
666 |
holiday_type => $HOLIDAYS->{WEEKLY} |
667 |
}, |
668 |
{ |
669 |
where => {-and => [ \["DAYOFWEEK(date) = ?", $weekday], date => { '>=' => $today}]}, |
670 |
} |
671 |
); |
672 |
|
673 |
while (my $date = $rs->next()) { |
674 |
$date->update($updateValues); |
675 |
} |
676 |
} |
677 |
|
678 |
=head2 remove_repeatable_holidays |
679 |
|
680 |
$calendar->remove_repeatable_holidays($startDate, $endDate, $today); |
681 |
|
682 |
Removes a repeatable holiday and updates the days' parameters |
683 |
C<$startDatey> is the start date of the repeatable holiday |
684 |
C<$endDate> is the end date of the repeatble holiday |
685 |
C<$updatevalues> is hashref containing the new parameters |
686 |
C<$today> is today's date |
687 |
|
688 |
=cut |
689 |
|
690 |
sub remove_repeatable_holidays { |
691 |
my ($self, $startDate, $endDate, $updateValues, $today) = @_; |
692 |
my $branchcode = $self->{branchcode}; |
693 |
my $schema = Koha::Database->new->schema; |
694 |
my $parser = DateTime::Format::Strptime->new( |
695 |
pattern => '%m-%d', |
696 |
on_error => 'croak', |
697 |
); |
698 |
#Format the dates to have only month-day ex: 01-04 for January 4th |
699 |
$startDate = $parser->format_datetime($startDate); |
700 |
$endDate = $parser->format_datetime($endDate); |
701 |
|
702 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
703 |
{ |
704 |
branchcode => $branchcode, |
705 |
is_opened => 0, |
706 |
holiday_type => $HOLIDAYS->{REPEATABLE}, |
707 |
}, |
708 |
{ |
709 |
where => { -and => [date => { '>=' => $today}, \["(DATE_FORMAT(date, '\%m-\%d') BETWEEN ? AND ?)", $startDate, $endDate]]}, |
710 |
} |
711 |
); |
712 |
|
713 |
while (my $date = $rs->next()) { |
714 |
$date->update($updateValues); |
715 |
} |
716 |
} |
717 |
|
718 |
=head2 copy_to_branch |
719 |
|
720 |
$calendar->copy_to_branch($branch2); |
721 |
|
722 |
Copies the days and holidays from this branch to $branch2, ignoring dates in C<$self> |
723 |
but not in C<$branch2> |
724 |
|
725 |
C<$branch2> the branch to copy into |
726 |
|
727 |
=cut |
728 |
|
729 |
sub copy_to_branch { |
730 |
my ($self, $newBranch) =@_; |
731 |
my $branchcode = $self->{branchcode}; |
732 |
my $schema = Koha::Database->new->schema; |
733 |
|
734 |
my $copyFrom = $schema->resultset('DiscreteCalendar')->search( |
735 |
{ |
736 |
branchcode => $branchcode |
737 |
}, |
738 |
{ |
739 |
columns => [ qw/ date is_opened note holiday_type open_hour close_hour /] |
740 |
} |
741 |
); |
742 |
while (my $copyDate = $copyFrom->next()) { |
743 |
my $copyTo = $schema->resultset('DiscreteCalendar')->search( |
744 |
{ |
745 |
branchcode => $newBranch, |
746 |
date => $copyDate->date(), |
747 |
}, |
748 |
{ |
749 |
columns => [ qw/ date branchcode is_opened note holiday_type open_hour close_hour /] |
750 |
} |
751 |
); |
752 |
#if the date does not exist in the copyTO branch, than skip it. |
753 |
if ($copyTo->count ==0) { |
754 |
next; |
755 |
} |
756 |
$copyTo->next()->update({ |
757 |
is_opened => $copyDate->is_opened(), |
758 |
holiday_type => $copyDate->holiday_type(), |
759 |
note => $copyDate->note(), |
760 |
open_hour => $copyDate->open_hour(), |
761 |
close_hour => $copyDate->close_hour() |
762 |
}); |
763 |
} |
764 |
} |
765 |
|
766 |
=head2 is_opened |
767 |
|
768 |
$calendar->is_opened($date) |
769 |
|
770 |
Returns whether the library is open on C<$date> |
771 |
|
772 |
=cut |
773 |
|
774 |
sub is_opened { |
775 |
my($self, $date) = @_; |
776 |
my $branchcode = $self->{branchcode}; |
777 |
my $schema = Koha::Database->new->schema; |
778 |
my $dtf = $schema->storage->datetime_parser; |
779 |
$date= $dtf->format_datetime($date); |
780 |
#if the date is not found |
781 |
my $is_opened = -1; |
782 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
783 |
{ |
784 |
branchcode => $branchcode, |
785 |
}, |
786 |
{ |
787 |
where => \['date = DATE(?)', $date] |
788 |
} |
789 |
); |
790 |
$is_opened = $rs->next()->is_opened() if $rs->count() != 0; |
791 |
|
792 |
return $is_opened; |
793 |
} |
794 |
|
795 |
=head2 is_holiday |
796 |
|
797 |
$calendar->is_holiday($date) |
798 |
|
799 |
Returns whether C<$date> is a holiday or not |
800 |
|
801 |
=cut |
802 |
|
803 |
sub is_holiday { |
804 |
my($self, $date) = @_; |
805 |
my $branchcode = $self->{branchcode}; |
806 |
my $schema = Koha::Database->new->schema; |
807 |
my $dtf = $schema->storage->datetime_parser; |
808 |
$date= $dtf->format_datetime($date); |
809 |
#if the date is not found |
810 |
my $isHoliday = -1; |
811 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
812 |
{ |
813 |
branchcode => $branchcode, |
814 |
}, |
815 |
{ |
816 |
where => \['date = DATE(?)', $date] |
817 |
} |
818 |
); |
819 |
|
820 |
if ($rs->count() != 0) { |
821 |
$isHoliday = ($rs->first()->is_opened() ? 0 : 1); |
822 |
} |
823 |
|
824 |
return $isHoliday; |
825 |
} |
826 |
|
827 |
=head2 copy_holiday |
828 |
|
829 |
$calendar->copy_holiday($from_startDate, $from_endDate, $to_startDate, $to_endDate, $daysnumber); |
830 |
|
831 |
Copies a holiday's parameters from a range to a new range |
832 |
C<$from_startDate> the source holiday's start date |
833 |
C<$from_endDate> the source holiday's end date |
834 |
C<$to_startDate> the destination holiday's start date |
835 |
C<$to_endDate> the destination holiday's end date |
836 |
C<$daysnumber> the number of days in the range. |
837 |
|
838 |
Both ranges should have the same number of days in them. |
839 |
|
840 |
=cut |
841 |
|
842 |
sub copy_holiday { |
843 |
my ($self, $from_startDate, $from_endDate, $to_startDate, $to_endDate, $daysnumber) = @_; |
844 |
my $branchcode = $self->{branchcode}; |
845 |
my $copyFromType = $from_startDate && $from_endDate eq '' ? 'oneDay': 'range'; |
846 |
my $schema = Koha::Database->new->schema; |
847 |
my $dtf = $schema->storage->datetime_parser; |
848 |
|
849 |
if ($copyFromType eq 'oneDay') { |
850 |
my $where; |
851 |
$to_startDate = $dtf->format_datetime($to_startDate); |
852 |
if ($to_startDate && $to_endDate) { |
853 |
$to_endDate = $dtf->format_datetime($to_endDate); |
854 |
$where = { date => { -between => [$to_startDate, $to_endDate]}}; |
855 |
} else { |
856 |
$where = { date => $to_startDate }; |
857 |
} |
858 |
|
859 |
$from_startDate = $dtf->format_datetime($from_startDate); |
860 |
my $fromDate = $schema->resultset('DiscreteCalendar')->search( |
861 |
{ |
862 |
branchcode => $branchcode, |
863 |
date => $from_startDate |
864 |
} |
865 |
); |
866 |
my $toDates = $schema->resultset('DiscreteCalendar')->search( |
867 |
{ |
868 |
branchcode => $branchcode, |
869 |
}, |
870 |
{ |
871 |
where => $where |
872 |
} |
873 |
); |
874 |
|
875 |
my $copyDate = $fromDate->next(); |
876 |
while (my $date = $toDates->next()) { |
877 |
$date->update({ |
878 |
is_opened => $copyDate->is_opened(), |
879 |
holiday_type => $copyDate->holiday_type(), |
880 |
note => $copyDate->note(), |
881 |
open_hour => $copyDate->open_hour(), |
882 |
close_hour => $copyDate->close_hour() |
883 |
}) |
884 |
} |
885 |
|
886 |
} else { |
887 |
my $endDate = dt_from_string($from_endDate); |
888 |
$to_startDate = $dtf->format_datetime($to_startDate); |
889 |
$to_endDate = $dtf->format_datetime($to_endDate); |
890 |
if ($daysnumber == 7) { |
891 |
for (my $tempDate = $from_startDate->clone(); $tempDate <= $endDate;$tempDate->add(days => 1)) { |
892 |
my $formatedDate = $dtf->format_datetime($tempDate); |
893 |
my $fromDate = $schema->resultset('DiscreteCalendar')->search( |
894 |
{ |
895 |
branchcode => $branchcode, |
896 |
date => $formatedDate, |
897 |
}, |
898 |
{ |
899 |
select => [{ DAYOFWEEK => 'date' }], |
900 |
as => [qw/ weekday /], |
901 |
columns =>[ qw/ holiday_type note open_hour close_hour note/] |
902 |
} |
903 |
); |
904 |
my $copyDate = $fromDate->next(); |
905 |
my $weekday = $copyDate->get_column('weekday'); |
906 |
|
907 |
my $toDate = $schema->resultset('DiscreteCalendar')->search( |
908 |
{ |
909 |
branchcode => $branchcode, |
910 |
|
911 |
}, |
912 |
{ |
913 |
where => {date => {-between => [$to_startDate, $to_endDate]}, "DAYOFWEEK(date)" => $weekday}, |
914 |
} |
915 |
); |
916 |
my $copyToDate = $toDate->next(); |
917 |
$copyToDate->update({ |
918 |
is_opened => $copyDate->is_opened(), |
919 |
holiday_type => $copyDate->holiday_type(), |
920 |
note => $copyDate->note(), |
921 |
open_hour => $copyDate->open_hour(), |
922 |
close_hour => $copyDate->close_hour() |
923 |
}); |
924 |
|
925 |
} |
926 |
} else { |
927 |
my $to_startDate = dt_from_string($to_startDate); |
928 |
my $to_endDate = dt_from_string($to_endDate); |
929 |
for (my $tempDate = $from_startDate->clone(); $tempDate <= $endDate;$tempDate->add(days => 1)) { |
930 |
my $from_formatedDate = $dtf->format_datetime($tempDate); |
931 |
my $fromDate = $schema->resultset('DiscreteCalendar')->search( |
932 |
{ |
933 |
branchcode => $branchcode, |
934 |
date => $from_formatedDate, |
935 |
}, |
936 |
{ |
937 |
order_by => { -asc => 'date' } |
938 |
} |
939 |
); |
940 |
my $to_formatedDate = $dtf->format_datetime($to_startDate); |
941 |
my $toDate = $schema->resultset('DiscreteCalendar')->search( |
942 |
{ |
943 |
branchcode => $branchcode, |
944 |
date => $to_formatedDate |
945 |
}, |
946 |
{ |
947 |
order_by => { -asc => 'date' } |
948 |
} |
949 |
); |
950 |
my $copyDate = $fromDate->next(); |
951 |
$toDate->next()->update({ |
952 |
is_opened => $copyDate->is_opened(), |
953 |
holiday_type => $copyDate->holiday_type(), |
954 |
note => $copyDate->note(), |
955 |
open_hour => $copyDate->open_hour(), |
956 |
close_hour => $copyDate->close_hour() |
957 |
}); |
958 |
$to_startDate->add(days =>1); |
959 |
} |
960 |
} |
961 |
|
962 |
|
963 |
} |
964 |
} |
965 |
|
966 |
=head2 days_between |
967 |
|
968 |
$cal->days_between( $start_date, $end_date ) |
969 |
|
970 |
Calculates the number of days the library is opened between C<$start_date> and C<$end_date> |
971 |
|
972 |
=cut |
973 |
|
974 |
sub days_between { |
975 |
my ($self, $start_date, $end_date, ) = @_; |
976 |
my $branchcode = $self->{branchcode}; |
977 |
|
978 |
if ( $start_date->compare($end_date) > 0 ) { |
979 |
# swap dates |
980 |
($start_date, $end_date) = ($end_date, $start_date); |
981 |
} |
982 |
|
983 |
my $schema = Koha::Database->new->schema; |
984 |
my $dtf = $schema->storage->datetime_parser; |
985 |
$start_date = $dtf->format_datetime($start_date->clone->truncate(to => 'day')); |
986 |
$end_date = $dtf->format_datetime($end_date->clone->truncate(to => 'day')); |
987 |
|
988 |
my $days_between = $schema->resultset('DiscreteCalendar')->search( |
989 |
{ |
990 |
branchcode => $branchcode, |
991 |
is_opened => 1, |
992 |
}, |
993 |
{ |
994 |
where => \['date >= date(?) AND date < date(?)', $start_date, $end_date] |
995 |
} |
996 |
); |
997 |
|
998 |
return DateTime::Duration->new( days => $days_between->count()); |
999 |
} |
1000 |
|
1001 |
=head2 next_open_day |
1002 |
|
1003 |
$open_date = $self->next_open_day($base_date); |
1004 |
|
1005 |
Returns a string representing the next day the library is open, starting from C<$base_date> |
1006 |
|
1007 |
=cut |
1008 |
|
1009 |
sub next_open_day { |
1010 |
my ( $self, $date ) = @_; |
1011 |
my $branchcode = $self->{branchcode}; |
1012 |
my $schema = Koha::Database->new->schema; |
1013 |
my $dtf = $schema->storage->datetime_parser; |
1014 |
my $formatted_date = $dtf->format_datetime($date); |
1015 |
|
1016 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
1017 |
{ |
1018 |
branchcode => $branchcode, |
1019 |
is_opened => 1, |
1020 |
}, |
1021 |
{ |
1022 |
where => \['date > date(?)', $formatted_date], |
1023 |
order_by => { -asc => 'date' }, |
1024 |
rows => 1 |
1025 |
} |
1026 |
); |
1027 |
|
1028 |
my $next = $rs->next(); |
1029 |
unless ( $next ) { |
1030 |
warn "No next opened date in calendar. Reduced to current date: $formatted_date."; |
1031 |
return $date; |
1032 |
} |
1033 |
return dt_from_string( $next->date(), 'iso'); |
1034 |
} |
1035 |
|
1036 |
=head2 prev_open_day |
1037 |
|
1038 |
$open_date = $self->prev_open_day($base_date); |
1039 |
|
1040 |
Returns a string representing the closest previous day the library was open, starting from C<$base_date> |
1041 |
|
1042 |
=cut |
1043 |
|
1044 |
sub prev_open_day { |
1045 |
my ( $self, $date ) = @_; |
1046 |
my $branchcode = $self->{branchcode}; |
1047 |
my $schema = Koha::Database->new->schema; |
1048 |
my $dtf = $schema->storage->datetime_parser; |
1049 |
my $formatted_date = $dtf->format_datetime($date); |
1050 |
|
1051 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
1052 |
{ |
1053 |
branchcode => $branchcode, |
1054 |
is_opened => 1, |
1055 |
}, |
1056 |
{ |
1057 |
where => \['date < date(?)', $formatted_date], |
1058 |
order_by => { -desc => 'date' }, |
1059 |
rows => 1 |
1060 |
} |
1061 |
); |
1062 |
|
1063 |
my $prev = $rs->next(); |
1064 |
unless ( $prev ) { |
1065 |
warn "No previous opened date in calendar. Reduced to current date: $formatted_date."; |
1066 |
return $date; |
1067 |
} |
1068 |
return dt_from_string( $prev->date(), 'iso'); |
1069 |
} |
1070 |
|
1071 |
=head2 days_forward |
1072 |
|
1073 |
$fwrd_date = $calendar->days_forward($start, $count) |
1074 |
|
1075 |
Returns the date C<$count> days in the future from C<$start>, ignoring days where the library is closed. |
1076 |
|
1077 |
=cut |
1078 |
|
1079 |
sub days_forward { |
1080 |
my $self = shift; |
1081 |
my $start_dt = shift; |
1082 |
my $num_days = shift; |
1083 |
|
1084 |
return $start_dt unless $num_days > 0; |
1085 |
|
1086 |
my $base_dt = $start_dt->clone(); |
1087 |
|
1088 |
while ($num_days--) { |
1089 |
$base_dt = $self->next_open_day($base_dt); |
1090 |
} |
1091 |
|
1092 |
return $base_dt; |
1093 |
} |
1094 |
|
1095 |
=head2 hours_between |
1096 |
|
1097 |
$hours = $calendar->hours_between($start_dt, $end_dt) |
1098 |
|
1099 |
Returns the number of hours between C<$start_dt> and C<$end_dt>. This is the imprecise |
1100 |
version, which simply calculates the number of day times 24. To take opening hours into account |
1101 |
see C<open_hours_between>/ |
1102 |
|
1103 |
=cut |
1104 |
|
1105 |
sub hours_between { |
1106 |
my ($self, $start_dt, $end_dt) = @_; |
1107 |
my $branchcode = $self->{branchcode}; |
1108 |
my $schema = Koha::Database->new->schema; |
1109 |
my $dtf = $schema->storage->datetime_parser; |
1110 |
my $start_date = $start_dt->clone(); |
1111 |
my $end_date = $end_dt->clone(); |
1112 |
my $duration = $end_date->delta_ms($start_date); |
1113 |
$start_date->truncate( to => 'day' ); |
1114 |
$end_date->truncate( to => 'day' ); |
1115 |
|
1116 |
# NB this is a kludge in that it assumes all days are 24 hours |
1117 |
# However for hourly loans the logic should be expanded to |
1118 |
# take into account open/close times then it would be a duration |
1119 |
# of library open hours |
1120 |
my $skipped_days = 0; |
1121 |
$start_date = $dtf->format_datetime($start_date); |
1122 |
$end_date = $dtf->format_datetime($end_date); |
1123 |
my $hours_between = $schema->resultset('DiscreteCalendar')->search( |
1124 |
{ |
1125 |
branchcode => $branchcode, |
1126 |
is_opened => 0 |
1127 |
}, |
1128 |
{ |
1129 |
where => {date => {-between => [$start_date, $end_date]}}, |
1130 |
} |
1131 |
); |
1132 |
|
1133 |
if ($skipped_days = $hours_between->count()) { |
1134 |
$duration->subtract_duration(DateTime::Duration->new( hours => 24 * $skipped_days)); |
1135 |
} |
1136 |
|
1137 |
return $duration; |
1138 |
} |
1139 |
|
1140 |
=head2 open_hours_between |
1141 |
|
1142 |
$hours = $calendar->open_hours_between($start_date, $end_date) |
1143 |
|
1144 |
Returns the number of hours between C<$start_date> and C<$end_date>, taking into |
1145 |
account the opening hours of the library. |
1146 |
|
1147 |
=cut |
1148 |
|
1149 |
sub open_hours_between { |
1150 |
my ($self, $start_date, $end_date) = @_; |
1151 |
my $branchcode = $self->{branchcode}; |
1152 |
my $schema = Koha::Database->new->schema; |
1153 |
my $dtf = DateTime::Format::Strptime->new(pattern => "%F %T"); |
1154 |
$start_date = $dtf->format_datetime($start_date); |
1155 |
$end_date = $dtf->format_datetime($end_date); |
1156 |
|
1157 |
my $working_hours_between = $schema->resultset('DiscreteCalendar')->search( |
1158 |
{ |
1159 |
branchcode => $branchcode, |
1160 |
is_opened => 1, |
1161 |
}, |
1162 |
{ |
1163 |
select => \['sum(time_to_sec(timediff(close_hour, open_hour)) / 3600)'], |
1164 |
as => [qw /hours_between/], |
1165 |
where => \['date BETWEEN DATE(?) AND DATE(?)', $start_date, $end_date] |
1166 |
} |
1167 |
); |
1168 |
|
1169 |
my $loan_day = $schema->resultset('DiscreteCalendar')->search( |
1170 |
{ |
1171 |
branchcode => $branchcode, |
1172 |
}, |
1173 |
{ |
1174 |
order_by => \[ 'ABS(DATEDIFF(date, ?))', $start_date ], |
1175 |
rows => 1, |
1176 |
} |
1177 |
); |
1178 |
|
1179 |
my $return_day = $schema->resultset('DiscreteCalendar')->search( |
1180 |
{ |
1181 |
branchcode => $branchcode, |
1182 |
}, |
1183 |
{ |
1184 |
order_by => \[ 'ABS(DATEDIFF(date, ?))', $end_date ], |
1185 |
rows => 1, |
1186 |
} |
1187 |
); |
1188 |
|
1189 |
#Capture the time portion of the date |
1190 |
$start_date =~ /\s(.*)/; |
1191 |
my $loan_date_time = $1; |
1192 |
$end_date =~ /\s(.*)/; |
1193 |
my $return_date_time = $1; |
1194 |
|
1195 |
my $not_used_hours = $schema->resultset('DiscreteCalendar')->search( |
1196 |
{ |
1197 |
branchcode => $branchcode, |
1198 |
is_opened => 1, |
1199 |
}, |
1200 |
{ |
1201 |
select => \[ '(time_to_sec(timediff(?, ?)) + time_to_sec(timediff(?, ?)) ) / 3600', $return_day->next()->close_hour(), $return_date_time, $loan_date_time, $loan_day->next()->open_hour()], |
1202 |
as => [qw /not_used_hours/], |
1203 |
} |
1204 |
); |
1205 |
|
1206 |
return ($working_hours_between->next()->get_column('hours_between') - $not_used_hours->next()->get_column('not_used_hours')); |
1207 |
} |
1208 |
|
1209 |
=head2 addDuration |
1210 |
|
1211 |
my $dt = $calendar->addDuration($date, $dur, $unit) |
1212 |
|
1213 |
C<$date> is a DateTime object representing the starting date of the interval. |
1214 |
C<$offset> is a duration to add to it (DateTime::Duration objects are supported as legacy) |
1215 |
C<$unit> is a string value 'days' or 'hours' toflag granularity of duration |
1216 |
|
1217 |
=cut |
1218 |
|
1219 |
sub addDuration { |
1220 |
my ( $self, $startdate, $add_duration, $unit ) = @_; |
1221 |
|
1222 |
# Default to days duration (legacy support I guess) |
1223 |
if ( ref $add_duration ne 'DateTime::Duration' ) { |
1224 |
$add_duration = DateTime::Duration->new( days => $add_duration ); |
1225 |
} |
1226 |
|
1227 |
$unit ||= 'days'; # default days ? |
1228 |
my $dt; |
1229 |
|
1230 |
if ( $unit eq 'hours' ) { |
1231 |
# Fixed for legacy support. Should be set as a branch parameter |
1232 |
my $return_by_hour = 10; |
1233 |
|
1234 |
$dt = $self->addHours($startdate, $add_duration, $return_by_hour); |
1235 |
} else { |
1236 |
# days |
1237 |
$dt = $self->addDays($startdate, $add_duration); |
1238 |
} |
1239 |
|
1240 |
return $dt; |
1241 |
} |
1242 |
|
1243 |
=head2 addHours |
1244 |
|
1245 |
$end = $calendar->addHours($start, $hours_duration, $return_by_hour) |
1246 |
|
1247 |
Add C<$hours_duration> to C<$start> date. |
1248 |
C<$return_by_hour> is an integer value representing the opening hour for the branch |
1249 |
|
1250 |
=cut |
1251 |
|
1252 |
sub addHours { |
1253 |
my ( $self, $startdate, $hours_duration, $return_by_hour ) = @_; |
1254 |
my $base_date = $startdate->clone(); |
1255 |
|
1256 |
$base_date->add_duration($hours_duration); |
1257 |
|
1258 |
# If we are using the calendar behave for now as if Datedue |
1259 |
# was the chosen option (current intended behaviour) |
1260 |
|
1261 |
if ( $self->{days_mode} ne 'Days' && |
1262 |
$self->is_holiday($base_date) ) { |
1263 |
|
1264 |
if ( $hours_duration->is_negative() ) { |
1265 |
$base_date = $self->prev_open_day($base_date); |
1266 |
} else { |
1267 |
$base_date = $self->next_open_day($base_date); |
1268 |
} |
1269 |
|
1270 |
$base_date->set_hour($return_by_hour); |
1271 |
|
1272 |
} |
1273 |
|
1274 |
return $base_date; |
1275 |
} |
1276 |
|
1277 |
=head2 addDays |
1278 |
|
1279 |
$date = $calendar->addDays($start, $duration) |
1280 |
|
1281 |
Add C<$days_duration> to C<$start> date. If the calendar's days_mode is set |
1282 |
to 'Calendar', it ignores closed days. Else if the calendar is set to 'Datedue' |
1283 |
it calculates the date normally, and then pushes to result to the next open day. |
1284 |
|
1285 |
=cut |
1286 |
|
1287 |
sub addDays { |
1288 |
my ( $self, $startdate, $days_duration ) = @_; |
1289 |
my $base_date = $startdate->clone(); |
1290 |
|
1291 |
$self->{days_mode} ||= q{}; |
1292 |
|
1293 |
if ( $self->{days_mode} eq 'Calendar' ) { |
1294 |
# use the calendar to skip all days the library is closed |
1295 |
# when adding |
1296 |
my $days = abs $days_duration->in_units('days'); |
1297 |
|
1298 |
if ( $days_duration->is_negative() ) { |
1299 |
while ($days) { |
1300 |
$base_date = $self->prev_open_day($base_date); |
1301 |
--$days; |
1302 |
} |
1303 |
} else { |
1304 |
while ($days) { |
1305 |
$base_date = $self->next_open_day($base_date); |
1306 |
--$days; |
1307 |
} |
1308 |
} |
1309 |
|
1310 |
} else { # Days or Datedue |
1311 |
# use straight days, then use calendar to push |
1312 |
# the date to the next open day if Datedue |
1313 |
$base_date->add_duration($days_duration); |
1314 |
|
1315 |
if ( $self->{days_mode} eq 'Datedue' ) { |
1316 |
# Datedue, then use the calendar to push |
1317 |
# the date to the next open day if holiday |
1318 |
if (!$self->is_opened($base_date) ) { |
1319 |
|
1320 |
if ( $days_duration->is_negative() ) { |
1321 |
$base_date = $self->prev_open_day($base_date); |
1322 |
} else { |
1323 |
$base_date = $self->next_open_day($base_date); |
1324 |
} |
1325 |
} |
1326 |
} |
1327 |
} |
1328 |
|
1329 |
return $base_date; |
1330 |
} |
1331 |
|
1332 |
1; |