Bug 24159: (QA follow-up) Make terminology consistent
[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
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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 use Koha::Calendar;
36
37 use Koha::Exceptions::Hold;
38
39 use base qw(Koha::Object);
40
41 =head1 NAME
42
43 Koha::Hold - Koha Hold object class
44
45 =head1 API
46
47 =head2 Class Methods
48
49 =cut
50
51 =head3 age
52
53 returns the number of days since a hold was placed, optionally
54 using the calendar
55
56 my $age = $hold->age( $use_calendar );
57
58 =cut
59
60 sub age {
61     my ( $self, $use_calendar ) = @_;
62
63     my $today = dt_from_string;
64     my $age;
65
66     if ( $use_calendar ) {
67         my $calendar = Koha::Calendar->new( branchcode => $self->branchcode );
68         $age = $calendar->days_between( dt_from_string( $self->reservedate ), $today );
69     }
70     else {
71         $age = $today->delta_days( dt_from_string( $self->reservedate ) );
72     }
73
74     $age = $age->in_units( 'days' );
75
76     return $age;
77 }
78
79 =head3 suspend_hold
80
81 my $hold = $hold->suspend_hold( $suspend_until_dt );
82
83 =cut
84
85 sub suspend_hold {
86     my ( $self, $dt ) = @_;
87
88     my $date = $dt ? $dt->clone()->truncate( to => 'day' )->datetime : undef;
89
90     if ( $self->is_found ) {    # We can't suspend found holds
91         if ( $self->is_waiting ) {
92             Koha::Exceptions::Hold::CannotSuspendFound->throw( status => 'W' );
93         }
94         elsif ( $self->is_in_transit ) {
95             Koha::Exceptions::Hold::CannotSuspendFound->throw( status => 'T' );
96         }
97         else {
98             Koha::Exceptions::Hold::CannotSuspendFound->throw(
99                       'Unhandled data exception on found hold (id='
100                     . $self->id
101                     . ', found='
102                     . $self->found
103                     . ')' );
104         }
105     }
106
107     $self->suspend(1);
108     $self->suspend_until($date);
109     $self->store();
110
111     logaction( 'HOLDS', 'SUSPEND', $self->reserve_id, Dumper( $self->unblessed ) )
112         if C4::Context->preference('HoldsLog');
113
114     return $self;
115 }
116
117 =head3 resume
118
119 my $hold = $hold->resume();
120
121 =cut
122
123 sub resume {
124     my ( $self ) = @_;
125
126     $self->suspend(0);
127     $self->suspend_until( undef );
128
129     $self->store();
130
131     logaction( 'HOLDS', 'RESUME', $self->reserve_id, Dumper($self->unblessed) )
132         if C4::Context->preference('HoldsLog');
133
134     return $self;
135 }
136
137 =head3 delete
138
139 $hold->delete();
140
141 =cut
142
143 sub delete {
144     my ( $self ) = @_;
145
146     my $deleted = $self->SUPER::delete($self);
147
148     logaction( 'HOLDS', 'DELETE', $self->reserve_id, Dumper($self->unblessed) )
149         if C4::Context->preference('HoldsLog');
150
151     return $deleted;
152 }
153
154 =head3 set_waiting
155
156 =cut
157
158 sub set_waiting {
159     my ( $self, $transferToDo ) = @_;
160
161     $self->priority(0);
162
163     if ($transferToDo) {
164         $self->found('T')->store();
165         return $self;
166     }
167
168     my $today = dt_from_string();
169     my $values = {
170         found => 'W',
171         waitingdate => $today->ymd,
172     };
173
174     my $requested_expiration;
175     if ($self->expirationdate) {
176         $requested_expiration = dt_from_string($self->expirationdate);
177     }
178
179     my $max_pickup_delay = C4::Context->preference("ReservesMaxPickUpDelay");
180     my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
181
182     my $expirationdate = $today->clone;
183     $expirationdate->add(days => $max_pickup_delay);
184
185     if ( C4::Context->preference("ExcludeHolidaysFromMaxPickUpDelay") ) {
186         my $itemtype = $self->item ? $self->item->effective_itemtype : $self->biblio->itemtype;
187         my $daysmode = Koha::CirculationRules->get_effective_daysmode(
188             {
189                 categorycode => $self->borrower->categorycode,
190                 itemtype     => $itemtype,
191                 branchcode   => $self->branchcode,
192             }
193         );
194         my $calendar = Koha::Calendar->new( branchcode => $self->branchcode, days_mode => $daysmode );
195
196         $expirationdate = $calendar->days_forward( dt_from_string(), $max_pickup_delay );
197     }
198
199     # If patron's requested expiration date is prior to the
200     # calculated one, we keep the patron's one.
201     my $cmp = $requested_expiration ? DateTime->compare($requested_expiration, $expirationdate) : 0;
202     $values->{expirationdate} = $cmp == -1 ? $requested_expiration->ymd : $expirationdate->ymd;
203
204     $self->set($values)->store();
205
206     return $self;
207 }
208
209 =head3 is_found
210
211 Returns true if hold is a waiting or in transit
212
213 =cut
214
215 sub is_found {
216     my ($self) = @_;
217
218     return 0 unless $self->found();
219     return 1 if $self->found() eq 'W';
220     return 1 if $self->found() eq 'T';
221 }
222
223 =head3 is_waiting
224
225 Returns true if hold is a waiting hold
226
227 =cut
228
229 sub is_waiting {
230     my ($self) = @_;
231
232     my $found = $self->found;
233     return $found && $found eq 'W';
234 }
235
236 =head3 is_in_transit
237
238 Returns true if hold is a in_transit hold
239
240 =cut
241
242 sub is_in_transit {
243     my ($self) = @_;
244
245     return 0 unless $self->found();
246     return $self->found() eq 'T';
247 }
248
249 =head3 is_cancelable_from_opac
250
251 Returns true if hold is a cancelable hold
252
253 Holds may be only canceled if they are not found.
254
255 This is used from the OPAC.
256
257 =cut
258
259 sub is_cancelable_from_opac {
260     my ($self) = @_;
261
262     return 1 unless $self->is_found();
263     return 0; # if ->is_in_transit or if ->is_waiting
264 }
265
266 =head3 is_at_destination
267
268 Returns true if hold is waiting
269 and the hold's pickup branch matches
270 the hold item's holding branch
271
272 =cut
273
274 sub is_at_destination {
275     my ($self) = @_;
276
277     return $self->is_waiting() && ( $self->branchcode() eq $self->item()->holdingbranch() );
278 }
279
280 =head3 biblio
281
282 Returns the related Koha::Biblio object for this hold
283
284 =cut
285
286 sub biblio {
287     my ($self) = @_;
288
289     $self->{_biblio} ||= Koha::Biblios->find( $self->biblionumber() );
290
291     return $self->{_biblio};
292 }
293
294 =head3 item
295
296 Returns the related Koha::Item object for this Hold
297
298 =cut
299
300 sub item {
301     my ($self) = @_;
302
303     $self->{_item} ||= Koha::Items->find( $self->itemnumber() );
304
305     return $self->{_item};
306 }
307
308 =head3 branch
309
310 Returns the related Koha::Library object for this Hold
311
312 =cut
313
314 sub branch {
315     my ($self) = @_;
316
317     $self->{_branch} ||= Koha::Libraries->find( $self->branchcode() );
318
319     return $self->{_branch};
320 }
321
322 =head3 borrower
323
324 Returns the related Koha::Patron object for this Hold
325
326 =cut
327
328 # FIXME Should be renamed with ->patron
329 sub borrower {
330     my ($self) = @_;
331
332     $self->{_borrower} ||= Koha::Patrons->find( $self->borrowernumber() );
333
334     return $self->{_borrower};
335 }
336
337 =head3 is_suspended
338
339 my $bool = $hold->is_suspended();
340
341 =cut
342
343 sub is_suspended {
344     my ( $self ) = @_;
345
346     return $self->suspend();
347 }
348
349
350 =head3 cancel
351
352 my $cancel_hold = $hold->cancel();
353
354 Cancel a hold:
355 - The hold will be moved to the old_reserves table with a priority=0
356 - The priority of other holds will be updated
357 - The patron will be charge (see ExpireReservesMaxPickUpDelayCharge) if the charge_cancel_fee parameter is set
358 - a CANCEL HOLDS log will be done if the pref HoldsLog is on
359
360 =cut
361
362 sub cancel {
363     my ( $self, $params ) = @_;
364     $self->_result->result_source->schema->txn_do(
365         sub {
366             $self->cancellationdate( dt_from_string->strftime( '%Y-%m-%d %H:%M:%S' ) );
367             $self->priority(0);
368             $self->_move_to_old;
369             $self->SUPER::delete(); # Do not add a DELETE log
370
371             # now fix the priority on the others....
372             C4::Reserves::_FixPriority({ biblionumber => $self->biblionumber });
373
374             # and, if desired, charge a cancel fee
375             my $charge = C4::Context->preference("ExpireReservesMaxPickUpDelayCharge");
376             if ( $charge && $params->{'charge_cancel_fee'} ) {
377                 my $account =
378                   Koha::Account->new( { patron_id => $self->borrowernumber } );
379                 $account->add_debit(
380                     {
381                         amount     => $charge,
382                         user_id    => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
383                         interface  => C4::Context->interface,
384                         library_id => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef,
385                         type       => 'RESERVE_EXPIRED',
386                         item_id    => $self->itemnumber
387                     }
388                 );
389             }
390
391             C4::Log::logaction( 'HOLDS', 'CANCEL', $self->reserve_id, Dumper($self->unblessed) )
392                 if C4::Context->preference('HoldsLog');
393         }
394     );
395     return $self;
396 }
397
398 =head3 _move_to_old
399
400 my $is_moved = $hold->_move_to_old;
401
402 Move a hold to the old_reserve table following the same pattern as Koha::Patron->move_to_deleted
403
404 =cut
405
406 sub _move_to_old {
407     my ($self) = @_;
408     my $hold_infos = $self->unblessed;
409     return Koha::Old::Hold->new( $hold_infos )->store;
410 }
411
412 =head3 to_api_mapping
413
414 This method returns the mapping for representing a Koha::Hold object
415 on the API.
416
417 =cut
418
419 sub to_api_mapping {
420     return {
421         reserve_id       => 'hold_id',
422         borrowernumber   => 'patron_id',
423         reservedate      => 'hold_date',
424         biblionumber     => 'biblio_id',
425         branchcode       => 'pickup_library_id',
426         notificationdate => undef,
427         reminderdate     => undef,
428         cancellationdate => 'cancellation_date',
429         reservenotes     => 'notes',
430         found            => 'status',
431         itemnumber       => 'item_id',
432         waitingdate      => 'waiting_date',
433         expirationdate   => 'expiration_date',
434         lowestPriority   => 'lowest_priority',
435         suspend          => 'suspended',
436         suspend_until    => 'suspended_until',
437         itemtype         => 'item_type',
438         item_level_hold  => 'item_level',
439     };
440 }
441
442 =head2 Internal methods
443
444 =head3 _type
445
446 =cut
447
448 sub _type {
449     return 'Reserve';
450 }
451
452 =head1 AUTHORS
453
454 Kyle M Hall <kyle@bywatersolutions.com>
455 Jonathan Druart <jonathan.druart@bugs.koha-community.org>
456 Martin Renvoize <martin.renvoize@ptfs-europe.com>
457
458 =cut
459
460 1;