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 ( exists $options{TEST_MODE
} ) {
25 if ( !defined $self->{branchcode
} ) {
26 croak
'No branchcode argument passed to Koha::Calendar->new';
34 my $branch = $self->{branchcode
};
35 my $dbh = C4
::Context
->dbh();
36 my $weekly_closed_days_sth = $dbh->prepare(
37 'SELECT weekday FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NOT NULL'
39 $weekly_closed_days_sth->execute( $branch );
40 $self->{weekly_closed_days
} = [ 0, 0, 0, 0, 0, 0, 0 ];
41 Readonly
::Scalar
my $sunday => 7;
42 while ( my $tuple = $weekly_closed_days_sth->fetchrow_hashref ) {
43 $self->{weekly_closed_days
}->[ $tuple->{weekday
} ] = 1;
45 my $day_month_closed_days_sth = $dbh->prepare(
46 'SELECT day, month FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NULL'
48 $day_month_closed_days_sth->execute( $branch );
49 $self->{day_month_closed_days
} = {};
50 while ( my $tuple = $day_month_closed_days_sth->fetchrow_hashref ) {
51 $self->{day_month_closed_days
}->{ $tuple->{month
} }->{ $tuple->{day
} } =
55 my $exception_holidays_sth = $dbh->prepare(
56 'SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 1'
58 $exception_holidays_sth->execute( $branch );
60 while ( my ( $day, $month, $year ) = $exception_holidays_sth->fetchrow ) {
66 time_zone
=> C4
::Context
->tz()
67 )->truncate( to
=> 'day' );
69 $self->{exception_holidays
} =
70 DateTime
::Set
->from_datetimes( dates
=> $dates );
72 my $single_holidays_sth = $dbh->prepare(
73 'SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 0'
75 $single_holidays_sth->execute( $branch );
77 while ( my ( $day, $month, $year ) = $single_holidays_sth->fetchrow ) {
83 time_zone
=> C4
::Context
->tz()
84 )->truncate( to
=> 'day' );
86 $self->{single_holidays
} = DateTime
::Set
->from_datetimes( dates
=> $dates );
87 $self->{days_mode
} = C4
::Context
->preference('useDaysMode');
93 my ( $self, $startdate, $add_duration, $unit ) = @_;
95 # Default to days duration (legacy support I guess)
96 if ( ref $add_duration ne 'DateTime::Duration' ) {
97 $add_duration = DateTime
::Duration
->new( days
=> $add_duration );
100 $unit ||= 'days'; # default days ?
103 if ( $unit eq 'hours' ) {
104 # Fixed for legacy support. Should be set as a branch parameter
105 Readonly
::Scalar
my $return_by_hour => 10;
107 $dt = $self->addHours($startdate, $add_duration, $return_by_hour);
110 $dt = $self->addDays($startdate, $add_duration);
117 my ( $self, $startdate, $hours_duration, $return_by_hour ) = @_;
118 my $base_date = $startdate->clone();
120 $base_date->add_duration($hours_duration);
122 # If we are using the calendar behave for now as if Datedue
123 # was the chosen option (current intended behaviour)
125 if ( $self->{days_mode
} ne 'Days' &&
126 $self->is_holiday($base_date) ) {
128 if ( $hours_duration->is_negative() ) {
129 $base_date = $self->prev_open_day($base_date);
131 $base_date = $self->next_open_day($base_date);
134 $base_date->set_hour($return_by_hour);
142 my ( $self, $startdate, $days_duration ) = @_;
143 my $base_date = $startdate->clone();
145 if ( $self->{days_mode
} eq 'Calendar' ) {
146 # use the calendar to skip all days the library is closed
148 my $days = abs $days_duration->in_units('days');
150 if ( $days_duration->is_negative() ) {
152 $base_date = $self->prev_open_day($base_date);
157 $base_date = $self->next_open_day($base_date);
162 } else { # Days or Datedue
163 # use straight days, then use calendar to push
164 # the date to the next open day if Datedue
165 $base_date->add_duration($days_duration);
167 if ( $self->{days_mode
} eq 'Datedue' ) {
168 # Datedue, then use the calendar to push
169 # the date to the next open day if holiday
170 if ( $self->is_holiday($base_date) ) {
171 if ( $days_duration->is_negative() ) {
172 $base_date = $self->prev_open_day($base_date);
174 $base_date = $self->next_open_day($base_date);
184 my ( $self, $dt ) = @_;
185 my $localdt = $dt->clone();
186 my $day = $localdt->day;
187 my $month = $localdt->month;
189 $localdt->truncate( to
=> 'day' );
191 if ( $self->{exception_holidays
}->contains($localdt) ) {
192 # exceptions are not holidays
196 my $dow = $localdt->day_of_week;
198 # TODO: Shouldn't we shift the rest of the $dow also?
203 if ( $self->{weekly_closed_days
}->[$dow] == 1 ) {
207 if ( exists $self->{day_month_closed_days
}->{$month}->{$day} ) {
211 if ( $self->{single_holidays
}->contains($localdt) ) {
215 # damn have to go to work after all
220 my ( $self, $dt ) = @_;
221 my $base_date = $dt->clone();
223 $base_date->add(days
=> 1);
225 while ($self->is_holiday($base_date)) {
226 $base_date->add(days
=> 1);
233 my ( $self, $dt ) = @_;
234 my $base_date = $dt->clone();
236 $base_date->add(days
=> -1);
238 while ($self->is_holiday($base_date)) {
239 $base_date->add(days
=> -1);
247 my $start_dt = shift;
250 if ( $start_dt->compare($end_dt) > 0 ) {
252 my $int_dt = $end_dt;
258 # start and end should not be closed days
259 my $days = $start_dt->delta_days($end_dt)->delta_days;
260 for (my $dt = $start_dt->clone();
264 if ($self->is_holiday($dt)) {
268 return DateTime
::Duration
->new( days
=> $days );
273 my ($self, $start_date, $end_date) = @_;
274 my $start_dt = $start_date->clone();
275 my $end_dt = $end_date->clone();
276 my $duration = $end_dt->delta_ms($start_dt);
277 $start_dt->truncate( to
=> 'day' );
278 $end_dt->truncate( to
=> 'day' );
279 # NB this is a kludge in that it assumes all days are 24 hours
280 # However for hourly loans the logic should be expanded to
281 # take into account open/close times then it would be a duration
282 # of library open hours
283 my $skipped_days = 0;
284 for (my $dt = $start_dt->clone();
288 if ($self->is_holiday($dt)) {
293 $duration->subtract_duration(DateTime
::Duration
->new( hours
=> 24 * $skipped_days));
302 $self->{weekly_closed_days
} = [ 1, 0, 0, 0, 0, 0, 0 ]; # Sunday only
303 $self->{day_month_closed_days
} = { 6 => { 16 => 1, } };
305 $self->{exception_holidays
} =
306 DateTime
::Set
->from_datetimes( dates
=> $dates );
307 my $special = DateTime
->new(
311 time_zone
=> 'Europe/London',
313 push @
{$dates}, $special;
314 $self->{single_holidays
} = DateTime
::Set
->from_datetimes( dates
=> $dates );
316 # if not defined, days_mode defaults to 'Calendar'
317 if ( !defined($self->{days_mode
}) ) {
318 $self->{days_mode
} = 'Calendar';
326 my ( $self, $mode ) = @_;
328 # if not testing this is a no op
329 if ( $self->{test
} ) {
330 $self->{days_mode
} = $mode;
336 sub clear_weekly_closed_days
{
338 $self->{weekly_closed_days
} = [ 0, 0, 0, 0, 0, 0, 0 ]; # Sunday only
345 my @dt = $self->{single_holidays
}->as_list;
347 $self->{single_holidays
} =
348 DateTime
::Set
->from_datetimes( dates
=> \
@dt );
358 Koha::Calendar - Object containing a branches calendar
362 This documentation refers to Koha::Calendar version 0.0.1
368 my $c = Koha::Calendar->new( branchcode => 'MAIN' );
369 my $dt = DateTime->now();
372 $open = $c->is_holiday($dt);
373 # when will item be due if loan period = $dur (a DateTime::Duration object)
374 $duedate = $c->addDate($dt,$dur,'days');
379 Implements those features of C4::Calendar needed for Staffs Rolling Loans
383 =head2 new : Create a calendar object
385 my $calendar = Koha::Calendar->new( branchcode => 'MAIN' );
387 The option branchcode is required
392 my $dt = $calendar->addDate($date, $dur, $unit)
394 C<$date> is a DateTime object representing the starting date of the interval.
396 C<$offset> is a DateTime::Duration to add to it
398 C<$unit> is a string value 'days' or 'hours' toflag granularity of duration
400 Currently unit is only used to invoke Staffs return Monday at 10 am rule this
401 parameter will be removed when issuingrules properly cope with that
406 my $dt = $calendar->addHours($date, $dur, $return_by_hour )
408 C<$date> is a DateTime object representing the starting date of the interval.
410 C<$offset> is a DateTime::Duration to add to it
412 C<$return_by_hour> is an integer value representing the opening hour for the branch
417 my $dt = $calendar->addDays($date, $dur)
419 C<$date> is a DateTime object representing the starting date of the interval.
421 C<$offset> is a DateTime::Duration to add to it
423 C<$unit> is a string value 'days' or 'hours' toflag granularity of duration
425 Currently unit is only used to invoke Staffs return Monday at 10 am rule this
426 parameter will be removed when issuingrules properly cope with that
431 $yesno = $calendar->is_holiday($dt);
433 passed a DateTime object returns 1 if it is a closed day
434 0 if not according to the calendar
438 $duration = $calendar->days_between($start_dt, $end_dt);
440 Passed two dates returns a DateTime::Duration object measuring the length between them
441 ignoring closed days. Always returns a positive number irrespective of the
442 relative order of the parameters
446 $datetime = $calendar->next_open_day($duedate_dt)
448 Passed a Datetime returns another Datetime representing the next open day. It is
449 intended for use to calculate the due date when useDaysMode syspref is set to either
450 'Datedue' or 'Calendar'.
454 $datetime = $calendar->prev_open_day($duedate_dt)
456 Passed a Datetime returns another Datetime representing the previous open day. It is
457 intended for use to calculate the due date when useDaysMode syspref is set to either
458 'Datedue' or 'Calendar'.
462 For testing only allows the calling script to change days mode
464 =head2 clear_weekly_closed_days
466 In test mode changes the testing set of closed days to a new set with
467 no closed days. TODO passing an array of closed days to this would
468 allow testing of more configurations
472 Passed a datetime object this will add it to the calendar's list of
473 closed days. This is for testing so that we can alter the Calenfar object's
474 list of specified dates
478 Will croak if not passed a branchcode in new
480 =head1 BUGS AND LIMITATIONS
482 This only contains a limited subset of the functionality in C4::Calendar
483 Only enough to support Staffs Rolling loans
487 Colin Campbell colin.campbell@ptfs-europe.com
489 =head1 LICENSE AND COPYRIGHT
491 Copyright (c) 2011 PTFS-Europe Ltd All rights reserved
493 This program is free software: you can redistribute it and/or modify
494 it under the terms of the GNU General Public License as published by
495 the Free Software Foundation, either version 2 of the License, or
496 (at your option) any later version.
498 This program is distributed in the hope that it will be useful,
499 but WITHOUT ANY WARRANTY; without even the implied warranty of
500 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
501 GNU General Public License for more details.
503 You should have received a copy of the GNU General Public License
504 along with this program. If not, see <http://www.gnu.org/licenses/>.