1 package Koha::Calendar;
13 my ( $classname, %options ) = @_;
15 for my $o_name ( keys %options ) {
17 $self->{$o} = $options{$o_name};
19 if ( !defined $self->{branchcode} ) {
20 croak 'No branchcode argument passed to Koha::Calendar->new';
22 bless $self, $classname;
29 my $branch = $self->{branchcode};
30 my $dbh = C4::Context->dbh();
31 my $repeat_sth = $dbh->prepare(
32 'SELECT * from repeatable_holidays WHERE branchcode = ? AND ISNULL(weekday) = ?'
34 $repeat_sth->execute( $branch, 0 );
35 $self->{weekly_closed_days} = [];
36 Readonly::Scalar my $sunday => 7;
37 while ( my $tuple = $repeat_sth->fetchrow_hashref ) {
38 my $day = $tuple->{weekday} == 0 ? $sunday : $tuple->{weekday};
39 push @{ $self->{weekly_closed_days} }, $day;
41 $repeat_sth->execute( $branch, 1 );
42 $self->{day_month_closed_days} = [];
43 while ( my $tuple = $repeat_sth->fetchrow_hashref ) {
44 push @{ $self->{day_month_closed_days} },
45 { day => $tuple->{day}, month => $tuple->{month}, };
47 my $special = $dbh->prepare(
48 'SELECT day, month, year, title, description FROM special_holidays WHERE ( branchcode = ? ) AND (isexception = ?)'
50 $special->execute( $branch, 1 );
52 while ( my ( $day, $month, $year, $title, $description ) =
53 $special->fetchrow ) {
59 time_zone => C4::Context->tz()
60 )->truncate( to => 'day' );
62 $self->{exception_holidays} =
63 DateTime::Set->from_datetimes( dates => $dates );
64 $special->execute( $branch, 1 );
66 while ( my ( $day, $month, $year, $title, $description ) =
67 $special->fetchrow ) {
73 time_zone => C4::Context->tz()
74 )->truncate( to => 'day' );
76 $self->{single_holidays} = DateTime::Set->from_datetimes( dates => $dates );
81 my ( $self, $base_date, $add_duration, $unit ) = @_;
82 my $days_mode = C4::Context->preference('useDaysMode');
83 Readonly::Scalar my $return_by_hour => 10;
84 my $day_dur = DateTime::Duration->new( days => 1);
85 if ($add_duration->is_negative()) {
88 if ( $days_mode eq 'Datedue' ) {
90 my $dt = $base_date + $add_duration;
91 while ( $self->is_holiday($dt) ) {
93 # TODOP if hours set to 10 am
94 $dt->add_duration( $day_dur );
95 if ( $unit eq 'hours' ) {
96 $dt->set_hour($return_by_hour); # Staffs specific
100 } elsif ( $days_mode eq 'Calendar' ) {
101 my $days = $add_duration->in_units('days');
103 $base_date->add_duration( $day_dur );
104 if ( $self->is_holiday($base_date) ) {
110 if ( $unit eq 'hours' ) {
111 my $dt = $base_date->clone()->subtract( days => 1 );
112 if ( $self->is_holiday($dt) ) {
113 $base_date->set_hour($return_by_hour); # Staffs specific
118 return $base_date + $add_duration;
123 my ( $self, $dt ) = @_;
124 my $dow = $dt->day_of_week;
125 my @matches = grep { $_ == $dow } @{ $self->{weekly_closed_days} };
129 $dt->truncate(to => 'days');
131 my $month = $dt->month;
132 for my $dm ( @{ $self->{day_month_closed_days} } ) {
133 if ( $month == $dm->{month} && $day == $dm->{day} ) {
137 if ( $self->{exception_holidays}->contains($dt) ) {
140 if ( $self->{single_holidays}->contains($dt) ) {
144 # damn have to go to work after all
153 Koha::Calendar - Object containing a branches calendar
157 This documentation refers to Koha::Calendar version 0.0.1
163 my $c = Koha::Calender->new( branchcode => 'MAIN' );
164 my $dt = DateTime->now();
167 $open = $c->is_holiday($dt);
168 # when will item be due if loan period = $dur (a DateTime::Duration object)
169 $duedate = $c->addDate($dt,$dur,'days');
174 Implements those features of C4::Calendar needed for Staffs Rolling Loans
178 =head2 new : Create a calendar object
180 my $calendar = Koha::Calendar->new( branchcode => 'MAIN' );
182 The option branchcode is required
187 my $dt = $calendar->addDate($date, $dur, $unit)
189 C<$date> is a DateTime object representing the starting date of the interval.
191 C<$offset> is a DateTime::Duration to add to it
193 C<$unit> is a string value 'days' or 'hours' toflag granularity of duration
195 Currently unit is only used to invoke Staffs return Monday at 10 am rule this
196 parameter will be removed when issuingrules properly cope with that
201 $yesno = $calendar->is_holiday($dt);
203 passed at DateTime object returns 1 if it is a closed day
204 0 if not according to the calendar
208 Will croak if not passed a branchcode in new
210 =head1 BUGS AND LIMITATIONS
212 This only contains a limited subset of the functionality in C4::Calendar
213 Only enough to support Staffs Rolling loans
217 Colin Campbell colin.campbell@ptfs-europe.com
219 =head1 LICENSE AND COPYRIGHT
221 Copyright (c) 2011 PTFS-Europe Ltd All rights reserved
223 This program is free software: you can redistribute it and/or modify
224 it under the terms of the GNU General Public License as published by
225 the Free Software Foundation, either version 2 of the License, or
226 (at your option) any later version.
228 This program is distributed in the hope that it will be useful,
229 but WITHOUT ANY WARRANTY; without even the implied warranty of
230 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
231 GNU General Public License for more details.
233 You should have received a copy of the GNU General Public License
234 along with this program. If not, see <http://www.gnu.org/licenses/>.