Bug 12063: Change date calculation for reserve expiration to skip all holiday
[koha_ffzg] / Koha / Hold.pm
1 package Koha::Hold;
2
3 # Copyright ByWater Solutions 2014
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21
22 use Carp;
23 use Data::Dumper qw(Dumper);
24
25 use C4::Context qw(preference);
26 use C4::Log;
27
28 use Koha::DateUtils qw(dt_from_string output_pref);
29 use Koha::Patrons;
30 use Koha::Biblios;
31 use Koha::Items;
32 use Koha::Libraries;
33
34 use base qw(Koha::Object);
35
36 =head1 NAME
37
38 Koha::Hold - Koha Hold object class
39
40 =head1 API
41
42 =head2 Class Methods
43
44 =cut
45
46 =head3 suspend_hold
47
48 my $hold = $hold->suspend_hold( $suspend_until_dt );
49
50 =cut
51
52 sub suspend_hold {
53     my ( $self, $dt ) = @_;
54
55     $dt = $dt ? $dt->clone()->truncate( to => 'day' ) : undef;
56
57     if ( $self->is_waiting ) {    # We can't suspend waiting holds
58         carp "Unable to suspend waiting hold!";
59         return $self;
60     }
61
62     $self->suspend(1);
63     $self->suspend_until( $dt );
64
65     $self->store();
66
67     logaction( 'HOLDS', 'SUSPEND', $self->reserve_id, Dumper($self->unblessed) )
68         if C4::Context->preference('HoldsLog');
69
70     return $self;
71 }
72
73 =head3 resume
74
75 my $hold = $hold->resume();
76
77 =cut
78
79 sub resume {
80     my ( $self ) = @_;
81
82     $self->suspend(0);
83     $self->suspend_until( undef );
84
85     $self->store();
86
87     logaction( 'HOLDS', 'RESUME', $self->reserve_id, Dumper($self->unblessed) )
88         if C4::Context->preference('HoldsLog');
89
90     return $self;
91 }
92
93 =head3 delete
94
95 $hold->delete();
96
97 =cut
98
99 sub delete {
100     my ( $self ) = @_;
101
102     my $deleted = $self->SUPER::delete($self);
103
104     logaction( 'HOLDS', 'DELETE', $self->reserve_id, Dumper($self->unblessed) )
105         if C4::Context->preference('HoldsLog');
106
107     return $deleted;
108 }
109
110 =head3 waiting_expires_on
111
112 Returns a DateTime for the date a waiting holds expires on.
113 Returns undef if the system peference ReservesMaxPickUpDelay is not set.
114 Returns undef if the hold is not waiting ( found = 'W' ).
115
116 =cut
117
118 sub waiting_expires_on {
119     my ($self) = @_;
120
121     my $found = $self->found;
122     return unless $found && $found eq 'W';
123
124     my $ReservesMaxPickUpDelay = C4::Context->preference('ReservesMaxPickUpDelay');
125     return unless $ReservesMaxPickUpDelay;
126
127     my $dt = dt_from_string( $self->waitingdate() );
128
129     $dt->add( days => $ReservesMaxPickUpDelay );
130
131     return $dt;
132 }
133
134 =head3 set_waiting
135
136 =cut
137
138 sub set_waiting {
139     my ( $self, $transferToDo ) = @_;
140
141     $self->priority(0);
142
143     if ($transferToDo) {
144         $self->found('T')->store();
145         return $self;
146     }
147
148     my $today = dt_from_string();
149     my $values = {
150         found => 'W',
151         waitingdate => $today->ymd,
152     };
153
154     if ( C4::Context->preference("ExpireReservesMaxPickUpDelay") ) {
155         my $max_pickup_delay = C4::Context->preference("ReservesMaxPickUpDelay");
156         my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
157         my $calendar = Koha::Calendar->new( branchcode => $self->branchcode );
158
159         my $expirationdate = $today->clone;
160         $expirationdate->add(days => $max_pickup_delay);
161
162         if ( C4::Context->preference("ExcludeHolidaysFromMaxPickUpDelay") ) {
163             $expirationdate = $calendar->days_forward( dt_from_string($self->waitingdate), $max_pickup_delay );
164         }
165
166         $values->{expirationdate} = $expirationdate->ymd;
167     }
168
169     $self->set($values)->store();
170
171     return $self;
172 }
173
174 =head3 is_found
175
176 Returns true if hold is a waiting or in transit
177
178 =cut
179
180 sub is_found {
181     my ($self) = @_;
182
183     return 0 unless $self->found();
184     return 1 if $self->found() eq 'W';
185     return 1 if $self->found() eq 'T';
186 }
187
188 =head3 is_waiting
189
190 Returns true if hold is a waiting hold
191
192 =cut
193
194 sub is_waiting {
195     my ($self) = @_;
196
197     my $found = $self->found;
198     return $found && $found eq 'W';
199 }
200
201 =head3 is_in_transit
202
203 Returns true if hold is a in_transit hold
204
205 =cut
206
207 sub is_in_transit {
208     my ($self) = @_;
209
210     return 0 unless $self->found();
211     return $self->found() eq 'T';
212 }
213
214 =head3 is_cancelable
215
216 Returns true if hold is a cancelable hold
217
218 Holds may be canceled if they not found, or
219 are found and waiting. A hold found but in
220 transit cannot be canceled.
221
222 =cut
223
224 sub is_cancelable {
225     my ($self) = @_;
226
227     return 1 unless $self->is_found();
228     return 0 if $self->is_in_transit();
229     return 1 if $self->is_waiting();
230     return 0;
231 }
232
233 =head3 is_at_destination
234
235 Returns true if hold is waiting
236 and the hold's pickup branch matches
237 the hold item's holding branch
238
239 =cut
240
241 sub is_at_destination {
242     my ($self) = @_;
243
244     return $self->is_waiting() && ( $self->branchcode() eq $self->item()->holdingbranch() );
245 }
246
247 =head3 biblio
248
249 Returns the related Koha::Biblio object for this hold
250
251 =cut
252
253 sub biblio {
254     my ($self) = @_;
255
256     $self->{_biblio} ||= Koha::Biblios->find( $self->biblionumber() );
257
258     return $self->{_biblio};
259 }
260
261 =head3 item
262
263 Returns the related Koha::Item object for this Hold
264
265 =cut
266
267 sub item {
268     my ($self) = @_;
269
270     $self->{_item} ||= Koha::Items->find( $self->itemnumber() );
271
272     return $self->{_item};
273 }
274
275 =head3 branch
276
277 Returns the related Koha::Library object for this Hold
278
279 =cut
280
281 sub branch {
282     my ($self) = @_;
283
284     $self->{_branch} ||= Koha::Libraries->find( $self->branchcode() );
285
286     return $self->{_branch};
287 }
288
289 =head3 borrower
290
291 Returns the related Koha::Patron object for this Hold
292
293 =cut
294
295 sub borrower {
296     my ($self) = @_;
297
298     $self->{_borrower} ||= Koha::Patrons->find( $self->borrowernumber() );
299
300     return $self->{_borrower};
301 }
302
303 =head3 is_suspended
304
305 my $bool = $hold->is_suspended();
306
307 =cut
308
309 sub is_suspended {
310     my ( $self ) = @_;
311
312     return $self->suspend();
313 }
314
315 =head3 type
316
317 =cut
318
319 sub _type {
320     return 'Reserve';
321 }
322
323 =head1 AUTHOR
324
325 Kyle M Hall <kyle@bywatersolutions.com>
326
327 =cut
328
329 1;