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 |
sub new { |
33 |
my ( $classname, %options ) = @_; |
34 |
my $self = {}; |
35 |
bless $self, $classname; |
36 |
for my $o_name ( keys %options ) { |
37 |
my $o = lc $o_name; |
38 |
$self->{$o} = $options{$o_name}; |
39 |
} |
40 |
if ( !defined $self->{branchcode} ) { |
41 |
croak 'No branchcode argument passed to Koha::DiscreteCalendar->new'; |
42 |
} |
43 |
$self->_init(); |
44 |
|
45 |
return $self; |
46 |
} |
47 |
|
48 |
sub _init { |
49 |
my $self = shift; |
50 |
$self->{days_mode} = C4::Context->preference('useDaysMode'); |
51 |
#If the branchcode doesn't exist we use the default calendar. |
52 |
my $schema = Koha::Database->new->schema; |
53 |
my $branchcode = $self->{branchcode}; |
54 |
my $dtf = $schema->storage->datetime_parser; |
55 |
my $today = $dtf->format_datetime(DateTime->today); |
56 |
my $rs = $schema->resultset('DiscreteCalendar')->single( |
57 |
{ |
58 |
branchcode => $branchcode, |
59 |
date => $today |
60 |
} |
61 |
); |
62 |
#use default if no calendar is found |
63 |
if (!$rs){ |
64 |
$self->{branchcode} = 'DFLT'; |
65 |
} |
66 |
|
67 |
} |
68 |
|
69 |
sub getDatesInfo { |
70 |
my $self = shift; |
71 |
my $branchcode = $self->{branchcode}; |
72 |
my @datesInfos =(); |
73 |
my $schema = Koha::Database->new->schema; |
74 |
|
75 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
76 |
{ |
77 |
branchcode => $branchcode |
78 |
}, |
79 |
{ |
80 |
select => [ 'date', { DATE => 'date' } ], |
81 |
as => [qw/ date date /], |
82 |
columns =>[ qw/ holidaytype openhour closehour note/] |
83 |
}, |
84 |
); |
85 |
|
86 |
while (my $date = $rs->next()){ |
87 |
my $outputdate = dt_from_string( $date->date(), 'iso'); |
88 |
$outputdate = output_pref( { dt => $outputdate, dateonly => 1 } ); |
89 |
push @datesInfos, { |
90 |
date => $date->date(), |
91 |
outputdate => $outputdate, |
92 |
holidaytype => $date->holidaytype() , |
93 |
openhour => $date->openhour(), |
94 |
closehour => $date->closehour(), |
95 |
note => $date->note() |
96 |
}; |
97 |
} |
98 |
|
99 |
return @datesInfos; |
100 |
} |
101 |
#This methode will copy everything from a given branch found to the new branch |
102 |
sub add_new_branch { |
103 |
my ($self, $copyBranch, $newBranch) = @_; |
104 |
$copyBranch = 'DFLT' unless $copyBranch; |
105 |
my $schema = Koha::Database->new->schema; |
106 |
|
107 |
my $branch_rs = $schema->resultset('DiscreteCalendar')->search({ |
108 |
branchcode => $copyBranch |
109 |
}); |
110 |
|
111 |
while(my $row = $branch_rs->next()){ |
112 |
$schema->resultset('DiscreteCalendar')->create({ |
113 |
date => $row->date(), |
114 |
branchcode => $newBranch, |
115 |
isopened => $row->isopened(), |
116 |
holidaytype => $row->holidaytype(), |
117 |
openhour => $row->openhour(), |
118 |
closehour => $row->closehour(), |
119 |
}); |
120 |
} |
121 |
|
122 |
} |
123 |
#DiscreteCalendar data transfer object (DTO) |
124 |
sub get_date_info { |
125 |
my ($self, $date) = @_; |
126 |
my $branchcode = $self->{branchcode}; |
127 |
my $schema = Koha::Database->new->schema; |
128 |
my $dtf = $schema->storage->datetime_parser; |
129 |
#String dates for Database usage |
130 |
my $date_string = $dtf->format_datetime($date); |
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 |
where => \['DATE(?) = date', $date_string ], |
140 |
columns =>[ qw/ branchcode holidaytype openhour closehour note/] |
141 |
}, |
142 |
); |
143 |
my $dateDTO; |
144 |
while (my $date = $rs->next()){ |
145 |
$dateDTO = { |
146 |
date => $date->date(), |
147 |
branchcode => $date->branchcode(), |
148 |
holidaytype => $date->holidaytype() , |
149 |
openhour => $date->openhour(), |
150 |
closehour => $date->closehour(), |
151 |
note => $date->note() |
152 |
}; |
153 |
} |
154 |
|
155 |
return $dateDTO; |
156 |
} |
157 |
|
158 |
|
159 |
sub getMaxDate { |
160 |
my $self = shift; |
161 |
my $branchcode = $self->{branchcode}; |
162 |
my $schema = Koha::Database->new->schema; |
163 |
|
164 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
165 |
{ |
166 |
branchcode => $branchcode |
167 |
}, |
168 |
{ |
169 |
select => [{ MAX => 'date' } ], |
170 |
as => [qw/ max /], |
171 |
} |
172 |
); |
173 |
|
174 |
return $rs->next()->get_column('max'); |
175 |
} |
176 |
|
177 |
sub getMinDate { |
178 |
my $self = shift; |
179 |
my $branchcode = $self->{branchcode}; |
180 |
my $schema = Koha::Database->new->schema; |
181 |
|
182 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
183 |
{ |
184 |
branchcode => $branchcode |
185 |
}, |
186 |
{ |
187 |
select => [{ MIN => 'date' } ], |
188 |
as => [qw/ min /], |
189 |
} |
190 |
); |
191 |
|
192 |
return $rs->next()->get_column('min'); |
193 |
} |
194 |
|
195 |
sub get_single_holidays { |
196 |
my $self = shift; |
197 |
my $branchcode = $self->{branchcode}; |
198 |
my @single_holidays; |
199 |
my $schema = Koha::Database->new->schema; |
200 |
|
201 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
202 |
{ |
203 |
branchcode => $branchcode, |
204 |
holidaytype => 'E' |
205 |
}, |
206 |
{ |
207 |
select => [{ DATE => 'date' }, 'note' ], |
208 |
as => [qw/ date note/], |
209 |
} |
210 |
); |
211 |
while (my $date = $rs->next()){ |
212 |
my $outputdate = dt_from_string($date->date(), 'iso'); |
213 |
$outputdate = output_pref( { dt => $outputdate, dateonly => 1 } ); |
214 |
push @single_holidays, { |
215 |
date => $date->date(), |
216 |
outputdate => $outputdate, |
217 |
note => $date->note() |
218 |
} |
219 |
} |
220 |
|
221 |
return @single_holidays; |
222 |
} |
223 |
|
224 |
sub get_float_holidays { |
225 |
my $self = shift; |
226 |
my $branchcode = $self->{branchcode}; |
227 |
my @float_holidays; |
228 |
my $schema = Koha::Database->new->schema; |
229 |
|
230 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
231 |
{ |
232 |
branchcode => $branchcode, |
233 |
holidaytype => 'F' |
234 |
}, |
235 |
{ |
236 |
select => [{ DATE => 'date' }, 'note' ], |
237 |
as => [qw/ date note/], |
238 |
} |
239 |
); |
240 |
while (my $date = $rs->next()){ |
241 |
my $outputdate = dt_from_string($date->date(), 'iso'); |
242 |
$outputdate = output_pref( { dt => $outputdate, dateonly => 1 } ); |
243 |
push @float_holidays, { |
244 |
date => $date->date(), |
245 |
outputdate => $outputdate, |
246 |
note => $date->note() |
247 |
} |
248 |
} |
249 |
|
250 |
return @float_holidays; |
251 |
} |
252 |
|
253 |
sub get_need_valdation_holidays { |
254 |
my $self = shift; |
255 |
my $branchcode = $self->{branchcode}; |
256 |
my @need_validation_holidays; |
257 |
my $schema = Koha::Database->new->schema; |
258 |
|
259 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
260 |
{ |
261 |
branchcode => $branchcode, |
262 |
holidaytype => 'N' |
263 |
}, |
264 |
{ |
265 |
select => [{ DATE => 'date' }, 'note' ], |
266 |
as => [qw/ date note/], |
267 |
} |
268 |
); |
269 |
while (my $date = $rs->next()){ |
270 |
my $outputdate = dt_from_string($date->date(), 'iso'); |
271 |
$outputdate = output_pref( { dt => $outputdate, dateonly => 1 } ); |
272 |
push @need_validation_holidays, { |
273 |
date => $date->date(), |
274 |
outputdate => $outputdate, |
275 |
note => $date->note() |
276 |
} |
277 |
} |
278 |
|
279 |
return @need_validation_holidays; |
280 |
} |
281 |
|
282 |
sub get_day_month_holidays { |
283 |
my $self = shift; |
284 |
my $branchcode = $self->{branchcode}; |
285 |
my @repeatable_holidays; |
286 |
my $schema = Koha::Database->new->schema; |
287 |
|
288 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
289 |
{ |
290 |
branchcode => $branchcode, |
291 |
holidaytype => 'R', |
292 |
|
293 |
}, |
294 |
{ |
295 |
select => \[ 'distinct DAY(date), MONTH(date), note'], |
296 |
as => [qw/ day month note/], |
297 |
} |
298 |
); |
299 |
|
300 |
while (my $date = $rs->next()){ |
301 |
push @repeatable_holidays, { |
302 |
day=> $date->get_column('day'), |
303 |
month => $date->get_column('month'), |
304 |
note => $date->note() |
305 |
}; |
306 |
} |
307 |
|
308 |
return @repeatable_holidays; |
309 |
} |
310 |
|
311 |
sub get_week_days_holidays { |
312 |
my $self = shift; |
313 |
my $branchcode = $self->{branchcode}; |
314 |
my @week_days; |
315 |
my $schema = Koha::Database->new->schema; |
316 |
|
317 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
318 |
{ |
319 |
holidaytype => 'W', |
320 |
branchcode => $branchcode, |
321 |
}, |
322 |
{ |
323 |
select => [{ DAYOFWEEK => 'date'}, 'note'], |
324 |
as => [qw/ weekday note /], |
325 |
distinct => 1, |
326 |
} |
327 |
); |
328 |
|
329 |
while (my $date = $rs->next()){ |
330 |
push @week_days, { |
331 |
weekday => ($date->get_column('weekday') -1), |
332 |
note => $date->note() |
333 |
}; |
334 |
} |
335 |
|
336 |
return @week_days; |
337 |
} |
338 |
=head1 edit_holiday |
339 |
|
340 |
Modifies a date or a range of dates |
341 |
|
342 |
C<$title> Is the title to be modified for the holiday formed by $year/$month/$day. |
343 |
|
344 |
C<$weekday> Is the day of week for the holiday |
345 |
|
346 |
C<$holidaytype> Is the type of the holiday : |
347 |
E : Exception holiday, single day. |
348 |
F : Floating holiday, different day each year. |
349 |
N : Needs validation, copied float holiday from the past |
350 |
R : Fixed holiday, repeated on same date. |
351 |
W : Weekly holiday, same day of the week. |
352 |
|
353 |
C<$openHour> Is the opening hour. |
354 |
C<$closeHour> Is the closing hour. |
355 |
C<$startDate> Is the start of the range of dates. |
356 |
C<$endDate> Is the end of the range of dates. |
357 |
=back |
358 |
=cut |
359 |
|
360 |
sub edit_holiday { |
361 |
my ($self, $title, $weekday, $holidaytype, $openHour, $closeHour, $startDate, $endDate, $deleteType) = @_; |
362 |
my $branchcode = $self->{branchcode}; |
363 |
my $today = DateTime->today; |
364 |
my $schema = Koha::Database->new->schema; |
365 |
my $dtf = $schema->storage->datetime_parser; |
366 |
#String dates for Database usage |
367 |
my $startDate_String = $dtf->format_datetime($startDate); |
368 |
my $endDate_String = $dtf->format_datetime($endDate); |
369 |
$today = $dtf->format_datetime($today); |
370 |
|
371 |
my %updateValues = ( |
372 |
isopened => 0, |
373 |
note => $title, |
374 |
holidaytype => $holidaytype, |
375 |
); |
376 |
$updateValues{openhour} = $openHour if $openHour ne ''; |
377 |
$updateValues{closehour} = $closeHour if $closeHour ne ''; |
378 |
|
379 |
if($holidaytype eq 'W') { |
380 |
#Insert/Update weekly holidays |
381 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
382 |
{ |
383 |
branchcode => $branchcode, |
384 |
}, |
385 |
{ |
386 |
where => \[ 'DAYOFWEEK(date) = ? and date >= ?', $weekday, $today], |
387 |
} |
388 |
); |
389 |
|
390 |
while (my $date = $rs->next()){ |
391 |
$date->update(\%updateValues); |
392 |
} |
393 |
}elsif ($holidaytype eq 'E' || $holidaytype eq 'F' || $holidaytype eq 'N') { |
394 |
#Insert/Update Exception Float and Needs Validation holidays |
395 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
396 |
{ |
397 |
branchcode => $branchcode, |
398 |
}, |
399 |
{ |
400 |
where => \['date between DATE(?) and DATE(?) and date >= ?',$startDate_String, $endDate_String, $today] |
401 |
} |
402 |
); |
403 |
while (my $date = $rs->next()){ |
404 |
$date->update(\%updateValues); |
405 |
} |
406 |
|
407 |
}elsif ($holidaytype eq 'R') { |
408 |
#Insert/Update repeatable holidays |
409 |
my $parser = DateTime::Format::Strptime->new( |
410 |
pattern => '%m-%d', |
411 |
on_error => 'croak', |
412 |
); |
413 |
#Format the dates to have only month-day ex: 01-04 for January 4th |
414 |
$startDate = $parser->format_datetime($startDate); |
415 |
$endDate = $parser->format_datetime($endDate); |
416 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
417 |
{ |
418 |
branchcode => $branchcode, |
419 |
}, |
420 |
{ |
421 |
where => \["(DATE_FORMAT(date,'\%m-\%d') BETWEEN ? AND ? ) AND date >= ?", $startDate, $endDate, $today], |
422 |
} |
423 |
); |
424 |
while (my $date = $rs->next()){ |
425 |
$date->update(\%updateValues); |
426 |
} |
427 |
|
428 |
}else { |
429 |
#Delete/Update date(s) |
430 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
431 |
{ |
432 |
branchcode => $branchcode, |
433 |
}, |
434 |
{ |
435 |
where => \['date between DATE(?) and DATE(?) and date >= ?',$startDate_String, $endDate_String, $today], |
436 |
} |
437 |
); |
438 |
#If none, the date(s) will be normal days, else, |
439 |
if($holidaytype eq 'none'){ |
440 |
$updateValues{holidaytype} =''; |
441 |
$updateValues{isopened} =1; |
442 |
}else{ |
443 |
delete $updateValues{holidaytype}; |
444 |
} |
445 |
while (my $date = $rs->next()){ |
446 |
if($deleteType){ |
447 |
if($date->holidaytype() eq 'W' && $startDate_String eq $endDate_String){ |
448 |
$self->remove_weekly_holidays($weekday, \%updateValues, $today); |
449 |
}elsif($date->holidaytype() eq 'R'){ |
450 |
$self->remove_fixed_holidays($startDate, $endDate, \%updateValues, $today); |
451 |
} |
452 |
}else{ |
453 |
$date->update(\%updateValues); |
454 |
} |
455 |
} |
456 |
} |
457 |
} |
458 |
|
459 |
sub remove_weekly_holidays { |
460 |
my ($self, $weekday, $updateValues, $today) = @_; |
461 |
my $branchcode = $self->{branchcode}; |
462 |
my $schema = Koha::Database->new->schema; |
463 |
|
464 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
465 |
{ |
466 |
branchcode => $branchcode, |
467 |
isopened => 0, |
468 |
holidaytype => 'W' |
469 |
}, |
470 |
{ |
471 |
where => \["DAYOFWEEK(date) = ? and date >= ?", $weekday,$today], |
472 |
} |
473 |
); |
474 |
|
475 |
while (my $date = $rs->next()){ |
476 |
$date->update($updateValues); |
477 |
} |
478 |
} |
479 |
|
480 |
sub remove_fixed_holidays { |
481 |
my ($self, $startDate, $endDate, $updateValues, $today) = @_; |
482 |
my $branchcode = $self->{branchcode}; |
483 |
my $schema = Koha::Database->new->schema; |
484 |
my $parser = DateTime::Format::Strptime->new( |
485 |
pattern => '%m-%d', |
486 |
on_error => 'croak', |
487 |
); |
488 |
#Format the dates to have only month-day ex: 01-04 for January 4th |
489 |
$startDate = $parser->format_datetime($startDate); |
490 |
$endDate = $parser->format_datetime($endDate); |
491 |
|
492 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
493 |
{ |
494 |
branchcode => $branchcode, |
495 |
isopened => 0, |
496 |
holidaytype => 'R', |
497 |
}, |
498 |
{ |
499 |
where => \["(DATE_FORMAT(date,'\%m-\%d') BETWEEN ? AND ? ) AND date >= ?", $startDate, $endDate, $today], |
500 |
} |
501 |
); |
502 |
|
503 |
while (my $date = $rs->next()){ |
504 |
$date->update($updateValues); |
505 |
} |
506 |
} |
507 |
|
508 |
sub copyToBranch { |
509 |
my ($self,$newBranch) =@_; |
510 |
my $branchcode = $self->{branchcode}; |
511 |
my $schema = Koha::Database->new->schema; |
512 |
|
513 |
my $copyFrom = $schema->resultset('DiscreteCalendar')->search( |
514 |
{ |
515 |
branchcode => $branchcode |
516 |
}, |
517 |
{ |
518 |
columns => [ qw/ date isopened note holidaytype openhour closehour /] |
519 |
} |
520 |
); |
521 |
while (my $copyDate = $copyFrom->next()){ |
522 |
my $copyTo = $schema->resultset('DiscreteCalendar')->search( |
523 |
{ |
524 |
branchcode => $newBranch, |
525 |
date => $copyDate->date(), |
526 |
}, |
527 |
{ |
528 |
columns => [ qw/ date branchcode isopened note holidaytype openhour closehour /] |
529 |
} |
530 |
); |
531 |
$copyTo->next()->update({ |
532 |
isopened => $copyDate->isopened(), |
533 |
holidaytype => $copyDate->holidaytype(), |
534 |
note => $copyDate->note(), |
535 |
openhour => $copyDate->openhour(), |
536 |
closehour => $copyDate->closehour() |
537 |
}); |
538 |
} |
539 |
} |
540 |
|
541 |
sub isOpened { |
542 |
my($self, $date) = @_; |
543 |
my $branchcode = $self->{branchcode}; |
544 |
my $schema = Koha::Database->new->schema; |
545 |
my $dtf = $schema->storage->datetime_parser; |
546 |
$date= $dtf->format_datetime($date); |
547 |
#if the date is not found |
548 |
my $isOpened = -1; |
549 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
550 |
{ |
551 |
branchcode => $branchcode, |
552 |
}, |
553 |
{ |
554 |
where => \['date = DATE(?)', $date] |
555 |
} |
556 |
); |
557 |
$isOpened = $rs->next()->isopened() if $rs->count() != 0; |
558 |
|
559 |
return $isOpened; |
560 |
} |
561 |
|
562 |
sub is_holiday { |
563 |
my($self, $date) = @_; |
564 |
my $branchcode = $self->{branchcode}; |
565 |
my $schema = Koha::Database->new->schema; |
566 |
my $dtf = $schema->storage->datetime_parser; |
567 |
$date= $dtf->format_datetime($date); |
568 |
#if the date is not found |
569 |
my $isHoliday = -1; |
570 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
571 |
{ |
572 |
branchcode => $branchcode, |
573 |
}, |
574 |
{ |
575 |
where => \['date = DATE(?)', $date] |
576 |
} |
577 |
); |
578 |
|
579 |
if($rs->count() != 0){ |
580 |
$isHoliday = 0 if $rs->first()->isopened(); |
581 |
$isHoliday = 1 if !$rs->first()->isopened(); |
582 |
} |
583 |
|
584 |
return $isHoliday; |
585 |
} |
586 |
|
587 |
sub copyHoliday { |
588 |
my ($self, $from_startDate, $from_endDate, $to_startDate, $to_endDate, $daysnumber) = @_; |
589 |
my $branchcode = $self->{branchcode}; |
590 |
my $copyFromType = $from_startDate && $from_endDate eq '' ? 'oneDay': 'range'; |
591 |
my $schema = Koha::Database->new->schema; |
592 |
my $dtf = $schema->storage->datetime_parser; |
593 |
my $parser = DateTime::Format::Strptime->new( |
594 |
pattern => '%Y-%m-%d', |
595 |
on_error => 'croak', |
596 |
); |
597 |
|
598 |
if ($copyFromType eq 'oneDay'){ |
599 |
my $where; |
600 |
$to_startDate = $dtf->format_datetime($to_startDate); |
601 |
if ($to_startDate && $to_endDate) { |
602 |
$to_endDate = $dtf->format_datetime($to_endDate); |
603 |
$where = \["date between ? and ?", $to_startDate, $to_endDate]; |
604 |
} else { |
605 |
$where = \['date = ?', $to_startDate]; |
606 |
} |
607 |
|
608 |
$from_startDate = $dtf->format_datetime($from_startDate); |
609 |
my $fromDate = $schema->resultset('DiscreteCalendar')->search( |
610 |
{ |
611 |
branchcode => $branchcode, |
612 |
date => $from_startDate |
613 |
} |
614 |
); |
615 |
my $toDates = $schema->resultset('DiscreteCalendar')->search( |
616 |
{ |
617 |
branchcode => $branchcode, |
618 |
}, |
619 |
{ |
620 |
where => $where |
621 |
} |
622 |
); |
623 |
|
624 |
my $copyDate = $fromDate->next(); |
625 |
while (my $date = $toDates->next()){ |
626 |
$date->update({ |
627 |
isopened => $copyDate->isopened(), |
628 |
holidaytype => $copyDate->holidaytype(), |
629 |
note => $copyDate->note(), |
630 |
openhour => $copyDate->openhour(), |
631 |
closehour => $copyDate->closehour() |
632 |
}) |
633 |
} |
634 |
|
635 |
}else{ |
636 |
my $endDate = $parser->parse_datetime($from_endDate); |
637 |
$to_startDate = $dtf->format_datetime($to_startDate); |
638 |
$to_endDate = $dtf->format_datetime($to_endDate); |
639 |
if($daysnumber ==6){ |
640 |
for (my $tempDate = $from_startDate->clone(); $tempDate <= $endDate;$tempDate->add(days => 1)){ |
641 |
my $formatedDate = $dtf->format_datetime($tempDate); |
642 |
my $fromDate = $schema->resultset('DiscreteCalendar')->search( |
643 |
{ |
644 |
branchcode => $branchcode, |
645 |
date => $formatedDate, |
646 |
}, |
647 |
{ |
648 |
select => [{ DAYOFWEEK => 'date' }], |
649 |
as => [qw/ weekday /], |
650 |
columns =>[ qw/ holidaytype note openhour closehour note/] |
651 |
} |
652 |
); |
653 |
my $copyDate = $fromDate->next(); |
654 |
my $weekday = $copyDate->get_column('weekday'); |
655 |
|
656 |
my $toDate = $schema->resultset('DiscreteCalendar')->search( |
657 |
{ |
658 |
branchcode => $branchcode, |
659 |
|
660 |
}, |
661 |
{ |
662 |
where => \['date between ? and ? and DAYOFWEEK(date) = ?',$to_startDate, $to_endDate, $weekday] |
663 |
} |
664 |
); |
665 |
my $copyToDate = $toDate->next(); |
666 |
$copyToDate->update({ |
667 |
isopened => $copyDate->isopened(), |
668 |
holidaytype => $copyDate->holidaytype(), |
669 |
note => $copyDate->note(), |
670 |
openhour => $copyDate->openhour(), |
671 |
closehour => $copyDate->closehour() |
672 |
}); |
673 |
|
674 |
} |
675 |
}else{ |
676 |
my $to_startDate = $parser->parse_datetime($to_startDate); |
677 |
my $to_endDate = $parser->parse_datetime($to_endDate); |
678 |
for (my $tempDate = $from_startDate->clone(); $tempDate <= $endDate;$tempDate->add(days => 1)){ |
679 |
my $from_formatedDate = $dtf->format_datetime($tempDate); |
680 |
my $fromDate = $schema->resultset('DiscreteCalendar')->search( |
681 |
{ |
682 |
branchcode => $branchcode, |
683 |
date => $from_formatedDate, |
684 |
}, |
685 |
{ |
686 |
order_by => { -asc => 'date' } |
687 |
} |
688 |
); |
689 |
my $to_formatedDate = $dtf->format_datetime($to_startDate); |
690 |
my $toDate = $schema->resultset('DiscreteCalendar')->search( |
691 |
{ |
692 |
branchcode => $branchcode, |
693 |
date => $to_formatedDate |
694 |
}, |
695 |
{ |
696 |
order_by => { -asc => 'date' } |
697 |
} |
698 |
); |
699 |
my $copyDate = $fromDate->next(); |
700 |
$toDate->next()->update({ |
701 |
isopened => $copyDate->isopened(), |
702 |
holidaytype => $copyDate->holidaytype(), |
703 |
note => $copyDate->note(), |
704 |
openhour => $copyDate->openhour(), |
705 |
closehour => $copyDate->closehour() |
706 |
}); |
707 |
$to_startDate->add(days =>1); |
708 |
} |
709 |
} |
710 |
|
711 |
|
712 |
} |
713 |
} |
714 |
|
715 |
sub days_between { |
716 |
my ($self, $start_date, $end_date, ) = @_; |
717 |
my $branchcode = $self->{branchcode}; |
718 |
|
719 |
if ( $start_date->compare($end_date) > 0 ) { |
720 |
# swap dates |
721 |
my $int_dt = $end_date; |
722 |
$end_date = $start_date; |
723 |
$start_date = $int_dt; |
724 |
} |
725 |
|
726 |
my $schema = Koha::Database->new->schema; |
727 |
my $dtf = $schema->storage->datetime_parser; |
728 |
$start_date = $dtf->format_datetime($start_date); |
729 |
$end_date = $dtf->format_datetime($end_date); |
730 |
|
731 |
my $days_between = $schema->resultset('DiscreteCalendar')->search( |
732 |
{ |
733 |
branchcode => $branchcode, |
734 |
isopened => 1, |
735 |
}, |
736 |
{ |
737 |
where => \['date >= date(?) and date < date(?)',$start_date, $end_date] |
738 |
} |
739 |
); |
740 |
|
741 |
return DateTime::Duration->new( days => $days_between->count()); |
742 |
} |
743 |
|
744 |
sub next_open_day { |
745 |
my ( $self, $date ) = @_; |
746 |
my $branchcode = $self->{branchcode}; |
747 |
my $schema = Koha::Database->new->schema; |
748 |
my $dtf = $schema->storage->datetime_parser; |
749 |
$date = $dtf->format_datetime($date); |
750 |
|
751 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
752 |
{ |
753 |
branchcode => $branchcode, |
754 |
isopened => 1, |
755 |
}, |
756 |
{ |
757 |
where => \['date > date(?)', $date], |
758 |
order_by => { -asc => 'date' }, |
759 |
rows => 1 |
760 |
} |
761 |
); |
762 |
return dt_from_string( $rs->next()->date(), 'iso'); |
763 |
} |
764 |
|
765 |
sub prev_open_day { |
766 |
my ( $self, $date ) = @_; |
767 |
my $branchcode = $self->{branchcode}; |
768 |
my $schema = Koha::Database->new->schema; |
769 |
my $dtf = $schema->storage->datetime_parser; |
770 |
$date = $dtf->format_datetime($date); |
771 |
|
772 |
my $rs = $schema->resultset('DiscreteCalendar')->search( |
773 |
{ |
774 |
branchcode => $branchcode, |
775 |
isopened => 1, |
776 |
}, |
777 |
{ |
778 |
where => \['date < date(?)', $date], |
779 |
order_by => { -desc => 'date' }, |
780 |
rows => 1 |
781 |
} |
782 |
); |
783 |
return dt_from_string( $rs->next()->date(), 'iso'); |
784 |
} |
785 |
|
786 |
sub hours_between { |
787 |
my ($self, $start_dt, $end_dt) = @_; |
788 |
my $branchcode = $self->{branchcode}; |
789 |
my $schema = Koha::Database->new->schema; |
790 |
my $dtf = $schema->storage->datetime_parser; |
791 |
my $start_date = $start_dt->clone(); |
792 |
my $end_date = $end_dt->clone(); |
793 |
my $duration = $end_date->delta_ms($start_date); |
794 |
$start_date->truncate( to => 'day' ); |
795 |
$end_date->truncate( to => 'day' ); |
796 |
|
797 |
# NB this is a kludge in that it assumes all days are 24 hours |
798 |
# However for hourly loans the logic should be expanded to |
799 |
# take into account open/close times then it would be a duration |
800 |
# of library open hours |
801 |
my $skipped_days = 0; |
802 |
$start_date = $dtf->format_datetime($start_date); |
803 |
$end_date = $dtf->format_datetime($end_date); |
804 |
my $hours_between = $schema->resultset('DiscreteCalendar')->search( |
805 |
{ |
806 |
branchcode => $branchcode, |
807 |
isopened => 0 |
808 |
}, |
809 |
{ |
810 |
where => \[ 'date between ? and ?', $start_date, $end_date], |
811 |
} |
812 |
); |
813 |
|
814 |
if ($skipped_days = $hours_between->count()) { |
815 |
$duration->subtract_duration(DateTime::Duration->new( hours => 24 * $skipped_days)); |
816 |
} |
817 |
|
818 |
return $duration; |
819 |
} |
820 |
|
821 |
sub open_hours_between { |
822 |
my ($self, $start_date, $end_date) = @_; |
823 |
my $branchcode = $self->{branchcode}; |
824 |
my $schema = Koha::Database->new->schema; |
825 |
my $dtf = $schema->storage->datetime_parser; |
826 |
$start_date = $dtf->format_datetime($start_date); |
827 |
$end_date = $dtf->format_datetime($end_date); |
828 |
|
829 |
my $working_hours_between = $schema->resultset('DiscreteCalendar')->search( |
830 |
{ |
831 |
branchcode => $branchcode, |
832 |
isopened => 1, |
833 |
}, |
834 |
{ |
835 |
select => \['sum(time_to_sec(timediff(closehour, openhour)) / 3600)'], |
836 |
as => [qw /hours_between/], |
837 |
where => \['date BETWEEN DATE(?) AND DATE(?)', $start_date, $end_date] |
838 |
} |
839 |
); |
840 |
|
841 |
my $loan_day = $schema->resultset('DiscreteCalendar')->search( |
842 |
{ |
843 |
branchcode => $branchcode, |
844 |
}, |
845 |
{ |
846 |
where => \['date = DATE(?)', $start_date], |
847 |
} |
848 |
); |
849 |
|
850 |
my $return_day = $schema->resultset('DiscreteCalendar')->search( |
851 |
{ |
852 |
branchcode => $branchcode, |
853 |
}, |
854 |
{ |
855 |
where => \['date = DATE(?)', $end_date], |
856 |
} |
857 |
); |
858 |
|
859 |
#Capture the time portion of the date |
860 |
$start_date =~ /\s(.*)/; |
861 |
my $loan_date_time = $1; |
862 |
$end_date =~ /\s(.*)/; |
863 |
my $return_date_time = $1; |
864 |
|
865 |
my $not_used_hours = $schema->resultset('DiscreteCalendar')->search( |
866 |
{ |
867 |
branchcode => $branchcode, |
868 |
isopened => 1, |
869 |
}, |
870 |
{ |
871 |
select => \[ '(time_to_sec(timediff(?, ?)) + time_to_sec(timediff(?, ?)) ) / 3600', $return_day->next()->closehour(), $return_date_time, $loan_date_time, $loan_day->next()->openhour()], |
872 |
as => [qw /not_used_hours/], |
873 |
} |
874 |
); |
875 |
|
876 |
return ($working_hours_between->next()->get_column('hours_between') - $not_used_hours->next()->get_column('not_used_hours')); |
877 |
} |
878 |
sub addDate { |
879 |
my ( $self, $startdate, $add_duration, $unit ) = @_; |
880 |
|
881 |
# Default to days duration (legacy support I guess) |
882 |
if ( ref $add_duration ne 'DateTime::Duration' ) { |
883 |
$add_duration = DateTime::Duration->new( days => $add_duration ); |
884 |
} |
885 |
|
886 |
$unit ||= 'days'; # default days ? |
887 |
my $dt; |
888 |
|
889 |
if ( $unit eq 'hours' ) { |
890 |
# Fixed for legacy support. Should be set as a branch parameter |
891 |
my $return_by_hour = 10; |
892 |
|
893 |
$dt = $self->addHours($startdate, $add_duration, $return_by_hour); |
894 |
} else { |
895 |
# days |
896 |
$dt = $self->addDays($startdate, $add_duration); |
897 |
} |
898 |
|
899 |
return $dt; |
900 |
} |
901 |
|
902 |
sub addHours { |
903 |
my ( $self, $startdate, $hours_duration, $return_by_hour ) = @_; |
904 |
my $base_date = $startdate->clone(); |
905 |
|
906 |
$base_date->add_duration($hours_duration); |
907 |
|
908 |
# If we are using the calendar behave for now as if Datedue |
909 |
# was the chosen option (current intended behaviour) |
910 |
|
911 |
if ( $self->{days_mode} ne 'Days' && |
912 |
$self->is_holiday($base_date) ) { |
913 |
|
914 |
if ( $hours_duration->is_negative() ) { |
915 |
$base_date = $self->prev_open_day($base_date); |
916 |
} else { |
917 |
$base_date = $self->next_open_day($base_date); |
918 |
} |
919 |
|
920 |
$base_date->set_hour($return_by_hour); |
921 |
|
922 |
} |
923 |
|
924 |
return $base_date; |
925 |
} |
926 |
|
927 |
sub addDays { |
928 |
my ( $self, $startdate, $days_duration ) = @_; |
929 |
my $base_date = $startdate->clone(); |
930 |
|
931 |
$self->{days_mode} ||= q{}; |
932 |
|
933 |
if ( $self->{days_mode} eq 'Calendar' ) { |
934 |
# use the calendar to skip all days the library is closed |
935 |
# when adding |
936 |
my $days = abs $days_duration->in_units('days'); |
937 |
|
938 |
if ( $days_duration->is_negative() ) { |
939 |
while ($days) { |
940 |
$base_date = $self->prev_open_day($base_date); |
941 |
--$days; |
942 |
} |
943 |
} else { |
944 |
while ($days) { |
945 |
$base_date = $self->next_open_day($base_date); |
946 |
--$days; |
947 |
} |
948 |
} |
949 |
|
950 |
} else { # Days or Datedue |
951 |
# use straight days, then use calendar to push |
952 |
# the date to the next open day if Datedue |
953 |
$base_date->add_duration($days_duration); |
954 |
|
955 |
if ( $self->{days_mode} eq 'Datedue' ) { |
956 |
# Datedue, then use the calendar to push |
957 |
# the date to the next open day if holiday |
958 |
if (!$self->isOpened($base_date) ) { |
959 |
|
960 |
if ( $days_duration->is_negative() ) { |
961 |
$base_date = $self->prev_open_day($base_date); |
962 |
} else { |
963 |
$base_date = $self->next_open_day($base_date); |
964 |
} |
965 |
} |
966 |
} |
967 |
} |
968 |
|
969 |
return $base_date; |
970 |
} |
971 |
|
972 |
1; |