Bug 24786: Update borroaccount to use session register
[koha.git] / Koha / Hold.pm
blob921906a07b8613877d4b311ae7f2968c7dc3439c
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 ) = @_;
180 $self->priority(0);
182 my $today = dt_from_string();
183 my $values = {
184 found => 'W',
185 waitingdate => $today->ymd,
188 my $requested_expiration;
189 if ($self->expirationdate) {
190 $requested_expiration = dt_from_string($self->expirationdate);
193 my $max_pickup_delay = C4::Context->preference("ReservesMaxPickUpDelay");
194 my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
196 my $expirationdate = $today->clone;
197 $expirationdate->add(days => $max_pickup_delay);
199 if ( C4::Context->preference("ExcludeHolidaysFromMaxPickUpDelay") ) {
200 my $itemtype = $self->item ? $self->item->effective_itemtype : $self->biblio->itemtype;
201 my $daysmode = Koha::CirculationRules->get_effective_daysmode(
203 categorycode => $self->borrower->categorycode,
204 itemtype => $itemtype,
205 branchcode => $self->branchcode,
208 my $calendar = Koha::Calendar->new( branchcode => $self->branchcode, days_mode => $daysmode );
210 $expirationdate = $calendar->days_forward( dt_from_string(), $max_pickup_delay );
213 # If patron's requested expiration date is prior to the
214 # calculated one, we keep the patron's one.
215 my $cmp = $requested_expiration ? DateTime->compare($requested_expiration, $expirationdate) : 0;
216 $values->{expirationdate} = $cmp == -1 ? $requested_expiration->ymd : $expirationdate->ymd;
218 $self->set($values)->store();
220 return $self;
223 =head3 set_processing
225 $hold->set_processing;
227 Mark the hold as in processing.
229 =cut
231 sub set_processing {
232 my ( $self ) = @_;
234 $self->priority(0);
235 $self->found('P');
236 $self->store();
238 return $self;
241 =head3 is_found
243 Returns true if hold is waiting, in transit or in processing
245 =cut
247 sub is_found {
248 my ($self) = @_;
250 return 0 unless $self->found();
251 return 1 if $self->found() eq 'W';
252 return 1 if $self->found() eq 'T';
253 return 1 if $self->found() eq 'P';
256 =head3 is_waiting
258 Returns true if hold is a waiting hold
260 =cut
262 sub is_waiting {
263 my ($self) = @_;
265 my $found = $self->found;
266 return $found && $found eq 'W';
269 =head3 is_in_transit
271 Returns true if hold is a in_transit hold
273 =cut
275 sub is_in_transit {
276 my ($self) = @_;
278 return 0 unless $self->found();
279 return $self->found() eq 'T';
282 =head3 is_in_processing
284 Returns true if hold is a in_processing hold
286 =cut
288 sub is_in_processing {
289 my ($self) = @_;
291 return 0 unless $self->found();
292 return $self->found() eq 'P';
295 =head3 is_cancelable_from_opac
297 Returns true if hold is a cancelable hold
299 Holds may be only canceled if they are not found.
301 This is used from the OPAC.
303 =cut
305 sub is_cancelable_from_opac {
306 my ($self) = @_;
308 return 1 unless $self->is_found();
309 return 0; # if ->is_in_transit or if ->is_waiting or ->is_in_processing
312 =head3 is_at_destination
314 Returns true if hold is waiting
315 and the hold's pickup branch matches
316 the hold item's holding branch
318 =cut
320 sub is_at_destination {
321 my ($self) = @_;
323 return $self->is_waiting() && ( $self->branchcode() eq $self->item()->holdingbranch() );
326 =head3 biblio
328 Returns the related Koha::Biblio object for this hold
330 =cut
332 sub biblio {
333 my ($self) = @_;
335 $self->{_biblio} ||= Koha::Biblios->find( $self->biblionumber() );
337 return $self->{_biblio};
340 =head3 item
342 Returns the related Koha::Item object for this Hold
344 =cut
346 sub item {
347 my ($self) = @_;
349 $self->{_item} ||= Koha::Items->find( $self->itemnumber() );
351 return $self->{_item};
354 =head3 branch
356 Returns the related Koha::Library object for this Hold
358 =cut
360 sub branch {
361 my ($self) = @_;
363 $self->{_branch} ||= Koha::Libraries->find( $self->branchcode() );
365 return $self->{_branch};
368 =head3 borrower
370 Returns the related Koha::Patron object for this Hold
372 =cut
374 # FIXME Should be renamed with ->patron
375 sub borrower {
376 my ($self) = @_;
378 $self->{_borrower} ||= Koha::Patrons->find( $self->borrowernumber() );
380 return $self->{_borrower};
383 =head3 is_suspended
385 my $bool = $hold->is_suspended();
387 =cut
389 sub is_suspended {
390 my ( $self ) = @_;
392 return $self->suspend();
396 =head3 cancel
398 my $cancel_hold = $hold->cancel(
400 [ charge_cancel_fee => 1||0, ]
401 [ cancellation_reason => $cancellation_reason, ]
405 Cancel a hold:
406 - The hold will be moved to the old_reserves table with a priority=0
407 - The priority of other holds will be updated
408 - The patron will be charge (see ExpireReservesMaxPickUpDelayCharge) if the charge_cancel_fee parameter is set
409 - The canceled hold will have the cancellation reason added to old_reserves.cancellation_reason if one is passed in
410 - a CANCEL HOLDS log will be done if the pref HoldsLog is on
412 =cut
414 sub cancel {
415 my ( $self, $params ) = @_;
416 $self->_result->result_source->schema->txn_do(
417 sub {
418 $self->cancellationdate( dt_from_string->strftime( '%Y-%m-%d %H:%M:%S' ) );
419 $self->priority(0);
420 $self->cancellation_reason( $params->{cancellation_reason} );
421 $self->store();
423 if ( $params->{cancellation_reason} ) {
424 my $letter = C4::Letters::GetPreparedLetter(
425 module => 'reserves',
426 letter_code => 'HOLD_CANCELLATION',
427 message_transport_type => 'email',
428 branchcode => $self->borrower->branchcode,
429 lang => $self->borrower->lang,
430 tables => {
431 branches => $self->borrower->branchcode,
432 borrowers => $self->borrowernumber,
433 items => $self->itemnumber,
434 biblio => $self->biblionumber,
435 biblioitems => $self->biblionumber,
436 reserves => $self->unblessed,
440 if ($letter) {
441 C4::Letters::EnqueueLetter(
443 letter => $letter,
444 borrowernumber => $self->borrowernumber,
445 message_transport_type => 'email',
451 $self->_move_to_old;
452 $self->SUPER::delete(); # Do not add a DELETE log
454 # now fix the priority on the others....
455 C4::Reserves::_FixPriority({ biblionumber => $self->biblionumber });
457 # and, if desired, charge a cancel fee
458 my $charge = C4::Context->preference("ExpireReservesMaxPickUpDelayCharge");
459 if ( $charge && $params->{'charge_cancel_fee'} ) {
460 my $account =
461 Koha::Account->new( { patron_id => $self->borrowernumber } );
462 $account->add_debit(
464 amount => $charge,
465 user_id => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
466 interface => C4::Context->interface,
467 library_id => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef,
468 type => 'RESERVE_EXPIRED',
469 item_id => $self->itemnumber
474 C4::Log::logaction( 'HOLDS', 'CANCEL', $self->reserve_id, Dumper($self->unblessed) )
475 if C4::Context->preference('HoldsLog');
478 return $self;
481 =head3 _move_to_old
483 my $is_moved = $hold->_move_to_old;
485 Move a hold to the old_reserve table following the same pattern as Koha::Patron->move_to_deleted
487 =cut
489 sub _move_to_old {
490 my ($self) = @_;
491 my $hold_infos = $self->unblessed;
492 return Koha::Old::Hold->new( $hold_infos )->store;
495 =head3 to_api_mapping
497 This method returns the mapping for representing a Koha::Hold object
498 on the API.
500 =cut
502 sub to_api_mapping {
503 return {
504 reserve_id => 'hold_id',
505 borrowernumber => 'patron_id',
506 reservedate => 'hold_date',
507 biblionumber => 'biblio_id',
508 branchcode => 'pickup_library_id',
509 notificationdate => undef,
510 reminderdate => undef,
511 cancellationdate => 'cancellation_date',
512 reservenotes => 'notes',
513 found => 'status',
514 itemnumber => 'item_id',
515 waitingdate => 'waiting_date',
516 expirationdate => 'expiration_date',
517 lowestPriority => 'lowest_priority',
518 suspend => 'suspended',
519 suspend_until => 'suspended_until',
520 itemtype => 'item_type',
521 item_level_hold => 'item_level',
525 =head2 Internal methods
527 =head3 _type
529 =cut
531 sub _type {
532 return 'Reserve';
535 =head1 AUTHORS
537 Kyle M Hall <kyle@bywatersolutions.com>
538 Jonathan Druart <jonathan.druart@bugs.koha-community.org>
539 Martin Renvoize <martin.renvoize@ptfs-europe.com>
541 =cut