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 if ($unit eq 'hours' ) {
102 $base_date->add_duration($add_duration);
103 while ($self->is_holiday($base_date)) {
104 $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
163 Koha::Calendar - Object containing a branches calendar
167 This documentation refers to Koha::Calendar version 0.0.1
173 my $c = Koha::Calender->new( branchcode => 'MAIN' );
174 my $dt = DateTime->now();
177 $open = $c->is_holiday($dt);
178 # when will item be due if loan period = $dur (a DateTime::Duration object)
179 $duedate = $c->addDate($dt,$dur,'days');
184 Implements those features of C4::Calendar needed for Staffs Rolling Loans
188 =head2 new : Create a calendar object
190 my $calendar = Koha::Calendar->new( branchcode => 'MAIN' );
192 The option branchcode is required
197 my $dt = $calendar->addDate($date, $dur, $unit)
199 C<$date> is a DateTime object representing the starting date of the interval.
201 C<$offset> is a DateTime::Duration to add to it
203 C<$unit> is a string value 'days' or 'hours' toflag granularity of duration
205 Currently unit is only used to invoke Staffs return Monday at 10 am rule this
206 parameter will be removed when issuingrules properly cope with that
211 $yesno = $calendar->is_holiday($dt);
213 passed at DateTime object returns 1 if it is a closed day
214 0 if not according to the calendar
218 Will croak if not passed a branchcode in new
220 =head1 BUGS AND LIMITATIONS
222 This only contains a limited subset of the functionality in C4::Calendar
223 Only enough to support Staffs Rolling loans
227 Colin Campbell colin.campbell@ptfs-europe.com
229 =head1 LICENSE AND COPYRIGHT
231 Copyright (c) 2011 PTFS-Europe Ltd All rights reserved
233 This program is free software: you can redistribute it and/or modify
234 it under the terms of the GNU General Public License as published by
235 the Free Software Foundation, either version 2 of the License, or
236 (at your option) any later version.
238 This program is distributed in the hope that it will be useful,
239 but WITHOUT ANY WARRANTY; without even the implied warranty of
240 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
241 GNU General Public License for more details.
243 You should have received a copy of the GNU General Public License
244 along with this program. If not, see <http://www.gnu.org/licenses/>.