Bug 23271: Replace search_limited with search_with_library_limits
[srvgit] / Koha / Item.pm
index fcb9f69..dcdcfbd 100644 (file)
@@ -94,9 +94,18 @@ sub store {
     my $action = 'create';
 
     unless ( $self->in_storage ) { #AddItem
+
         unless ( $self->permanent_location ) {
             $self->permanent_location($self->location);
         }
+
+        my $default_location = C4::Context->preference('NewItemsDefaultLocation');
+        unless ( $self->location || !$default_location ) {
+            $self->permanent_location( $self->location || $default_location )
+              unless $self->permanent_location;
+            $self->location($default_location);
+        }
+
         unless ( $self->replacementpricedate ) {
             $self->replacementpricedate($today);
         }
@@ -405,17 +414,25 @@ sub holds {
 =head3 request_transfer
 
   my $transfer = $item->request_transfer(
-      { to => $to_library, reason => $reason, ignore_limits => 0 } );
+    {
+        to     => $to_library,
+        reason => $reason,
+        [ ignore_limits => 0, enqueue => 1, replace => 1 ]
+    }
+  );
 
 Add a transfer request for this item to the given branch for the given reason.
 
 An exception will be thrown if the BranchTransferLimits would prevent the requested
 transfer, unless 'ignore_limits' is passed to override the limits.
 
-Note: At this time, only one active transfer (i.e pending arrival date) may exist
-at a time for any given item. An exception will be thrown should you attempt to
-add a request when a transfer has already been queued, whether it is in transit
-or just at the request stage.
+An exception will be thrown if an active transfer (i.e pending arrival date) is found;
+The caller should catch such cases and retry the transfer request as appropriate passing
+an appropriate override.
+
+Overrides
+* enqueue - Used to queue up the transfer when the existing transfer is found to be in transit.
+* replace - Used to replace the existing transfer request with your own.
 
 =cut
 
@@ -431,14 +448,17 @@ sub request_transfer {
         }
     }
 
-    my $request;
-    Koha::Exceptions::Item::Transfer::Found->throw( transfer => $request )
-      if ( $request = $self->get_transfer );
-
     Koha::Exceptions::Item::Transfer::Limit->throw()
       unless ( $params->{ignore_limits}
         || $self->can_be_transferred( { to => $params->{to} } ) );
 
+    my $request = $self->get_transfer;
+    Koha::Exceptions::Item::Transfer::InQueue->throw( transfer => $request )
+      if ( $request && !$params->{enqueue} && !$params->{replace} );
+
+    $request->cancel( { reason => $params->{reason}, force => 1 } )
+      if ( defined($request) && $params->{replace} );
+
     my $transfer = Koha::Item::Transfer->new(
         {
             itemnumber    => $self->itemnumber,
@@ -449,6 +469,7 @@ sub request_transfer {
             comments      => $params->{comment}
         }
     )->store();
+
     return $transfer;
 }
 
@@ -471,10 +492,14 @@ we still expect the item to end up at a final location eventually.
 sub get_transfer {
     my ($self) = @_;
     my $transfer_rs = $self->_result->branchtransfers->search(
-        { datearrived => undef },
         {
-            order_by => [ { -desc => 'datesent' }, { -asc => 'daterequested' } ],
-            rows     => 1
+            datearrived   => undef,
+            datecancelled => undef
+        },
+        {
+            order_by =>
+              [ { -desc => 'datesent' }, { -asc => 'daterequested' } ],
+            rows => 1
         }
     )->first;
     return unless $transfer_rs;
@@ -638,8 +663,8 @@ sub pickup_locations {
       C4::Circulation::GetBranchItemRule( $circ_control_branch, $self->itype );
 
     if(defined $patron) {
-        return Koha::Libraries->new()->empty if $branchitemrule->{holdallowed} == 3 && !$self->home_branch->validate_hold_sibling( {branchcode => $patron->branchcode} );
-        return Koha::Libraries->new()->empty if $branchitemrule->{holdallowed} == 1 && $self->home_branch->branchcode ne $patron->branchcode;
+        return Koha::Libraries->new()->empty if $branchitemrule->{holdallowed} eq 'from_local_hold_group' && !$self->home_branch->validate_hold_sibling( {branchcode => $patron->branchcode} );
+        return Koha::Libraries->new()->empty if $branchitemrule->{holdallowed} eq 'from_home_library' && $self->home_branch->branchcode ne $patron->branchcode;
     }
 
     my $pickup_libraries = Koha::Libraries->search();