Bug 34883: Stop patron expiry date being set to NULL during import
[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 $new_expiration_date = dt_from_string($self->waitingdate)->clone->add( days => $max_pickup_delay );
235
236     if ( C4::Context->preference("ExcludeHolidaysFromMaxPickUpDelay") ) {
237         my $itemtype = $self->item ? $self->item->effective_itemtype : $self->biblio->itemtype;
238         my $daysmode = Koha::CirculationRules->get_effective_daysmode(
239             {
240                 categorycode => $self->borrower->categorycode,
241                 itemtype     => $itemtype,
242                 branchcode   => $self->branchcode,
243             }
244         );
245         my $calendar = Koha::Calendar->new( branchcode => $self->branchcode, days_mode => $daysmode );
246
247         $new_expiration_date = $calendar->days_forward( dt_from_string($self->waitingdate), $max_pickup_delay );
248     }
249
250     # If patron's requested expiration date is prior to the
251     # calculated one, we keep the patron's one.
252     if ( $self->patron_expiration_date ) {
253         my $requested_expiration = dt_from_string( $self->patron_expiration_date );
254
255         my $cmp =
256           $requested_expiration
257           ? DateTime->compare( $requested_expiration, $new_expiration_date )
258           : 0;
259
260         $new_expiration_date =
261           $cmp == -1 ? $requested_expiration : $new_expiration_date;
262     }
263
264     $values->{expirationdate} = $new_expiration_date->ymd;
265
266     $self->set($values)->store();
267
268     Koha::Plugins->call(
269         'after_hold_action',
270         {
271             action  => 'waiting',
272             payload => { hold => $self->get_from_storage }
273         }
274     );
275
276     return $self;
277 }
278
279 =head3 is_pickup_location_valid
280
281     if ($hold->is_pickup_location_valid({ library_id => $library->id }) ) {
282         ...
283     }
284
285 Returns a I<boolean> representing if the passed pickup location is valid for the hold.
286 It throws a I<Koha::Exceptions::_MissingParameter> if the library_id parameter is not
287 passed.
288
289 =cut
290
291 sub is_pickup_location_valid {
292     my ( $self, $params ) = @_;
293
294     Koha::Exceptions::MissingParameter->throw('The library_id parameter is mandatory')
295         unless $params->{library_id};
296
297     my $pickup_locations;
298
299     if ( $self->itemnumber ) { # item-level
300         $pickup_locations = $self->item->pickup_locations({ patron => $self->patron });
301     }
302     else { # biblio-level
303         $pickup_locations = $self->biblio->pickup_locations({ patron => $self->patron });
304     }
305
306     return any { $_->branchcode eq $params->{library_id} } $pickup_locations->as_list;
307 }
308
309 =head3 set_pickup_location
310
311     $hold->set_pickup_location(
312         {
313             library_id => $library->id,
314           [ force   => 0|1 ]
315         }
316     );
317
318 Updates the hold pickup location. It throws a I<Koha::Exceptions::Hold::InvalidPickupLocation> if
319 the passed pickup location is not valid.
320
321 Note: It is up to the caller to verify if I<AllowHoldPolicyOverride> is set when setting the
322 B<force> parameter.
323
324 =cut
325
326 sub set_pickup_location {
327     my ( $self, $params ) = @_;
328
329     Koha::Exceptions::MissingParameter->throw('The library_id parameter is mandatory')
330         unless $params->{library_id};
331
332     if (
333         $params->{force}
334         || $self->is_pickup_location_valid(
335             { library_id => $params->{library_id} }
336         )
337       )
338     {
339         # all good, set the new pickup location
340         $self->branchcode( $params->{library_id} )->store;
341     }
342     else {
343         Koha::Exceptions::Hold::InvalidPickupLocation->throw;
344     }
345
346     return $self;
347 }
348
349 =head3 set_processing
350
351 $hold->set_processing;
352
353 Mark the hold as in processing.
354
355 =cut
356
357 sub set_processing {
358     my ( $self ) = @_;
359
360     $self->priority(0);
361     $self->found('P');
362     $self->store();
363
364     Koha::Plugins->call(
365         'after_hold_action',
366         {
367             action  => 'processing',
368             payload => { hold => $self->get_from_storage }
369         }
370     );
371
372     return $self;
373 }
374
375 =head3 is_found
376
377 Returns true if hold is waiting, in transit or in processing
378
379 =cut
380
381 sub is_found {
382     my ($self) = @_;
383
384     return 0 unless $self->found();
385     return 1 if $self->found() eq 'W';
386     return 1 if $self->found() eq 'T';
387     return 1 if $self->found() eq 'P';
388 }
389
390 =head3 is_waiting
391
392 Returns true if hold is a waiting hold
393
394 =cut
395
396 sub is_waiting {
397     my ($self) = @_;
398
399     my $found = $self->found;
400     return $found && $found eq 'W';
401 }
402
403 =head3 is_in_transit
404
405 Returns true if hold is a in_transit hold
406
407 =cut
408
409 sub is_in_transit {
410     my ($self) = @_;
411
412     return 0 unless $self->found();
413     return $self->found() eq 'T';
414 }
415
416 =head3 is_in_processing
417
418 Returns true if hold is a in_processing hold
419
420 =cut
421
422 sub is_in_processing {
423     my ($self) = @_;
424
425     return 0 unless $self->found();
426     return $self->found() eq 'P';
427 }
428
429 =head3 is_cancelable_from_opac
430
431 Returns true if hold is a cancelable hold
432
433 Holds may be only canceled if they are not found.
434
435 This is used from the OPAC.
436
437 =cut
438
439 sub is_cancelable_from_opac {
440     my ($self) = @_;
441
442     return 1 unless $self->is_found();
443     return 0; # if ->is_in_transit or if ->is_waiting or ->is_in_processing
444 }
445
446 =head3 cancellation_requestable_from_opac
447
448     if ( $hold->cancellation_requestable_from_opac ) { ... }
449
450 Returns a I<boolean> representing if a cancellation request can be placed on the hold
451 from the OPAC. It targets holds that cannot be cancelled from the OPAC (see the
452 B<is_cancelable_from_opac> method above), but for which circulation rules allow
453 requesting cancellation.
454
455 Throws a B<Koha::Exceptions::InvalidStatus> exception with the following I<invalid_status>
456 values:
457
458 =over 4
459
460 =item B<'hold_not_waiting'>: the hold is expected to be waiting and it is not.
461
462 =item B<'no_item_linked'>: the waiting hold doesn't have an item properly linked.
463
464 =back
465
466 =cut
467
468 sub cancellation_requestable_from_opac {
469     my ( $self ) = @_;
470
471     Koha::Exceptions::InvalidStatus->throw( invalid_status => 'hold_not_waiting' )
472       unless $self->is_waiting;
473
474     my $item = $self->item;
475
476     Koha::Exceptions::InvalidStatus->throw( invalid_status => 'no_item_linked' )
477       unless $item;
478
479     my $patron = $self->patron;
480
481     my $controlbranch = $patron->branchcode;
482
483     if ( C4::Context->preference('ReservesControlBranch') eq 'ItemHomeLibrary' ) {
484         $controlbranch = $item->homebranch;
485     }
486
487     return Koha::CirculationRules->get_effective_rule_value(
488         {
489             categorycode => $patron->categorycode,
490             itemtype     => $item->itype,
491             branchcode   => $controlbranch,
492             rule_name    => 'waiting_hold_cancellation',
493         }
494     ) ? 1 : 0;
495 }
496
497 =head3 is_at_destination
498
499 Returns true if hold is waiting
500 and the hold's pickup branch matches
501 the hold item's holding branch
502
503 =cut
504
505 sub is_at_destination {
506     my ($self) = @_;
507
508     return $self->is_waiting() && ( $self->branchcode() eq $self->item()->holdingbranch() );
509 }
510
511 =head3 biblio
512
513 Returns the related Koha::Biblio object for this hold
514
515 =cut
516
517 sub biblio {
518     my ($self) = @_;
519     my $rs = $self->_result->biblionumber;
520     return Koha::Biblio->_new_from_dbic($rs);
521 }
522
523 =head3 patron
524
525 Returns the related Koha::Patron object for this hold
526
527 =cut
528
529 sub patron {
530     my ($self) = @_;
531     my $rs = $self->_result->patron;
532     return Koha::Patron->_new_from_dbic($rs);
533 }
534
535 =head3 item
536
537 Returns the related Koha::Item object for this Hold
538
539 =cut
540
541 sub item {
542     my ($self) = @_;
543     my $rs = $self->_result->itemnumber;
544     return unless $rs;
545     return Koha::Item->_new_from_dbic($rs);
546 }
547
548 =head3 item_group
549
550 Returns the related Koha::Biblio::ItemGroup object for this Hold
551
552 =cut
553
554 sub item_group {
555     my ($self) = @_;
556     my $rs = $self->_result->item_group;
557     return unless $rs;
558     return Koha::Biblio::ItemGroup->_new_from_dbic($rs);
559 }
560
561 =head3 branch
562
563 Returns the related Koha::Library object for this Hold
564
565 =cut
566
567 sub branch {
568     my ($self) = @_;
569     my $rs = $self->_result->branchcode;
570     return Koha::Library->_new_from_dbic($rs);
571 }
572
573 =head3 desk
574
575 Returns the related Koha::Desk object for this Hold
576
577 =cut
578
579 sub desk {
580     my $self = shift;
581     my $desk_rs = $self->_result->desk;
582     return unless $desk_rs;
583     return Koha::Desk->_new_from_dbic($desk_rs);
584 }
585
586 =head3 borrower
587
588 Returns the related Koha::Patron object for this Hold
589
590 =cut
591
592 # FIXME Should be renamed with ->patron
593 sub borrower {
594     my ($self) = @_;
595     my $rs = $self->_result->borrowernumber;
596     return Koha::Patron->_new_from_dbic($rs);
597 }
598
599 =head3 is_suspended
600
601 my $bool = $hold->is_suspended();
602
603 =cut
604
605 sub is_suspended {
606     my ( $self ) = @_;
607
608     return $self->suspend();
609 }
610
611 =head3 add_cancellation_request
612
613     my $cancellation_request = $hold->add_cancellation_request({ [ creation_date => $creation_date ] });
614
615 Adds a cancellation request to the hold. Returns the generated
616 I<Koha::Hold::CancellationRequest> object.
617
618 =cut
619
620 sub add_cancellation_request {
621     my ( $self, $params ) = @_;
622
623     my $request = Koha::Hold::CancellationRequest->new(
624         {   hold_id      => $self->id,
625             ( $params->{creation_date} ? ( creation_date => $params->{creation_date} ) : () ),
626         }
627     )->store;
628
629     $request->discard_changes;
630
631     return $request;
632 }
633
634 =head3 cancellation_requests
635
636     my $cancellation_requests = $hold->cancellation_requests;
637
638 Returns related a I<Koha::Hold::CancellationRequests> resultset.
639
640 =cut
641
642 sub cancellation_requests {
643     my ($self) = @_;
644
645     return Koha::Hold::CancellationRequests->search( { hold_id => $self->id } );
646 }
647
648 =head3 cancellation_requested
649
650     if ( $hold->cancellation_requested ) { ... }
651
652 Returns true if a cancellation request has been placed for the hold.
653
654 =cut
655
656 sub cancellation_requested {
657     my ($self) = @_;
658
659     return Koha::Hold::CancellationRequests->search( { hold_id => $self->id } )->count > 0;
660 }
661
662 =head3 cancel
663
664 my $cancel_hold = $hold->cancel(
665     {
666         [ charge_cancel_fee   => 1||0, ]
667         [ cancellation_reason => $cancellation_reason, ]
668         [ skip_holds_queue    => 1||0 ]
669     }
670 );
671
672 Cancel a hold:
673 - The hold will be moved to the old_reserves table with a priority=0
674 - The priority of other holds will be updated
675 - The patron will be charge (see ExpireReservesMaxPickUpDelayCharge) if the charge_cancel_fee parameter is set
676 - The canceled hold will have the cancellation reason added to old_reserves.cancellation_reason if one is passed in
677 - a CANCEL HOLDS log will be done if the pref HoldsLog is on
678
679 =cut
680
681 sub cancel {
682     my ( $self, $params ) = @_;
683
684     my $autofill_next = $params->{autofill} && $self->itemnumber && $self->found && $self->found eq 'W';
685
686     $self->_result->result_source->schema->txn_do(
687         sub {
688             my $patron = $self->patron;
689
690             $self->cancellationdate( dt_from_string->strftime( '%Y-%m-%d %H:%M:%S' ) );
691             $self->priority(0);
692             $self->cancellation_reason( $params->{cancellation_reason} );
693             $self->store();
694
695             if ( $params->{cancellation_reason} ) {
696                 my $letter = C4::Letters::GetPreparedLetter(
697                     module                 => 'reserves',
698                     letter_code            => 'HOLD_CANCELLATION',
699                     message_transport_type => 'email',
700                     branchcode             => $self->borrower->branchcode,
701                     lang                   => $self->borrower->lang,
702                     tables => {
703                         branches    => $self->borrower->branchcode,
704                         borrowers   => $self->borrowernumber,
705                         items       => $self->itemnumber,
706                         biblio      => $self->biblionumber,
707                         biblioitems => $self->biblionumber,
708                         reserves    => $self->unblessed,
709                     }
710                 );
711
712                 if ($letter) {
713                     C4::Letters::EnqueueLetter(
714                         {
715                             letter                   => $letter,
716                             borrowernumber         => $self->borrowernumber,
717                             message_transport_type => 'email',
718                         }
719                     );
720                 }
721             }
722
723             my $old_me = $self->_move_to_old;
724
725             Koha::Plugins->call(
726                 'after_hold_action',
727                 {
728                     action  => 'cancel',
729                     payload => { hold => $old_me->get_from_storage }
730                 }
731             );
732
733             # anonymize if required
734             $old_me->anonymize
735                 if $patron->privacy == 2;
736
737             $self->SUPER::delete(); # Do not add a DELETE log
738             # now fix the priority on the others....
739             C4::Reserves::_FixPriority({ biblionumber => $self->biblionumber });
740
741             # and, if desired, charge a cancel fee
742             my $charge = C4::Context->preference("ExpireReservesMaxPickUpDelayCharge");
743             if ( $charge && $params->{'charge_cancel_fee'} ) {
744                 my $account =
745                   Koha::Account->new( { patron_id => $self->borrowernumber } );
746                 $account->add_debit(
747                     {
748                         amount     => $charge,
749                         user_id    => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
750                         interface  => C4::Context->interface,
751                         library_id => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef,
752                         type       => 'RESERVE_EXPIRED',
753                         item_id    => $self->itemnumber
754                     }
755                 );
756             }
757
758             C4::Log::logaction( 'HOLDS', 'CANCEL', $self->reserve_id, $self )
759                 if C4::Context->preference('HoldsLog');
760
761             Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue->new->enqueue(
762                 {
763                     biblio_ids => [ $old_me->biblionumber ]
764                 }
765             ) unless $params->{skip_holds_queue} or !C4::Context->preference('RealTimeHoldsQueue');
766         }
767     );
768
769     if ($autofill_next) {
770         my ( undef, $next_hold ) = C4::Reserves::CheckReserves( $self->item );
771         if ($next_hold) {
772             my $is_transfer = $self->branchcode ne $next_hold->{branchcode};
773
774             C4::Reserves::ModReserveAffect( $self->itemnumber, $self->borrowernumber, $is_transfer, $next_hold->{reserve_id}, $self->desk_id, $autofill_next );
775             C4::Items::ModItemTransfer( $self->itemnumber, $self->branchcode, $next_hold->{branchcode}, "Reserve" ) if $is_transfer;
776         }
777     }
778
779     return $self;
780 }
781
782 =head3 fill
783
784     $hold->fill({ [ item_id => $item->id ] });
785
786 This method marks the hold as filled. It effectively moves it to old_reserves.
787 The optional I<item_id> parameter is used to set the information about the
788 item that filled the hold.
789
790 =cut
791
792 sub fill {
793     my ( $self, $params ) = @_;
794     $self->_result->result_source->schema->txn_do(
795         sub {
796             my $patron = $self->patron;
797
798             $self->set(
799                 {
800                     found    => 'F',
801                     priority => 0,
802                     $params->{item_id} ? ( itemnumber => $params->{item_id} ) : (),
803                 }
804             );
805
806             my $old_me = $self->_move_to_old;
807
808             Koha::Plugins->call(
809                 'after_hold_action',
810                 {
811                     action  => 'fill',
812                     payload => { hold => $old_me->get_from_storage }
813                 }
814             );
815
816             # anonymize if required
817             $old_me->anonymize
818                 if $patron->privacy == 2;
819
820             $self->SUPER::delete(); # Do not add a DELETE log
821
822             # now fix the priority on the others....
823             C4::Reserves::_FixPriority({ biblionumber => $self->biblionumber });
824
825             if ( C4::Context->preference('HoldFeeMode') eq 'any_time_is_collected' ) {
826                 my $fee = $patron->category->reservefee // 0;
827                 if ( $fee > 0 ) {
828                     $patron->account->add_debit(
829                         {
830                             amount       => $fee,
831                             description  => $self->biblio->title,
832                             user_id      => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
833                             library_id   => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef,
834                             interface    => C4::Context->interface,
835                             type         => 'RESERVE',
836                             item_id      => $self->itemnumber
837                         }
838                     );
839                 }
840             }
841
842             C4::Log::logaction( 'HOLDS', 'FILL', $self->id, $self )
843                 if C4::Context->preference('HoldsLog');
844
845             Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue->new->enqueue(
846                 {
847                     biblio_ids => [ $old_me->biblionumber ]
848                 }
849             ) if C4::Context->preference('RealTimeHoldsQueue');
850         }
851     );
852     return $self;
853 }
854
855 =head3 store
856
857 Override base store method to set default
858 expirationdate for holds.
859
860 =cut
861
862 sub store {
863     my ($self) = @_;
864
865     Koha::Exceptions::Hold::MissingPickupLocation->throw() unless $self->branchcode;
866
867     if ( !$self->in_storage ) {
868         if ( ! $self->expirationdate && $self->patron_expiration_date ) {
869             $self->expirationdate($self->patron_expiration_date);
870         }
871
872         if (
873             C4::Context->preference('DefaultHoldExpirationdate')
874                 && !$self->expirationdate
875           )
876         {
877             $self->_set_default_expirationdate;
878         }
879     }
880     else {
881
882         my %updated_columns = $self->_result->get_dirty_columns;
883         return $self->SUPER::store unless %updated_columns;
884
885         if ( exists $updated_columns{reservedate} ) {
886             if (
887                 C4::Context->preference('DefaultHoldExpirationdate')
888                 && ! exists $updated_columns{expirationdate}
889               )
890             {
891                 $self->_set_default_expirationdate;
892             }
893         }
894     }
895
896     $self = $self->SUPER::store;
897 }
898
899 sub _set_default_expirationdate {
900     my $self = shift;
901
902     my $period = C4::Context->preference('DefaultHoldExpirationdatePeriod') || 0;
903     my $timeunit =
904       C4::Context->preference('DefaultHoldExpirationdateUnitOfTime') || 'days';
905
906     $self->expirationdate(
907         dt_from_string( $self->reservedate )->add( $timeunit => $period ) );
908 }
909
910 =head3 _move_to_old
911
912 my $is_moved = $hold->_move_to_old;
913
914 Move a hold to the old_reserve table following the same pattern as Koha::Patron->move_to_deleted
915
916 =cut
917
918 sub _move_to_old {
919     my ($self) = @_;
920     my $hold_infos = $self->unblessed;
921     return Koha::Old::Hold->new( $hold_infos )->store;
922 }
923
924 =head3 to_api_mapping
925
926 This method returns the mapping for representing a Koha::Hold object
927 on the API.
928
929 =cut
930
931 sub to_api_mapping {
932     return {
933         reserve_id       => 'hold_id',
934         borrowernumber   => 'patron_id',
935         reservedate      => 'hold_date',
936         biblionumber     => 'biblio_id',
937         branchcode       => 'pickup_library_id',
938         notificationdate => undef,
939         reminderdate     => undef,
940         cancellationdate => 'cancellation_date',
941         reservenotes     => 'notes',
942         found            => 'status',
943         itemnumber       => 'item_id',
944         waitingdate      => 'waiting_date',
945         expirationdate   => 'expiration_date',
946         patron_expiration_date => undef,
947         lowestPriority   => 'lowest_priority',
948         suspend          => 'suspended',
949         suspend_until    => 'suspended_until',
950         itemtype         => 'item_type',
951         item_level_hold  => 'item_level',
952     };
953 }
954
955 =head3 can_update_pickup_location_opac
956
957     my $can_update_pickup_location_opac = $hold->can_update_pickup_location_opac;
958
959 Returns if a hold can change pickup location from opac
960
961 =cut
962
963 sub can_update_pickup_location_opac {
964     my ($self) = @_;
965
966     my @statuses = split /,/, C4::Context->preference("OPACAllowUserToChangeBranch");
967     foreach my $status ( @statuses ){
968         return 1 if ($status eq 'pending' && !$self->is_found && !$self->is_suspended );
969         return 1 if ($status eq 'intransit' && $self->is_in_transit);
970         return 1 if ($status eq 'suspended' && $self->is_suspended);
971     }
972     return 0;
973 }
974
975 =head2 Internal methods
976
977 =head3 _type
978
979 =cut
980
981 sub _type {
982     return 'Reserve';
983 }
984
985 =head1 AUTHORS
986
987 Kyle M Hall <kyle@bywatersolutions.com>
988 Jonathan Druart <jonathan.druart@bugs.koha-community.org>
989 Martin Renvoize <martin.renvoize@ptfs-europe.com>
990
991 =cut
992
993 1;