1 package C4::HoldsQueue;
3 # Copyright 2011 Catalyst IT
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 # FIXME: expand perldoc, explain intended logic
36 use List::Util qw(shuffle);
37 use List::MoreUtils qw(any);
40 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
49 &UpdateTransportCostMatrix
56 =head2 TransportCostMatrix
58 TransportCostMatrix();
60 Returns Transport Cost Matrix as a hashref <to branch code> => <from branch code> => cost
64 sub TransportCostMatrix {
66 my $ignore_holds_queue_skip_closed = $params->{ignore_holds_queue_skip_closed};
68 my $dbh = C4::Context->dbh;
69 my $transport_costs = $dbh->selectall_arrayref("SELECT * FROM transport_cost",{ Slice => {} });
71 my $today = dt_from_string();
73 my %transport_cost_matrix;
74 foreach (@$transport_costs) {
75 my $from = $_->{frombranch};
76 my $to = $_->{tobranch};
77 my $cost = $_->{cost};
78 my $disabled = $_->{disable_transfer};
79 $transport_cost_matrix{$to}{$from} = {
81 disable_transfer => $disabled
84 if ( !$ignore_holds_queue_skip_closed && C4::Context->preference("HoldsQueueSkipClosed") ) {
85 $calendars->{$from} ||= Koha::Calendar->new( branchcode => $from );
86 $transport_cost_matrix{$to}{$from}{disable_transfer} ||=
87 $calendars->{$from}->is_holiday( $today );
91 return \%transport_cost_matrix;
94 =head2 UpdateTransportCostMatrix
96 UpdateTransportCostMatrix($records);
98 Updates full Transport Cost Matrix table. $records is an arrayref of records.
99 Records: { frombranch => <code>, tobranch => <code>, cost => <figure>, disable_transfer => <0,1> }
103 sub UpdateTransportCostMatrix {
105 my $dbh = C4::Context->dbh;
107 my $sth = $dbh->prepare("INSERT INTO transport_cost (frombranch, tobranch, cost, disable_transfer) VALUES (?, ?, ?, ?)");
109 $dbh->do("DELETE FROM transport_cost");
110 foreach (@$records) {
111 my $cost = $_->{cost};
112 my $from = $_->{frombranch};
113 my $to = $_->{tobranch};
114 if ($_->{disable_transfer}) {
117 elsif ( !defined ($cost) || ($cost !~ m/(0|[1-9][0-9]*)(\.[0-9]*)?/o) ) {
118 warn "Invalid $from -> $to cost $cost - must be a number >= 0, disabling";
120 $_->{disable_transfer} = 1;
122 $sth->execute( $from, $to, $cost, $_->{disable_transfer} ? 1 : 0 );
126 =head2 GetHoldsQueueItems
128 GetHoldsQueueItems($branch);
130 Returns hold queue for a holding branch. If branch is omitted, then whole queue is returned
134 sub GetHoldsQueueItems {
135 my ($branchlimit) = @_;
136 my $dbh = C4::Context->dbh;
138 my @bind_params = ();
139 my $query = q/SELECT tmp_holdsqueue.*, biblio.author, items.ccode, items.itype, biblioitems.itemtype, items.location,
140 items.enumchron, items.cn_sort, biblioitems.publishercode,
141 biblio.copyrightdate, biblio.subtitle, biblio.medium,
142 biblio.part_number, biblio.part_name,
143 biblioitems.publicationyear, biblioitems.pages, biblioitems.size,
144 biblioitems.isbn, biblioitems.editionstatement, items.copynumber
146 JOIN biblio USING (biblionumber)
147 LEFT JOIN biblioitems USING (biblionumber)
148 LEFT JOIN items USING ( itemnumber)
151 $query .=" WHERE tmp_holdsqueue.holdingbranch = ?";
152 push @bind_params, $branchlimit;
154 $query .= " ORDER BY ccode, location, cn_sort, author, title, pickbranch, reservedate";
155 my $sth = $dbh->prepare($query);
156 $sth->execute(@bind_params);
158 while ( my $row = $sth->fetchrow_hashref ){
159 # return the bib-level or item-level itype per syspref
160 if (!C4::Context->preference('item-level_itypes')) {
161 $row->{itype} = $row->{itemtype};
163 delete $row->{itemtype};
174 Top level function that turns reserves into tmp_holdsqueue and hold_fill_targets.
179 my $dbh = C4::Context->dbh;
181 $dbh->do("DELETE FROM tmp_holdsqueue"); # clear the old table for new info
182 $dbh->do("DELETE FROM hold_fill_targets");
185 my $total_requests = 0;
186 my $total_available_items = 0;
187 my $num_items_mapped = 0;
190 my $transport_cost_matrix;
191 my $use_transport_cost_matrix = C4::Context->preference("UseTransportCostMatrix");
192 if ($use_transport_cost_matrix) {
193 $transport_cost_matrix = TransportCostMatrix();
194 unless (keys %$transport_cost_matrix) {
195 warn "UseTransportCostMatrix set to yes, but matrix not populated";
196 undef $transport_cost_matrix;
199 unless ($transport_cost_matrix) {
200 $branches_to_use = load_branches_to_pull_from();
203 my $bibs_with_pending_requests = GetBibsWithPendingHoldRequests();
205 foreach my $biblionumber (@$bibs_with_pending_requests) {
207 my $hold_requests = GetPendingHoldRequestsForBib($biblionumber);
208 my $available_items = GetItemsAvailableToFillHoldRequestsForBib($biblionumber, $branches_to_use);
209 $total_requests += scalar(@$hold_requests);
210 $total_available_items += scalar(@$available_items);
212 my $item_map = MapItemsToHoldRequests($hold_requests, $available_items, $branches_to_use, $transport_cost_matrix);
214 my $item_map_size = scalar(keys %$item_map)
217 $num_items_mapped += $item_map_size;
218 CreatePicklistFromItemMap($item_map);
219 AddToHoldTargetMap($item_map);
220 if (($item_map_size < scalar(@$hold_requests )) and
221 ($item_map_size < scalar(@$available_items))) {
222 # DOUBLE CHECK, but this is probably OK - unfilled item-level requests
224 #warn "unfilled requests for $biblionumber";
225 #warn Dumper($hold_requests), Dumper($available_items), Dumper($item_map);
230 =head2 GetBibsWithPendingHoldRequests
232 my $biblionumber_aref = GetBibsWithPendingHoldRequests();
234 Return an arrayref of the biblionumbers of all bibs
235 that have one or more unfilled hold requests.
239 sub GetBibsWithPendingHoldRequests {
240 my $dbh = C4::Context->dbh;
242 my $bib_query = "SELECT DISTINCT biblionumber
246 AND reservedate <= CURRENT_DATE()
249 my $sth = $dbh->prepare($bib_query);
252 my $biblionumbers = $sth->fetchall_arrayref();
254 return [ map { $_->[0] } @$biblionumbers ];
257 =head2 GetPendingHoldRequestsForBib
259 my $requests = GetPendingHoldRequestsForBib($biblionumber);
261 Returns an arrayref of hashrefs to pending, unfilled hold requests
262 on the bib identified by $biblionumber. The following keys
263 are present in each hashref:
274 The arrayref is sorted in order of increasing priority.
278 sub GetPendingHoldRequestsForBib {
279 my $biblionumber = shift;
281 my $dbh = C4::Context->dbh;
283 my $request_query = "SELECT biblionumber, borrowernumber, itemnumber, priority, reserve_id, reserves.branchcode,
284 reservedate, reservenotes, borrowers.branchcode AS borrowerbranch, itemtype, item_level_hold
286 JOIN borrowers USING (borrowernumber)
287 WHERE biblionumber = ?
290 AND reservedate <= CURRENT_DATE()
293 my $sth = $dbh->prepare($request_query);
294 $sth->execute($biblionumber);
296 my $requests = $sth->fetchall_arrayref({});
301 =head2 GetItemsAvailableToFillHoldRequestsForBib
303 my $available_items = GetItemsAvailableToFillHoldRequestsForBib($biblionumber, $branches_ar);
305 Returns an arrayref of items available to fill hold requests
306 for the bib identified by C<$biblionumber>. An item is available
307 to fill a hold request if and only if:
310 * it is not withdrawn
311 * it is not marked notforloan
312 * it is not currently in transit
314 * it is not sitting on the hold shelf
315 * it is not damaged (unless AllowHoldsOnDamagedItems is on)
319 sub GetItemsAvailableToFillHoldRequestsForBib {
320 my ($biblionumber, $branches_to_use) = @_;
322 my $dbh = C4::Context->dbh;
323 my $items_query = "SELECT itemnumber, homebranch, holdingbranch, itemtypes.itemtype AS itype
326 if (C4::Context->preference('item-level_itypes')) {
327 $items_query .= "LEFT JOIN itemtypes ON (itemtypes.itemtype = items.itype) ";
329 $items_query .= "JOIN biblioitems USING (biblioitemnumber)
330 LEFT JOIN itemtypes USING (itemtype) ";
332 $items_query .= "WHERE items.notforloan = 0
333 AND holdingbranch IS NOT NULL
336 $items_query .= " AND damaged = 0" unless C4::Context->preference('AllowHoldsOnDamagedItems');
337 $items_query .= " AND items.onloan IS NULL
338 AND (itemtypes.notforloan IS NULL OR itemtypes.notforloan = 0)
339 AND itemnumber NOT IN (
342 WHERE biblionumber = ?
343 AND itemnumber IS NOT NULL
344 AND (found IS NOT NULL OR priority = 0)
346 AND items.biblionumber = ?";
348 my @params = ($biblionumber, $biblionumber);
349 if ($branches_to_use && @$branches_to_use) {
350 $items_query .= " AND holdingbranch IN (" . join (",", map { "?" } @$branches_to_use) . ")";
351 push @params, @$branches_to_use;
353 my $sth = $dbh->prepare($items_query);
354 $sth->execute(@params);
356 my $itm = $sth->fetchall_arrayref({});
357 my @items = grep { ! scalar GetTransfers($_->{itemnumber}) } @$itm;
359 my $rule = GetBranchItemRule($_->{homebranch}, $_->{itype});
360 $_->{holdallowed} = $rule->{holdallowed};
361 $_->{hold_fulfillment_policy} = $rule->{hold_fulfillment_policy};
365 =head2 _checkHoldPolicy
367 _checkHoldPolicy($item, $request)
369 check if item agrees with hold policies
374 sub _checkHoldPolicy {
375 my ($item, $request) = @_;
377 return 0 unless $item->{holdallowed};
378 return 0 if $item->{holdallowed} == 1 && $item->{homebranch} ne $request->{borrowerbranch};
380 $libraries->{$item->{homebranch}} ||= Koha::Libraries->find($item->{homebranch});
381 my $library = $libraries->{$item->{homebranch}};
383 return 0 if $item->{'holdallowed'} == 3 && !$library->validate_hold_sibling({branchcode => $request->{borrowerbranch}});
385 my $hold_fulfillment_policy = $item->{hold_fulfillment_policy};
387 return 0 if $hold_fulfillment_policy eq 'holdgroup' && !$library->validate_hold_sibling({branchcode => $request->{branchcode}});
388 return 0 if $hold_fulfillment_policy eq 'homebranch' && $request->{branchcode} ne $item->{$hold_fulfillment_policy};
389 return 0 if $hold_fulfillment_policy eq 'holdingbranch' && $request->{branchcode} ne $item->{$hold_fulfillment_policy};
391 $libraries->{$request->{borrowerbranch}} ||= Koha::Libraries->find($request->{borrowerbranch});
392 my $patronLibrary = $libraries->{$request->{borrowerbranch}};
394 return 0 if $hold_fulfillment_policy eq 'patrongroup' && !$patronLibrary->validate_hold_sibling({branchcode => $request->{branchcode}});
400 =head2 MapItemsToHoldRequests
402 MapItemsToHoldRequests($hold_requests, $available_items, $branches, $transport_cost_matrix)
406 sub MapItemsToHoldRequests {
407 my ($hold_requests, $available_items, $branches_to_use, $transport_cost_matrix) = @_;
409 # handle trival cases
410 return unless scalar(@$hold_requests) > 0;
411 return unless scalar(@$available_items) > 0;
413 # identify item-level requests
414 my %specific_items_requested = map { $_->{itemnumber} => 1 }
415 grep { defined($_->{itemnumber}) }
418 map { $_->{_object} = Koha::Items->find( $_->{itemnumber} ) } @$available_items;
420 map { $libraries->{$_->id} = $_ } Koha::Libraries->search();
422 # group available items by itemnumber
423 my %items_by_itemnumber = map { $_->{itemnumber} => $_ } @$available_items;
425 # items already allocated
426 my %allocated_items = ();
428 # map of items to hold requests
431 # figure out which item-level requests can be filled
432 my $num_items_remaining = scalar(@$available_items);
434 # Look for Local Holds Priority matches first
435 if ( C4::Context->preference('LocalHoldsPriority') ) {
436 my $LocalHoldsPriorityPatronControl =
437 C4::Context->preference('LocalHoldsPriorityPatronControl');
438 my $LocalHoldsPriorityItemControl =
439 C4::Context->preference('LocalHoldsPriorityItemControl');
441 foreach my $request (@$hold_requests) {
442 last if $num_items_remaining == 0;
443 my $patron = Koha::Patrons->find($request->{borrowernumber});
444 next if $patron->category->exclude_from_local_holds_priority;
446 my $local_hold_match;
447 foreach my $item (@$available_items) {
448 next if $item->{_object}->exclude_from_local_holds_priority;
450 next unless _checkHoldPolicy($item, $request);
452 next if $request->{itemnumber} && $request->{itemnumber} != $item->{itemnumber};
454 next unless $item->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } );
456 my $local_holds_priority_item_branchcode =
457 $item->{$LocalHoldsPriorityItemControl};
459 my $local_holds_priority_patron_branchcode =
460 ( $LocalHoldsPriorityPatronControl eq 'PickupLibrary' )
461 ? $request->{branchcode}
462 : ( $LocalHoldsPriorityPatronControl eq 'HomeLibrary' )
463 ? $request->{borrowerbranch}
467 $local_holds_priority_item_branchcode eq
468 $local_holds_priority_patron_branchcode;
470 if ($local_hold_match) {
471 if ( exists $items_by_itemnumber{ $item->{itemnumber} }
472 and not exists $allocated_items{ $item->{itemnumber} }
473 and not $request->{allocated})
475 $item_map{ $item->{itemnumber} } = {
476 borrowernumber => $request->{borrowernumber},
477 biblionumber => $request->{biblionumber},
478 holdingbranch => $item->{holdingbranch},
479 pickup_branch => $request->{branchcode}
480 || $request->{borrowerbranch},
481 reserve_id => $request->{reserve_id},
482 item_level => $request->{item_level_hold},
483 reservedate => $request->{reservedate},
484 reservenotes => $request->{reservenotes},
486 $allocated_items{ $item->{itemnumber} }++;
487 $request->{allocated} = 1;
488 $num_items_remaining--;
495 foreach my $request (@$hold_requests) {
496 last if $num_items_remaining == 0;
497 next if $request->{allocated};
499 # is this an item-level request?
500 if (defined($request->{itemnumber})) {
501 # fill it if possible; if not skip it
503 exists $items_by_itemnumber{ $request->{itemnumber} }
504 and not exists $allocated_items{ $request->{itemnumber} }
505 and _checkHoldPolicy($items_by_itemnumber{ $request->{itemnumber} }, $request) # Don't fill item level holds that contravene the hold pickup policy at this time
506 and ( !$request->{itemtype} # If hold itemtype is set, item's itemtype must match
507 || $items_by_itemnumber{ $request->{itemnumber} }->{itype} eq $request->{itemtype} )
509 and $items_by_itemnumber{ $request->{itemnumber} }->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } )
514 $item_map{ $request->{itemnumber} } = {
515 borrowernumber => $request->{borrowernumber},
516 biblionumber => $request->{biblionumber},
517 holdingbranch => $items_by_itemnumber{ $request->{itemnumber} }->{holdingbranch},
518 pickup_branch => $request->{branchcode} || $request->{borrowerbranch},
519 reserve_id => $request->{reserve_id},
520 item_level => $request->{item_level_hold},
521 reservedate => $request->{reservedate},
522 reservenotes => $request->{reservenotes},
524 $allocated_items{ $request->{itemnumber} }++;
525 $num_items_remaining--;
528 # it's title-level request that will take up one item
529 $num_items_remaining--;
533 # group available items by branch
534 my %items_by_branch = ();
535 foreach my $item (@$available_items) {
536 next unless $item->{holdallowed};
538 push @{ $items_by_branch{ $item->{holdingbranch} } }, $item
539 unless exists $allocated_items{ $item->{itemnumber} };
541 return \%item_map unless keys %items_by_branch;
543 # now handle the title-level requests
544 $num_items_remaining = scalar(@$available_items) - scalar(keys %allocated_items);
546 foreach my $request (@$hold_requests) {
547 last if $num_items_remaining == 0;
548 next if $request->{allocated};
549 next if defined($request->{itemnumber}); # already handled these
551 # look for local match first
552 my $pickup_branch = $request->{branchcode} || $request->{borrowerbranch};
553 my ($itemnumber, $holdingbranch);
555 my $holding_branch_items = $items_by_branch{$pickup_branch};
556 if ( $holding_branch_items ) {
557 foreach my $item (@$holding_branch_items) {
558 next unless $items_by_itemnumber{ $item->{itemnumber} }->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } );
561 $request->{borrowerbranch} eq $item->{homebranch}
562 && _checkHoldPolicy($item, $request) # Don't fill item level holds that contravene the hold pickup policy at this time
563 && ( !$request->{itemtype} # If hold itemtype is set, item's itemtype must match
564 || ( $request->{itemnumber} && ( $items_by_itemnumber{ $request->{itemnumber} }->{itype} eq $request->{itemtype} ) ) )
567 $itemnumber = $item->{itemnumber};
571 $holdingbranch = $pickup_branch;
573 elsif ($transport_cost_matrix) {
574 $pull_branches = [keys %items_by_branch];
575 $holdingbranch = least_cost_branch( $pickup_branch, $pull_branches, $transport_cost_matrix );
576 if ( $holdingbranch ) {
578 my $holding_branch_items = $items_by_branch{$holdingbranch};
579 foreach my $item (@$holding_branch_items) {
580 next if $request->{borrowerbranch} ne $item->{homebranch};
581 next unless $items_by_itemnumber{ $item->{itemnumber} }->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } );
583 # Don't fill item level holds that contravene the hold pickup policy at this time
584 next unless _checkHoldPolicy($item, $request);
586 # If hold itemtype is set, item's itemtype must match
587 next unless ( !$request->{itemtype}
588 || $item->{itype} eq $request->{itemtype} );
590 $itemnumber = $item->{itemnumber};
599 unless ($itemnumber) {
600 # not found yet, fall back to basics
601 if ($branches_to_use) {
602 $pull_branches = $branches_to_use;
604 $pull_branches = [keys %items_by_branch];
607 # Try picking items where the home and pickup branch match first
609 foreach my $branch (@$pull_branches) {
610 my $holding_branch_items = $items_by_branch{$branch}
613 $holdingbranch ||= $branch;
614 foreach my $item (@$holding_branch_items) {
615 next if $pickup_branch ne $item->{homebranch};
616 next unless _checkHoldPolicy($item, $request);
617 next unless $items_by_itemnumber{ $item->{itemnumber} }->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } );
619 # Don't fill item level holds that contravene the hold pickup policy at this time
620 next unless $item->{hold_fulfillment_policy} eq 'any'
621 || $request->{branchcode} eq $item->{ $item->{hold_fulfillment_policy} };
623 # If hold itemtype is set, item's itemtype must match
624 next unless ( !$request->{itemtype}
625 || $item->{itype} eq $request->{itemtype} );
627 $itemnumber = $item->{itemnumber};
628 $holdingbranch = $branch;
633 # Now try items from the least cost branch based on the transport cost matrix or StaticHoldsQueueWeight
634 unless ( $itemnumber || !$holdingbranch) {
635 foreach my $current_item ( @{ $items_by_branch{$holdingbranch} } ) {
636 next unless _checkHoldPolicy($current_item, $request); # Don't fill item level holds that contravene the hold pickup policy at this time
638 # If hold itemtype is set, item's itemtype must match
639 next unless ( !$request->{itemtype}
640 || $current_item->{itype} eq $request->{itemtype} );
642 next unless $items_by_itemnumber{ $current_item->{itemnumber} }->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } );
644 $itemnumber = $current_item->{itemnumber};
645 last; # quit this loop as soon as we have a suitable item
649 # Now try for items for any item that can fill this hold
650 unless ( $itemnumber ) {
652 foreach my $branch (@$pull_branches) {
653 my $holding_branch_items = $items_by_branch{$branch}
656 foreach my $item (@$holding_branch_items) {
657 # Don't fill item level holds that contravene the hold pickup policy at this time
658 next unless _checkHoldPolicy($item, $request);
660 # If hold itemtype is set, item's itemtype must match
661 next unless ( !$request->{itemtype}
662 || $item->{itype} eq $request->{itemtype} );
664 next unless $items_by_itemnumber{ $item->{itemnumber} }->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } );
666 $itemnumber = $item->{itemnumber};
667 $holdingbranch = $branch;
675 my $holding_branch_items = $items_by_branch{$holdingbranch}
676 or die "Have $itemnumber, $holdingbranch, but no items!";
677 @$holding_branch_items = grep { $_->{itemnumber} != $itemnumber } @$holding_branch_items;
678 delete $items_by_branch{$holdingbranch} unless @$holding_branch_items;
680 $item_map{$itemnumber} = {
681 borrowernumber => $request->{borrowernumber},
682 biblionumber => $request->{biblionumber},
683 holdingbranch => $holdingbranch,
684 pickup_branch => $pickup_branch,
685 reserve_id => $request->{reserve_id},
686 item_level => $request->{item_level_hold},
687 reservedate => $request->{reservedate},
688 reservenotes => $request->{reservenotes},
690 $num_items_remaining--;
696 =head2 CreatePickListFromItemMap
700 sub CreatePicklistFromItemMap {
701 my $item_map = shift;
703 my $dbh = C4::Context->dbh;
705 my $sth_load=$dbh->prepare("
706 INSERT INTO tmp_holdsqueue (biblionumber,itemnumber,barcode,surname,firstname,phone,borrowernumber,
707 cardnumber,reservedate,title, itemcallnumber,
708 holdingbranch,pickbranch,notes, item_level_request)
709 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
712 foreach my $itemnumber (sort keys %$item_map) {
713 my $mapped_item = $item_map->{$itemnumber};
714 my $biblionumber = $mapped_item->{biblionumber};
715 my $borrowernumber = $mapped_item->{borrowernumber};
716 my $pickbranch = $mapped_item->{pickup_branch};
717 my $holdingbranch = $mapped_item->{holdingbranch};
718 my $reservedate = $mapped_item->{reservedate};
719 my $reservenotes = $mapped_item->{reservenotes};
720 my $item_level = $mapped_item->{item_level};
722 my $item = Koha::Items->find($itemnumber);
723 my $barcode = $item->barcode;
724 my $itemcallnumber = $item->itemcallnumber;
726 my $patron = Koha::Patrons->find( $borrowernumber );
727 my $cardnumber = $patron->cardnumber;
728 my $surname = $patron->surname;
729 my $firstname = $patron->firstname;
730 my $phone = $patron->phone;
732 my $biblio = Koha::Biblios->find( $biblionumber );
733 my $title = $biblio->title;
735 $sth_load->execute($biblionumber, $itemnumber, $barcode, $surname, $firstname, $phone, $borrowernumber,
736 $cardnumber, $reservedate, $title, $itemcallnumber,
737 $holdingbranch, $pickbranch, $reservenotes, $item_level);
741 =head2 AddToHoldTargetMap
745 sub AddToHoldTargetMap {
746 my $item_map = shift;
748 my $dbh = C4::Context->dbh;
751 INSERT INTO hold_fill_targets (borrowernumber, biblionumber, itemnumber, source_branchcode, item_level_request, reserve_id)
752 VALUES (?, ?, ?, ?, ?, ?)
754 my $sth_insert = $dbh->prepare($insert_sql);
756 foreach my $itemnumber (keys %$item_map) {
757 my $mapped_item = $item_map->{$itemnumber};
758 $sth_insert->execute($mapped_item->{borrowernumber}, $mapped_item->{biblionumber}, $itemnumber,
759 $mapped_item->{holdingbranch}, $mapped_item->{item_level}, $mapped_item->{reserve_id});
763 # Helper functions, not part of any interface
766 return $_[0] unless $_[0];
772 sub load_branches_to_pull_from {
775 my $static_branch_list = C4::Context->preference("StaticHoldsQueueWeight");
776 @branches_to_use = map { _trim($_) } split( /,/, $static_branch_list )
777 if $static_branch_list;
780 Koha::Database->new()->schema()->resultset('Branch')
781 ->get_column('branchcode')->all()
782 unless (@branches_to_use);
784 @branches_to_use = shuffle(@branches_to_use)
785 if C4::Context->preference("RandomizeHoldsQueueWeight");
787 my $today = dt_from_string();
788 if ( C4::Context->preference('HoldsQueueSkipClosed') ) {
789 @branches_to_use = grep {
790 !Koha::Calendar->new( branchcode => $_ )
791 ->is_holiday( $today )
795 return \@branches_to_use;
798 sub least_cost_branch {
801 my ($to, $from, $transport_cost_matrix) = @_;
803 # Nothing really spectacular: supply to branch, a list of potential from branches
804 # and find the minimum from - to value from the transport_cost_matrix
805 return $from->[0] if ( @$from == 1 && $transport_cost_matrix->{$to}{$from->[0]}->{disable_transfer} != 1 );
807 # If the pickup library is in the list of libraries to pull from,
808 # return that library right away, it is obviously the least costly
809 return ($to) if any { $_ eq $to } @$from;
811 my ($least_cost, @branch);
813 my $cell = $transport_cost_matrix->{$to}{$_};
814 next if $cell->{disable_transfer};
816 my $cost = $cell->{cost};
817 next unless defined $cost; # XXX should this be reported?
819 unless (defined $least_cost) {
825 next if $cost > $least_cost;
827 if ($cost == $least_cost) {
838 # XXX return a random @branch with minimum cost instead of the first one;
839 # return $branch[0] if @branch == 1;