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