Bug 10403: (follow-up) fix test to use vendor created earlier during test
[koha.git] / C4 / HoldsQueue.pm
index e423283..7062cbe 100755 (executable)
@@ -345,7 +345,7 @@ sub GetItemsAvailableToFillHoldRequestsForBib {
     my @items = grep { ! scalar GetTransfers($_->{itemnumber}) } @$itm;
     return [ grep {
         my $rule = GetBranchItemRule($_->{homebranch}, $_->{itype});
-        $_->{holdallowed} = $rule->{holdallowed} != 0
+        $_->{holdallowed} = $rule->{holdallowed};
     } @items ];
 }
 
@@ -448,7 +448,6 @@ sub MapItemsToHoldRequests {
                     $itemnumber = $item->{itemnumber};
                     last;
                 }
-                $itemnumber ||= $holding_branch_items->[0]->{itemnumber};
             }
             else {
                 warn "No transport costs for $pickup_branch";
@@ -476,8 +475,15 @@ sub MapItemsToHoldRequests {
                     last PULL_BRANCHES;
                 }
             }
-            $itemnumber ||= $items_by_branch{$holdingbranch}->[0]->{itemnumber}
-              if $holdingbranch;
+
+            unless ( $itemnumber ) {
+                foreach my $current_item ( @{ $items_by_branch{$holdingbranch} } ) {
+                    if ( $holdingbranch && ( $current_item->{holdallowed} == 2 || $pickup_branch eq $current_item->{homebranch} ) ) {
+                        $itemnumber = $current_item->{itemnumber};
+                        last; # quit this loop as soon as we have a suitable item
+                    }
+                }
+            }
         }
 
         if ($itemnumber) {