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