Bug 19059: Remove CancelReserve - do not log DELETE
[koha.git] / Koha / Hold.pm
1 package Koha::Hold;
2
3 # Copyright ByWater Solutions 2014
4 # Copyright 2017 Koha Development team
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 3 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 use Modern::Perl;
22
23 use Carp;
24 use Data::Dumper qw(Dumper);
25
26 use C4::Context qw(preference);
27 use C4::Log;
28
29 use Koha::DateUtils qw(dt_from_string output_pref);
30 use Koha::Patrons;
31 use Koha::Biblios;
32 use Koha::Items;
33 use Koha::Libraries;
34 use Koha::Old::Holds;
35
36 use base qw(Koha::Object);
37
38 =head1 NAME
39
40 Koha::Hold - Koha Hold object class
41
42 =head1 API
43
44 =head2 Class Methods
45
46 =cut
47
48 =head3 suspend_hold
49
50 my $hold = $hold->suspend_hold( $suspend_until_dt );
51
52 =cut
53
54 sub suspend_hold {
55     my ( $self, $dt ) = @_;
56
57     $dt = $dt ? $dt->clone()->truncate( to => 'day' ) : undef;
58
59     if ( $self->is_waiting ) {    # We can't suspend waiting holds
60         carp "Unable to suspend waiting hold!";
61         return $self;
62     }
63
64     $self->suspend(1);
65     $self->suspend_until( $dt );
66
67     $self->store();
68
69     logaction( 'HOLDS', 'SUSPEND', $self->reserve_id, Dumper($self->unblessed) )
70         if C4::Context->preference('HoldsLog');
71
72     return $self;
73 }
74
75 =head3 resume
76
77 my $hold = $hold->resume();
78
79 =cut
80
81 sub resume {
82     my ( $self ) = @_;
83
84     $self->suspend(0);
85     $self->suspend_until( undef );
86
87     $self->store();
88
89     logaction( 'HOLDS', 'RESUME', $self->reserve_id, Dumper($self->unblessed) )
90         if C4::Context->preference('HoldsLog');
91
92     return $self;
93 }
94
95 =head3 delete
96
97 $hold->delete();
98
99 =cut
100
101 sub delete {
102     my ( $self ) = @_;
103
104     my $deleted = $self->SUPER::delete($self);
105
106     logaction( 'HOLDS', 'DELETE', $self->reserve_id, Dumper($self->unblessed) )
107         if C4::Context->preference('HoldsLog');
108
109     return $deleted;
110 }
111
112 =head3 set_waiting
113
114 =cut
115
116 sub set_waiting {
117     my ( $self, $transferToDo ) = @_;
118
119     $self->priority(0);
120
121     if ($transferToDo) {
122         $self->found('T')->store();
123         return $self;
124     }
125
126     my $today = dt_from_string();
127     my $values = {
128         found => 'W',
129         waitingdate => $today->ymd,
130     };
131
132     my $requested_expiration;
133     if ($self->expirationdate) {
134         $requested_expiration = dt_from_string($self->expirationdate);
135     }
136
137     my $max_pickup_delay = C4::Context->preference("ReservesMaxPickUpDelay");
138     my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
139     my $calendar = Koha::Calendar->new( branchcode => $self->branchcode );
140
141     my $expirationdate = $today->clone;
142     $expirationdate->add(days => $max_pickup_delay);
143
144     if ( C4::Context->preference("ExcludeHolidaysFromMaxPickUpDelay") ) {
145         $expirationdate = $calendar->days_forward( dt_from_string(), $max_pickup_delay );
146     }
147
148     # If patron's requested expiration date is prior to the
149     # calculated one, we keep the patron's one.
150     my $cmp = $requested_expiration ? DateTime->compare($requested_expiration, $expirationdate) : 0;
151     $values->{expirationdate} = $cmp == -1 ? $requested_expiration->ymd : $expirationdate->ymd;
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
300 =head3 cancel
301
302 my $cancel_hold = $hold->cancel();
303
304 Cancel a hold:
305 - The hold will be moved to the old_reserves table with a priority=0
306 - The priority of other holds will be updated
307 - The patron will be charge (see ExpireReservesMaxPickUpDelayCharge) if the charge_cancel_fee parameter is set
308 - a CANCEL HOLDS log will be done if the pref HoldsLog is on
309
310 =cut
311
312 sub cancel {
313     my ( $self, $params ) = @_;
314     $self->_result->result_source->schema->txn_do(
315         sub {
316             $self->cancellationdate(dt_from_string);
317             $self->priority(0);
318             $self->_move_to_old;
319             $self->SUPER::delete(); # Do not add a DELETE log
320
321             # now fix the priority on the others....
322             C4::Reserves::_FixPriority({ biblionumber => $self->biblionumber });
323
324             # and, if desired, charge a cancel fee
325             my $charge = C4::Context->preference("ExpireReservesMaxPickUpDelayCharge");
326             if ( $charge && $params->{'charge_cancel_fee'} ) {
327                 C4::Accounts::manualinvoice($self->borrowernumber, $self->itemnumber, '', 'HE', $charge);
328             }
329
330             C4::Log::logaction( 'HOLDS', 'CANCEL', $self->reserve_id, Dumper($self->unblessed) )
331                 if C4::Context->preference('HoldsLog');
332         }
333     );
334     return $self;
335 }
336
337 =head3 _move_to_old
338
339 my $is_moved = $hold->_move_to_old;
340
341 Move a hold to the old_reserve table following the same pattern as Koha::Patron->move_to_deleted
342
343 =cut
344
345 sub _move_to_old {
346     my ($self) = @_;
347     my $hold_infos = $self->unblessed;
348     return Koha::Old::Hold->new( $hold_infos )->store;
349 }
350
351 =head3 type
352
353 =cut
354
355 sub _type {
356     return 'Reserve';
357 }
358
359 =head1 AUTHORS
360
361 Kyle M Hall <kyle@bywatersolutions.com>
362
363 Jonathan Druart <jonathan.druart@bugs.koha-community.org>
364
365 =cut
366
367 1;