1 package Koha::Calendar;
8 use DateTime::Duration;
14 my ( $classname, %options ) = @_;
16 bless $self, $classname;
17 for my $o_name ( keys %options ) {
19 $self->{$o} = $options{$o_name};
21 if ( !defined $self->{branchcode} ) {
22 croak 'No branchcode argument passed to Koha::Calendar->new';
30 my $branch = $self->{branchcode};
31 my $dbh = C4::Context->dbh();
32 my $weekly_closed_days_sth = $dbh->prepare(
33 'SELECT weekday FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NOT NULL'
35 $weekly_closed_days_sth->execute( $branch );
36 $self->{weekly_closed_days} = [ 0, 0, 0, 0, 0, 0, 0 ];
37 while ( my $tuple = $weekly_closed_days_sth->fetchrow_hashref ) {
38 $self->{weekly_closed_days}->[ $tuple->{weekday} ] = 1;
40 my $day_month_closed_days_sth = $dbh->prepare(
41 'SELECT day, month FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NULL'
43 $day_month_closed_days_sth->execute( $branch );
44 $self->{day_month_closed_days} = {};
45 while ( my $tuple = $day_month_closed_days_sth->fetchrow_hashref ) {
46 $self->{day_month_closed_days}->{ $tuple->{month} }->{ $tuple->{day} } =
50 $self->{days_mode} ||= C4::Context->preference('useDaysMode');
55 sub exception_holidays {
58 my $cache = Koha::Caches->get_instance();
59 my $cached = $cache->get_from_cache('exception_holidays');
60 return $cached if $cached;
62 my $dbh = C4::Context->dbh;
63 my $branch = $self->{branchcode};
64 my $exception_holidays_sth = $dbh->prepare(
65 'SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 1'
67 $exception_holidays_sth->execute( $branch );
69 while ( my ( $day, $month, $year ) = $exception_holidays_sth->fetchrow ) {
75 time_zone => "floating",
76 )->truncate( to => 'day' );
78 $self->{exception_holidays} =
79 DateTime::Set->from_datetimes( dates => $dates );
80 $cache->set_in_cache( 'exception_holidays', $self->{exception_holidays} );
81 return $self->{exception_holidays};
85 my ( $self, $date ) = @_;
86 my $branchcode = $self->{branchcode};
87 my $cache = Koha::Caches->get_instance();
88 my $single_holidays = $cache->get_from_cache('single_holidays');
90 # $single_holidays looks like:
99 unless ($single_holidays) {
100 my $dbh = C4::Context->dbh;
101 $single_holidays = {};
103 # push holidays for each branch
105 $dbh->prepare('SELECT distinct(branchcode) FROM special_holidays');
106 $branches_sth->execute();
107 while ( my $br = $branches_sth->fetchrow ) {
108 my $single_holidays_sth = $dbh->prepare(
109 'SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 0'
111 $single_holidays_sth->execute($br);
114 while ( my ( $day, $month, $year ) =
115 $single_holidays_sth->fetchrow )
117 my $dt = DateTime->new(
121 time_zone => 'floating',
122 )->truncate( to => 'day' );
123 push @ymd_arr, $dt->ymd('');
125 $single_holidays->{$br} = \@ymd_arr;
127 $cache->set_in_cache( 'single_holidays', $single_holidays,
128 { expiry => 76800 } ) #24 hrs ;
130 my $holidays = ( $single_holidays->{$branchcode} );
131 for my $hols (@$holidays ) {
132 return 1 if ( $date == $hols ) #match ymds;
138 my ( $self, $startdate, $add_duration, $unit ) = @_;
140 # Default to days duration (legacy support I guess)
141 if ( ref $add_duration ne 'DateTime::Duration' ) {
142 $add_duration = DateTime::Duration->new( days => $add_duration );
145 $unit ||= 'days'; # default days ?
147 if ( $unit eq 'hours' ) {
148 # Fixed for legacy support. Should be set as a branch parameter
149 my $return_by_hour = 10;
151 $dt = $self->addHours($startdate, $add_duration, $return_by_hour);
154 $dt = $self->addDays($startdate, $add_duration);
160 my ( $self, $startdate, $hours_duration, $return_by_hour ) = @_;
161 my $base_date = $startdate->clone();
163 $base_date->add_duration($hours_duration);
165 # If we are using the calendar behave for now as if Datedue
166 # was the chosen option (current intended behaviour)
168 if ( $self->{days_mode} ne 'Days' &&
169 $self->is_holiday($base_date) ) {
171 if ( $hours_duration->is_negative() ) {
172 $base_date = $self->prev_open_days($base_date, 1);
174 $base_date = $self->next_open_days($base_date, 1);
177 $base_date->set_hour($return_by_hour);
185 my ( $self, $startdate, $days_duration ) = @_;
186 my $base_date = $startdate->clone();
188 $self->{days_mode} ||= q{};
190 if ( $self->{days_mode} eq 'Calendar' ) {
191 # use the calendar to skip all days the library is closed
193 my $days = abs $days_duration->in_units('days');
195 if ( $days_duration->is_negative() ) {
197 $base_date = $self->prev_open_days($base_date, 1);
202 $base_date = $self->next_open_days($base_date, 1);
207 } else { # Days, Datedue or Dayweek
208 # use straight days, then use calendar to push
209 # the date to the next open day as appropriate
210 # if Datedue or Dayweek
211 $base_date->add_duration($days_duration);
213 if ( $self->{days_mode} eq 'Datedue' ||
214 $self->{days_mode} eq 'Dayweek') {
215 # Datedue or Dayweek, then use the calendar to push
216 # the date to the next open day if holiday
217 if ( $self->is_holiday($base_date) ) {
218 my $dow = $base_date->day_of_week;
219 my $days = $days_duration->in_units('days');
220 # Is it a period based on weeks
221 my $push_amt = $days % 7 == 0 ?
222 $self->get_push_amt($base_date) : 1;
223 if ( $days_duration->is_negative() ) {
224 $base_date = $self->prev_open_days($base_date, $push_amt);
226 $base_date = $self->next_open_days($base_date, $push_amt);
236 my ( $self, $base_date) = @_;
238 my $dow = $base_date->day_of_week;
240 # DateTime object dow (1-7) where Monday is 1
241 # Arrays are 0-based where 0 = Sunday, not 7.
247 # We're using Dayweek useDaysMode option
248 $self->{days_mode} eq 'Dayweek' &&
249 # It's not a permanently closed day
250 !$self->{weekly_closed_days}->[$dow]
255 my ( $self, $dt ) = @_;
257 my $localdt = $dt->clone();
258 my $day = $localdt->day;
259 my $month = $localdt->month;
261 #Change timezone to "floating" before doing any calculations or comparisons
262 $localdt->set_time_zone("floating");
263 $localdt->truncate( to => 'day' );
266 if ( $self->exception_holidays->contains($localdt) ) {
267 # exceptions are not holidays
271 my $dow = $localdt->day_of_week;
273 # DateTime object dow (1-7) where Monday is 1
274 # Arrays are 0-based where 0 = Sunday, not 7.
279 if ( $self->{weekly_closed_days}->[$dow] == 1 ) {
283 if ( exists $self->{day_month_closed_days}->{$month}->{$day} ) {
287 my $ymd = $localdt->ymd('') ;
288 if ($self->single_holidays( $ymd ) == 1 ) {
292 # damn have to go to work after all
297 my ( $self, $dt, $to_add ) = @_;
298 my $base_date = $dt->clone();
300 $base_date->add(days => $to_add);
301 while ($self->is_holiday($base_date)) {
302 my $add_next = $self->get_push_amt($base_date);
303 $base_date->add(days => $add_next);
309 my ( $self, $dt, $to_sub ) = @_;
310 my $base_date = $dt->clone();
312 # It feels logical to be passed a positive number, though we're
313 # subtracting, so do the right thing
314 $to_sub = $to_sub > 0 ? 0 - $to_sub : $to_sub;
316 $base_date->add(days => $to_sub);
318 while ($self->is_holiday($base_date)) {
319 my $sub_next = $self->get_push_amt($base_date);
320 # Ensure we're subtracting when we need to be
321 $sub_next = $sub_next > 0 ? 0 - $sub_next : $sub_next;
322 $base_date->add(days => $sub_next);
330 my $start_dt = shift;
331 my $num_days = shift;
333 return $start_dt unless $num_days > 0;
335 my $base_dt = $start_dt->clone();
337 while ($num_days--) {
338 $base_dt = $self->next_open_days($base_dt, 1);
346 my $start_dt = shift;
349 # Change time zone for date math and swap if needed
350 $start_dt = $start_dt->clone->set_time_zone('floating');
351 $end_dt = $end_dt->clone->set_time_zone('floating');
352 if( $start_dt->compare($end_dt) > 0 ) {
353 ( $start_dt, $end_dt ) = ( $end_dt, $start_dt );
356 # start and end should not be closed days
357 my $delta_days = $start_dt->delta_days($end_dt)->delta_days;
358 while( $start_dt->compare($end_dt) < 1 ) {
359 $delta_days-- if $self->is_holiday($start_dt);
360 $start_dt->add( days => 1 );
362 return DateTime::Duration->new( days => $delta_days );
366 my ($self, $start_date, $end_date) = @_;
367 my $start_dt = $start_date->clone()->set_time_zone('floating');
368 my $end_dt = $end_date->clone()->set_time_zone('floating');
370 my $duration = $end_dt->delta_ms($start_dt);
371 $start_dt->truncate( to => 'day' );
372 $end_dt->truncate( to => 'day' );
374 # NB this is a kludge in that it assumes all days are 24 hours
375 # However for hourly loans the logic should be expanded to
376 # take into account open/close times then it would be a duration
377 # of library open hours
378 my $skipped_days = 0;
379 while( $start_dt->compare($end_dt) < 1 ) {
380 $skipped_days++ if $self->is_holiday($start_dt);
381 $start_dt->add( days => 1 );
385 $duration->subtract_duration(DateTime::Duration->new( hours => 24 * $skipped_days));
392 my ( $self, $mode ) = @_;
394 # if not testing this is a no op
395 if ( $self->{test} ) {
396 $self->{days_mode} = $mode;
402 sub clear_weekly_closed_days {
404 $self->{weekly_closed_days} = [ 0, 0, 0, 0, 0, 0, 0 ]; # Sunday only
413 Koha::Calendar - Object containing a branches calendar
419 my $c = Koha::Calendar->new( branchcode => 'MAIN' );
420 my $dt = dt_from_string();
423 $open = $c->is_holiday($dt);
424 # when will item be due if loan period = $dur (a DateTime::Duration object)
425 $duedate = $c->addDate($dt,$dur,'days');
430 Implements those features of C4::Calendar needed for Staffs Rolling Loans
434 =head2 new : Create a calendar object
436 my $calendar = Koha::Calendar->new( branchcode => 'MAIN' );
438 The option branchcode is required
443 my $dt = $calendar->addDate($date, $dur, $unit)
445 C<$date> is a DateTime object representing the starting date of the interval.
447 C<$offset> is a DateTime::Duration to add to it
449 C<$unit> is a string value 'days' or 'hours' toflag granularity of duration
451 Currently unit is only used to invoke Staffs return Monday at 10 am rule this
452 parameter will be removed when issuingrules properly cope with that
457 my $dt = $calendar->addHours($date, $dur, $return_by_hour )
459 C<$date> is a DateTime object representing the starting date of the interval.
461 C<$offset> is a DateTime::Duration to add to it
463 C<$return_by_hour> is an integer value representing the opening hour for the branch
467 my $amt = $calendar->get_push_amt($date)
469 C<$date> is a DateTime object representing a closed return date
471 Using the days_mode syspref value and the nature of the closed return
472 date, return how many days we should jump forward to find another return date
476 my $dt = $calendar->addDays($date, $dur)
478 C<$date> is a DateTime object representing the starting date of the interval.
480 C<$offset> is a DateTime::Duration to add to it
482 C<$unit> is a string value 'days' or 'hours' toflag granularity of duration
484 Currently unit is only used to invoke Staffs return Monday at 10 am rule this
485 parameter will be removed when issuingrules properly cope with that
488 =head2 single_holidays
490 my $rc = $self->single_holidays( $ymd );
492 Passed a $date in Ymd (yyyymmdd) format - returns 1 if date is a single_holiday, or 0 if not.
497 $yesno = $calendar->is_holiday($dt);
499 passed a DateTime object returns 1 if it is a closed day
500 0 if not according to the calendar
504 $duration = $calendar->days_between($start_dt, $end_dt);
506 Passed two dates returns a DateTime::Duration object measuring the length between them
507 ignoring closed days. Always returns a positive number irrespective of the
508 relative order of the parameters.
510 Note: This routine assumes neither the passed start_dt nor end_dt can be a closed day
514 $duration = $calendar->hours_between($start_dt, $end_dt);
516 Passed two dates returns a DateTime::Duration object measuring the length between them
517 ignoring closed days. Always returns a positive number irrespective of the
518 relative order of the parameters.
520 Note: This routine assumes neither the passed start_dt nor end_dt can be a closed day
522 =head2 next_open_days
524 $datetime = $calendar->next_open_days($duedate_dt, $to_add)
526 Passed a Datetime and number of days, returns another Datetime representing
527 the next open day after adding the passed number of days. It is intended for
528 use to calculate the due date when useDaysMode syspref is set to either
529 'Datedue', 'Calendar' or 'Dayweek'.
531 =head2 prev_open_days
533 $datetime = $calendar->prev_open_days($duedate_dt, $to_sub)
535 Passed a Datetime and a number of days, returns another Datetime
536 representing the previous open day after subtracting the number of passed
537 days. It is intended for use to calculate the due date when useDaysMode
538 syspref is set to either 'Datedue', 'Calendar' or 'Dayweek'.
542 For testing only allows the calling script to change days mode
544 =head2 clear_weekly_closed_days
546 In test mode changes the testing set of closed days to a new set with
547 no closed days. TODO passing an array of closed days to this would
548 allow testing of more configurations
552 Passed a datetime object this will add it to the calendar's list of
553 closed days. This is for testing so that we can alter the Calenfar object's
554 list of specified dates
558 Will croak if not passed a branchcode in new
560 =head1 BUGS AND LIMITATIONS
562 This only contains a limited subset of the functionality in C4::Calendar
563 Only enough to support Staffs Rolling loans
567 Colin Campbell colin.campbell@ptfs-europe.com
569 =head1 LICENSE AND COPYRIGHT
571 Copyright (c) 2011 PTFS-Europe Ltd All rights reserved
573 This program is free software: you can redistribute it and/or modify
574 it under the terms of the GNU General Public License as published by
575 the Free Software Foundation, either version 2 of the License, or
576 (at your option) any later version.
578 This program is distributed in the hope that it will be useful,
579 but WITHOUT ANY WARRANTY; without even the implied warranty of
580 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
581 GNU General Public License for more details.
583 You should have received a copy of the GNU General Public License
584 along with this program. If not, see <http://www.gnu.org/licenses/>.