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 our ( $exception_holidays, $single_holidays );
58 sub exception_holidays {
60 my $dbh = C4::Context->dbh;
61 my $branch = $self->{branchcode};
62 if ( $exception_holidays ) {
63 $self->{exception_holidays} = $exception_holidays;
64 return $exception_holidays;
66 my $exception_holidays_sth = $dbh->prepare(
67 'SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 1'
69 $exception_holidays_sth->execute( $branch );
71 while ( my ( $day, $month, $year ) = $exception_holidays_sth->fetchrow ) {
77 time_zone => C4::Context->tz()
78 )->truncate( to => 'day' );
80 $self->{exception_holidays} =
81 DateTime::Set->from_datetimes( dates => $dates );
82 $exception_holidays = $self->{exception_holidays};
83 return $exception_holidays;
88 my $dbh = C4::Context->dbh;
89 my $branch = $self->{branchcode};
90 if ( $single_holidays ) {
91 $self->{single_holidays} = $single_holidays;
92 return $single_holidays;
94 my $single_holidays_sth = $dbh->prepare(
95 'SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 0'
97 $single_holidays_sth->execute( $branch );
99 while ( my ( $day, $month, $year ) = $single_holidays_sth->fetchrow ) {
105 time_zone => C4::Context->tz()
106 )->truncate( to => 'day' );
108 $self->{single_holidays} = DateTime::Set->from_datetimes( dates => $dates );
109 $single_holidays = $self->{single_holidays};
110 return $single_holidays;
113 my ( $self, $startdate, $add_duration, $unit ) = @_;
115 # Default to days duration (legacy support I guess)
116 if ( ref $add_duration ne 'DateTime::Duration' ) {
117 $add_duration = DateTime::Duration->new( days => $add_duration );
120 $unit ||= 'days'; # default days ?
123 if ( $unit eq 'hours' ) {
124 # Fixed for legacy support. Should be set as a branch parameter
125 Readonly::Scalar my $return_by_hour => 10;
127 $dt = $self->addHours($startdate, $add_duration, $return_by_hour);
130 $dt = $self->addDays($startdate, $add_duration);
137 my ( $self, $startdate, $hours_duration, $return_by_hour ) = @_;
138 my $base_date = $startdate->clone();
140 $base_date->add_duration($hours_duration);
142 # If we are using the calendar behave for now as if Datedue
143 # was the chosen option (current intended behaviour)
145 if ( $self->{days_mode} ne 'Days' &&
146 $self->is_holiday($base_date) ) {
148 if ( $hours_duration->is_negative() ) {
149 $base_date = $self->prev_open_day($base_date);
151 $base_date = $self->next_open_day($base_date);
154 $base_date->set_hour($return_by_hour);
162 my ( $self, $startdate, $days_duration ) = @_;
163 my $base_date = $startdate->clone();
165 if ( $self->{days_mode} eq 'Calendar' ) {
166 # use the calendar to skip all days the library is closed
168 my $days = abs $days_duration->in_units('days');
170 if ( $days_duration->is_negative() ) {
172 $base_date = $self->prev_open_day($base_date);
177 $base_date = $self->next_open_day($base_date);
182 } else { # Days or Datedue
183 # use straight days, then use calendar to push
184 # the date to the next open day if Datedue
185 $base_date->add_duration($days_duration);
187 if ( $self->{days_mode} eq 'Datedue' ) {
188 # Datedue, then use the calendar to push
189 # the date to the next open day if holiday
190 if ( $self->is_holiday($base_date) ) {
191 if ( $days_duration->is_negative() ) {
192 $base_date = $self->prev_open_day($base_date);
194 $base_date = $self->next_open_day($base_date);
204 my ( $self, $dt ) = @_;
205 my $localdt = $dt->clone();
206 my $day = $localdt->day;
207 my $month = $localdt->month;
209 $localdt->truncate( to => 'day' );
211 if ( $self->exception_holidays->contains($localdt) ) {
212 # exceptions are not holidays
216 my $dow = $localdt->day_of_week;
218 # TODO: Shouldn't we shift the rest of the $dow also?
223 if ( $self->{weekly_closed_days}->[$dow] == 1 ) {
227 if ( exists $self->{day_month_closed_days}->{$month}->{$day} ) {
231 if ( $self->single_holidays->contains($localdt) ) {
235 # damn have to go to work after all
240 my ( $self, $dt ) = @_;
241 my $base_date = $dt->clone();
243 $base_date->add(days => 1);
245 while ($self->is_holiday($base_date)) {
246 $base_date->add(days => 1);
253 my ( $self, $dt ) = @_;
254 my $base_date = $dt->clone();
256 $base_date->add(days => -1);
258 while ($self->is_holiday($base_date)) {
259 $base_date->add(days => -1);
267 my $start_dt = shift;
270 if ( $start_dt->compare($end_dt) > 0 ) {
272 my $int_dt = $end_dt;
278 # start and end should not be closed days
279 my $days = $start_dt->delta_days($end_dt)->delta_days;
280 for (my $dt = $start_dt->clone();
284 if ($self->is_holiday($dt)) {
288 return DateTime::Duration->new( days => $days );
293 my ($self, $start_date, $end_date) = @_;
294 my $start_dt = $start_date->clone();
295 my $end_dt = $end_date->clone();
296 my $duration = $end_dt->delta_ms($start_dt);
297 $start_dt->truncate( to => 'day' );
298 $end_dt->truncate( to => 'day' );
299 # NB this is a kludge in that it assumes all days are 24 hours
300 # However for hourly loans the logic should be expanded to
301 # take into account open/close times then it would be a duration
302 # of library open hours
303 my $skipped_days = 0;
304 for (my $dt = $start_dt->clone();
308 if ($self->is_holiday($dt)) {
313 $duration->subtract_duration(DateTime::Duration->new( hours => 24 * $skipped_days));
321 my ( $self, $mode ) = @_;
323 # if not testing this is a no op
324 if ( $self->{test} ) {
325 $self->{days_mode} = $mode;
331 sub clear_weekly_closed_days {
333 $self->{weekly_closed_days} = [ 0, 0, 0, 0, 0, 0, 0 ]; # Sunday only
340 my @dt = $self->single_holidays->as_list;
342 $self->{single_holidays} =
343 DateTime::Set->from_datetimes( dates => \@dt );
344 $single_holidays = $self->{single_holidays};
354 Koha::Calendar - Object containing a branches calendar
358 This documentation refers to Koha::Calendar version 0.0.1
364 my $c = Koha::Calendar->new( branchcode => 'MAIN' );
365 my $dt = DateTime->now();
368 $open = $c->is_holiday($dt);
369 # when will item be due if loan period = $dur (a DateTime::Duration object)
370 $duedate = $c->addDate($dt,$dur,'days');
375 Implements those features of C4::Calendar needed for Staffs Rolling Loans
379 =head2 new : Create a calendar object
381 my $calendar = Koha::Calendar->new( branchcode => 'MAIN' );
383 The option branchcode is required
388 my $dt = $calendar->addDate($date, $dur, $unit)
390 C<$date> is a DateTime object representing the starting date of the interval.
392 C<$offset> is a DateTime::Duration to add to it
394 C<$unit> is a string value 'days' or 'hours' toflag granularity of duration
396 Currently unit is only used to invoke Staffs return Monday at 10 am rule this
397 parameter will be removed when issuingrules properly cope with that
402 my $dt = $calendar->addHours($date, $dur, $return_by_hour )
404 C<$date> is a DateTime object representing the starting date of the interval.
406 C<$offset> is a DateTime::Duration to add to it
408 C<$return_by_hour> is an integer value representing the opening hour for the branch
413 my $dt = $calendar->addDays($date, $dur)
415 C<$date> is a DateTime object representing the starting date of the interval.
417 C<$offset> is a DateTime::Duration to add to it
419 C<$unit> is a string value 'days' or 'hours' toflag granularity of duration
421 Currently unit is only used to invoke Staffs return Monday at 10 am rule this
422 parameter will be removed when issuingrules properly cope with that
427 $yesno = $calendar->is_holiday($dt);
429 passed a DateTime object returns 1 if it is a closed day
430 0 if not according to the calendar
434 $duration = $calendar->days_between($start_dt, $end_dt);
436 Passed two dates returns a DateTime::Duration object measuring the length between them
437 ignoring closed days. Always returns a positive number irrespective of the
438 relative order of the parameters
442 $datetime = $calendar->next_open_day($duedate_dt)
444 Passed a Datetime returns another Datetime representing the next open day. It is
445 intended for use to calculate the due date when useDaysMode syspref is set to either
446 'Datedue' or 'Calendar'.
450 $datetime = $calendar->prev_open_day($duedate_dt)
452 Passed a Datetime returns another Datetime representing the previous 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 For testing only allows the calling script to change days mode
460 =head2 clear_weekly_closed_days
462 In test mode changes the testing set of closed days to a new set with
463 no closed days. TODO passing an array of closed days to this would
464 allow testing of more configurations
468 Passed a datetime object this will add it to the calendar's list of
469 closed days. This is for testing so that we can alter the Calenfar object's
470 list of specified dates
474 Will croak if not passed a branchcode in new
476 =head1 BUGS AND LIMITATIONS
478 This only contains a limited subset of the functionality in C4::Calendar
479 Only enough to support Staffs Rolling loans
483 Colin Campbell colin.campbell@ptfs-europe.com
485 =head1 LICENSE AND COPYRIGHT
487 Copyright (c) 2011 PTFS-Europe Ltd All rights reserved
489 This program is free software: you can redistribute it and/or modify
490 it under the terms of the GNU General Public License as published by
491 the Free Software Foundation, either version 2 of the License, or
492 (at your option) any later version.
494 This program is distributed in the hope that it will be useful,
495 but WITHOUT ANY WARRANTY; without even the implied warranty of
496 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
497 GNU General Public License for more details.
499 You should have received a copy of the GNU General Public License
500 along with this program. If not, see <http://www.gnu.org/licenses/>.