Bug 5549 : Add days_between method to Koha::Calendar
[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 DateTime::Duration;
9 use C4::Context;
10 use Carp;
11 use Readonly;
12
13 sub new {
14     my ( $classname, %options ) = @_;
15     my $self = {};
16     for my $o_name ( keys %options ) {
17         my $o = lc $o_name;
18         $self->{$o} = $options{$o_name};
19     }
20     if ( !defined $self->{branchcode} ) {
21         croak 'No branchcode argument passed to Koha::Calendar->new';
22     }
23     bless $self, $classname;
24     $self->_init();
25     return $self;
26 }
27
28 sub _init {
29     my $self       = shift;
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) = ?'
34     );
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;
41     }
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}, };
47     }
48     my $special = $dbh->prepare(
49 'SELECT day, month, year, title, description FROM special_holidays WHERE ( branchcode = ? ) AND (isexception = ?)'
50     );
51     $special->execute( $branch, 1 );
52     my $dates = [];
53     while ( my ( $day, $month, $year, $title, $description ) =
54         $special->fetchrow ) {
55         push @{$dates},
56           DateTime->new(
57             day       => $day,
58             month     => $month,
59             year      => $year,
60             time_zone => C4::Context->tz()
61           )->truncate( to => 'day' );
62     }
63     $self->{exception_holidays} =
64       DateTime::Set->from_datetimes( dates => $dates );
65     $special->execute( $branch, 1 );
66     $dates = [];
67     while ( my ( $day, $month, $year, $title, $description ) =
68         $special->fetchrow ) {
69         push @{$dates},
70           DateTime->new(
71             day       => $day,
72             month     => $month,
73             year      => $year,
74             time_zone => C4::Context->tz()
75           )->truncate( to => 'day' );
76     }
77     $self->{single_holidays} = DateTime::Set->from_datetimes( dates => $dates );
78     return;
79 }
80
81 sub addDate {
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() ) {
87         $day_dur->inverse();
88     }
89     if ( $days_mode eq 'Datedue' ) {
90
91         my $dt = $base_date + $add_duration;
92         while ( $self->is_holiday($dt) ) {
93
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
98             }
99         }
100         return $dt;
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);
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 sub days_between {
159     my $self     = shift;
160     my $start_dt = shift;
161     my $end_dt   = shift;
162     $start_dt->truncate( to => 'hours' );
163     $end_dt->truncate( to => 'hours' );
164
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 );
173         }
174     }
175     return $duration;
176
177 }
178
179 1;
180 __END__
181
182 =head1 NAME
183
184 Koha::Calendar - Object containing a branches calendar
185
186 =head1 VERSION
187
188 This documentation refers to Koha::Calendar version 0.0.1
189
190 =head1 SYNOPSIS
191
192   use Koha::Calendat
193
194   my $c = Koha::Calender->new( branchcode => 'MAIN' );
195   my $dt = DateTime->now();
196
197   # are we open
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');
201
202
203 =head1 DESCRIPTION
204
205   Implements those features of C4::Calendar needed for Staffs Rolling Loans
206
207 =head1 METHODS
208
209 =head2 new : Create a calendar object
210
211 my $calendar = Koha::Calendar->new( branchcode => 'MAIN' );
212
213 The option branchcode is required
214
215
216 =head2 addDate
217
218     my $dt = $calendar->addDate($date, $dur, $unit)
219
220 C<$date> is a DateTime object representing the starting date of the interval.
221
222 C<$offset> is a DateTime::Duration to add to it
223
224 C<$unit> is a string value 'days' or 'hours' toflag granularity of duration
225
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
228
229
230 =head2 is_holiday
231
232 $yesno = $calendar->is_holiday($dt);
233
234 passed at DateTime object returns 1 if it is a closed day
235 0 if not according to the calendar
236
237 =head2 days_between
238
239 $duration = $calendar->days_between($start_dt, $end_dt);
240
241 Passed two dates returns a DateTime::Duration object measuring the length between them
242 ignoring closed days
243
244 =head1 DIAGNOSTICS
245
246 Will croak if not passed a branchcode in new
247
248 =head1 BUGS AND LIMITATIONS
249
250 This only contains a limited subset of the functionality in C4::Calendar
251 Only enough to support Staffs Rolling loans
252
253 =head1 AUTHOR
254
255 Colin Campbell colin.campbell@ptfs-europe.com
256
257 =head1 LICENSE AND COPYRIGHT
258
259 Copyright (c) 2011 PTFS-Europe Ltd All rights reserved
260
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.
265
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.
270
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/>.