Bug 11175: (follow-up) Don't return explicitly undef
[koha-ffzg.git] / Koha / Hold.pm
index 892fb20..26c41d7 100644 (file)
@@ -20,16 +20,15 @@ package Koha::Hold;
 
 use Modern::Perl;
 
-use Carp;
-use Data::Dumper qw(Dumper);
-use List::MoreUtils qw(any);
+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;
@@ -47,7 +46,7 @@ Koha::Hold - Koha Hold object class
 
 =head1 API
 
-=head2 Class Methods
+=head2 Class methods
 
 =cut
 
@@ -254,11 +253,19 @@ sub is_pickup_location_valid {
 
 =head3 set_pickup_location
 
-    $hold->set_pickup_location({ library_id => $library->id });
+    $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 {
@@ -267,7 +274,13 @@ sub set_pickup_location {
     Koha::Exceptions::MissingParameter->throw('The library_id parameter is mandatory')
         unless $params->{library_id};
 
-    if ( $self->is_pickup_location_valid({ library_id => $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;
     }
@@ -562,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;