Bug 26922: Regression tests
[koha.git] / Koha / Hold.pm
blob7754691238b8cf997359fb4c18eb346e2b05b811
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 elsif ( $self->is_in_processing ) {
100 Koha::Exceptions::Hold::CannotSuspendFound->throw( status => 'P' );
102 else {
103 Koha::Exceptions::Hold::CannotSuspendFound->throw(
104 'Unhandled data exception on found hold (id='
105 . $self->id
106 . ', found='
107 . $self->found
108 . ')' );
112 $self->suspend(1);
113 $self->suspend_until($date);
114 $self->store();
116 logaction( 'HOLDS', 'SUSPEND', $self->reserve_id, Dumper( $self->unblessed ) )
117 if C4::Context->preference('HoldsLog');
119 return $self;
122 =head3 resume
124 my $hold = $hold->resume();
126 =cut
128 sub resume {
129 my ( $self ) = @_;
131 $self->suspend(0);
132 $self->suspend_until( undef );
134 $self->store();
136 logaction( 'HOLDS', 'RESUME', $self->reserve_id, Dumper($self->unblessed) )
137 if C4::Context->preference('HoldsLog');
139 return $self;
142 =head3 delete
144 $hold->delete();
146 =cut
148 sub delete {
149 my ( $self ) = @_;
151 my $deleted = $self->SUPER::delete($self);
153 logaction( 'HOLDS', 'DELETE', $self->reserve_id, Dumper($self->unblessed) )
154 if C4::Context->preference('HoldsLog');
156 return $deleted;
159 =head3 set_transfer
161 =cut
163 sub set_transfer {
164 my ( $self ) = @_;
166 $self->priority(0);
167 $self->found('T');
168 $self->store();
170 return $self;
173 =head3 set_waiting
175 =cut
177 sub set_waiting {
178 my ( $self, $desk_id ) = @_;
180 $self->priority(0);
182 my $today = dt_from_string();
183 my $values = {
184 found => 'W',
185 waitingdate => $today->ymd,
186 desk_id => $desk_id,
189 my $requested_expiration;
190 if ($self->expirationdate) {
191 $requested_expiration = dt_from_string($self->expirationdate);
194 my $max_pickup_delay = C4::Context->preference("ReservesMaxPickUpDelay");
195 my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
197 my $expirationdate = $today->clone;
198 $expirationdate->add(days => $max_pickup_delay);
200 if ( C4::Context->preference("ExcludeHolidaysFromMaxPickUpDelay") ) {
201 my $itemtype = $self->item ? $self->item->effective_itemtype : $self->biblio->itemtype;
202 my $daysmode = Koha::CirculationRules->get_effective_daysmode(
204 categorycode => $self->borrower->categorycode,
205 itemtype => $itemtype,
206 branchcode => $self->branchcode,
209 my $calendar = Koha::Calendar->new( branchcode => $self->branchcode, days_mode => $daysmode );
211 $expirationdate = $calendar->days_forward( dt_from_string(), $max_pickup_delay );
214 # If patron's requested expiration date is prior to the
215 # calculated one, we keep the patron's one.
216 my $cmp = $requested_expiration ? DateTime->compare($requested_expiration, $expirationdate) : 0;
217 $values->{expirationdate} = $cmp == -1 ? $requested_expiration->ymd : $expirationdate->ymd;
219 $self->set($values)->store();
221 return $self;
224 =head3 set_processing
226 $hold->set_processing;
228 Mark the hold as in processing.
230 =cut
232 sub set_processing {
233 my ( $self ) = @_;
235 $self->priority(0);
236 $self->found('P');
237 $self->store();
239 return $self;
242 =head3 is_found
244 Returns true if hold is waiting, in transit or in processing
246 =cut
248 sub is_found {
249 my ($self) = @_;
251 return 0 unless $self->found();
252 return 1 if $self->found() eq 'W';
253 return 1 if $self->found() eq 'T';
254 return 1 if $self->found() eq 'P';
257 =head3 is_waiting
259 Returns true if hold is a waiting hold
261 =cut
263 sub is_waiting {
264 my ($self) = @_;
266 my $found = $self->found;
267 return $found && $found eq 'W';
270 =head3 is_in_transit
272 Returns true if hold is a in_transit hold
274 =cut
276 sub is_in_transit {
277 my ($self) = @_;
279 return 0 unless $self->found();
280 return $self->found() eq 'T';
283 =head3 is_in_processing
285 Returns true if hold is a in_processing hold
287 =cut
289 sub is_in_processing {
290 my ($self) = @_;
292 return 0 unless $self->found();
293 return $self->found() eq 'P';
296 =head3 is_cancelable_from_opac
298 Returns true if hold is a cancelable hold
300 Holds may be only canceled if they are not found.
302 This is used from the OPAC.
304 =cut
306 sub is_cancelable_from_opac {
307 my ($self) = @_;
309 return 1 unless $self->is_found();
310 return 0; # if ->is_in_transit or if ->is_waiting or ->is_in_processing
313 =head3 is_at_destination
315 Returns true if hold is waiting
316 and the hold's pickup branch matches
317 the hold item's holding branch
319 =cut
321 sub is_at_destination {
322 my ($self) = @_;
324 return $self->is_waiting() && ( $self->branchcode() eq $self->item()->holdingbranch() );
327 =head3 biblio
329 Returns the related Koha::Biblio object for this hold
331 =cut
333 sub biblio {
334 my ($self) = @_;
336 $self->{_biblio} ||= Koha::Biblios->find( $self->biblionumber() );
338 return $self->{_biblio};
341 =head3 patron
343 Returns the related Koha::Patron object for this hold
345 =cut
347 sub patron {
348 my ($self) = @_;
350 my $patron_rs = $self->_result->patron;
351 return Koha::Patron->_new_from_dbic($patron_rs);
354 =head3 item
356 Returns the related Koha::Item object for this Hold
358 =cut
360 sub item {
361 my ($self) = @_;
363 $self->{_item} ||= Koha::Items->find( $self->itemnumber() );
365 return $self->{_item};
368 =head3 branch
370 Returns the related Koha::Library object for this Hold
372 =cut
374 sub branch {
375 my ($self) = @_;
377 $self->{_branch} ||= Koha::Libraries->find( $self->branchcode() );
379 return $self->{_branch};
382 =head3 desk
384 Returns the related Koha::Desk object for this Hold
386 =cut
388 sub desk {
389 my $self = shift;
390 my $desk_rs = $self->_result->desk;
391 return unless $desk_rs;
392 return Koha::Desk->_new_from_dbic($desk_rs);
395 =head3 borrower
397 Returns the related Koha::Patron object for this Hold
399 =cut
401 # FIXME Should be renamed with ->patron
402 sub borrower {
403 my ($self) = @_;
405 $self->{_borrower} ||= Koha::Patrons->find( $self->borrowernumber() );
407 return $self->{_borrower};
410 =head3 is_suspended
412 my $bool = $hold->is_suspended();
414 =cut
416 sub is_suspended {
417 my ( $self ) = @_;
419 return $self->suspend();
423 =head3 cancel
425 my $cancel_hold = $hold->cancel(
427 [ charge_cancel_fee => 1||0, ]
428 [ cancellation_reason => $cancellation_reason, ]
432 Cancel a hold:
433 - The hold will be moved to the old_reserves table with a priority=0
434 - The priority of other holds will be updated
435 - The patron will be charge (see ExpireReservesMaxPickUpDelayCharge) if the charge_cancel_fee parameter is set
436 - The canceled hold will have the cancellation reason added to old_reserves.cancellation_reason if one is passed in
437 - a CANCEL HOLDS log will be done if the pref HoldsLog is on
439 =cut
441 sub cancel {
442 my ( $self, $params ) = @_;
443 $self->_result->result_source->schema->txn_do(
444 sub {
445 $self->cancellationdate( dt_from_string->strftime( '%Y-%m-%d %H:%M:%S' ) );
446 $self->priority(0);
447 $self->cancellation_reason( $params->{cancellation_reason} );
448 $self->store();
450 if ( $params->{cancellation_reason} ) {
451 my $letter = C4::Letters::GetPreparedLetter(
452 module => 'reserves',
453 letter_code => 'HOLD_CANCELLATION',
454 message_transport_type => 'email',
455 branchcode => $self->borrower->branchcode,
456 lang => $self->borrower->lang,
457 tables => {
458 branches => $self->borrower->branchcode,
459 borrowers => $self->borrowernumber,
460 items => $self->itemnumber,
461 biblio => $self->biblionumber,
462 biblioitems => $self->biblionumber,
463 reserves => $self->unblessed,
467 if ($letter) {
468 C4::Letters::EnqueueLetter(
470 letter => $letter,
471 borrowernumber => $self->borrowernumber,
472 message_transport_type => 'email',
478 $self->_move_to_old;
479 $self->SUPER::delete(); # Do not add a DELETE log
481 # now fix the priority on the others....
482 C4::Reserves::_FixPriority({ biblionumber => $self->biblionumber });
484 # and, if desired, charge a cancel fee
485 my $charge = C4::Context->preference("ExpireReservesMaxPickUpDelayCharge");
486 if ( $charge && $params->{'charge_cancel_fee'} ) {
487 my $account =
488 Koha::Account->new( { patron_id => $self->borrowernumber } );
489 $account->add_debit(
491 amount => $charge,
492 user_id => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
493 interface => C4::Context->interface,
494 library_id => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef,
495 type => 'RESERVE_EXPIRED',
496 item_id => $self->itemnumber
501 C4::Log::logaction( 'HOLDS', 'CANCEL', $self->reserve_id, Dumper($self->unblessed) )
502 if C4::Context->preference('HoldsLog');
505 return $self;
508 =head3 _move_to_old
510 my $is_moved = $hold->_move_to_old;
512 Move a hold to the old_reserve table following the same pattern as Koha::Patron->move_to_deleted
514 =cut
516 sub _move_to_old {
517 my ($self) = @_;
518 my $hold_infos = $self->unblessed;
519 return Koha::Old::Hold->new( $hold_infos )->store;
522 =head3 to_api_mapping
524 This method returns the mapping for representing a Koha::Hold object
525 on the API.
527 =cut
529 sub to_api_mapping {
530 return {
531 reserve_id => 'hold_id',
532 borrowernumber => 'patron_id',
533 reservedate => 'hold_date',
534 biblionumber => 'biblio_id',
535 branchcode => 'pickup_library_id',
536 notificationdate => undef,
537 reminderdate => undef,
538 cancellationdate => 'cancellation_date',
539 reservenotes => 'notes',
540 found => 'status',
541 itemnumber => 'item_id',
542 waitingdate => 'waiting_date',
543 expirationdate => 'expiration_date',
544 lowestPriority => 'lowest_priority',
545 suspend => 'suspended',
546 suspend_until => 'suspended_until',
547 itemtype => 'item_type',
548 item_level_hold => 'item_level',
552 =head2 Internal methods
554 =head3 _type
556 =cut
558 sub _type {
559 return 'Reserve';
562 =head1 AUTHORS
564 Kyle M Hall <kyle@bywatersolutions.com>
565 Jonathan Druart <jonathan.druart@bugs.koha-community.org>
566 Martin Renvoize <martin.renvoize@ptfs-europe.com>
568 =cut