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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 3 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 use Data::Dumper qw(Dumper);
26 use C4::Context qw(preference);
29 use Koha::DateUtils qw(dt_from_string output_pref);
37 use base qw(Koha::Object);
41 Koha::Hold - Koha Hold object class
51 returns the number of days since a hold was placed, optionally
54 my $age = $hold->age( $use_calendar );
59 my ( $self, $use_calendar ) = @_;
61 my $today = dt_from_string;
64 if ( $use_calendar ) {
65 my $calendar = Koha::Calendar->new( branchcode => $self->branchcode );
66 $age = $calendar->days_between( dt_from_string( $self->reservedate ), $today );
69 $age = $today->delta_days( dt_from_string( $self->reservedate ) );
72 $age = $age->in_units( 'days' );
79 my $hold = $hold->suspend_hold( $suspend_until_dt );
84 my ( $self, $dt ) = @_;
86 $dt = $dt ? $dt->clone()->truncate( to => 'day' ) : undef;
88 if ( $self->is_waiting ) { # We can't suspend waiting holds
89 carp "Unable to suspend waiting hold!";
95 $self->suspend_until( $dt->ymd );
97 $self->suspend_until( $dt );
101 logaction( 'HOLDS', 'SUSPEND', $self->reserve_id, Dumper($self->unblessed) )
102 if C4::Context->preference('HoldsLog');
109 my $hold = $hold->resume();
117 $self->suspend_until( undef );
121 logaction( 'HOLDS', 'RESUME', $self->reserve_id, Dumper($self->unblessed) )
122 if C4::Context->preference('HoldsLog');
136 my $deleted = $self->SUPER::delete($self);
138 logaction( 'HOLDS', 'DELETE', $self->reserve_id, Dumper($self->unblessed) )
139 if C4::Context->preference('HoldsLog');
149 my ( $self, $transferToDo ) = @_;
154 $self->found('T')->store();
158 my $today = dt_from_string();
161 waitingdate => $today->ymd,
164 my $requested_expiration;
165 if ($self->expirationdate) {
166 $requested_expiration = dt_from_string($self->expirationdate);
169 my $max_pickup_delay = C4::Context->preference("ReservesMaxPickUpDelay");
170 my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
171 my $calendar = Koha::Calendar->new( branchcode => $self->branchcode );
173 my $expirationdate = $today->clone;
174 $expirationdate->add(days => $max_pickup_delay);
176 if ( C4::Context->preference("ExcludeHolidaysFromMaxPickUpDelay") ) {
177 $expirationdate = $calendar->days_forward( dt_from_string(), $max_pickup_delay );
180 # If patron's requested expiration date is prior to the
181 # calculated one, we keep the patron's one.
182 my $cmp = $requested_expiration ? DateTime->compare($requested_expiration, $expirationdate) : 0;
183 $values->{expirationdate} = $cmp == -1 ? $requested_expiration->ymd : $expirationdate->ymd;
185 $self->set($values)->store();
192 Returns true if hold is a waiting or in transit
199 return 0 unless $self->found();
200 return 1 if $self->found() eq 'W';
201 return 1 if $self->found() eq 'T';
206 Returns true if hold is a waiting hold
213 my $found = $self->found;
214 return $found && $found eq 'W';
219 Returns true if hold is a in_transit hold
226 return 0 unless $self->found();
227 return $self->found() eq 'T';
232 Returns true if hold is a cancelable hold
234 Holds may be canceled if they not found, or
235 are found and waiting. A hold found but in
236 transit cannot be canceled.
243 return 1 unless $self->is_found();
244 return 0 if $self->is_in_transit();
245 return 1 if $self->is_waiting();
249 =head3 is_at_destination
251 Returns true if hold is waiting
252 and the hold's pickup branch matches
253 the hold item's holding branch
257 sub is_at_destination {
260 return $self->is_waiting() && ( $self->branchcode() eq $self->item()->holdingbranch() );
265 Returns the related Koha::Biblio object for this hold
272 $self->{_biblio} ||= Koha::Biblios->find( $self->biblionumber() );
274 return $self->{_biblio};
279 Returns the related Koha::Item object for this Hold
286 $self->{_item} ||= Koha::Items->find( $self->itemnumber() );
288 return $self->{_item};
293 Returns the related Koha::Library object for this Hold
300 $self->{_branch} ||= Koha::Libraries->find( $self->branchcode() );
302 return $self->{_branch};
307 Returns the related Koha::Patron object for this Hold
311 # FIXME Should be renamed with ->patron
315 $self->{_borrower} ||= Koha::Patrons->find( $self->borrowernumber() );
317 return $self->{_borrower};
322 my $bool = $hold->is_suspended();
329 return $self->suspend();
335 my $cancel_hold = $hold->cancel();
338 - The hold will be moved to the old_reserves table with a priority=0
339 - The priority of other holds will be updated
340 - The patron will be charge (see ExpireReservesMaxPickUpDelayCharge) if the charge_cancel_fee parameter is set
341 - a CANCEL HOLDS log will be done if the pref HoldsLog is on
346 my ( $self, $params ) = @_;
347 $self->_result->result_source->schema->txn_do(
349 $self->cancellationdate(dt_from_string);
352 $self->SUPER::delete(); # Do not add a DELETE log
354 # now fix the priority on the others....
355 C4::Reserves::_FixPriority({ biblionumber => $self->biblionumber });
357 # and, if desired, charge a cancel fee
358 my $charge = C4::Context->preference("ExpireReservesMaxPickUpDelayCharge");
359 if ( $charge && $params->{'charge_cancel_fee'} ) {
360 C4::Accounts::manualinvoice($self->borrowernumber, $self->itemnumber, '', 'HE', $charge);
363 C4::Log::logaction( 'HOLDS', 'CANCEL', $self->reserve_id, Dumper($self->unblessed) )
364 if C4::Context->preference('HoldsLog');
372 my $is_moved = $hold->_move_to_old;
374 Move a hold to the old_reserve table following the same pattern as Koha::Patron->move_to_deleted
380 my $hold_infos = $self->unblessed;
381 return Koha::Old::Hold->new( $hold_infos )->store;
394 Kyle M Hall <kyle@bywatersolutions.com>
396 Jonathan Druart <jonathan.druart@bugs.koha-community.org>