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 Readonly
::Scalar
my $sunday => 7;
38 while ( my $tuple = $weekly_closed_days_sth->fetchrow_hashref ) {
39 $self->{weekly_closed_days
}->[ $tuple->{weekday
} ] = 1;
41 my $day_month_closed_days_sth = $dbh->prepare(
42 'SELECT day, month FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NULL'
44 $day_month_closed_days_sth->execute( $branch );
45 $self->{day_month_closed_days
} = {};
46 while ( my $tuple = $day_month_closed_days_sth->fetchrow_hashref ) {
47 $self->{day_month_closed_days
}->{ $tuple->{month
} }->{ $tuple->{day
} } =
51 $self->{days_mode
} = C4
::Context
->preference('useDaysMode');
57 # FIXME: use of package-level variables for caching the holiday
58 # lists breaks persistance engines. As of 2013-12-10, the RM
59 # is allowing this with the expectation that prior to release of
60 # 3.16, bug 8089 will be fixed and we can switch the caching over
62 our ( $exception_holidays, $single_holidays );
63 sub exception_holidays
{
65 my $dbh = C4
::Context
->dbh;
66 my $branch = $self->{branchcode
};
67 if ( $exception_holidays ) {
68 $self->{exception_holidays
} = $exception_holidays;
69 return $exception_holidays;
71 my $exception_holidays_sth = $dbh->prepare(
72 'SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 1'
74 $exception_holidays_sth->execute( $branch );
76 while ( my ( $day, $month, $year ) = $exception_holidays_sth->fetchrow ) {
82 time_zone
=> C4
::Context
->tz()
83 )->truncate( to
=> 'day' );
85 $self->{exception_holidays
} =
86 DateTime
::Set
->from_datetimes( dates
=> $dates );
87 $exception_holidays = $self->{exception_holidays
};
88 return $exception_holidays;
93 my $dbh = C4
::Context
->dbh;
94 my $branchcode = $self->{branchcode
};
95 if ( $single_holidays->{$branchcode} ) {
96 $self->{single_holidays
}{$branchcode} = $single_holidays->{$branchcode};
97 return $single_holidays->{$branchcode};
99 my $single_holidays_sth = $dbh->prepare(
100 'SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 0'
102 $single_holidays_sth->execute( $branchcode );
104 while ( my ( $day, $month, $year ) = $single_holidays_sth->fetchrow ) {
110 time_zone
=> C4
::Context
->tz()
111 )->truncate( to
=> 'day' );
113 $self->{single_holidays
}{$branchcode} = DateTime
::Set
->from_datetimes( dates
=> $dates );
114 $single_holidays->{$branchcode} = $self->{single_holidays
}{$branchcode};
115 return $single_holidays->{$branchcode};
118 my ( $self, $startdate, $add_duration, $unit ) = @_;
120 # Default to days duration (legacy support I guess)
121 if ( ref $add_duration ne 'DateTime::Duration' ) {
122 $add_duration = DateTime
::Duration
->new( days
=> $add_duration );
125 $unit ||= 'days'; # default days ?
128 if ( $unit eq 'hours' ) {
129 # Fixed for legacy support. Should be set as a branch parameter
130 Readonly
::Scalar
my $return_by_hour => 10;
132 $dt = $self->addHours($startdate, $add_duration, $return_by_hour);
135 $dt = $self->addDays($startdate, $add_duration);
142 my ( $self, $startdate, $hours_duration, $return_by_hour ) = @_;
143 my $base_date = $startdate->clone();
145 $base_date->add_duration($hours_duration);
147 # If we are using the calendar behave for now as if Datedue
148 # was the chosen option (current intended behaviour)
150 if ( $self->{days_mode
} ne 'Days' &&
151 $self->is_holiday($base_date) ) {
153 if ( $hours_duration->is_negative() ) {
154 $base_date = $self->prev_open_day($base_date);
156 $base_date = $self->next_open_day($base_date);
159 $base_date->set_hour($return_by_hour);
167 my ( $self, $startdate, $days_duration ) = @_;
168 my $base_date = $startdate->clone();
170 $self->{days_mode
} ||= q{};
172 if ( $self->{days_mode
} eq 'Calendar' ) {
173 # use the calendar to skip all days the library is closed
175 my $days = abs $days_duration->in_units('days');
177 if ( $days_duration->is_negative() ) {
179 $base_date = $self->prev_open_day($base_date);
184 $base_date = $self->next_open_day($base_date);
189 } else { # Days or Datedue
190 # use straight days, then use calendar to push
191 # the date to the next open day if Datedue
192 $base_date->add_duration($days_duration);
194 if ( $self->{days_mode
} eq 'Datedue' ) {
195 # Datedue, then use the calendar to push
196 # the date to the next open day if holiday
197 if ( $self->is_holiday($base_date) ) {
198 if ( $days_duration->is_negative() ) {
199 $base_date = $self->prev_open_day($base_date);
201 $base_date = $self->next_open_day($base_date);
211 my ( $self, $dt ) = @_;
212 my $localdt = $dt->clone();
213 my $day = $localdt->day;
214 my $month = $localdt->month;
216 $localdt->truncate( to
=> 'day' );
218 if ( $self->exception_holidays->contains($localdt) ) {
219 # exceptions are not holidays
223 my $dow = $localdt->day_of_week;
225 # TODO: Shouldn't we shift the rest of the $dow also?
230 if ( $self->{weekly_closed_days
}->[$dow] == 1 ) {
234 if ( exists $self->{day_month_closed_days
}->{$month}->{$day} ) {
238 if ( $self->single_holidays->contains($localdt) ) {
242 # damn have to go to work after all
247 my ( $self, $dt ) = @_;
248 my $base_date = $dt->clone();
250 $base_date->add(days
=> 1);
252 while ($self->is_holiday($base_date)) {
253 $base_date->add(days
=> 1);
260 my ( $self, $dt ) = @_;
261 my $base_date = $dt->clone();
263 $base_date->add(days
=> -1);
265 while ($self->is_holiday($base_date)) {
266 $base_date->add(days
=> -1);
274 my $start_dt = shift;
277 if ( $start_dt->compare($end_dt) > 0 ) {
279 my $int_dt = $end_dt;
285 # start and end should not be closed days
286 my $days = $start_dt->delta_days($end_dt)->delta_days;
287 for (my $dt = $start_dt->clone();
291 if ($self->is_holiday($dt)) {
295 return DateTime
::Duration
->new( days
=> $days );
300 my ($self, $start_date, $end_date) = @_;
301 my $start_dt = $start_date->clone();
302 my $end_dt = $end_date->clone();
303 my $duration = $end_dt->delta_ms($start_dt);
304 $start_dt->truncate( to
=> 'day' );
305 $end_dt->truncate( to
=> 'day' );
306 # NB this is a kludge in that it assumes all days are 24 hours
307 # However for hourly loans the logic should be expanded to
308 # take into account open/close times then it would be a duration
309 # of library open hours
310 my $skipped_days = 0;
311 for (my $dt = $start_dt->clone();
315 if ($self->is_holiday($dt)) {
320 $duration->subtract_duration(DateTime
::Duration
->new( hours
=> 24 * $skipped_days));
328 my ( $self, $mode ) = @_;
330 # if not testing this is a no op
331 if ( $self->{test
} ) {
332 $self->{days_mode
} = $mode;
338 sub clear_weekly_closed_days
{
340 $self->{weekly_closed_days
} = [ 0, 0, 0, 0, 0, 0, 0 ]; # Sunday only
347 my @dt = $self->single_holidays->as_list;
349 my $branchcode = $self->{branchcode
};
350 $self->{single_holidays
}{$branchcode} =
351 DateTime
::Set
->from_datetimes( dates
=> \
@dt );
352 $single_holidays->{$branchcode} = $self->{single_holidays
}{$branchcode};
362 Koha::Calendar - Object containing a branches calendar
366 This documentation refers to Koha::Calendar version 0.0.1
372 my $c = Koha::Calendar->new( branchcode => 'MAIN' );
373 my $dt = DateTime->now();
376 $open = $c->is_holiday($dt);
377 # when will item be due if loan period = $dur (a DateTime::Duration object)
378 $duedate = $c->addDate($dt,$dur,'days');
383 Implements those features of C4::Calendar needed for Staffs Rolling Loans
387 =head2 new : Create a calendar object
389 my $calendar = Koha::Calendar->new( branchcode => 'MAIN' );
391 The option branchcode is required
396 my $dt = $calendar->addDate($date, $dur, $unit)
398 C<$date> is a DateTime object representing the starting date of the interval.
400 C<$offset> is a DateTime::Duration to add to it
402 C<$unit> is a string value 'days' or 'hours' toflag granularity of duration
404 Currently unit is only used to invoke Staffs return Monday at 10 am rule this
405 parameter will be removed when issuingrules properly cope with that
410 my $dt = $calendar->addHours($date, $dur, $return_by_hour )
412 C<$date> is a DateTime object representing the starting date of the interval.
414 C<$offset> is a DateTime::Duration to add to it
416 C<$return_by_hour> is an integer value representing the opening hour for the branch
421 my $dt = $calendar->addDays($date, $dur)
423 C<$date> is a DateTime object representing the starting date of the interval.
425 C<$offset> is a DateTime::Duration to add to it
427 C<$unit> is a string value 'days' or 'hours' toflag granularity of duration
429 Currently unit is only used to invoke Staffs return Monday at 10 am rule this
430 parameter will be removed when issuingrules properly cope with that
435 $yesno = $calendar->is_holiday($dt);
437 passed a DateTime object returns 1 if it is a closed day
438 0 if not according to the calendar
442 $duration = $calendar->days_between($start_dt, $end_dt);
444 Passed two dates returns a DateTime::Duration object measuring the length between them
445 ignoring closed days. Always returns a positive number irrespective of the
446 relative order of the parameters
450 $datetime = $calendar->next_open_day($duedate_dt)
452 Passed a Datetime returns another Datetime representing the next open day. It is
453 intended for use to calculate the due date when useDaysMode syspref is set to either
454 'Datedue' or 'Calendar'.
458 $datetime = $calendar->prev_open_day($duedate_dt)
460 Passed a Datetime returns another Datetime representing the previous open day. It is
461 intended for use to calculate the due date when useDaysMode syspref is set to either
462 'Datedue' or 'Calendar'.
466 For testing only allows the calling script to change days mode
468 =head2 clear_weekly_closed_days
470 In test mode changes the testing set of closed days to a new set with
471 no closed days. TODO passing an array of closed days to this would
472 allow testing of more configurations
476 Passed a datetime object this will add it to the calendar's list of
477 closed days. This is for testing so that we can alter the Calenfar object's
478 list of specified dates
482 Will croak if not passed a branchcode in new
484 =head1 BUGS AND LIMITATIONS
486 This only contains a limited subset of the functionality in C4::Calendar
487 Only enough to support Staffs Rolling loans
491 Colin Campbell colin.campbell@ptfs-europe.com
493 =head1 LICENSE AND COPYRIGHT
495 Copyright (c) 2011 PTFS-Europe Ltd All rights reserved
497 This program is free software: you can redistribute it and/or modify
498 it under the terms of the GNU General Public License as published by
499 the Free Software Foundation, either version 2 of the License, or
500 (at your option) any later version.
502 This program is distributed in the hope that it will be useful,
503 but WITHOUT ANY WARRANTY; without even the implied warranty of
504 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
505 GNU General Public License for more details.
507 You should have received a copy of the GNU General Public License
508 along with this program. If not, see <http://www.gnu.org/licenses/>.