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 $repeat_sth = $dbh->prepare(
37 'SELECT * from repeatable_holidays WHERE branchcode = ? AND ISNULL(weekday) = ?'
39 $repeat_sth->execute( $branch, 0 );
40 $self->{weekly_closed_days} = [ 0, 0, 0, 0, 0, 0, 0 ];
41 Readonly::Scalar my $sunday => 7;
42 while ( my $tuple = $repeat_sth->fetchrow_hashref ) {
43 $self->{weekly_closed_days}->[ $tuple->{weekday} ] = 1;
45 $repeat_sth->execute( $branch, 1 );
46 $self->{day_month_closed_days} = {};
47 while ( my $tuple = $repeat_sth->fetchrow_hashref ) {
48 $self->{day_month_closed_days}->{ $tuple->{month} }->{ $tuple->{day} } =
52 my $special = $dbh->prepare(
53 'SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = ?'
55 $special->execute( $branch, 1 );
57 while ( my ( $day, $month, $year ) = $special->fetchrow ) {
63 time_zone => C4::Context->tz()
64 )->truncate( to => 'day' );
66 $self->{exception_holidays} =
67 DateTime::Set->from_datetimes( dates => $dates );
69 $special->execute( $branch, 0 );
71 while ( my ( $day, $month, $year ) = $special->fetchrow ) {
77 time_zone => C4::Context->tz()
78 )->truncate( to => 'day' );
80 $self->{single_holidays} = DateTime::Set->from_datetimes( dates => $dates );
81 $self->{days_mode} = C4::Context->preference('useDaysMode');
87 my ( $self, $startdate, $add_duration, $unit ) = @_;
89 # Default to days duration (legacy support I guess)
90 if ( ref $add_duration ne 'DateTime::Duration' ) {
91 $add_duration = DateTime::Duration->new( days => $add_duration );
94 $unit ||= 'days'; # default days ?
97 if ( $unit eq 'hours' ) {
98 # Fixed for legacy support. Should be set as a branch parameter
99 Readonly::Scalar my $return_by_hour => 10;
101 $dt = $self->addHours($startdate, $add_duration, $return_by_hour);
104 $dt = $self->addDays($startdate, $add_duration);
111 my ( $self, $startdate, $hours_duration, $return_by_hour ) = @_;
112 my $base_date = $startdate->clone();
114 $base_date->add_duration($hours_duration);
116 # If we are using the calendar behave for now as if Datedue
117 # was the chosen option (current intended behaviour)
119 if ( $self->{days_mode} ne 'Days' &&
120 $self->is_holiday($base_date) ) {
122 if ( $hours_duration->is_negative() ) {
123 $base_date = $self->prev_open_day($base_date);
125 $base_date = $self->next_open_day($base_date);
128 $base_date->set_hour($return_by_hour);
136 my ( $self, $startdate, $days_duration ) = @_;
137 my $base_date = $startdate->clone();
139 if ( $self->{days_mode} eq 'Calendar' ) {
140 # use the calendar to skip all days the library is closed
142 my $days = abs $days_duration->in_units('days');
144 if ( $days_duration->is_negative() ) {
146 $base_date = $self->prev_open_day($base_date);
151 $base_date = $self->next_open_day($base_date);
156 } else { # Days or Datedue
157 # use straight days, then use calendar to push
158 # the date to the next open day if Datedue
159 $base_date->add_duration($days_duration);
161 if ( $self->{days_mode} eq 'Datedue' ) {
162 # Datedue, then use the calendar to push
163 # the date to the next open day if holiday
164 if ( $self->is_holiday($base_date) ) {
165 if ( $days_duration->is_negative() ) {
166 $base_date = $self->prev_open_day($base_date);
168 $base_date = $self->next_open_day($base_date);
178 my ( $self, $dt ) = @_;
179 my $localdt = $dt->clone();
180 my $dow = $localdt->day_of_week;
184 if ( $self->{weekly_closed_days}->[$dow] == 1 ) {
187 $localdt->truncate( to => 'day' );
188 my $day = $localdt->day;
189 my $month = $localdt->month;
190 if ( exists $self->{day_month_closed_days}->{$month}->{$day} ) {
193 if ( $self->{exception_holidays}->contains($localdt) ) {
196 if ( $self->{single_holidays}->contains($localdt) ) {
200 # damn have to go to work after all
205 my ( $self, $dt ) = @_;
206 my $base_date = $dt->clone();
208 $base_date->add(days => 1);
210 while ($self->is_holiday($base_date)) {
211 $base_date->add(days => 1);
218 my ( $self, $dt ) = @_;
219 my $base_date = $dt->clone();
221 $base_date->add(days => -1);
223 while ($self->is_holiday($base_date)) {
224 $base_date->add(days => -1);
232 my $start_dt = shift;
236 # start and end should not be closed days
237 my $days = $start_dt->delta_days($end_dt)->delta_days;
238 for (my $dt = $start_dt->clone();
242 if ($self->is_holiday($dt)) {
246 return DateTime::Duration->new( days => $days );
251 my ($self, $start_date, $end_date) = @_;
252 my $start_dt = $start_date->clone();
253 my $end_dt = $end_date->clone();
254 my $duration = $end_dt->delta_ms($start_dt);
255 $start_dt->truncate( to => 'day' );
256 $end_dt->truncate( to => 'day' );
257 # NB this is a kludge in that it assumes all days are 24 hours
258 # However for hourly loans the logic should be expanded to
259 # take into account open/close times then it would be a duration
260 # of library open hours
261 my $skipped_days = 0;
262 for (my $dt = $start_dt->clone();
266 if ($self->is_holiday($dt)) {
271 $duration->subtract_duration(DateTime::Duration->new( hours => 24 * $skipped_days));
280 $self->{weekly_closed_days} = [ 1, 0, 0, 0, 0, 0, 0 ]; # Sunday only
281 $self->{day_month_closed_days} = { 6 => { 16 => 1, } };
283 $self->{exception_holidays} =
284 DateTime::Set->from_datetimes( dates => $dates );
285 my $special = DateTime->new(
289 time_zone => 'Europe/London',
291 push @{$dates}, $special;
292 $self->{single_holidays} = DateTime::Set->from_datetimes( dates => $dates );
294 # if not defined, days_mode defaults to 'Calendar'
295 if ( !defined($self->{days_mode}) ) {
296 $self->{days_mode} = 'Calendar';
304 my ( $self, $mode ) = @_;
306 # if not testing this is a no op
307 if ( $self->{test} ) {
308 $self->{days_mode} = $mode;
314 sub clear_weekly_closed_days {
316 $self->{weekly_closed_days} = [ 0, 0, 0, 0, 0, 0, 0 ]; # Sunday only
323 my @dt = $self->{exception_holidays}->as_list;
325 $self->{exception_holidays} =
326 DateTime::Set->from_datetimes( dates => \@dt );
336 Koha::Calendar - Object containing a branches calendar
340 This documentation refers to Koha::Calendar version 0.0.1
346 my $c = Koha::Calendar->new( branchcode => 'MAIN' );
347 my $dt = DateTime->now();
350 $open = $c->is_holiday($dt);
351 # when will item be due if loan period = $dur (a DateTime::Duration object)
352 $duedate = $c->addDate($dt,$dur,'days');
357 Implements those features of C4::Calendar needed for Staffs Rolling Loans
361 =head2 new : Create a calendar object
363 my $calendar = Koha::Calendar->new( branchcode => 'MAIN' );
365 The option branchcode is required
370 my $dt = $calendar->addDate($date, $dur, $unit)
372 C<$date> is a DateTime object representing the starting date of the interval.
374 C<$offset> is a DateTime::Duration to add to it
376 C<$unit> is a string value 'days' or 'hours' toflag granularity of duration
378 Currently unit is only used to invoke Staffs return Monday at 10 am rule this
379 parameter will be removed when issuingrules properly cope with that
384 my $dt = $calendar->addHours($date, $dur, $return_by_hour )
386 C<$date> is a DateTime object representing the starting date of the interval.
388 C<$offset> is a DateTime::Duration to add to it
390 C<$return_by_hour> is an integer value representing the opening hour for the branch
395 my $dt = $calendar->addDays($date, $dur)
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 $yesno = $calendar->is_holiday($dt);
411 passed a DateTime object returns 1 if it is a closed day
412 0 if not according to the calendar
416 $duration = $calendar->days_between($start_dt, $end_dt);
418 Passed two dates returns a DateTime::Duration object measuring the length between them
419 ignoring closed days. Always returns a positive number irrespective of the
420 relative order of the parameters
424 $datetime = $calendar->next_open_day($duedate_dt)
426 Passed a Datetime returns another Datetime representing the next open day. It is
427 intended for use to calculate the due date when useDaysMode syspref is set to either
428 'Datedue' or 'Calendar'.
432 $datetime = $calendar->prev_open_day($duedate_dt)
434 Passed a Datetime returns another Datetime representing the previous open day. It is
435 intended for use to calculate the due date when useDaysMode syspref is set to either
436 'Datedue' or 'Calendar'.
440 For testing only allows the calling script to change days mode
442 =head2 clear_weekly_closed_days
444 In test mode changes the testing set of closed days to a new set with
445 no closed days. TODO passing an array of closed days to this would
446 allow testing of more configurations
450 Passed a datetime object this will add it to the calendar's list of
451 closed days. This is for testing so that we can alter the Calenfar object's
452 list of specified dates
456 Will croak if not passed a branchcode in new
458 =head1 BUGS AND LIMITATIONS
460 This only contains a limited subset of the functionality in C4::Calendar
461 Only enough to support Staffs Rolling loans
465 Colin Campbell colin.campbell@ptfs-europe.com
467 =head1 LICENSE AND COPYRIGHT
469 Copyright (c) 2011 PTFS-Europe Ltd All rights reserved
471 This program is free software: you can redistribute it and/or modify
472 it under the terms of the GNU General Public License as published by
473 the Free Software Foundation, either version 2 of the License, or
474 (at your option) any later version.
476 This program is distributed in the hope that it will be useful,
477 but WITHOUT ANY WARRANTY; without even the implied warranty of
478 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
479 GNU General Public License for more details.
481 You should have received a copy of the GNU General Public License
482 along with this program. If not, see <http://www.gnu.org/licenses/>.