Bug 12063 - Keep patron's requested expiration date if it is prior to the calculated one
[koha.git] / 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 set_waiting
111
112 =cut
113
114 sub set_waiting {
115     my ( $self, $transferToDo ) = @_;
116
117     $self->priority(0);
118
119     if ($transferToDo) {
120         $self->found('T')->store();
121         return $self;
122     }
123
124     my $today = dt_from_string();
125     my $values = {
126         found => 'W',
127         waitingdate => $today->ymd,
128     };
129
130     my $requested_expiration;
131     if ($self->expirationdate) {
132         $requested_expiration = dt_from_string($self->expirationdate);
133     }
134
135     if ( C4::Context->preference("ExpireReservesMaxPickUpDelay") ) {
136         my $max_pickup_delay = C4::Context->preference("ReservesMaxPickUpDelay");
137         my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
138         my $calendar = Koha::Calendar->new( branchcode => $self->branchcode );
139
140         my $expirationdate = $today->clone;
141         $expirationdate->add(days => $max_pickup_delay);
142
143         if ( C4::Context->preference("ExcludeHolidaysFromMaxPickUpDelay") ) {
144             $expirationdate = $calendar->days_forward( dt_from_string($self->waitingdate), $max_pickup_delay );
145         }
146
147         # If patron's requested expiration date is prior to the
148         # calculated one, we keep the patron's one.
149         my $cmp = $requested_expiration ? DateTime->compare($requested_expiration, $expirationdate) : 0;
150         $values->{expirationdate} = $cmp == -1 ? $requested_expiration->ymd : $expirationdate->ymd;
151     }
152
153     $self->set($values)->store();
154
155     return $self;
156 }
157
158 =head3 is_found
159
160 Returns true if hold is a waiting or in transit
161
162 =cut
163
164 sub is_found {
165     my ($self) = @_;
166
167     return 0 unless $self->found();
168     return 1 if $self->found() eq 'W';
169     return 1 if $self->found() eq 'T';
170 }
171
172 =head3 is_waiting
173
174 Returns true if hold is a waiting hold
175
176 =cut
177
178 sub is_waiting {
179     my ($self) = @_;
180
181     my $found = $self->found;
182     return $found && $found eq 'W';
183 }
184
185 =head3 is_in_transit
186
187 Returns true if hold is a in_transit hold
188
189 =cut
190
191 sub is_in_transit {
192     my ($self) = @_;
193
194     return 0 unless $self->found();
195     return $self->found() eq 'T';
196 }
197
198 =head3 is_cancelable
199
200 Returns true if hold is a cancelable hold
201
202 Holds may be canceled if they not found, or
203 are found and waiting. A hold found but in
204 transit cannot be canceled.
205
206 =cut
207
208 sub is_cancelable {
209     my ($self) = @_;
210
211     return 1 unless $self->is_found();
212     return 0 if $self->is_in_transit();
213     return 1 if $self->is_waiting();
214     return 0;
215 }
216
217 =head3 is_at_destination
218
219 Returns true if hold is waiting
220 and the hold's pickup branch matches
221 the hold item's holding branch
222
223 =cut
224
225 sub is_at_destination {
226     my ($self) = @_;
227
228     return $self->is_waiting() && ( $self->branchcode() eq $self->item()->holdingbranch() );
229 }
230
231 =head3 biblio
232
233 Returns the related Koha::Biblio object for this hold
234
235 =cut
236
237 sub biblio {
238     my ($self) = @_;
239
240     $self->{_biblio} ||= Koha::Biblios->find( $self->biblionumber() );
241
242     return $self->{_biblio};
243 }
244
245 =head3 item
246
247 Returns the related Koha::Item object for this Hold
248
249 =cut
250
251 sub item {
252     my ($self) = @_;
253
254     $self->{_item} ||= Koha::Items->find( $self->itemnumber() );
255
256     return $self->{_item};
257 }
258
259 =head3 branch
260
261 Returns the related Koha::Library object for this Hold
262
263 =cut
264
265 sub branch {
266     my ($self) = @_;
267
268     $self->{_branch} ||= Koha::Libraries->find( $self->branchcode() );
269
270     return $self->{_branch};
271 }
272
273 =head3 borrower
274
275 Returns the related Koha::Patron object for this Hold
276
277 =cut
278
279 sub borrower {
280     my ($self) = @_;
281
282     $self->{_borrower} ||= Koha::Patrons->find( $self->borrowernumber() );
283
284     return $self->{_borrower};
285 }
286
287 =head3 is_suspended
288
289 my $bool = $hold->is_suspended();
290
291 =cut
292
293 sub is_suspended {
294     my ( $self ) = @_;
295
296     return $self->suspend();
297 }
298
299 =head3 type
300
301 =cut
302
303 sub _type {
304     return 'Reserve';
305 }
306
307 =head1 AUTHOR
308
309 Kyle M Hall <kyle@bywatersolutions.com>
310
311 =cut
312
313 1;