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>.
24 use Data::Dumper qw(Dumper);
25 use List::MoreUtils qw(any);
27 use C4::Context qw(preference);
30 use Koha::DateUtils qw(dt_from_string output_pref);
38 use Koha::Exceptions::Hold;
40 use base qw(Koha::Object);
44 Koha::Hold - Koha Hold object class
54 returns the number of days since a hold was placed, optionally
57 my $age = $hold->age( $use_calendar );
62 my ( $self, $use_calendar ) = @_;
64 my $today = dt_from_string;
67 if ( $use_calendar ) {
68 my $calendar = Koha::Calendar->new( branchcode => $self->branchcode );
69 $age = $calendar->days_between( dt_from_string( $self->reservedate ), $today );
72 $age = $today->delta_days( dt_from_string( $self->reservedate ) );
75 $age = $age->in_units( 'days' );
82 my $hold = $hold->suspend_hold( $suspend_until_dt );
87 my ( $self, $dt ) = @_;
89 my $date = $dt ? $dt->clone()->truncate( to => 'day' )->datetime : undef;
91 if ( $self->is_found ) { # We can't suspend found holds
92 if ( $self->is_waiting ) {
93 Koha::Exceptions::Hold::CannotSuspendFound->throw( status => 'W' );
95 elsif ( $self->is_in_transit ) {
96 Koha::Exceptions::Hold::CannotSuspendFound->throw( status => 'T' );
99 Koha::Exceptions::Hold::CannotSuspendFound->throw(
100 'Unhandled data exception on found hold (id='
109 $self->suspend_until($date);
112 logaction( 'HOLDS', 'SUSPEND', $self->reserve_id, Dumper( $self->unblessed ) )
113 if C4::Context->preference('HoldsLog');
120 my $hold = $hold->resume();
128 $self->suspend_until( undef );
132 logaction( 'HOLDS', 'RESUME', $self->reserve_id, Dumper($self->unblessed) )
133 if C4::Context->preference('HoldsLog');
147 my $deleted = $self->SUPER::delete($self);
149 logaction( 'HOLDS', 'DELETE', $self->reserve_id, Dumper($self->unblessed) )
150 if C4::Context->preference('HoldsLog');
160 my ( $self, $transferToDo ) = @_;
165 $self->found('T')->store();
169 my $today = dt_from_string();
172 waitingdate => $today->ymd,
175 my $requested_expiration;
176 if ($self->expirationdate) {
177 $requested_expiration = dt_from_string($self->expirationdate);
180 my $max_pickup_delay = C4::Context->preference("ReservesMaxPickUpDelay");
181 my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
182 my $calendar = Koha::Calendar->new( branchcode => $self->branchcode );
184 my $expirationdate = $today->clone;
185 $expirationdate->add(days => $max_pickup_delay);
187 if ( C4::Context->preference("ExcludeHolidaysFromMaxPickUpDelay") ) {
188 $expirationdate = $calendar->days_forward( dt_from_string(), $max_pickup_delay );
191 # If patron's requested expiration date is prior to the
192 # calculated one, we keep the patron's one.
193 my $cmp = $requested_expiration ? DateTime->compare($requested_expiration, $expirationdate) : 0;
194 $values->{expirationdate} = $cmp == -1 ? $requested_expiration->ymd : $expirationdate->ymd;
196 $self->set($values)->store();
201 =head3 is_pickup_location_valid
203 if ($hold->is_pickup_location_valid({ library_id => $library->id }) ) {
207 Returns a I<boolean> representing if the passed pickup location is valid for the hold.
208 It throws a I<Koha::Exceptions::_MissingParameter> if the library_id parameter is not
213 sub is_pickup_location_valid {
214 my ( $self, $params ) = @_;
216 Koha::Exceptions::MissingParameter->throw('The library_id parameter is mandatory')
217 unless $params->{library_id};
219 my @pickup_locations;
221 if ( $self->itemnumber ) { # item-level
222 @pickup_locations = $self->item->pickup_locations({ patron => $self->patron });
224 else { # biblio-level
225 @pickup_locations = $self->biblio->pickup_locations({ patron => $self->patron });
228 return any { $_->branchcode eq $params->{library_id} } @pickup_locations;
231 =head3 set_pickup_location
233 $hold->set_pickup_location({ library_id => $library->id });
235 Updates the hold pickup location. It throws a I<Koha::Exceptions::Hold::InvalidPickupLocation> if
236 the passed pickup location is not valid.
240 sub set_pickup_location {
241 my ( $self, $params ) = @_;
243 Koha::Exceptions::MissingParameter->throw('The library_id parameter is mandatory')
244 unless $params->{library_id};
246 if ( $self->is_pickup_location_valid({ library_id => $params->{library_id} }) ) {
247 # all good, set the new pickup location
248 $self->branchcode( $params->{library_id} )->store;
251 Koha::Exceptions::Hold::InvalidPickupLocation->throw;
257 =head3 set_processing
259 $hold->set_processing;
261 Mark the hold as in processing.
277 Returns true if hold is a waiting or in transit
284 return 0 unless $self->found();
285 return 1 if $self->found() eq 'W';
286 return 1 if $self->found() eq 'T';
291 Returns true if hold is a waiting hold
298 my $found = $self->found;
299 return $found && $found eq 'W';
304 Returns true if hold is a in_transit hold
311 return 0 unless $self->found();
312 return $self->found() eq 'T';
315 =head3 is_cancelable_from_opac
317 Returns true if hold is a cancelable hold
319 Holds may be only canceled if they are not found.
321 This is used from the OPAC.
325 sub is_cancelable_from_opac {
328 return 1 unless $self->is_found();
329 return 0; # if ->is_in_transit or if ->is_waiting
332 =head3 is_at_destination
334 Returns true if hold is waiting
335 and the hold's pickup branch matches
336 the hold item's holding branch
340 sub is_at_destination {
343 return $self->is_waiting() && ( $self->branchcode() eq $self->item()->holdingbranch() );
348 Returns the related Koha::Biblio object for this hold
355 $self->{_biblio} ||= Koha::Biblios->find( $self->biblionumber() );
357 return $self->{_biblio};
362 Returns the related Koha::Patron object for this hold
369 my $patron_rs = $self->_result->patron;
370 return Koha::Patron->_new_from_dbic($patron_rs);
375 Returns the related Koha::Item object for this Hold
382 $self->{_item} ||= Koha::Items->find( $self->itemnumber() );
384 return $self->{_item};
389 Returns the related Koha::Library object for this Hold
396 $self->{_branch} ||= Koha::Libraries->find( $self->branchcode() );
398 return $self->{_branch};
403 Returns the related Koha::Patron object for this Hold
407 # FIXME Should be renamed with ->patron
411 $self->{_borrower} ||= Koha::Patrons->find( $self->borrowernumber() );
413 return $self->{_borrower};
418 my $bool = $hold->is_suspended();
425 return $self->suspend();
431 my $cancel_hold = $hold->cancel();
434 - The hold will be moved to the old_reserves table with a priority=0
435 - The priority of other holds will be updated
436 - The patron will be charge (see ExpireReservesMaxPickUpDelayCharge) if the charge_cancel_fee parameter is set
437 - a CANCEL HOLDS log will be done if the pref HoldsLog is on
442 my ( $self, $params ) = @_;
443 $self->_result->result_source->schema->txn_do(
445 $self->cancellationdate( dt_from_string->strftime( '%Y-%m-%d %H:%M:%S' ) );
448 $self->SUPER::delete(); # Do not add a DELETE log
450 # now fix the priority on the others....
451 C4::Reserves::_FixPriority({ biblionumber => $self->biblionumber });
453 # and, if desired, charge a cancel fee
454 my $charge = C4::Context->preference("ExpireReservesMaxPickUpDelayCharge");
455 if ( $charge && $params->{'charge_cancel_fee'} ) {
457 Koha::Account->new( { patron_id => $self->borrowernumber } );
461 user_id => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
462 interface => C4::Context->interface,
463 library_id => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef,
464 type => 'RESERVE_EXPIRED',
465 item_id => $self->itemnumber
470 C4::Log::logaction( 'HOLDS', 'CANCEL', $self->reserve_id, Dumper($self->unblessed) )
471 if C4::Context->preference('HoldsLog');
479 my $is_moved = $hold->_move_to_old;
481 Move a hold to the old_reserve table following the same pattern as Koha::Patron->move_to_deleted
487 my $hold_infos = $self->unblessed;
488 return Koha::Old::Hold->new( $hold_infos )->store;
491 =head3 to_api_mapping
493 This method returns the mapping for representing a Koha::Hold object
500 reserve_id => 'hold_id',
501 borrowernumber => 'patron_id',
502 reservedate => 'hold_date',
503 biblionumber => 'biblio_id',
504 branchcode => 'pickup_library_id',
505 notificationdate => undef,
506 reminderdate => undef,
507 cancellationdate => 'cancellation_date',
508 reservenotes => 'notes',
510 itemnumber => 'item_id',
511 waitingdate => 'waiting_date',
512 expirationdate => 'expiration_date',
513 lowestPriority => 'lowest_priority',
514 suspend => 'suspended',
515 suspend_until => 'suspended_until',
516 itemtype => 'item_type',
517 item_level_hold => 'item_level',
521 =head2 Internal methods
533 Kyle M Hall <kyle@bywatersolutions.com>
534 Jonathan Druart <jonathan.druart@bugs.koha-community.org>
535 Martin Renvoize <martin.renvoize@ptfs-europe.com>