1 package Koha::Calendar;
8 use DateTime::Duration;
13 my ( $classname, %options ) = @_;
15 bless $self, $classname;
16 for my $o_name ( keys %options ) {
18 $self->{$o} = $options{$o_name};
20 if ( !defined $self->{branchcode} ) {
21 croak 'No branchcode argument passed to Koha::Calendar->new';
29 my $branch = $self->{branchcode};
30 my $dbh = C4::Context->dbh();
31 my $weekly_closed_days_sth = $dbh->prepare(
32 'SELECT weekday FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NOT NULL'
34 $weekly_closed_days_sth->execute( $branch );
35 $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');
56 # FIXME: use of package-level variables for caching the holiday
57 # lists breaks persistance engines. As of 2013-12-10, the RM
58 # is allowing this with the expectation that prior to release of
59 # 3.16, bug 8089 will be fixed and we can switch the caching over
61 our ( $exception_holidays, $single_holidays );
62 sub exception_holidays {
64 my $dbh = C4::Context->dbh;
65 my $branch = $self->{branchcode};
66 if ( $exception_holidays ) {
67 $self->{exception_holidays} = $exception_holidays;
68 return $exception_holidays;
70 my $exception_holidays_sth = $dbh->prepare(
71 'SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 1'
73 $exception_holidays_sth->execute( $branch );
75 while ( my ( $day, $month, $year ) = $exception_holidays_sth->fetchrow ) {
81 time_zone => C4::Context->tz()
82 )->truncate( to => 'day' );
84 $self->{exception_holidays} =
85 DateTime::Set->from_datetimes( dates => $dates );
86 $exception_holidays = $self->{exception_holidays};
87 return $exception_holidays;
92 my $dbh = C4::Context->dbh;
93 my $branchcode = $self->{branchcode};
94 if ( $single_holidays->{$branchcode} ) {
95 $self->{single_holidays}{$branchcode} = $single_holidays->{$branchcode};
96 return $single_holidays->{$branchcode};
98 my $single_holidays_sth = $dbh->prepare(
99 'SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 0'
101 $single_holidays_sth->execute( $branchcode );
103 while ( my ( $day, $month, $year ) = $single_holidays_sth->fetchrow ) {
109 time_zone => C4::Context->tz()
110 )->truncate( to => 'day' );
112 $self->{single_holidays}{$branchcode} = DateTime::Set->from_datetimes( dates => $dates );
113 $single_holidays->{$branchcode} = $self->{single_holidays}{$branchcode};
114 return $single_holidays->{$branchcode};
117 my ( $self, $startdate, $add_duration, $unit ) = @_;
119 # Default to days duration (legacy support I guess)
120 if ( ref $add_duration ne 'DateTime::Duration' ) {
121 $add_duration = DateTime::Duration->new( days => $add_duration );
124 $unit ||= 'days'; # default days ?
127 if ( $unit eq 'hours' ) {
128 # Fixed for legacy support. Should be set as a branch parameter
129 my $return_by_hour => 10;
131 $dt = $self->addHours($startdate, $add_duration, $return_by_hour);
134 $dt = $self->addDays($startdate, $add_duration);
141 my ( $self, $startdate, $hours_duration, $return_by_hour ) = @_;
142 my $base_date = $startdate->clone();
144 $base_date->add_duration($hours_duration);
146 # If we are using the calendar behave for now as if Datedue
147 # was the chosen option (current intended behaviour)
149 if ( $self->{days_mode} ne 'Days' &&
150 $self->is_holiday($base_date) ) {
152 if ( $hours_duration->is_negative() ) {
153 $base_date = $self->prev_open_day($base_date);
155 $base_date = $self->next_open_day($base_date);
158 $base_date->set_hour($return_by_hour);
166 my ( $self, $startdate, $days_duration ) = @_;
167 my $base_date = $startdate->clone();
169 $self->{days_mode} ||= q{};
171 if ( $self->{days_mode} eq 'Calendar' ) {
172 # use the calendar to skip all days the library is closed
174 my $days = abs $days_duration->in_units('days');
176 if ( $days_duration->is_negative() ) {
178 $base_date = $self->prev_open_day($base_date);
183 $base_date = $self->next_open_day($base_date);
188 } else { # Days or Datedue
189 # use straight days, then use calendar to push
190 # the date to the next open day if Datedue
191 $base_date->add_duration($days_duration);
193 if ( $self->{days_mode} eq 'Datedue' ) {
194 # Datedue, then use the calendar to push
195 # the date to the next open day if holiday
196 if ( $self->is_holiday($base_date) ) {
197 if ( $days_duration->is_negative() ) {
198 $base_date = $self->prev_open_day($base_date);
200 $base_date = $self->next_open_day($base_date);
210 my ( $self, $dt ) = @_;
211 my $localdt = $dt->clone();
212 my $day = $localdt->day;
213 my $month = $localdt->month;
215 $localdt->truncate( to => 'day' );
217 if ( $self->exception_holidays->contains($localdt) ) {
218 # exceptions are not holidays
222 my $dow = $localdt->day_of_week;
224 # TODO: Shouldn't we shift the rest of the $dow also?
229 if ( $self->{weekly_closed_days}->[$dow] == 1 ) {
233 if ( exists $self->{day_month_closed_days}->{$month}->{$day} ) {
237 if ( $self->single_holidays->contains($localdt) ) {
241 # damn have to go to work after all
246 my ( $self, $dt ) = @_;
247 my $base_date = $dt->clone();
249 $base_date->add(days => 1);
251 while ($self->is_holiday($base_date)) {
252 $base_date->add(days => 1);
259 my ( $self, $dt ) = @_;
260 my $base_date = $dt->clone();
262 $base_date->add(days => -1);
264 while ($self->is_holiday($base_date)) {
265 $base_date->add(days => -1);
273 my $start_dt = shift;
276 if ( $start_dt->compare($end_dt) > 0 ) {
278 my $int_dt = $end_dt;
284 # start and end should not be closed days
285 my $days = $start_dt->delta_days($end_dt)->delta_days;
286 for (my $dt = $start_dt->clone();
290 if ($self->is_holiday($dt)) {
294 return DateTime::Duration->new( days => $days );
299 my ($self, $start_date, $end_date) = @_;
300 my $start_dt = $start_date->clone();
301 my $end_dt = $end_date->clone();
302 my $duration = $end_dt->delta_ms($start_dt);
303 $start_dt->truncate( to => 'day' );
304 $end_dt->truncate( to => 'day' );
305 # NB this is a kludge in that it assumes all days are 24 hours
306 # However for hourly loans the logic should be expanded to
307 # take into account open/close times then it would be a duration
308 # of library open hours
309 my $skipped_days = 0;
310 for (my $dt = $start_dt->clone();
314 if ($self->is_holiday($dt)) {
319 $duration->subtract_duration(DateTime::Duration->new( hours => 24 * $skipped_days));
327 my ( $self, $mode ) = @_;
329 # if not testing this is a no op
330 if ( $self->{test} ) {
331 $self->{days_mode} = $mode;
337 sub clear_weekly_closed_days {
339 $self->{weekly_closed_days} = [ 0, 0, 0, 0, 0, 0, 0 ]; # Sunday only
346 my @dt = $self->single_holidays->as_list;
348 my $branchcode = $self->{branchcode};
349 $self->{single_holidays}{$branchcode} =
350 DateTime::Set->from_datetimes( dates => \@dt );
351 $single_holidays->{$branchcode} = $self->{single_holidays}{$branchcode};
361 Koha::Calendar - Object containing a branches calendar
365 This documentation refers to Koha::Calendar version 0.0.1
371 my $c = Koha::Calendar->new( branchcode => 'MAIN' );
372 my $dt = DateTime->now();
375 $open = $c->is_holiday($dt);
376 # when will item be due if loan period = $dur (a DateTime::Duration object)
377 $duedate = $c->addDate($dt,$dur,'days');
382 Implements those features of C4::Calendar needed for Staffs Rolling Loans
386 =head2 new : Create a calendar object
388 my $calendar = Koha::Calendar->new( branchcode => 'MAIN' );
390 The option branchcode is required
395 my $dt = $calendar->addDate($date, $dur, $unit)
397 C<$date> is a DateTime object representing the starting date of the interval.
399 C<$offset> is a DateTime::Duration to add to it
401 C<$unit> is a string value 'days' or 'hours' toflag granularity of duration
403 Currently unit is only used to invoke Staffs return Monday at 10 am rule this
404 parameter will be removed when issuingrules properly cope with that
409 my $dt = $calendar->addHours($date, $dur, $return_by_hour )
411 C<$date> is a DateTime object representing the starting date of the interval.
413 C<$offset> is a DateTime::Duration to add to it
415 C<$return_by_hour> is an integer value representing the opening hour for the branch
420 my $dt = $calendar->addDays($date, $dur)
422 C<$date> is a DateTime object representing the starting date of the interval.
424 C<$offset> is a DateTime::Duration to add to it
426 C<$unit> is a string value 'days' or 'hours' toflag granularity of duration
428 Currently unit is only used to invoke Staffs return Monday at 10 am rule this
429 parameter will be removed when issuingrules properly cope with that
434 $yesno = $calendar->is_holiday($dt);
436 passed a DateTime object returns 1 if it is a closed day
437 0 if not according to the calendar
441 $duration = $calendar->days_between($start_dt, $end_dt);
443 Passed two dates returns a DateTime::Duration object measuring the length between them
444 ignoring closed days. Always returns a positive number irrespective of the
445 relative order of the parameters
449 $datetime = $calendar->next_open_day($duedate_dt)
451 Passed a Datetime returns another Datetime representing the next open day. It is
452 intended for use to calculate the due date when useDaysMode syspref is set to either
453 'Datedue' or 'Calendar'.
457 $datetime = $calendar->prev_open_day($duedate_dt)
459 Passed a Datetime returns another Datetime representing the previous open day. It is
460 intended for use to calculate the due date when useDaysMode syspref is set to either
461 'Datedue' or 'Calendar'.
465 For testing only allows the calling script to change days mode
467 =head2 clear_weekly_closed_days
469 In test mode changes the testing set of closed days to a new set with
470 no closed days. TODO passing an array of closed days to this would
471 allow testing of more configurations
475 Passed a datetime object this will add it to the calendar's list of
476 closed days. This is for testing so that we can alter the Calenfar object's
477 list of specified dates
481 Will croak if not passed a branchcode in new
483 =head1 BUGS AND LIMITATIONS
485 This only contains a limited subset of the functionality in C4::Calendar
486 Only enough to support Staffs Rolling loans
490 Colin Campbell colin.campbell@ptfs-europe.com
492 =head1 LICENSE AND COPYRIGHT
494 Copyright (c) 2011 PTFS-Europe Ltd All rights reserved
496 This program is free software: you can redistribute it and/or modify
497 it under the terms of the GNU General Public License as published by
498 the Free Software Foundation, either version 2 of the License, or
499 (at your option) any later version.
501 This program is distributed in the hope that it will be useful,
502 but WITHOUT ANY WARRANTY; without even the implied warranty of
503 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
504 GNU General Public License for more details.
506 You should have received a copy of the GNU General Public License
507 along with this program. If not, see <http://www.gnu.org/licenses/>.