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