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