Bug 26456: Reindent MARC subfield structure template
[koha.git] / Koha / Hold.pm
blob360b43e7b5b4c8bfd632318937192e8eaa88d9cc
1 package Koha::Hold;
3 # Copyright ByWater Solutions 2014
4 # Copyright 2017 Koha Development team
6 # This file is part of Koha.
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.
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.
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>.
21 use Modern::Perl;
23 use Carp;
24 use Data::Dumper qw(Dumper);
26 use C4::Context qw(preference);
27 use C4::Letters;
28 use C4::Log;
30 use Koha::AuthorisedValues;
31 use Koha::DateUtils qw(dt_from_string output_pref);
32 use Koha::Patrons;
33 use Koha::Biblios;
34 use Koha::Items;
35 use Koha::Libraries;
36 use Koha::Old::Holds;
37 use Koha::Calendar;
39 use Koha::Exceptions::Hold;
41 use base qw(Koha::Object);
43 =head1 NAME
45 Koha::Hold - Koha Hold object class
47 =head1 API
49 =head2 Class Methods
51 =cut
53 =head3 age
55 returns the number of days since a hold was placed, optionally
56 using the calendar
58 my $age = $hold->age( $use_calendar );
60 =cut
62 sub age {
63 my ( $self, $use_calendar ) = @_;
65 my $today = dt_from_string;
66 my $age;
68 if ( $use_calendar ) {
69 my $calendar = Koha::Calendar->new( branchcode => $self->branchcode );
70 $age = $calendar->days_between( dt_from_string( $self->reservedate ), $today );
72 else {
73 $age = $today->delta_days( dt_from_string( $self->reservedate ) );
76 $age = $age->in_units( 'days' );
78 return $age;
81 =head3 suspend_hold
83 my $hold = $hold->suspend_hold( $suspend_until_dt );
85 =cut
87 sub suspend_hold {
88 my ( $self, $dt ) = @_;
90 my $date = $dt ? $dt->clone()->truncate( to => 'day' )->datetime : undef;
92 if ( $self->is_found ) { # We can't suspend found holds
93 if ( $self->is_waiting ) {
94 Koha::Exceptions::Hold::CannotSuspendFound->throw( status => 'W' );
96 elsif ( $self->is_in_transit ) {
97 Koha::Exceptions::Hold::CannotSuspendFound->throw( status => 'T' );
99 else {
100 Koha::Exceptions::Hold::CannotSuspendFound->throw(
101 'Unhandled data exception on found hold (id='
102 . $self->id
103 . ', found='
104 . $self->found
105 . ')' );
109 $self->suspend(1);
110 $self->suspend_until($date);
111 $self->store();
113 logaction( 'HOLDS', 'SUSPEND', $self->reserve_id, Dumper( $self->unblessed ) )
114 if C4::Context->preference('HoldsLog');
116 return $self;
119 =head3 resume
121 my $hold = $hold->resume();
123 =cut
125 sub resume {
126 my ( $self ) = @_;
128 $self->suspend(0);
129 $self->suspend_until( undef );
131 $self->store();
133 logaction( 'HOLDS', 'RESUME', $self->reserve_id, Dumper($self->unblessed) )
134 if C4::Context->preference('HoldsLog');
136 return $self;
139 =head3 delete
141 $hold->delete();
143 =cut
145 sub delete {
146 my ( $self ) = @_;
148 my $deleted = $self->SUPER::delete($self);
150 logaction( 'HOLDS', 'DELETE', $self->reserve_id, Dumper($self->unblessed) )
151 if C4::Context->preference('HoldsLog');
153 return $deleted;
156 =head3 set_waiting
158 =cut
160 sub set_waiting {
161 my ( $self, $transferToDo ) = @_;
163 $self->priority(0);
165 if ($transferToDo) {
166 $self->found('T')->store();
167 return $self;
170 my $today = dt_from_string();
171 my $values = {
172 found => 'W',
173 waitingdate => $today->ymd,
176 my $requested_expiration;
177 if ($self->expirationdate) {
178 $requested_expiration = dt_from_string($self->expirationdate);
181 my $max_pickup_delay = C4::Context->preference("ReservesMaxPickUpDelay");
182 my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
184 my $expirationdate = $today->clone;
185 $expirationdate->add(days => $max_pickup_delay);
187 if ( C4::Context->preference("ExcludeHolidaysFromMaxPickUpDelay") ) {
188 my $itemtype = $self->item ? $self->item->effective_itemtype : $self->biblio->itemtype;
189 my $daysmode = Koha::CirculationRules->get_effective_daysmode(
191 categorycode => $self->borrower->categorycode,
192 itemtype => $itemtype,
193 branchcode => $self->branchcode,
196 my $calendar = Koha::Calendar->new( branchcode => $self->branchcode, days_mode => $daysmode );
198 $expirationdate = $calendar->days_forward( dt_from_string(), $max_pickup_delay );
201 # If patron's requested expiration date is prior to the
202 # calculated one, we keep the patron's one.
203 my $cmp = $requested_expiration ? DateTime->compare($requested_expiration, $expirationdate) : 0;
204 $values->{expirationdate} = $cmp == -1 ? $requested_expiration->ymd : $expirationdate->ymd;
206 $self->set($values)->store();
208 return $self;
211 =head3 is_found
213 Returns true if hold is a waiting or in transit
215 =cut
217 sub is_found {
218 my ($self) = @_;
220 return 0 unless $self->found();
221 return 1 if $self->found() eq 'W';
222 return 1 if $self->found() eq 'T';
225 =head3 is_waiting
227 Returns true if hold is a waiting hold
229 =cut
231 sub is_waiting {
232 my ($self) = @_;
234 my $found = $self->found;
235 return $found && $found eq 'W';
238 =head3 is_in_transit
240 Returns true if hold is a in_transit hold
242 =cut
244 sub is_in_transit {
245 my ($self) = @_;
247 return 0 unless $self->found();
248 return $self->found() eq 'T';
251 =head3 is_cancelable_from_opac
253 Returns true if hold is a cancelable hold
255 Holds may be only canceled if they are not found.
257 This is used from the OPAC.
259 =cut
261 sub is_cancelable_from_opac {
262 my ($self) = @_;
264 return 1 unless $self->is_found();
265 return 0; # if ->is_in_transit or if ->is_waiting
268 =head3 is_at_destination
270 Returns true if hold is waiting
271 and the hold's pickup branch matches
272 the hold item's holding branch
274 =cut
276 sub is_at_destination {
277 my ($self) = @_;
279 return $self->is_waiting() && ( $self->branchcode() eq $self->item()->holdingbranch() );
282 =head3 biblio
284 Returns the related Koha::Biblio object for this hold
286 =cut
288 sub biblio {
289 my ($self) = @_;
291 $self->{_biblio} ||= Koha::Biblios->find( $self->biblionumber() );
293 return $self->{_biblio};
296 =head3 item
298 Returns the related Koha::Item object for this Hold
300 =cut
302 sub item {
303 my ($self) = @_;
305 $self->{_item} ||= Koha::Items->find( $self->itemnumber() );
307 return $self->{_item};
310 =head3 branch
312 Returns the related Koha::Library object for this Hold
314 =cut
316 sub branch {
317 my ($self) = @_;
319 $self->{_branch} ||= Koha::Libraries->find( $self->branchcode() );
321 return $self->{_branch};
324 =head3 borrower
326 Returns the related Koha::Patron object for this Hold
328 =cut
330 # FIXME Should be renamed with ->patron
331 sub borrower {
332 my ($self) = @_;
334 $self->{_borrower} ||= Koha::Patrons->find( $self->borrowernumber() );
336 return $self->{_borrower};
339 =head3 is_suspended
341 my $bool = $hold->is_suspended();
343 =cut
345 sub is_suspended {
346 my ( $self ) = @_;
348 return $self->suspend();
352 =head3 cancel
354 my $cancel_hold = $hold->cancel(
356 [ charge_cancel_fee => 1||0, ]
357 [ cancellation_reason => $cancellation_reason, ]
361 Cancel a hold:
362 - The hold will be moved to the old_reserves table with a priority=0
363 - The priority of other holds will be updated
364 - The patron will be charge (see ExpireReservesMaxPickUpDelayCharge) if the charge_cancel_fee parameter is set
365 - The canceled hold will have the cancellation reason added to old_reserves.cancellation_reason if one is passed in
366 - a CANCEL HOLDS log will be done if the pref HoldsLog is on
368 =cut
370 sub cancel {
371 my ( $self, $params ) = @_;
372 $self->_result->result_source->schema->txn_do(
373 sub {
374 $self->cancellationdate( dt_from_string->strftime( '%Y-%m-%d %H:%M:%S' ) );
375 $self->priority(0);
376 $self->cancellation_reason( $params->{cancellation_reason} );
377 $self->store();
379 if ( $params->{cancellation_reason} ) {
380 my $letter = C4::Letters::GetPreparedLetter(
381 module => 'reserves',
382 letter_code => 'HOLD_CANCELLATION',
383 message_transport_type => 'email',
384 branchcode => $self->borrower->branchcode,
385 lang => $self->borrower->lang,
386 tables => {
387 branches => $self->borrower->branchcode,
388 borrowers => $self->borrowernumber,
389 items => $self->itemnumber,
390 biblio => $self->biblionumber,
391 biblioitems => $self->biblionumber,
392 reserves => $self->unblessed,
396 if ($letter) {
397 C4::Letters::EnqueueLetter(
399 letter => $letter,
400 borrowernumber => $self->borrowernumber,
401 message_transport_type => 'email',
407 $self->_move_to_old;
408 $self->SUPER::delete(); # Do not add a DELETE log
410 # now fix the priority on the others....
411 C4::Reserves::_FixPriority({ biblionumber => $self->biblionumber });
413 # and, if desired, charge a cancel fee
414 my $charge = C4::Context->preference("ExpireReservesMaxPickUpDelayCharge");
415 if ( $charge && $params->{'charge_cancel_fee'} ) {
416 my $account =
417 Koha::Account->new( { patron_id => $self->borrowernumber } );
418 $account->add_debit(
420 amount => $charge,
421 user_id => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
422 interface => C4::Context->interface,
423 library_id => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef,
424 type => 'RESERVE_EXPIRED',
425 item_id => $self->itemnumber
430 C4::Log::logaction( 'HOLDS', 'CANCEL', $self->reserve_id, Dumper($self->unblessed) )
431 if C4::Context->preference('HoldsLog');
434 return $self;
437 =head3 _move_to_old
439 my $is_moved = $hold->_move_to_old;
441 Move a hold to the old_reserve table following the same pattern as Koha::Patron->move_to_deleted
443 =cut
445 sub _move_to_old {
446 my ($self) = @_;
447 my $hold_infos = $self->unblessed;
448 return Koha::Old::Hold->new( $hold_infos )->store;
451 =head3 to_api_mapping
453 This method returns the mapping for representing a Koha::Hold object
454 on the API.
456 =cut
458 sub to_api_mapping {
459 return {
460 reserve_id => 'hold_id',
461 borrowernumber => 'patron_id',
462 reservedate => 'hold_date',
463 biblionumber => 'biblio_id',
464 branchcode => 'pickup_library_id',
465 notificationdate => undef,
466 reminderdate => undef,
467 cancellationdate => 'cancellation_date',
468 reservenotes => 'notes',
469 found => 'status',
470 itemnumber => 'item_id',
471 waitingdate => 'waiting_date',
472 expirationdate => 'expiration_date',
473 lowestPriority => 'lowest_priority',
474 suspend => 'suspended',
475 suspend_until => 'suspended_until',
476 itemtype => 'item_type',
477 item_level_hold => 'item_level',
481 =head2 Internal methods
483 =head3 _type
485 =cut
487 sub _type {
488 return 'Reserve';
491 =head1 AUTHORS
493 Kyle M Hall <kyle@bywatersolutions.com>
494 Jonathan Druart <jonathan.druart@bugs.koha-community.org>
495 Martin Renvoize <martin.renvoize@ptfs-europe.com>
497 =cut