Bug 33749: Use TrimFields instead of stripWhitespaceChars
[koha.git] / Koha / Hold.pm
1 package Koha::Hold;
2
3 # Copyright ByWater Solutions 2014
4 # Copyright 2017 Koha Development team
5 #
6 # This file is part of Koha.
7 #
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.
12 #
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.
17 #
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>.
20
21 use Modern::Perl;
22
23 use List::MoreUtils qw( any );
24
25 use C4::Context qw(preference);
26 use C4::Letters qw( GetPreparedLetter EnqueueLetter );
27 use C4::Log qw( logaction );
28 use C4::Reserves;
29
30 use Koha::AuthorisedValues;
31 use Koha::DateUtils qw( dt_from_string );
32 use Koha::Patrons;
33 use Koha::Biblios;
34 use Koha::Hold::CancellationRequests;
35 use Koha::Items;
36 use Koha::Libraries;
37 use Koha::Old::Holds;
38 use Koha::Calendar;
39 use Koha::Plugins;
40
41 use Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue;
42
43 use Koha::Exceptions;
44 use Koha::Exceptions::Hold;
45
46 use base qw(Koha::Object);
47
48 =head1 NAME
49
50 Koha::Hold - Koha Hold object class
51
52 =head1 API
53
54 =head2 Class methods
55
56 =cut
57
58 =head3 age
59
60 returns the number of days since a hold was placed, optionally
61 using the calendar
62
63 my $age = $hold->age( $use_calendar );
64
65 =cut
66
67 sub age {
68     my ( $self, $use_calendar ) = @_;
69
70     my $today = dt_from_string;
71     my $age;
72
73     if ( $use_calendar ) {
74         my $calendar = Koha::Calendar->new( branchcode => $self->branchcode );
75         $age = $calendar->days_between( dt_from_string( $self->reservedate ), $today );
76     }
77     else {
78         $age = $today->delta_days( dt_from_string( $self->reservedate ) );
79     }
80
81     $age = $age->in_units( 'days' );
82
83     return $age;
84 }
85
86 =head3 suspend_hold
87
88 my $hold = $hold->suspend_hold( $suspend_until );
89
90 =cut
91
92 sub suspend_hold {
93     my ( $self, $date ) = @_;
94
95     $date &&= dt_from_string($date)->truncate( to => 'day' )->datetime;
96
97     if ( $self->is_found ) {    # We can't suspend found holds
98         if ( $self->is_waiting ) {
99             Koha::Exceptions::Hold::CannotSuspendFound->throw( status => 'W' );
100         }
101         elsif ( $self->is_in_transit ) {
102             Koha::Exceptions::Hold::CannotSuspendFound->throw( status => 'T' );
103         }
104         elsif ( $self->is_in_processing ) {
105             Koha::Exceptions::Hold::CannotSuspendFound->throw( status => 'P' );
106         }
107         else {
108             Koha::Exceptions::Hold::CannotSuspendFound->throw(
109                       'Unhandled data exception on found hold (id='
110                     . $self->id
111                     . ', found='
112                     . $self->found
113                     . ')' );
114         }
115     }
116
117     $self->suspend(1);
118     $self->suspend_until($date);
119     $self->store();
120
121     Koha::Plugins->call(
122         'after_hold_action',
123         {
124             action  => 'suspend',
125             payload => { hold => $self->get_from_storage }
126         }
127     );
128
129     logaction( 'HOLDS', 'SUSPEND', $self->reserve_id, $self )
130         if C4::Context->preference('HoldsLog');
131
132     Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue->new->enqueue(
133         {
134             biblio_ids => [ $self->biblionumber ]
135         }
136     ) if C4::Context->preference('RealTimeHoldsQueue');
137
138     return $self;
139 }
140
141 =head3 resume
142
143 my $hold = $hold->resume();
144
145 =cut
146
147 sub resume {
148     my ( $self ) = @_;
149
150     $self->suspend(0);
151     $self->suspend_until( undef );
152
153     $self->store();
154
155     Koha::Plugins->call(
156         'after_hold_action',
157         {
158             action  => 'resume',
159             payload => { hold => $self->get_from_storage }
160         }
161     );
162
163     logaction( 'HOLDS', 'RESUME', $self->reserve_id, $self )
164         if C4::Context->preference('HoldsLog');
165
166     Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue->new->enqueue(
167         {
168             biblio_ids => [ $self->biblionumber ]
169         }
170     ) if C4::Context->preference('RealTimeHoldsQueue');
171
172     return $self;
173 }
174
175 =head3 delete
176
177 $hold->delete();
178
179 =cut
180
181 sub delete {
182     my ( $self ) = @_;
183
184     my $deleted = $self->SUPER::delete($self);
185
186     logaction( 'HOLDS', 'DELETE', $self->reserve_id, $self )
187         if C4::Context->preference('HoldsLog');
188
189     return $deleted;
190 }
191
192 =head3 set_transfer
193
194 =cut
195
196 sub set_transfer {
197     my ( $self ) = @_;
198
199     $self->priority(0);
200     $self->found('T');
201     $self->store();
202
203     Koha::Plugins->call(
204         'after_hold_action',
205         {
206             action  => 'transfer',
207             payload => { hold => $self->get_from_storage }
208         }
209     );
210
211     return $self;
212 }
213
214 =head3 set_waiting
215
216 =cut
217
218 sub set_waiting {
219     my ( $self, $desk_id ) = @_;
220
221     $self->priority(0);
222
223     my $today = dt_from_string();
224
225     my $values = {
226         found => 'W',
227         ( !$self->waitingdate ? ( waitingdate => $today->ymd ) : () ),
228         desk_id => $desk_id,
229     };
230
231     my $max_pickup_delay = C4::Context->preference("ReservesMaxPickUpDelay");
232     my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
233
234     my $rule = Koha::CirculationRules->get_effective_rule(
235         {
236             categorycode => $self->borrower->categorycode,
237             itemtype     => $self->item->effective_itemtype,
238             branchcode   => $self->branchcode,
239             rule_name    => 'holds_pickup_period',
240         }
241     );
242     if ( defined($rule) and $rule->rule_value ne '' ) {
243
244         # circulation rule overrides ReservesMaxPickUpDelay
245         $max_pickup_delay = $rule->rule_value;
246     }
247
248     my $new_expiration_date = dt_from_string($self->waitingdate)->clone->add( days => $max_pickup_delay );
249
250     if ( C4::Context->preference("ExcludeHolidaysFromMaxPickUpDelay") ) {
251         my $itemtype = $self->item ? $self->item->effective_itemtype : $self->biblio->itemtype;
252         my $daysmode = Koha::CirculationRules->get_effective_daysmode(
253             {
254                 categorycode => $self->borrower->categorycode,
255                 itemtype     => $itemtype,
256                 branchcode   => $self->branchcode,
257             }
258         );
259         my $calendar = Koha::Calendar->new( branchcode => $self->branchcode, days_mode => $daysmode );
260
261         $new_expiration_date = $calendar->days_forward( dt_from_string($self->waitingdate), $max_pickup_delay );
262     }
263
264     # If patron's requested expiration date is prior to the
265     # calculated one, we keep the patron's one.
266     if ( $self->patron_expiration_date ) {
267         my $requested_expiration = dt_from_string( $self->patron_expiration_date );
268
269         my $cmp =
270           $requested_expiration
271           ? DateTime->compare( $requested_expiration, $new_expiration_date )
272           : 0;
273
274         $new_expiration_date =
275           $cmp == -1 ? $requested_expiration : $new_expiration_date;
276     }
277
278     $values->{expirationdate} = $new_expiration_date->ymd;
279
280     $self->set($values)->store();
281
282     Koha::Plugins->call(
283         'after_hold_action',
284         {
285             action  => 'waiting',
286             payload => { hold => $self->get_from_storage }
287         }
288     );
289
290     return $self;
291 }
292
293 =head3 is_pickup_location_valid
294
295     if ($hold->is_pickup_location_valid({ library_id => $library->id }) ) {
296         ...
297     }
298
299 Returns a I<boolean> representing if the passed pickup location is valid for the hold.
300 It throws a I<Koha::Exceptions::_MissingParameter> if the library_id parameter is not
301 passed.
302
303 =cut
304
305 sub is_pickup_location_valid {
306     my ( $self, $params ) = @_;
307
308     Koha::Exceptions::MissingParameter->throw('The library_id parameter is mandatory')
309         unless $params->{library_id};
310
311     my $pickup_locations;
312
313     if ( $self->itemnumber ) { # item-level
314         $pickup_locations = $self->item->pickup_locations({ patron => $self->patron });
315     }
316     else { # biblio-level
317         $pickup_locations = $self->biblio->pickup_locations({ patron => $self->patron });
318     }
319
320     return any { $_->branchcode eq $params->{library_id} } $pickup_locations->as_list;
321 }
322
323 =head3 set_pickup_location
324
325     $hold->set_pickup_location(
326         {
327             library_id => $library->id,
328           [ force   => 0|1 ]
329         }
330     );
331
332 Updates the hold pickup location. It throws a I<Koha::Exceptions::Hold::InvalidPickupLocation> if
333 the passed pickup location is not valid.
334
335 Note: It is up to the caller to verify if I<AllowHoldPolicyOverride> is set when setting the
336 B<force> parameter.
337
338 =cut
339
340 sub set_pickup_location {
341     my ( $self, $params ) = @_;
342
343     Koha::Exceptions::MissingParameter->throw('The library_id parameter is mandatory')
344         unless $params->{library_id};
345
346     if (
347         $params->{force}
348         || $self->is_pickup_location_valid(
349             { library_id => $params->{library_id} }
350         )
351       )
352     {
353         # all good, set the new pickup location
354         $self->branchcode( $params->{library_id} )->store;
355     }
356     else {
357         Koha::Exceptions::Hold::InvalidPickupLocation->throw;
358     }
359
360     return $self;
361 }
362
363 =head3 set_processing
364
365 $hold->set_processing;
366
367 Mark the hold as in processing.
368
369 =cut
370
371 sub set_processing {
372     my ( $self ) = @_;
373
374     $self->priority(0);
375     $self->found('P');
376     $self->store();
377
378     Koha::Plugins->call(
379         'after_hold_action',
380         {
381             action  => 'processing',
382             payload => { hold => $self->get_from_storage }
383         }
384     );
385
386     return $self;
387 }
388
389 =head3 is_found
390
391 Returns true if hold is waiting, in transit or in processing
392
393 =cut
394
395 sub is_found {
396     my ($self) = @_;
397
398     return 0 unless $self->found();
399     return 1 if $self->found() eq 'W';
400     return 1 if $self->found() eq 'T';
401     return 1 if $self->found() eq 'P';
402 }
403
404 =head3 is_waiting
405
406 Returns true if hold is a waiting hold
407
408 =cut
409
410 sub is_waiting {
411     my ($self) = @_;
412
413     my $found = $self->found;
414     return $found && $found eq 'W';
415 }
416
417 =head3 is_in_transit
418
419 Returns true if hold is a in_transit hold
420
421 =cut
422
423 sub is_in_transit {
424     my ($self) = @_;
425
426     return 0 unless $self->found();
427     return $self->found() eq 'T';
428 }
429
430 =head3 is_in_processing
431
432 Returns true if hold is a in_processing hold
433
434 =cut
435
436 sub is_in_processing {
437     my ($self) = @_;
438
439     return 0 unless $self->found();
440     return $self->found() eq 'P';
441 }
442
443 =head3 is_cancelable_from_opac
444
445 Returns true if hold is a cancelable hold
446
447 Holds may be only canceled if they are not found.
448
449 This is used from the OPAC.
450
451 =cut
452
453 sub is_cancelable_from_opac {
454     my ($self) = @_;
455
456     return 1 unless $self->is_found();
457     return 0; # if ->is_in_transit or if ->is_waiting or ->is_in_processing
458 }
459
460 =head3 cancellation_requestable_from_opac
461
462     if ( $hold->cancellation_requestable_from_opac ) { ... }
463
464 Returns a I<boolean> representing if a cancellation request can be placed on the hold
465 from the OPAC. It targets holds that cannot be cancelled from the OPAC (see the
466 B<is_cancelable_from_opac> method above), but for which circulation rules allow
467 requesting cancellation.
468
469 Throws a B<Koha::Exceptions::InvalidStatus> exception with the following I<invalid_status>
470 values:
471
472 =over 4
473
474 =item B<'hold_not_waiting'>: the hold is expected to be waiting and it is not.
475
476 =item B<'no_item_linked'>: the waiting hold doesn't have an item properly linked.
477
478 =back
479
480 =cut
481
482 sub cancellation_requestable_from_opac {
483     my ( $self ) = @_;
484
485     Koha::Exceptions::InvalidStatus->throw( invalid_status => 'hold_not_waiting' )
486       unless $self->is_waiting;
487
488     my $item = $self->item;
489
490     Koha::Exceptions::InvalidStatus->throw( invalid_status => 'no_item_linked' )
491       unless $item;
492
493     my $patron = $self->patron;
494
495     my $controlbranch = $patron->branchcode;
496
497     if ( C4::Context->preference('ReservesControlBranch') eq 'ItemHomeLibrary' ) {
498         $controlbranch = $item->homebranch;
499     }
500
501     return Koha::CirculationRules->get_effective_rule_value(
502         {
503             categorycode => $patron->categorycode,
504             itemtype     => $item->itype,
505             branchcode   => $controlbranch,
506             rule_name    => 'waiting_hold_cancellation',
507         }
508     ) ? 1 : 0;
509 }
510
511 =head3 is_at_destination
512
513 Returns true if hold is waiting
514 and the hold's pickup branch matches
515 the hold item's holding branch
516
517 =cut
518
519 sub is_at_destination {
520     my ($self) = @_;
521
522     return $self->is_waiting() && ( $self->branchcode() eq $self->item()->holdingbranch() );
523 }
524
525 =head3 biblio
526
527 Returns the related Koha::Biblio object for this hold
528
529 =cut
530
531 sub biblio {
532     my ($self) = @_;
533     my $rs = $self->_result->biblionumber;
534     return Koha::Biblio->_new_from_dbic($rs);
535 }
536
537 =head3 patron
538
539 Returns the related Koha::Patron object for this hold
540
541 =cut
542
543 sub patron {
544     my ($self) = @_;
545     my $rs = $self->_result->patron;
546     return Koha::Patron->_new_from_dbic($rs);
547 }
548
549 =head3 item
550
551 Returns the related Koha::Item object for this Hold
552
553 =cut
554
555 sub item {
556     my ($self) = @_;
557     my $rs = $self->_result->itemnumber;
558     return unless $rs;
559     return Koha::Item->_new_from_dbic($rs);
560 }
561
562 =head3 item_group
563
564 Returns the related Koha::Biblio::ItemGroup object for this Hold
565
566 =cut
567
568 sub item_group {
569     my ($self) = @_;
570     my $rs = $self->_result->item_group;
571     return unless $rs;
572     return Koha::Biblio::ItemGroup->_new_from_dbic($rs);
573 }
574
575 =head3 branch
576
577 Returns the related Koha::Library object for this Hold
578
579 =cut
580
581 sub branch {
582     my ($self) = @_;
583     my $rs = $self->_result->branchcode;
584     return Koha::Library->_new_from_dbic($rs);
585 }
586
587 =head3 desk
588
589 Returns the related Koha::Desk object for this Hold
590
591 =cut
592
593 sub desk {
594     my $self = shift;
595     my $desk_rs = $self->_result->desk;
596     return unless $desk_rs;
597     return Koha::Desk->_new_from_dbic($desk_rs);
598 }
599
600 =head3 borrower
601
602 Returns the related Koha::Patron object for this Hold
603
604 =cut
605
606 # FIXME Should be renamed with ->patron
607 sub borrower {
608     my ($self) = @_;
609     my $rs = $self->_result->borrowernumber;
610     return Koha::Patron->_new_from_dbic($rs);
611 }
612
613 =head3 is_suspended
614
615 my $bool = $hold->is_suspended();
616
617 =cut
618
619 sub is_suspended {
620     my ( $self ) = @_;
621
622     return $self->suspend();
623 }
624
625 =head3 add_cancellation_request
626
627     my $cancellation_request = $hold->add_cancellation_request({ [ creation_date => $creation_date ] });
628
629 Adds a cancellation request to the hold. Returns the generated
630 I<Koha::Hold::CancellationRequest> object.
631
632 =cut
633
634 sub add_cancellation_request {
635     my ( $self, $params ) = @_;
636
637     my $request = Koha::Hold::CancellationRequest->new(
638         {   hold_id      => $self->id,
639             ( $params->{creation_date} ? ( creation_date => $params->{creation_date} ) : () ),
640         }
641     )->store;
642
643     $request->discard_changes;
644
645     return $request;
646 }
647
648 =head3 cancellation_requests
649
650     my $cancellation_requests = $hold->cancellation_requests;
651
652 Returns related a I<Koha::Hold::CancellationRequests> resultset.
653
654 =cut
655
656 sub cancellation_requests {
657     my ($self) = @_;
658
659     return Koha::Hold::CancellationRequests->search( { hold_id => $self->id } );
660 }
661
662 =head3 cancellation_requested
663
664     if ( $hold->cancellation_requested ) { ... }
665
666 Returns true if a cancellation request has been placed for the hold.
667
668 =cut
669
670 sub cancellation_requested {
671     my ($self) = @_;
672
673     return Koha::Hold::CancellationRequests->search( { hold_id => $self->id } )->count > 0;
674 }
675
676 =head3 cancel
677
678 my $cancel_hold = $hold->cancel(
679     {
680         [ charge_cancel_fee   => 1||0, ]
681         [ cancellation_reason => $cancellation_reason, ]
682         [ skip_holds_queue    => 1||0 ]
683     }
684 );
685
686 Cancel a hold:
687 - The hold will be moved to the old_reserves table with a priority=0
688 - The priority of other holds will be updated
689 - The patron will be charge (see ExpireReservesMaxPickUpDelayCharge) if the charge_cancel_fee parameter is set
690 - The canceled hold will have the cancellation reason added to old_reserves.cancellation_reason if one is passed in
691 - a CANCEL HOLDS log will be done if the pref HoldsLog is on
692
693 =cut
694
695 sub cancel {
696     my ( $self, $params ) = @_;
697
698     my $autofill_next = $params->{autofill} && $self->itemnumber && $self->found && $self->found eq 'W';
699
700     $self->_result->result_source->schema->txn_do(
701         sub {
702             my $patron = $self->patron;
703
704             $self->cancellationdate( dt_from_string->strftime( '%Y-%m-%d %H:%M:%S' ) );
705             $self->priority(0);
706             $self->cancellation_reason( $params->{cancellation_reason} );
707             $self->store();
708
709             if ( $params->{cancellation_reason} ) {
710                 my $letter = C4::Letters::GetPreparedLetter(
711                     module                 => 'reserves',
712                     letter_code            => 'HOLD_CANCELLATION',
713                     message_transport_type => 'email',
714                     branchcode             => $self->borrower->branchcode,
715                     lang                   => $self->borrower->lang,
716                     tables => {
717                         branches    => $self->borrower->branchcode,
718                         borrowers   => $self->borrowernumber,
719                         items       => $self->itemnumber,
720                         biblio      => $self->biblionumber,
721                         biblioitems => $self->biblionumber,
722                         reserves    => $self->unblessed,
723                     }
724                 );
725
726                 if ($letter) {
727                     C4::Letters::EnqueueLetter(
728                         {
729                             letter                   => $letter,
730                             borrowernumber         => $self->borrowernumber,
731                             message_transport_type => 'email',
732                         }
733                     );
734                 }
735             }
736
737             my $old_me = $self->_move_to_old;
738
739             Koha::Plugins->call(
740                 'after_hold_action',
741                 {
742                     action  => 'cancel',
743                     payload => { hold => $old_me->get_from_storage }
744                 }
745             );
746
747             # anonymize if required
748             $old_me->anonymize
749                 if $patron->privacy == 2;
750
751             $self->SUPER::delete(); # Do not add a DELETE log
752             # now fix the priority on the others....
753             C4::Reserves::_FixPriority({ biblionumber => $self->biblionumber });
754
755             # and, if desired, charge a cancel fee
756             my $charge = C4::Context->preference("ExpireReservesMaxPickUpDelayCharge");
757             if ( $charge && $params->{'charge_cancel_fee'} ) {
758                 my $account =
759                   Koha::Account->new( { patron_id => $self->borrowernumber } );
760                 $account->add_debit(
761                     {
762                         amount     => $charge,
763                         user_id    => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
764                         interface  => C4::Context->interface,
765                         library_id => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef,
766                         type       => 'RESERVE_EXPIRED',
767                         item_id    => $self->itemnumber
768                     }
769                 );
770             }
771
772             C4::Log::logaction( 'HOLDS', 'CANCEL', $self->reserve_id, $self )
773                 if C4::Context->preference('HoldsLog');
774
775             Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue->new->enqueue(
776                 {
777                     biblio_ids => [ $old_me->biblionumber ]
778                 }
779             ) unless $params->{skip_holds_queue} or !C4::Context->preference('RealTimeHoldsQueue');
780         }
781     );
782
783     if ($autofill_next) {
784         my ( undef, $next_hold ) = C4::Reserves::CheckReserves( $self->item );
785         if ($next_hold) {
786             my $is_transfer = $self->branchcode ne $next_hold->{branchcode};
787
788             C4::Reserves::ModReserveAffect( $self->itemnumber, $self->borrowernumber, $is_transfer, $next_hold->{reserve_id}, $self->desk_id, $autofill_next );
789             C4::Items::ModItemTransfer( $self->itemnumber, $self->branchcode, $next_hold->{branchcode}, "Reserve" ) if $is_transfer;
790         }
791     }
792
793     return $self;
794 }
795
796 =head3 fill
797
798     $hold->fill({ [ item_id => $item->id ] });
799
800 This method marks the hold as filled. It effectively moves it to old_reserves.
801 The optional I<item_id> parameter is used to set the information about the
802 item that filled the hold.
803
804 =cut
805
806 sub fill {
807     my ( $self, $params ) = @_;
808     $self->_result->result_source->schema->txn_do(
809         sub {
810             my $patron = $self->patron;
811
812             $self->set(
813                 {
814                     found    => 'F',
815                     priority => 0,
816                     $params->{item_id} ? ( itemnumber => $params->{item_id} ) : (),
817                 }
818             );
819
820             my $old_me = $self->_move_to_old;
821
822             Koha::Plugins->call(
823                 'after_hold_action',
824                 {
825                     action  => 'fill',
826                     payload => { hold => $old_me->get_from_storage }
827                 }
828             );
829
830             # anonymize if required
831             $old_me->anonymize
832                 if $patron->privacy == 2;
833
834             $self->SUPER::delete(); # Do not add a DELETE log
835
836             # now fix the priority on the others....
837             C4::Reserves::_FixPriority({ biblionumber => $self->biblionumber });
838
839             if ( C4::Context->preference('HoldFeeMode') eq 'any_time_is_collected' ) {
840                 my $fee = $patron->category->reservefee // 0;
841                 if ( $fee > 0 ) {
842                     $patron->account->add_debit(
843                         {
844                             amount       => $fee,
845                             description  => $self->biblio->title,
846                             user_id      => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
847                             library_id   => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef,
848                             interface    => C4::Context->interface,
849                             type         => 'RESERVE',
850                             item_id      => $self->itemnumber
851                         }
852                     );
853                 }
854             }
855
856             C4::Log::logaction( 'HOLDS', 'FILL', $self->id, $self )
857                 if C4::Context->preference('HoldsLog');
858
859             Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue->new->enqueue(
860                 {
861                     biblio_ids => [ $old_me->biblionumber ]
862                 }
863             ) if C4::Context->preference('RealTimeHoldsQueue');
864         }
865     );
866     return $self;
867 }
868
869 =head3 sub change_type
870
871     $hold->change_type                # to record level
872     $hold->change_type( $itemnumber ) # to item level
873
874 Changes hold type between record and item level holds, only if record has
875 exactly one hold for a patron. This is because Koha expects all holds for
876 a patron on a record to be alike.
877
878 =cut
879
880 sub change_type {
881     my ( $self, $itemnumber ) = @_;
882
883     my $record_holds_per_patron = Koha::Holds->search(
884         {
885             borrowernumber => $self->borrowernumber,
886             biblionumber   => $self->biblionumber,
887         }
888     );
889
890     if ( $itemnumber && $self->itemnumber ) {
891         $self->itemnumber($itemnumber)->store;
892         return $self;
893     }
894
895     if ( $record_holds_per_patron->count == 1 ) {
896         $self->set(
897             {
898                 itemnumber      => $itemnumber ? $itemnumber : undef,
899                 item_level_hold => $itemnumber ? 1           : 0,
900             }
901         )->store;
902     } else {
903         Koha::Exceptions::Hold::CannotChangeHoldType->throw();
904     }
905
906     return $self;
907 }
908
909 =head3 store
910
911 Override base store method to set default
912 expirationdate for holds.
913
914 =cut
915
916 sub store {
917     my ($self) = @_;
918
919     Koha::Exceptions::Hold::MissingPickupLocation->throw() unless $self->branchcode;
920
921     if ( !$self->in_storage ) {
922         if ( ! $self->expirationdate && $self->patron_expiration_date ) {
923             $self->expirationdate($self->patron_expiration_date);
924         }
925
926         if (
927             C4::Context->preference('DefaultHoldExpirationdate')
928                 && !$self->expirationdate
929           )
930         {
931             $self->_set_default_expirationdate;
932         }
933     }
934     else {
935
936         my %updated_columns = $self->_result->get_dirty_columns;
937         return $self->SUPER::store unless %updated_columns;
938
939         if ( exists $updated_columns{reservedate} ) {
940             if (
941                 C4::Context->preference('DefaultHoldExpirationdate')
942                 && ! exists $updated_columns{expirationdate}
943               )
944             {
945                 $self->_set_default_expirationdate;
946             }
947         }
948     }
949
950     $self = $self->SUPER::store;
951 }
952
953 sub _set_default_expirationdate {
954     my $self = shift;
955
956     my $period = C4::Context->preference('DefaultHoldExpirationdatePeriod') || 0;
957     my $timeunit =
958       C4::Context->preference('DefaultHoldExpirationdateUnitOfTime') || 'days';
959
960     $self->expirationdate(
961         dt_from_string( $self->reservedate )->add( $timeunit => $period ) );
962 }
963
964 =head3 _move_to_old
965
966 my $is_moved = $hold->_move_to_old;
967
968 Move a hold to the old_reserve table following the same pattern as Koha::Patron->move_to_deleted
969
970 =cut
971
972 sub _move_to_old {
973     my ($self) = @_;
974     my $hold_infos = $self->unblessed;
975     return Koha::Old::Hold->new( $hold_infos )->store;
976 }
977
978 =head3 to_api_mapping
979
980 This method returns the mapping for representing a Koha::Hold object
981 on the API.
982
983 =cut
984
985 sub to_api_mapping {
986     return {
987         reserve_id       => 'hold_id',
988         borrowernumber   => 'patron_id',
989         reservedate      => 'hold_date',
990         biblionumber     => 'biblio_id',
991         branchcode       => 'pickup_library_id',
992         notificationdate => undef,
993         reminderdate     => undef,
994         cancellationdate => 'cancellation_date',
995         reservenotes     => 'notes',
996         found            => 'status',
997         itemnumber       => 'item_id',
998         waitingdate      => 'waiting_date',
999         expirationdate   => 'expiration_date',
1000         patron_expiration_date => undef,
1001         lowestPriority   => 'lowest_priority',
1002         suspend          => 'suspended',
1003         suspend_until    => 'suspended_until',
1004         itemtype         => 'item_type',
1005         item_level_hold  => 'item_level',
1006     };
1007 }
1008
1009 =head3 can_update_pickup_location_opac
1010
1011     my $can_update_pickup_location_opac = $hold->can_update_pickup_location_opac;
1012
1013 Returns if a hold can change pickup location from opac
1014
1015 =cut
1016
1017 sub can_update_pickup_location_opac {
1018     my ($self) = @_;
1019
1020     my @statuses = split /,/, C4::Context->preference("OPACAllowUserToChangeBranch");
1021     foreach my $status ( @statuses ){
1022         return 1 if ($status eq 'pending' && !$self->is_found && !$self->is_suspended );
1023         return 1 if ($status eq 'intransit' && $self->is_in_transit);
1024         return 1 if ($status eq 'suspended' && $self->is_suspended);
1025     }
1026     return 0;
1027 }
1028
1029 =head2 Internal methods
1030
1031 =head3 _type
1032
1033 =cut
1034
1035 sub _type {
1036     return 'Reserve';
1037 }
1038
1039 =head1 AUTHORS
1040
1041 Kyle M Hall <kyle@bywatersolutions.com>
1042 Jonathan Druart <jonathan.druart@bugs.koha-community.org>
1043 Martin Renvoize <martin.renvoize@ptfs-europe.com>
1044
1045 =cut
1046
1047 1;