Bug 11175: (follow-up) Don't return explicitly undef
[koha-ffzg.git] / Koha / Hold.pm
index 360b43e..26c41d7 100644 (file)
@@ -20,15 +20,15 @@ package Koha::Hold;
 
 use Modern::Perl;
 
-use Carp;
-use Data::Dumper qw(Dumper);
+use Data::Dumper qw( Dumper );
+use List::MoreUtils qw( any );
 
-use C4::Context qw(preference);
-use C4::Letters;
-use C4::Log;
+use C4::Context;
+use C4::Letters qw( GetPreparedLetter EnqueueLetter );
+use C4::Log qw( logaction );
 
 use Koha::AuthorisedValues;
-use Koha::DateUtils qw(dt_from_string output_pref);
+use Koha::DateUtils qw( dt_from_string );
 use Koha::Patrons;
 use Koha::Biblios;
 use Koha::Items;
@@ -46,7 +46,7 @@ Koha::Hold - Koha Hold object class
 
 =head1 API
 
-=head2 Class Methods
+=head2 Class methods
 
 =cut
 
@@ -96,6 +96,9 @@ sub suspend_hold {
         elsif ( $self->is_in_transit ) {
             Koha::Exceptions::Hold::CannotSuspendFound->throw( status => 'T' );
         }
+        elsif ( $self->is_in_processing ) {
+            Koha::Exceptions::Hold::CannotSuspendFound->throw( status => 'P' );
+        }
         else {
             Koha::Exceptions::Hold::CannotSuspendFound->throw(
                       'Unhandled data exception on found hold (id='
@@ -153,24 +156,34 @@ sub delete {
     return $deleted;
 }
 
+=head3 set_transfer
+
+=cut
+
+sub set_transfer {
+    my ( $self ) = @_;
+
+    $self->priority(0);
+    $self->found('T');
+    $self->store();
+
+    return $self;
+}
+
 =head3 set_waiting
 
 =cut
 
 sub set_waiting {
-    my ( $self, $transferToDo ) = @_;
+    my ( $self, $desk_id ) = @_;
 
     $self->priority(0);
 
-    if ($transferToDo) {
-        $self->found('T')->store();
-        return $self;
-    }
-
     my $today = dt_from_string();
     my $values = {
         found => 'W',
         waitingdate => $today->ymd,
+        desk_id => $desk_id,
     };
 
     my $requested_expiration;
@@ -208,9 +221,97 @@ sub set_waiting {
     return $self;
 }
 
+=head3 is_pickup_location_valid
+
+    if ($hold->is_pickup_location_valid({ library_id => $library->id }) ) {
+        ...
+    }
+
+Returns a I<boolean> representing if the passed pickup location is valid for the hold.
+It throws a I<Koha::Exceptions::_MissingParameter> if the library_id parameter is not
+passed.
+
+=cut
+
+sub is_pickup_location_valid {
+    my ( $self, $params ) = @_;
+
+    Koha::Exceptions::MissingParameter->throw('The library_id parameter is mandatory')
+        unless $params->{library_id};
+
+    my @pickup_locations;
+
+    if ( $self->itemnumber ) { # item-level
+        @pickup_locations = $self->item->pickup_locations({ patron => $self->patron });
+    }
+    else { # biblio-level
+        @pickup_locations = $self->biblio->pickup_locations({ patron => $self->patron });
+    }
+
+    return any { $_->branchcode eq $params->{library_id} } @pickup_locations;
+}
+
+=head3 set_pickup_location
+
+    $hold->set_pickup_location(
+        {
+            library_id => $library->id,
+          [ force   => 0|1 ]
+        }
+    );
+
+Updates the hold pickup location. It throws a I<Koha::Exceptions::Hold::InvalidPickupLocation> if
+the passed pickup location is not valid.
+
+Note: It is up to the caller to verify if I<AllowHoldPolicyOverride> is set when setting the
+B<force> parameter.
+
+=cut
+
+sub set_pickup_location {
+    my ( $self, $params ) = @_;
+
+    Koha::Exceptions::MissingParameter->throw('The library_id parameter is mandatory')
+        unless $params->{library_id};
+
+    if (
+        $params->{force}
+        || $self->is_pickup_location_valid(
+            { library_id => $params->{library_id} }
+        )
+      )
+    {
+        # all good, set the new pickup location
+        $self->branchcode( $params->{library_id} )->store;
+    }
+    else {
+        Koha::Exceptions::Hold::InvalidPickupLocation->throw;
+    }
+
+    return $self;
+}
+
+=head3 set_processing
+
+$hold->set_processing;
+
+Mark the hold as in processing.
+
+=cut
+
+sub set_processing {
+    my ( $self ) = @_;
+
+    $self->priority(0);
+    $self->found('P');
+    $self->store();
+
+    return $self;
+}
+
 =head3 is_found
 
-Returns true if hold is a waiting or in transit
+Returns true if hold is waiting, in transit or in processing
 
 =cut
 
@@ -220,6 +321,7 @@ sub is_found {
     return 0 unless $self->found();
     return 1 if $self->found() eq 'W';
     return 1 if $self->found() eq 'T';
+    return 1 if $self->found() eq 'P';
 }
 
 =head3 is_waiting
@@ -248,6 +350,19 @@ sub is_in_transit {
     return $self->found() eq 'T';
 }
 
+=head3 is_in_processing
+
+Returns true if hold is a in_processing hold
+
+=cut
+
+sub is_in_processing {
+    my ($self) = @_;
+
+    return 0 unless $self->found();
+    return $self->found() eq 'P';
+}
+
 =head3 is_cancelable_from_opac
 
 Returns true if hold is a cancelable hold
@@ -262,7 +377,7 @@ sub is_cancelable_from_opac {
     my ($self) = @_;
 
     return 1 unless $self->is_found();
-    return 0; # if ->is_in_transit or if ->is_waiting
+    return 0; # if ->is_in_transit or if ->is_waiting or ->is_in_processing
 }
 
 =head3 is_at_destination
@@ -293,6 +408,19 @@ sub biblio {
     return $self->{_biblio};
 }
 
+=head3 patron
+
+Returns the related Koha::Patron object for this hold
+
+=cut
+
+sub patron {
+    my ($self) = @_;
+
+    my $patron_rs = $self->_result->patron;
+    return Koha::Patron->_new_from_dbic($patron_rs);
+}
+
 =head3 item
 
 Returns the related Koha::Item object for this Hold
@@ -321,6 +449,19 @@ sub branch {
     return $self->{_branch};
 }
 
+=head3 desk
+
+Returns the related Koha::Desk object for this Hold
+
+=cut
+
+sub desk {
+    my $self = shift;
+    my $desk_rs = $self->_result->desk;
+    return unless $desk_rs;
+    return Koha::Desk->_new_from_dbic($desk_rs);
+}
+
 =head3 borrower
 
 Returns the related Koha::Patron object for this Hold
@@ -434,6 +575,58 @@ sub cancel {
     return $self;
 }
 
+=head3 store
+
+Override base store method to set default
+expirationdate for holds.
+
+=cut
+
+sub store {
+    my ($self) = @_;
+
+    if ( !$self->in_storage ) {
+        if (
+            C4::Context->preference('DefaultHoldExpirationdate')
+            and ( not defined $self->expirationdate
+                or $self->expirationdate eq '' )
+          )
+        {
+            $self->_set_default_expirationdate;
+        }
+    }
+    else {
+
+        my %updated_columns = $self->_result->get_dirty_columns;
+        return $self->SUPER::store unless %updated_columns;
+
+        if ( exists $updated_columns{reservedate} ) {
+            if (
+                C4::Context->preference('DefaultHoldExpirationdate')
+                and ( not exists $updated_columns{expirationdate}
+                    or exists $updated_columns{expirationdate}
+                    and $updated_columns{expirationdate} eq '' )
+              )
+            {
+                $self->_set_default_expirationdate;
+            }
+        }
+    }
+
+    $self = $self->SUPER::store;
+}
+
+sub _set_default_expirationdate {
+    my $self = shift;
+
+    my $period = C4::Context->preference('DefaultHoldExpirationdatePeriod') || 0;
+    my $timeunit =
+      C4::Context->preference('DefaultHoldExpirationdateUnitOfTime') || 'days';
+
+    $self->expirationdate(
+        dt_from_string( $self->reservedate )->add( $timeunit => $period ) );
+}
+
 =head3 _move_to_old
 
 my $is_moved = $hold->_move_to_old;