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 my $exception_holidays_sth = $dbh->prepare(
52 'SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 1'
54 $exception_holidays_sth->execute( $branch );
56 while ( my ( $day, $month, $year ) = $exception_holidays_sth->fetchrow ) {
62 time_zone => C4::Context->tz()
63 )->truncate( to => 'day' );
65 $self->{exception_holidays} =
66 DateTime::Set->from_datetimes( dates => $dates );
68 my $single_holidays_sth = $dbh->prepare(
69 'SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 0'
71 $single_holidays_sth->execute( $branch );
73 while ( my ( $day, $month, $year ) = $single_holidays_sth->fetchrow ) {
79 time_zone => C4::Context->tz()
80 )->truncate( to => 'day' );
82 $self->{single_holidays} = DateTime::Set->from_datetimes( dates => $dates );
83 $self->{days_mode} = C4::Context->preference('useDaysMode');
89 my ( $self, $startdate, $add_duration, $unit ) = @_;
91 # Default to days duration (legacy support I guess)
92 if ( ref $add_duration ne 'DateTime::Duration' ) {
93 $add_duration = DateTime::Duration->new( days => $add_duration );
96 $unit ||= 'days'; # default days ?
99 if ( $unit eq 'hours' ) {
100 # Fixed for legacy support. Should be set as a branch parameter
101 Readonly::Scalar my $return_by_hour => 10;
103 $dt = $self->addHours($startdate, $add_duration, $return_by_hour);
106 $dt = $self->addDays($startdate, $add_duration);
113 my ( $self, $startdate, $hours_duration, $return_by_hour ) = @_;
114 my $base_date = $startdate->clone();
116 $base_date->add_duration($hours_duration);
118 # If we are using the calendar behave for now as if Datedue
119 # was the chosen option (current intended behaviour)
121 if ( $self->{days_mode} ne 'Days' &&
122 $self->is_holiday($base_date) ) {
124 if ( $hours_duration->is_negative() ) {
125 $base_date = $self->prev_open_day($base_date);
127 $base_date = $self->next_open_day($base_date);
130 $base_date->set_hour($return_by_hour);
138 my ( $self, $startdate, $days_duration ) = @_;
139 my $base_date = $startdate->clone();
141 if ( $self->{days_mode} eq 'Calendar' ) {
142 # use the calendar to skip all days the library is closed
144 my $days = abs $days_duration->in_units('days');
146 if ( $days_duration->is_negative() ) {
148 $base_date = $self->prev_open_day($base_date);
153 $base_date = $self->next_open_day($base_date);
158 } else { # Days or Datedue
159 # use straight days, then use calendar to push
160 # the date to the next open day if Datedue
161 $base_date->add_duration($days_duration);
163 if ( $self->{days_mode} eq 'Datedue' ) {
164 # Datedue, then use the calendar to push
165 # the date to the next open day if holiday
166 if ( $self->is_holiday($base_date) ) {
167 if ( $days_duration->is_negative() ) {
168 $base_date = $self->prev_open_day($base_date);
170 $base_date = $self->next_open_day($base_date);
180 my ( $self, $dt ) = @_;
181 my $localdt = $dt->clone();
182 my $day = $localdt->day;
183 my $month = $localdt->month;
185 $localdt->truncate( to => 'day' );
187 if ( $self->{exception_holidays}->contains($localdt) ) {
188 # exceptions are not holidays
192 my $dow = $localdt->day_of_week;
194 # TODO: Shouldn't we shift the rest of the $dow also?
199 if ( $self->{weekly_closed_days}->[$dow] == 1 ) {
203 if ( exists $self->{day_month_closed_days}->{$month}->{$day} ) {
207 if ( $self->{single_holidays}->contains($localdt) ) {
211 # damn have to go to work after all
216 my ( $self, $dt ) = @_;
217 my $base_date = $dt->clone();
219 $base_date->add(days => 1);
221 while ($self->is_holiday($base_date)) {
222 $base_date->add(days => 1);
229 my ( $self, $dt ) = @_;
230 my $base_date = $dt->clone();
232 $base_date->add(days => -1);
234 while ($self->is_holiday($base_date)) {
235 $base_date->add(days => -1);
243 my $start_dt = shift;
246 if ( $start_dt->compare($end_dt) > 0 ) {
248 my $int_dt = $end_dt;
254 # start and end should not be closed days
255 my $days = $start_dt->delta_days($end_dt)->delta_days;
256 for (my $dt = $start_dt->clone();
260 if ($self->is_holiday($dt)) {
264 return DateTime::Duration->new( days => $days );
269 my ($self, $start_date, $end_date) = @_;
270 my $start_dt = $start_date->clone();
271 my $end_dt = $end_date->clone();
272 my $duration = $end_dt->delta_ms($start_dt);
273 $start_dt->truncate( to => 'day' );
274 $end_dt->truncate( to => 'day' );
275 # NB this is a kludge in that it assumes all days are 24 hours
276 # However for hourly loans the logic should be expanded to
277 # take into account open/close times then it would be a duration
278 # of library open hours
279 my $skipped_days = 0;
280 for (my $dt = $start_dt->clone();
284 if ($self->is_holiday($dt)) {
289 $duration->subtract_duration(DateTime::Duration->new( hours => 24 * $skipped_days));
297 my ( $self, $mode ) = @_;
299 # if not testing this is a no op
300 if ( $self->{test} ) {
301 $self->{days_mode} = $mode;
307 sub clear_weekly_closed_days {
309 $self->{weekly_closed_days} = [ 0, 0, 0, 0, 0, 0, 0 ]; # Sunday only
316 my @dt = $self->{single_holidays}->as_list;
318 $self->{single_holidays} =
319 DateTime::Set->from_datetimes( dates => \@dt );
329 Koha::Calendar - Object containing a branches calendar
333 This documentation refers to Koha::Calendar version 0.0.1
339 my $c = Koha::Calendar->new( branchcode => 'MAIN' );
340 my $dt = DateTime->now();
343 $open = $c->is_holiday($dt);
344 # when will item be due if loan period = $dur (a DateTime::Duration object)
345 $duedate = $c->addDate($dt,$dur,'days');
350 Implements those features of C4::Calendar needed for Staffs Rolling Loans
354 =head2 new : Create a calendar object
356 my $calendar = Koha::Calendar->new( branchcode => 'MAIN' );
358 The option branchcode is required
363 my $dt = $calendar->addDate($date, $dur, $unit)
365 C<$date> is a DateTime object representing the starting date of the interval.
367 C<$offset> is a DateTime::Duration to add to it
369 C<$unit> is a string value 'days' or 'hours' toflag granularity of duration
371 Currently unit is only used to invoke Staffs return Monday at 10 am rule this
372 parameter will be removed when issuingrules properly cope with that
377 my $dt = $calendar->addHours($date, $dur, $return_by_hour )
379 C<$date> is a DateTime object representing the starting date of the interval.
381 C<$offset> is a DateTime::Duration to add to it
383 C<$return_by_hour> is an integer value representing the opening hour for the branch
388 my $dt = $calendar->addDays($date, $dur)
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 $yesno = $calendar->is_holiday($dt);
404 passed a DateTime object returns 1 if it is a closed day
405 0 if not according to the calendar
409 $duration = $calendar->days_between($start_dt, $end_dt);
411 Passed two dates returns a DateTime::Duration object measuring the length between them
412 ignoring closed days. Always returns a positive number irrespective of the
413 relative order of the parameters
417 $datetime = $calendar->next_open_day($duedate_dt)
419 Passed a Datetime returns another Datetime representing the next open day. It is
420 intended for use to calculate the due date when useDaysMode syspref is set to either
421 'Datedue' or 'Calendar'.
425 $datetime = $calendar->prev_open_day($duedate_dt)
427 Passed a Datetime returns another Datetime representing the previous open day. It is
428 intended for use to calculate the due date when useDaysMode syspref is set to either
429 'Datedue' or 'Calendar'.
433 For testing only allows the calling script to change days mode
435 =head2 clear_weekly_closed_days
437 In test mode changes the testing set of closed days to a new set with
438 no closed days. TODO passing an array of closed days to this would
439 allow testing of more configurations
443 Passed a datetime object this will add it to the calendar's list of
444 closed days. This is for testing so that we can alter the Calenfar object's
445 list of specified dates
449 Will croak if not passed a branchcode in new
451 =head1 BUGS AND LIMITATIONS
453 This only contains a limited subset of the functionality in C4::Calendar
454 Only enough to support Staffs Rolling loans
458 Colin Campbell colin.campbell@ptfs-europe.com
460 =head1 LICENSE AND COPYRIGHT
462 Copyright (c) 2011 PTFS-Europe Ltd All rights reserved
464 This program is free software: you can redistribute it and/or modify
465 it under the terms of the GNU General Public License as published by
466 the Free Software Foundation, either version 2 of the License, or
467 (at your option) any later version.
469 This program is distributed in the hope that it will be useful,
470 but WITHOUT ANY WARRANTY; without even the implied warranty of
471 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
472 GNU General Public License for more details.
474 You should have received a copy of the GNU General Public License
475 along with this program. If not, see <http://www.gnu.org/licenses/>.