use C4::Context;
use C4::Search;
use C4::Items;
-use C4::Branch;
use C4::Circulation;
use C4::Members;
use C4::Biblio;
my $dbh = C4::Context->dbh;
my $request_query = "SELECT biblionumber, borrowernumber, itemnumber, priority, reserves.branchcode,
- reservedate, reservenotes, borrowers.branchcode AS borrowerbranch
+ reservedate, reservenotes, borrowers.branchcode AS borrowerbranch, itemtype
FROM reserves
JOIN borrowers USING (borrowernumber)
WHERE biblionumber = ?
sub MapItemsToHoldRequests {
my ($hold_requests, $available_items, $branches_to_use, $transport_cost_matrix) = @_;
+
# handle trival cases
return unless scalar(@$hold_requests) > 0;
return unless scalar(@$available_items) > 0;
# figure out which item-level requests can be filled
my $num_items_remaining = scalar(@$available_items);
+
+ # Look for Local Holds Priority matches first
+ if ( C4::Context->preference('LocalHoldsPriority') ) {
+ my $LocalHoldsPriorityPatronControl =
+ C4::Context->preference('LocalHoldsPriorityPatronControl');
+ my $LocalHoldsPriorityItemControl =
+ C4::Context->preference('LocalHoldsPriorityItemControl');
+
+ foreach my $request (@$hold_requests) {
+ last if $num_items_remaining == 0;
+
+ my $local_hold_match;
+ foreach my $item (@$available_items) {
+ next
+ if ( !$item->{holdallowed} )
+ || ( $item->{holdallowed} == 1
+ && $item->{homebranch} ne $request->{borrowerbranch} );
+
+ my $local_holds_priority_item_branchcode =
+ $item->{$LocalHoldsPriorityItemControl};
+
+ my $local_holds_priority_patron_branchcode =
+ ( $LocalHoldsPriorityPatronControl eq 'PickupLibrary' )
+ ? $request->{branchcode}
+ : ( $LocalHoldsPriorityPatronControl eq 'HomeLibrary' )
+ ? $request->{borrowerbranch}
+ : undef;
+
+ $local_hold_match =
+ $local_holds_priority_item_branchcode eq
+ $local_holds_priority_patron_branchcode;
+
+ if ($local_hold_match) {
+ if ( exists $items_by_itemnumber{ $item->{itemnumber} }
+ and not exists $allocated_items{ $item->{itemnumber} } )
+ {
+ $item_map{ $item->{itemnumber} } = {
+ borrowernumber => $request->{borrowernumber},
+ biblionumber => $request->{biblionumber},
+ holdingbranch => $item->{holdingbranch},
+ pickup_branch => $request->{branchcode}
+ || $request->{borrowerbranch},
+ item_level => 0,
+ reservedate => $request->{reservedate},
+ reservenotes => $request->{reservenotes},
+ };
+ $allocated_items{ $item->{itemnumber} }++;
+ $num_items_remaining--;
+ }
+ }
+ }
+ }
+ }
+
foreach my $request (@$hold_requests) {
last if $num_items_remaining == 0;
and ( # Don't fill item level holds that contravene the hold pickup policy at this time
( $items_by_itemnumber{ $request->{itemnumber} }->{hold_fulfillment_policy} eq 'any' )
|| ( $request->{branchcode} eq $items_by_itemnumber{ $request->{itemnumber} }->{ $items_by_itemnumber{ $request->{itemnumber} }->{hold_fulfillment_policy} } )
+ and ( !$request->{itemtype} # If hold itemtype is set, item's itemtype must match
+ || $items_by_itemnumber{ $request->{itemnumber} }->{itype} eq $request->{itemtype} )
)
)
$request->{borrowerbranch} eq $item->{homebranch}
&& ( ( $item->{hold_fulfillment_policy} eq 'any' ) # Don't fill item level holds that contravene the hold pickup policy at this time
|| $request->{branchcode} eq $item->{ $item->{hold_fulfillment_policy} } )
+ && ( !$request->{itemtype} # If hold itemtype is set, item's itemtype must match
+ || $items_by_itemnumber{ $request->{itemnumber} }->{itype} eq $request->{itemtype} )
)
{
$itemnumber = $item->{itemnumber};
next unless $item->{hold_fulfillment_policy} eq 'any'
|| $request->{branchcode} eq $item->{ $item->{hold_fulfillment_policy} };
+ # If hold itemtype is set, item's itemtype must match
+ next unless ( !$request->{itemtype}
+ || $item->{itype} eq $request->{itemtype} );
+
$itemnumber = $item->{itemnumber};
last;
}
next unless $item->{hold_fulfillment_policy} eq 'any'
|| $request->{branchcode} eq $item->{ $item->{hold_fulfillment_policy} };
+ # If hold itemtype is set, item's itemtype must match
+ next unless ( !$request->{itemtype}
+ || $item->{itype} eq $request->{itemtype} );
+
$itemnumber = $item->{itemnumber};
$holdingbranch = $branch;
last PULL_BRANCHES;
next unless $current_item->{hold_fulfillment_policy} eq 'any'
|| $request->{branchcode} eq $current_item->{ $current_item->{hold_fulfillment_policy} };
+ # If hold itemtype is set, item's itemtype must match
+ next unless ( !$request->{itemtype}
+ || $current_item->{itype} eq $request->{itemtype} );
+
$itemnumber = $current_item->{itemnumber};
last; # quit this loop as soon as we have a suitable item
}
next unless $item->{hold_fulfillment_policy} eq 'any'
|| $request->{branchcode} eq $item->{ $item->{hold_fulfillment_policy} };
+ # If hold itemtype is set, item's itemtype must match
+ next unless ( !$request->{itemtype}
+ || $item->{itype} eq $request->{itemtype} );
+
$itemnumber = $item->{itemnumber};
$holdingbranch = $branch;
last PULL_BRANCHES2;