1 package Koha::Calendar;
8 use DateTime::Duration;
14 my ( $classname, %options ) = @_;
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';
23 bless $self, $classname;
30 my $branch = $self->{branchcode};
31 my $dbh = C4::Context->dbh();
32 my $repeat_sth = $dbh->prepare(
33 'SELECT * from repeatable_holidays WHERE branchcode = ? AND ISNULL(weekday) = ?'
35 $repeat_sth->execute( $branch, 0 );
36 $self->{weekly_closed_days} = [];
37 Readonly::Scalar my $sunday => 7;
38 while ( my $tuple = $repeat_sth->fetchrow_hashref ) {
39 my $day = $tuple->{weekday} == 0 ? $sunday : $tuple->{weekday};
40 push @{ $self->{weekly_closed_days} }, $day;
42 $repeat_sth->execute( $branch, 1 );
43 $self->{day_month_closed_days} = [];
44 while ( my $tuple = $repeat_sth->fetchrow_hashref ) {
45 push @{ $self->{day_month_closed_days} },
46 { day => $tuple->{day}, month => $tuple->{month}, };
48 my $special = $dbh->prepare(
49 'SELECT day, month, year, title, description FROM special_holidays WHERE ( branchcode = ? ) AND (isexception = ?)'
51 $special->execute( $branch, 1 );
53 while ( my ( $day, $month, $year, $title, $description ) =
54 $special->fetchrow ) {
60 time_zone => C4::Context->tz()
61 )->truncate( to => 'day' );
63 $self->{exception_holidays} =
64 DateTime::Set->from_datetimes( dates => $dates );
65 $special->execute( $branch, 1 );
67 while ( my ( $day, $month, $year, $title, $description ) =
68 $special->fetchrow ) {
74 time_zone => C4::Context->tz()
75 )->truncate( to => 'day' );
77 $self->{single_holidays} = DateTime::Set->from_datetimes( dates => $dates );
82 my ( $self, $base_date, $add_duration, $unit ) = @_;
83 my $days_mode = C4::Context->preference('useDaysMode');
84 Readonly::Scalar my $return_by_hour => 10;
85 my $day_dur = DateTime::Duration->new( days => 1 );
86 if ( $add_duration->is_negative() ) {
89 if ( $days_mode eq 'Datedue' ) {
91 my $dt = $base_date + $add_duration;
92 while ( $self->is_holiday($dt) ) {
94 # TODOP if hours set to 10 am
95 $dt->add_duration($day_dur);
96 if ( $unit eq 'hours' ) {
97 $dt->set_hour($return_by_hour); # Staffs specific
101 } elsif ( $days_mode eq 'Calendar' ) {
102 if ( $unit eq 'hours' ) {
103 $base_date->add_duration($add_duration);
104 while ( $self->is_holiday($base_date) ) {
105 $base_date->add_duration($day_dur);
110 my $days = $add_duration->in_units('days');
112 $base_date->add_duration($day_dur);
113 if ( $self->is_holiday($base_date) ) {
120 if ( $unit eq 'hours' ) {
121 my $dt = $base_date->clone()->subtract( days => 1 );
122 if ( $self->is_holiday($dt) ) {
123 $base_date->set_hour($return_by_hour); # Staffs specific
128 return $base_date + $add_duration;
133 my ( $self, $dt ) = @_;
134 my $dow = $dt->day_of_week;
135 my @matches = grep { $_ == $dow } @{ $self->{weekly_closed_days} };
139 $dt->truncate( to => 'days' );
141 my $month = $dt->month;
142 for my $dm ( @{ $self->{day_month_closed_days} } ) {
143 if ( $month == $dm->{month} && $day == $dm->{day} ) {
147 if ( $self->{exception_holidays}->contains($dt) ) {
150 if ( $self->{single_holidays}->contains($dt) ) {
154 # damn have to go to work after all
160 my $start_dt = shift;
162 $start_dt->truncate( to => 'hours' );
163 $end_dt->truncate( to => 'hours' );
165 # start and end should not be closed days
166 my $duration = $end_dt - $start_dt;
167 $start_dt->truncate( to => 'days' );
168 $end_dt->truncate( to => 'days' );
169 while ( DateTime->compare( $start_dt, $end_dt ) == -1 ) {
170 $start_dt->add( days => 1 );
171 if ( $self->is_holiday($start_dt) ) {
172 $duration->subtract( days => 1 );
184 Koha::Calendar - Object containing a branches calendar
188 This documentation refers to Koha::Calendar version 0.0.1
194 my $c = Koha::Calender->new( branchcode => 'MAIN' );
195 my $dt = DateTime->now();
198 $open = $c->is_holiday($dt);
199 # when will item be due if loan period = $dur (a DateTime::Duration object)
200 $duedate = $c->addDate($dt,$dur,'days');
205 Implements those features of C4::Calendar needed for Staffs Rolling Loans
209 =head2 new : Create a calendar object
211 my $calendar = Koha::Calendar->new( branchcode => 'MAIN' );
213 The option branchcode is required
218 my $dt = $calendar->addDate($date, $dur, $unit)
220 C<$date> is a DateTime object representing the starting date of the interval.
222 C<$offset> is a DateTime::Duration to add to it
224 C<$unit> is a string value 'days' or 'hours' toflag granularity of duration
226 Currently unit is only used to invoke Staffs return Monday at 10 am rule this
227 parameter will be removed when issuingrules properly cope with that
232 $yesno = $calendar->is_holiday($dt);
234 passed at DateTime object returns 1 if it is a closed day
235 0 if not according to the calendar
239 $duration = $calendar->days_between($start_dt, $end_dt);
241 Passed two dates returns a DateTime::Duration object measuring the length between them
246 Will croak if not passed a branchcode in new
248 =head1 BUGS AND LIMITATIONS
250 This only contains a limited subset of the functionality in C4::Calendar
251 Only enough to support Staffs Rolling loans
255 Colin Campbell colin.campbell@ptfs-europe.com
257 =head1 LICENSE AND COPYRIGHT
259 Copyright (c) 2011 PTFS-Europe Ltd All rights reserved
261 This program is free software: you can redistribute it and/or modify
262 it under the terms of the GNU General Public License as published by
263 the Free Software Foundation, either version 2 of the License, or
264 (at your option) any later version.
266 This program is distributed in the hope that it will be useful,
267 but WITHOUT ANY WARRANTY; without even the implied warranty of
268 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
269 GNU General Public License for more details.
271 You should have received a copy of the GNU General Public License
272 along with this program. If not, see <http://www.gnu.org/licenses/>.