Bug 5549 : Get basic 24 Hr loan working
[koha.git] / Koha / Calendar.pm
1 package Koha::Calendar;
2 use strict;
3 use warnings;
4 use 5.010;
5
6 use DateTime;
7 use DateTime::Set;
8 use C4::Context;
9 use Carp;
10 use Readonly;
11
12 sub new {
13     my ( $classname, %options ) = @_;
14     my $self = {};
15     for my $o_name ( keys %options ) {
16         my $o = lc $o_name;
17         $self->{$o} = $options{$o_name};
18     }
19     if ( !defined $self->{branchcode} ) {
20         croak 'No branchcode argument passed to Koha::Calendar->new';
21     }
22     bless $self, $classname;
23     $self->_init();
24     return $self;
25 }
26
27 sub _init {
28     my $self       = shift;
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) = ?'
33     );
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;
40     }
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}, };
46     }
47     my $special = $dbh->prepare(
48 'SELECT day, month, year, title, description FROM special_holidays WHERE ( branchcode = ? ) AND (isexception = ?)'
49     );
50     $special->execute( $branch, 1 );
51     my $dates = [];
52     while ( my ( $day, $month, $year, $title, $description ) =
53         $special->fetchrow ) {
54         push @{$dates},
55           DateTime->new(
56             day       => $day,
57             month     => $month,
58             year      => $year,
59             time_zone => C4::Context->tz()
60           )->truncate( to => 'day' );
61     }
62     $self->{exception_holidays} =
63       DateTime::Set->from_datetimes( dates => $dates );
64     $special->execute( $branch, 1 );
65     $dates = [];
66     while ( my ( $day, $month, $year, $title, $description ) =
67         $special->fetchrow ) {
68         push @{$dates},
69           DateTime->new(
70             day       => $day,
71             month     => $month,
72             year      => $year,
73             time_zone => C4::Context->tz()
74           )->truncate( to => 'day' );
75     }
76     $self->{single_holidays} = DateTime::Set->from_datetimes( dates => $dates );
77     return;
78 }
79
80 sub addDate {
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()) {
86         $day_dur->inverse();
87     }
88     if ( $days_mode eq 'Datedue' ) {
89
90         my $dt = $base_date + $add_duration;
91         while ( $self->is_holiday($dt) ) {
92
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
97             }
98         }
99         return $dt;
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 );
105
106             }
107
108         }
109         else {
110         my $days = $add_duration->in_units('days');
111         while ($days) {
112             $base_date->add_duration( $day_dur );
113             if ( $self->is_holiday($base_date) ) {
114                 next;
115             } else {
116                 --$days;
117             }
118         }
119     }
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
124             }
125         }
126         return $base_date;
127     } else {    # Days
128         return $base_date + $add_duration;
129     }
130 }
131
132 sub is_holiday {
133     my ( $self, $dt ) = @_;
134     my $dow = $dt->day_of_week;
135     my @matches = grep { $_ == $dow } @{ $self->{weekly_closed_days} };
136     if (@matches) {
137         return 1;
138     }
139     $dt->truncate(to => 'days');
140     my $day   = $dt->day;
141     my $month = $dt->month;
142     for my $dm ( @{ $self->{day_month_closed_days} } ) {
143         if ( $month == $dm->{month} && $day == $dm->{day} ) {
144             return 1;
145         }
146     }
147     if ( $self->{exception_holidays}->contains($dt) ) {
148         return 1;
149     }
150     if ( $self->{single_holidays}->contains($dt) ) {
151         return 1;
152     }
153
154     # damn have to go to work after all
155     return 0;
156 }
157
158 1;
159 __END__
160
161 =head1 NAME
162
163 Koha::Calendar - Object containing a branches calendar
164
165 =head1 VERSION
166
167 This documentation refers to Koha::Calendar version 0.0.1
168
169 =head1 SYNOPSIS
170
171   use Koha::Calendat
172
173   my $c = Koha::Calender->new( branchcode => 'MAIN' );
174   my $dt = DateTime->now();
175
176   # are we open
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');
180
181
182 =head1 DESCRIPTION
183
184   Implements those features of C4::Calendar needed for Staffs Rolling Loans
185
186 =head1 METHODS
187
188 =head2 new : Create a calendar object
189
190 my $calendar = Koha::Calendar->new( branchcode => 'MAIN' );
191
192 The option branchcode is required
193
194
195 =head2 addDate
196
197     my $dt = $calendar->addDate($date, $dur, $unit)
198
199 C<$date> is a DateTime object representing the starting date of the interval.
200
201 C<$offset> is a DateTime::Duration to add to it
202
203 C<$unit> is a string value 'days' or 'hours' toflag granularity of duration
204
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
207
208
209 =head2 is_holiday
210
211 $yesno = $calendar->is_holiday($dt);
212
213 passed at DateTime object returns 1 if it is a closed day
214 0 if not according to the calendar
215
216 =head1 DIAGNOSTICS
217
218 Will croak if not passed a branchcode in new
219
220 =head1 BUGS AND LIMITATIONS
221
222 This only contains a limited subset of the functionality in C4::Calendar
223 Only enough to support Staffs Rolling loans
224
225 =head1 AUTHOR
226
227 Colin Campbell colin.campbell@ptfs-europe.com
228
229 =head1 LICENSE AND COPYRIGHT
230
231 Copyright (c) 2011 PTFS-Europe Ltd All rights reserved
232
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.
237
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.
242
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/>.