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>.
23 use List::MoreUtils qw( any );
26 use C4::Letters qw( GetPreparedLetter EnqueueLetter );
27 use C4::Log qw( logaction );
29 use Koha::AuthorisedValues;
30 use Koha::DateUtils qw( dt_from_string );
39 use Koha::Exceptions::Hold;
41 use base qw(Koha::Object);
45 Koha::Hold - Koha Hold object class
55 returns the number of days since a hold was placed, optionally
58 my $age = $hold->age( $use_calendar );
63 my ( $self, $use_calendar ) = @_;
65 my $today = dt_from_string;
68 if ( $use_calendar ) {
69 my $calendar = Koha::Calendar->new( branchcode => $self->branchcode );
70 $age = $calendar->days_between( dt_from_string( $self->reservedate ), $today );
73 $age = $today->delta_days( dt_from_string( $self->reservedate ) );
76 $age = $age->in_units( 'days' );
83 my $hold = $hold->suspend_hold( $suspend_until_dt );
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 elsif ( $self->is_in_processing ) {
100 Koha::Exceptions::Hold::CannotSuspendFound->throw( status => 'P' );
103 Koha::Exceptions::Hold::CannotSuspendFound->throw(
104 'Unhandled data exception on found hold (id='
113 $self->suspend_until($date);
120 payload => { hold => $self->get_from_storage }
124 logaction( 'HOLDS', 'SUSPEND', $self->reserve_id, $self )
125 if C4::Context->preference('HoldsLog');
132 my $hold = $hold->resume();
140 $self->suspend_until( undef );
148 payload => { hold => $self->get_from_storage }
152 logaction( 'HOLDS', 'RESUME', $self->reserve_id, $self )
153 if C4::Context->preference('HoldsLog');
167 my $deleted = $self->SUPER::delete($self);
169 logaction( 'HOLDS', 'DELETE', $self->reserve_id, $self )
170 if C4::Context->preference('HoldsLog');
194 my ( $self, $desk_id ) = @_;
198 my $today = dt_from_string();
202 ( !$self->waitingdate ? ( waitingdate => $today->ymd ) : () ),
206 my $requested_expiration;
207 if ($self->expirationdate) {
208 $requested_expiration = dt_from_string($self->expirationdate);
211 my $max_pickup_delay = C4::Context->preference("ReservesMaxPickUpDelay");
212 my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
214 my $expirationdate = $today->clone;
215 $expirationdate->add(days => $max_pickup_delay);
217 if ( C4::Context->preference("ExcludeHolidaysFromMaxPickUpDelay") ) {
218 my $itemtype = $self->item ? $self->item->effective_itemtype : $self->biblio->itemtype;
219 my $daysmode = Koha::CirculationRules->get_effective_daysmode(
221 categorycode => $self->borrower->categorycode,
222 itemtype => $itemtype,
223 branchcode => $self->branchcode,
226 my $calendar = Koha::Calendar->new( branchcode => $self->branchcode, days_mode => $daysmode );
228 $expirationdate = $calendar->days_forward( dt_from_string(), $max_pickup_delay );
231 # If patron's requested expiration date is prior to the
232 # calculated one, we keep the patron's one.
233 my $cmp = $requested_expiration ? DateTime->compare($requested_expiration, $expirationdate) : 0;
234 $values->{expirationdate} = $cmp == -1 ? $requested_expiration->ymd : $expirationdate->ymd;
236 $self->set($values)->store();
241 =head3 is_pickup_location_valid
243 if ($hold->is_pickup_location_valid({ library_id => $library->id }) ) {
247 Returns a I<boolean> representing if the passed pickup location is valid for the hold.
248 It throws a I<Koha::Exceptions::_MissingParameter> if the library_id parameter is not
253 sub is_pickup_location_valid {
254 my ( $self, $params ) = @_;
256 Koha::Exceptions::MissingParameter->throw('The library_id parameter is mandatory')
257 unless $params->{library_id};
259 my $pickup_locations;
261 if ( $self->itemnumber ) { # item-level
262 $pickup_locations = $self->item->pickup_locations({ patron => $self->patron });
264 else { # biblio-level
265 $pickup_locations = $self->biblio->pickup_locations({ patron => $self->patron });
268 return any { $_->branchcode eq $params->{library_id} } $pickup_locations->as_list;
271 =head3 set_pickup_location
273 $hold->set_pickup_location(
275 library_id => $library->id,
280 Updates the hold pickup location. It throws a I<Koha::Exceptions::Hold::InvalidPickupLocation> if
281 the passed pickup location is not valid.
283 Note: It is up to the caller to verify if I<AllowHoldPolicyOverride> is set when setting the
288 sub set_pickup_location {
289 my ( $self, $params ) = @_;
291 Koha::Exceptions::MissingParameter->throw('The library_id parameter is mandatory')
292 unless $params->{library_id};
296 || $self->is_pickup_location_valid(
297 { library_id => $params->{library_id} }
301 # all good, set the new pickup location
302 $self->branchcode( $params->{library_id} )->store;
305 Koha::Exceptions::Hold::InvalidPickupLocation->throw;
311 =head3 set_processing
313 $hold->set_processing;
315 Mark the hold as in processing.
331 Returns true if hold is waiting, in transit or in processing
338 return 0 unless $self->found();
339 return 1 if $self->found() eq 'W';
340 return 1 if $self->found() eq 'T';
341 return 1 if $self->found() eq 'P';
346 Returns true if hold is a waiting hold
353 my $found = $self->found;
354 return $found && $found eq 'W';
359 Returns true if hold is a in_transit hold
366 return 0 unless $self->found();
367 return $self->found() eq 'T';
370 =head3 is_in_processing
372 Returns true if hold is a in_processing hold
376 sub is_in_processing {
379 return 0 unless $self->found();
380 return $self->found() eq 'P';
383 =head3 is_cancelable_from_opac
385 Returns true if hold is a cancelable hold
387 Holds may be only canceled if they are not found.
389 This is used from the OPAC.
393 sub is_cancelable_from_opac {
396 return 1 unless $self->is_found();
397 return 0; # if ->is_in_transit or if ->is_waiting or ->is_in_processing
400 =head3 is_at_destination
402 Returns true if hold is waiting
403 and the hold's pickup branch matches
404 the hold item's holding branch
408 sub is_at_destination {
411 return $self->is_waiting() && ( $self->branchcode() eq $self->item()->holdingbranch() );
416 Returns the related Koha::Biblio object for this hold
423 $self->{_biblio} ||= Koha::Biblios->find( $self->biblionumber() );
425 return $self->{_biblio};
430 Returns the related Koha::Patron object for this hold
437 my $patron_rs = $self->_result->patron;
438 return Koha::Patron->_new_from_dbic($patron_rs);
443 Returns the related Koha::Item object for this Hold
450 $self->{_item} ||= Koha::Items->find( $self->itemnumber() );
452 return $self->{_item};
457 Returns the related Koha::Library object for this Hold
464 $self->{_branch} ||= Koha::Libraries->find( $self->branchcode() );
466 return $self->{_branch};
471 Returns the related Koha::Desk object for this Hold
477 my $desk_rs = $self->_result->desk;
478 return unless $desk_rs;
479 return Koha::Desk->_new_from_dbic($desk_rs);
484 Returns the related Koha::Patron object for this Hold
488 # FIXME Should be renamed with ->patron
492 $self->{_borrower} ||= Koha::Patrons->find( $self->borrowernumber() );
494 return $self->{_borrower};
499 my $bool = $hold->is_suspended();
506 return $self->suspend();
512 my $cancel_hold = $hold->cancel(
514 [ charge_cancel_fee => 1||0, ]
515 [ cancellation_reason => $cancellation_reason, ]
520 - The hold will be moved to the old_reserves table with a priority=0
521 - The priority of other holds will be updated
522 - The patron will be charge (see ExpireReservesMaxPickUpDelayCharge) if the charge_cancel_fee parameter is set
523 - The canceled hold will have the cancellation reason added to old_reserves.cancellation_reason if one is passed in
524 - a CANCEL HOLDS log will be done if the pref HoldsLog is on
529 my ( $self, $params ) = @_;
530 $self->_result->result_source->schema->txn_do(
532 $self->cancellationdate( dt_from_string->strftime( '%Y-%m-%d %H:%M:%S' ) );
534 $self->cancellation_reason( $params->{cancellation_reason} );
537 if ( $params->{cancellation_reason} ) {
538 my $letter = C4::Letters::GetPreparedLetter(
539 module => 'reserves',
540 letter_code => 'HOLD_CANCELLATION',
541 message_transport_type => 'email',
542 branchcode => $self->borrower->branchcode,
543 lang => $self->borrower->lang,
545 branches => $self->borrower->branchcode,
546 borrowers => $self->borrowernumber,
547 items => $self->itemnumber,
548 biblio => $self->biblionumber,
549 biblioitems => $self->biblionumber,
550 reserves => $self->unblessed,
555 C4::Letters::EnqueueLetter(
558 borrowernumber => $self->borrowernumber,
559 message_transport_type => 'email',
565 my $old_me = $self->_move_to_old;
571 payload => { hold => $old_me->get_from_storage }
575 $self->SUPER::delete(); # Do not add a DELETE log
577 # now fix the priority on the others....
578 C4::Reserves::_FixPriority({ biblionumber => $self->biblionumber });
580 # and, if desired, charge a cancel fee
581 my $charge = C4::Context->preference("ExpireReservesMaxPickUpDelayCharge");
582 if ( $charge && $params->{'charge_cancel_fee'} ) {
584 Koha::Account->new( { patron_id => $self->borrowernumber } );
588 user_id => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
589 interface => C4::Context->interface,
590 library_id => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef,
591 type => 'RESERVE_EXPIRED',
592 item_id => $self->itemnumber
597 C4::Log::logaction( 'HOLDS', 'CANCEL', $self->reserve_id, $self )
598 if C4::Context->preference('HoldsLog');
606 Override base store method to set default
607 expirationdate for holds.
614 if ( !$self->in_storage ) {
616 C4::Context->preference('DefaultHoldExpirationdate')
617 and ( not defined $self->expirationdate
618 or $self->expirationdate eq '' )
621 $self->_set_default_expirationdate;
626 my %updated_columns = $self->_result->get_dirty_columns;
627 return $self->SUPER::store unless %updated_columns;
629 if ( exists $updated_columns{reservedate} ) {
631 C4::Context->preference('DefaultHoldExpirationdate')
632 and ( not exists $updated_columns{expirationdate}
633 or exists $updated_columns{expirationdate}
634 and $updated_columns{expirationdate} eq '' )
637 $self->_set_default_expirationdate;
642 $self = $self->SUPER::store;
645 sub _set_default_expirationdate {
648 my $period = C4::Context->preference('DefaultHoldExpirationdatePeriod') || 0;
650 C4::Context->preference('DefaultHoldExpirationdateUnitOfTime') || 'days';
652 $self->expirationdate(
653 dt_from_string( $self->reservedate )->add( $timeunit => $period ) );
658 my $is_moved = $hold->_move_to_old;
660 Move a hold to the old_reserve table following the same pattern as Koha::Patron->move_to_deleted
666 my $hold_infos = $self->unblessed;
667 return Koha::Old::Hold->new( $hold_infos )->store;
670 =head3 to_api_mapping
672 This method returns the mapping for representing a Koha::Hold object
679 reserve_id => 'hold_id',
680 borrowernumber => 'patron_id',
681 reservedate => 'hold_date',
682 biblionumber => 'biblio_id',
683 branchcode => 'pickup_library_id',
684 notificationdate => undef,
685 reminderdate => undef,
686 cancellationdate => 'cancellation_date',
687 reservenotes => 'notes',
689 itemnumber => 'item_id',
690 waitingdate => 'waiting_date',
691 expirationdate => 'expiration_date',
692 lowestPriority => 'lowest_priority',
693 suspend => 'suspended',
694 suspend_until => 'suspended_until',
695 itemtype => 'item_type',
696 item_level_hold => 'item_level',
700 =head2 Internal methods
712 Kyle M Hall <kyle@bywatersolutions.com>
713 Jonathan Druart <jonathan.druart@bugs.koha-community.org>
714 Martin Renvoize <martin.renvoize@ptfs-europe.com>