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