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