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 );
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' );
98 elsif ( $self->is_in_processing ) {
99 Koha::Exceptions::Hold::CannotSuspendFound->throw( status => 'P' );
102 Koha::Exceptions::Hold::CannotSuspendFound->throw(
103 'Unhandled data exception on found hold (id='
112 $self->suspend_until($date);
115 logaction( 'HOLDS', 'SUSPEND', $self->reserve_id, $self )
116 if C4::Context->preference('HoldsLog');
123 my $hold = $hold->resume();
131 $self->suspend_until( undef );
135 logaction( 'HOLDS', 'RESUME', $self->reserve_id, $self )
136 if C4::Context->preference('HoldsLog');
150 my $deleted = $self->SUPER::delete($self);
152 logaction( 'HOLDS', 'DELETE', $self->reserve_id, $self )
153 if C4::Context->preference('HoldsLog');
177 my ( $self, $desk_id ) = @_;
181 my $today = dt_from_string();
184 waitingdate => $today->ymd,
188 my $max_pickup_delay = C4::Context->preference("ReservesMaxPickUpDelay");
189 my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
191 my $new_expiration_date = $today->clone->add(days => $max_pickup_delay);
193 if ( C4::Context->preference("ExcludeHolidaysFromMaxPickUpDelay") ) {
194 my $itemtype = $self->item ? $self->item->effective_itemtype : $self->biblio->itemtype;
195 my $daysmode = Koha::CirculationRules->get_effective_daysmode(
197 categorycode => $self->borrower->categorycode,
198 itemtype => $itemtype,
199 branchcode => $self->branchcode,
202 my $calendar = Koha::Calendar->new( branchcode => $self->branchcode, days_mode => $daysmode );
204 $new_expiration_date = $calendar->days_forward( dt_from_string(), $max_pickup_delay );
207 # If patron's requested expiration date is prior to the
208 # calculated one, we keep the patron's one.
209 if ( $self->patron_expiration_date ) {
210 my $requested_expiration = dt_from_string( $self->patron_expiration_date );
213 $requested_expiration
214 ? DateTime->compare( $requested_expiration, $new_expiration_date )
217 $new_expiration_date =
218 $cmp == -1 ? $requested_expiration : $new_expiration_date;
221 $values->{expirationdate} = $new_expiration_date->ymd;
223 $self->set($values)->store();
228 =head3 is_pickup_location_valid
230 if ($hold->is_pickup_location_valid({ library_id => $library->id }) ) {
234 Returns a I<boolean> representing if the passed pickup location is valid for the hold.
235 It throws a I<Koha::Exceptions::_MissingParameter> if the library_id parameter is not
240 sub is_pickup_location_valid {
241 my ( $self, $params ) = @_;
243 Koha::Exceptions::MissingParameter->throw('The library_id parameter is mandatory')
244 unless $params->{library_id};
246 my $pickup_locations;
248 if ( $self->itemnumber ) { # item-level
249 $pickup_locations = $self->item->pickup_locations({ patron => $self->patron });
251 else { # biblio-level
252 $pickup_locations = $self->biblio->pickup_locations({ patron => $self->patron });
255 return any { $_->branchcode eq $params->{library_id} } $pickup_locations->as_list;
258 =head3 set_pickup_location
260 $hold->set_pickup_location(
262 library_id => $library->id,
267 Updates the hold pickup location. It throws a I<Koha::Exceptions::Hold::InvalidPickupLocation> if
268 the passed pickup location is not valid.
270 Note: It is up to the caller to verify if I<AllowHoldPolicyOverride> is set when setting the
275 sub set_pickup_location {
276 my ( $self, $params ) = @_;
278 Koha::Exceptions::MissingParameter->throw('The library_id parameter is mandatory')
279 unless $params->{library_id};
283 || $self->is_pickup_location_valid(
284 { library_id => $params->{library_id} }
288 # all good, set the new pickup location
289 $self->branchcode( $params->{library_id} )->store;
292 Koha::Exceptions::Hold::InvalidPickupLocation->throw;
298 =head3 set_processing
300 $hold->set_processing;
302 Mark the hold as in processing.
318 Returns true if hold is waiting, in transit or in processing
325 return 0 unless $self->found();
326 return 1 if $self->found() eq 'W';
327 return 1 if $self->found() eq 'T';
328 return 1 if $self->found() eq 'P';
333 Returns true if hold is a waiting hold
340 my $found = $self->found;
341 return $found && $found eq 'W';
346 Returns true if hold is a in_transit hold
353 return 0 unless $self->found();
354 return $self->found() eq 'T';
357 =head3 is_in_processing
359 Returns true if hold is a in_processing hold
363 sub is_in_processing {
366 return 0 unless $self->found();
367 return $self->found() eq 'P';
370 =head3 is_cancelable_from_opac
372 Returns true if hold is a cancelable hold
374 Holds may be only canceled if they are not found.
376 This is used from the OPAC.
380 sub is_cancelable_from_opac {
383 return 1 unless $self->is_found();
384 return 0; # if ->is_in_transit or if ->is_waiting or ->is_in_processing
387 =head3 is_at_destination
389 Returns true if hold is waiting
390 and the hold's pickup branch matches
391 the hold item's holding branch
395 sub is_at_destination {
398 return $self->is_waiting() && ( $self->branchcode() eq $self->item()->holdingbranch() );
403 Returns the related Koha::Biblio object for this hold
410 $self->{_biblio} ||= Koha::Biblios->find( $self->biblionumber() );
412 return $self->{_biblio};
417 Returns the related Koha::Patron object for this hold
424 my $patron_rs = $self->_result->patron;
425 return Koha::Patron->_new_from_dbic($patron_rs);
430 Returns the related Koha::Item object for this Hold
437 $self->{_item} ||= Koha::Items->find( $self->itemnumber() );
439 return $self->{_item};
444 Returns the related Koha::Library object for this Hold
451 $self->{_branch} ||= Koha::Libraries->find( $self->branchcode() );
453 return $self->{_branch};
458 Returns the related Koha::Desk object for this Hold
464 my $desk_rs = $self->_result->desk;
465 return unless $desk_rs;
466 return Koha::Desk->_new_from_dbic($desk_rs);
471 Returns the related Koha::Patron object for this Hold
475 # FIXME Should be renamed with ->patron
479 $self->{_borrower} ||= Koha::Patrons->find( $self->borrowernumber() );
481 return $self->{_borrower};
486 my $bool = $hold->is_suspended();
493 return $self->suspend();
499 my $cancel_hold = $hold->cancel(
501 [ charge_cancel_fee => 1||0, ]
502 [ cancellation_reason => $cancellation_reason, ]
507 - The hold will be moved to the old_reserves table with a priority=0
508 - The priority of other holds will be updated
509 - The patron will be charge (see ExpireReservesMaxPickUpDelayCharge) if the charge_cancel_fee parameter is set
510 - The canceled hold will have the cancellation reason added to old_reserves.cancellation_reason if one is passed in
511 - a CANCEL HOLDS log will be done if the pref HoldsLog is on
516 my ( $self, $params ) = @_;
517 $self->_result->result_source->schema->txn_do(
519 $self->cancellationdate( dt_from_string->strftime( '%Y-%m-%d %H:%M:%S' ) );
521 $self->cancellation_reason( $params->{cancellation_reason} );
524 if ( $params->{cancellation_reason} ) {
525 my $letter = C4::Letters::GetPreparedLetter(
526 module => 'reserves',
527 letter_code => 'HOLD_CANCELLATION',
528 message_transport_type => 'email',
529 branchcode => $self->borrower->branchcode,
530 lang => $self->borrower->lang,
532 branches => $self->borrower->branchcode,
533 borrowers => $self->borrowernumber,
534 items => $self->itemnumber,
535 biblio => $self->biblionumber,
536 biblioitems => $self->biblionumber,
537 reserves => $self->unblessed,
542 C4::Letters::EnqueueLetter(
545 borrowernumber => $self->borrowernumber,
546 message_transport_type => 'email',
553 $self->SUPER::delete(); # Do not add a DELETE log
555 # now fix the priority on the others....
556 C4::Reserves::_FixPriority({ biblionumber => $self->biblionumber });
558 # and, if desired, charge a cancel fee
559 my $charge = C4::Context->preference("ExpireReservesMaxPickUpDelayCharge");
560 if ( $charge && $params->{'charge_cancel_fee'} ) {
562 Koha::Account->new( { patron_id => $self->borrowernumber } );
566 user_id => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
567 interface => C4::Context->interface,
568 library_id => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef,
569 type => 'RESERVE_EXPIRED',
570 item_id => $self->itemnumber
575 C4::Log::logaction( 'HOLDS', 'CANCEL', $self->reserve_id, $self )
576 if C4::Context->preference('HoldsLog');
584 Override base store method to set default
585 expirationdate for holds.
592 if ( !$self->in_storage ) {
593 if ( ! $self->expirationdate && $self->patron_expiration_date ) {
594 $self->expirationdate($self->patron_expiration_date);
598 C4::Context->preference('DefaultHoldExpirationdate')
599 and ( not defined $self->expirationdate
600 or $self->expirationdate eq '' )
603 $self->_set_default_expirationdate;
608 my %updated_columns = $self->_result->get_dirty_columns;
609 return $self->SUPER::store unless %updated_columns;
611 if ( exists $updated_columns{reservedate} ) {
613 C4::Context->preference('DefaultHoldExpirationdate')
614 and ( not exists $updated_columns{expirationdate}
615 or exists $updated_columns{expirationdate}
616 and $updated_columns{expirationdate} eq '' )
619 $self->_set_default_expirationdate;
624 $self = $self->SUPER::store;
627 sub _set_default_expirationdate {
630 my $period = C4::Context->preference('DefaultHoldExpirationdatePeriod') || 0;
632 C4::Context->preference('DefaultHoldExpirationdateUnitOfTime') || 'days';
634 $self->expirationdate(
635 dt_from_string( $self->reservedate )->add( $timeunit => $period ) );
640 my $is_moved = $hold->_move_to_old;
642 Move a hold to the old_reserve table following the same pattern as Koha::Patron->move_to_deleted
648 my $hold_infos = $self->unblessed;
649 return Koha::Old::Hold->new( $hold_infos )->store;
652 =head3 to_api_mapping
654 This method returns the mapping for representing a Koha::Hold object
661 reserve_id => 'hold_id',
662 borrowernumber => 'patron_id',
663 reservedate => 'hold_date',
664 biblionumber => 'biblio_id',
665 branchcode => 'pickup_library_id',
666 notificationdate => undef,
667 reminderdate => undef,
668 cancellationdate => 'cancellation_date',
669 reservenotes => 'notes',
671 itemnumber => 'item_id',
672 waitingdate => 'waiting_date',
673 expirationdate => 'expiration_date',
674 lowestPriority => 'lowest_priority',
675 suspend => 'suspended',
676 suspend_until => 'suspended_until',
677 itemtype => 'item_type',
678 item_level_hold => 'item_level',
682 =head2 Internal methods
694 Kyle M Hall <kyle@bywatersolutions.com>
695 Jonathan Druart <jonathan.druart@bugs.koha-community.org>
696 Martin Renvoize <martin.renvoize@ptfs-europe.com>