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
35 use List::Util qw(shuffle);
36 use List::MoreUtils qw(any);
39 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
48 &UpdateTransportCostMatrix
55 =head2 TransportCostMatrix
57 TransportCostMatrix();
59 Returns Transport Cost Matrix as a hashref <to branch code> => <from branch code> => cost
63 sub TransportCostMatrix {
64 my $dbh = C4::Context->dbh;
65 my $transport_costs = $dbh->selectall_arrayref("SELECT * FROM transport_cost",{ Slice => {} });
67 my $today = dt_from_string();
69 my %transport_cost_matrix;
70 foreach (@$transport_costs) {
71 my $from = $_->{frombranch};
72 my $to = $_->{tobranch};
73 my $cost = $_->{cost};
74 my $disabled = $_->{disable_transfer};
75 $transport_cost_matrix{$to}{$from} = {
77 disable_transfer => $disabled
80 if ( C4::Context->preference("HoldsQueueSkipClosed") ) {
81 $calendars->{$from} ||= Koha::Calendar->new( branchcode => $from );
82 $transport_cost_matrix{$to}{$from}{disable_transfer} ||=
83 $calendars->{$from}->is_holiday( $today );
87 return \%transport_cost_matrix;
90 =head2 UpdateTransportCostMatrix
92 UpdateTransportCostMatrix($records);
94 Updates full Transport Cost Matrix table. $records is an arrayref of records.
95 Records: { frombranch => <code>, tobranch => <code>, cost => <figure>, disable_transfer => <0,1> }
99 sub UpdateTransportCostMatrix {
101 my $dbh = C4::Context->dbh;
103 my $sth = $dbh->prepare("INSERT INTO transport_cost (frombranch, tobranch, cost, disable_transfer) VALUES (?, ?, ?, ?)");
105 $dbh->do("DELETE FROM transport_cost");
106 foreach (@$records) {
107 my $cost = $_->{cost};
108 my $from = $_->{frombranch};
109 my $to = $_->{tobranch};
110 if ($_->{disable_transfer}) {
113 elsif ( !defined ($cost) || ($cost !~ m/(0|[1-9][0-9]*)(\.[0-9]*)?/o) ) {
114 warn "Invalid $from -> $to cost $cost - must be a number >= 0, disabling";
116 $_->{disable_transfer} = 1;
118 $sth->execute( $from, $to, $cost, $_->{disable_transfer} ? 1 : 0 );
122 =head2 GetHoldsQueueItems
124 GetHoldsQueueItems($branch);
126 Returns hold queue for a holding branch. If branch is omitted, then whole queue is returned
130 sub GetHoldsQueueItems {
131 my ($branchlimit) = @_;
132 my $dbh = C4::Context->dbh;
134 my @bind_params = ();
135 my $query = q/SELECT tmp_holdsqueue.*, biblio.author, items.ccode, items.itype, biblioitems.itemtype, items.location,
136 items.enumchron, items.cn_sort, biblioitems.publishercode,
137 biblio.copyrightdate, biblio.subtitle, biblio.part_number,
139 biblioitems.publicationyear, biblioitems.pages, biblioitems.size, biblioitems.publicationyear,
140 biblioitems.isbn, items.copynumber
142 JOIN biblio USING (biblionumber)
143 LEFT JOIN biblioitems USING (biblionumber)
144 LEFT JOIN items USING ( itemnumber)
147 $query .=" WHERE tmp_holdsqueue.holdingbranch = ?";
148 push @bind_params, $branchlimit;
150 $query .= " ORDER BY ccode, location, cn_sort, author, title, pickbranch, reservedate";
151 my $sth = $dbh->prepare($query);
152 $sth->execute(@bind_params);
154 while ( my $row = $sth->fetchrow_hashref ){
155 # return the bib-level or item-level itype per syspref
156 if (!C4::Context->preference('item-level_itypes')) {
157 $row->{itype} = $row->{itemtype};
159 delete $row->{itemtype};
161 my @subtitles = split(/ \| /, $row->{'subtitle'} // '' );
162 $row->{'subtitle'} = \@subtitles;
173 Top level function that turns reserves into tmp_holdsqueue and hold_fill_targets.
178 my $dbh = C4::Context->dbh;
180 $dbh->do("DELETE FROM tmp_holdsqueue"); # clear the old table for new info
181 $dbh->do("DELETE FROM hold_fill_targets");
184 my $total_requests = 0;
185 my $total_available_items = 0;
186 my $num_items_mapped = 0;
189 my $transport_cost_matrix;
190 my $use_transport_cost_matrix = C4::Context->preference("UseTransportCostMatrix");
191 if ($use_transport_cost_matrix) {
192 $transport_cost_matrix = TransportCostMatrix();
193 unless (keys %$transport_cost_matrix) {
194 warn "UseTransportCostMatrix set to yes, but matrix not populated";
195 undef $transport_cost_matrix;
198 unless ($transport_cost_matrix) {
199 $branches_to_use = load_branches_to_pull_from();
202 my $bibs_with_pending_requests = GetBibsWithPendingHoldRequests();
204 foreach my $biblionumber (@$bibs_with_pending_requests) {
206 my $hold_requests = GetPendingHoldRequestsForBib($biblionumber);
207 my $available_items = GetItemsAvailableToFillHoldRequestsForBib($biblionumber, $branches_to_use);
208 $total_requests += scalar(@$hold_requests);
209 $total_available_items += scalar(@$available_items);
211 my $item_map = MapItemsToHoldRequests($hold_requests, $available_items, $branches_to_use, $transport_cost_matrix);
213 my $item_map_size = scalar(keys %$item_map)
216 $num_items_mapped += $item_map_size;
217 CreatePicklistFromItemMap($item_map);
218 AddToHoldTargetMap($item_map);
219 if (($item_map_size < scalar(@$hold_requests )) and
220 ($item_map_size < scalar(@$available_items))) {
221 # DOUBLE CHECK, but this is probably OK - unfilled item-level requests
223 #warn "unfilled requests for $biblionumber";
224 #warn Dumper($hold_requests), Dumper($available_items), Dumper($item_map);
229 =head2 GetBibsWithPendingHoldRequests
231 my $biblionumber_aref = GetBibsWithPendingHoldRequests();
233 Return an arrayref of the biblionumbers of all bibs
234 that have one or more unfilled hold requests.
238 sub GetBibsWithPendingHoldRequests {
239 my $dbh = C4::Context->dbh;
241 my $bib_query = "SELECT DISTINCT biblionumber
245 AND reservedate <= CURRENT_DATE()
248 my $sth = $dbh->prepare($bib_query);
251 my $biblionumbers = $sth->fetchall_arrayref();
253 return [ map { $_->[0] } @$biblionumbers ];
256 =head2 GetPendingHoldRequestsForBib
258 my $requests = GetPendingHoldRequestsForBib($biblionumber);
260 Returns an arrayref of hashrefs to pending, unfilled hold requests
261 on the bib identified by $biblionumber. The following keys
262 are present in each hashref:
273 The arrayref is sorted in order of increasing priority.
277 sub GetPendingHoldRequestsForBib {
278 my $biblionumber = shift;
280 my $dbh = C4::Context->dbh;
282 my $request_query = "SELECT biblionumber, borrowernumber, itemnumber, priority, reserves.branchcode,
283 reservedate, reservenotes, borrowers.branchcode AS borrowerbranch, itemtype
285 JOIN borrowers USING (borrowernumber)
286 WHERE biblionumber = ?
289 AND reservedate <= CURRENT_DATE()
292 my $sth = $dbh->prepare($request_query);
293 $sth->execute($biblionumber);
295 my $requests = $sth->fetchall_arrayref({});
300 =head2 GetItemsAvailableToFillHoldRequestsForBib
302 my $available_items = GetItemsAvailableToFillHoldRequestsForBib($biblionumber, $branches_ar);
304 Returns an arrayref of items available to fill hold requests
305 for the bib identified by C<$biblionumber>. An item is available
306 to fill a hold request if and only if:
309 * it is not withdrawn
310 * it is not marked notforloan
311 * it is not currently in transit
313 * it is not sitting on the hold shelf
314 * it is not damaged (unless AllowHoldsOnDamagedItems is on)
318 sub GetItemsAvailableToFillHoldRequestsForBib {
319 my ($biblionumber, $branches_to_use) = @_;
321 my $dbh = C4::Context->dbh;
322 my $items_query = "SELECT itemnumber, homebranch, holdingbranch, itemtypes.itemtype AS itype
325 if (C4::Context->preference('item-level_itypes')) {
326 $items_query .= "LEFT JOIN itemtypes ON (itemtypes.itemtype = items.itype) ";
328 $items_query .= "JOIN biblioitems USING (biblioitemnumber)
329 LEFT JOIN itemtypes USING (itemtype) ";
331 $items_query .= "WHERE items.notforloan = 0
332 AND holdingbranch IS NOT NULL
335 $items_query .= " AND damaged = 0" unless C4::Context->preference('AllowHoldsOnDamagedItems');
336 $items_query .= " AND items.onloan IS NULL
337 AND (itemtypes.notforloan IS NULL OR itemtypes.notforloan = 0)
338 AND itemnumber NOT IN (
341 WHERE biblionumber = ?
342 AND itemnumber IS NOT NULL
343 AND (found IS NOT NULL OR priority = 0)
345 AND items.biblionumber = ?";
347 my @params = ($biblionumber, $biblionumber);
348 if ($branches_to_use && @$branches_to_use) {
349 $items_query .= " AND holdingbranch IN (" . join (",", map { "?" } @$branches_to_use) . ")";
350 push @params, @$branches_to_use;
352 my $sth = $dbh->prepare($items_query);
353 $sth->execute(@params);
355 my $itm = $sth->fetchall_arrayref({});
356 my @items = grep { ! scalar GetTransfers($_->{itemnumber}) } @$itm;
358 my $rule = GetBranchItemRule($_->{homebranch}, $_->{itype});
359 $_->{holdallowed} = $rule->{holdallowed};
360 $_->{hold_fulfillment_policy} = $rule->{hold_fulfillment_policy};
364 =head2 MapItemsToHoldRequests
366 MapItemsToHoldRequests($hold_requests, $available_items, $branches, $transport_cost_matrix)
370 sub MapItemsToHoldRequests {
371 my ($hold_requests, $available_items, $branches_to_use, $transport_cost_matrix) = @_;
373 # handle trival cases
374 return unless scalar(@$hold_requests) > 0;
375 return unless scalar(@$available_items) > 0;
377 # identify item-level requests
378 my %specific_items_requested = map { $_->{itemnumber} => 1 }
379 grep { defined($_->{itemnumber}) }
382 map { $_->{_object} = Koha::Items->find( $_->{itemnumber} ) } @$available_items;
384 map { $libraries->{$_->id} = $_ } Koha::Libraries->search();
386 # group available items by itemnumber
387 my %items_by_itemnumber = map { $_->{itemnumber} => $_ } @$available_items;
389 # items already allocated
390 my %allocated_items = ();
392 # map of items to hold requests
395 # figure out which item-level requests can be filled
396 my $num_items_remaining = scalar(@$available_items);
398 # Look for Local Holds Priority matches first
399 if ( C4::Context->preference('LocalHoldsPriority') ) {
400 my $LocalHoldsPriorityPatronControl =
401 C4::Context->preference('LocalHoldsPriorityPatronControl');
402 my $LocalHoldsPriorityItemControl =
403 C4::Context->preference('LocalHoldsPriorityItemControl');
405 foreach my $request (@$hold_requests) {
406 next if (defined($request->{itemnumber})); #skip item level holds in local priority checking
407 last if $num_items_remaining == 0;
409 my $local_hold_match;
410 foreach my $item (@$available_items) {
412 if ( !$item->{holdallowed} )
413 || ( $item->{holdallowed} == 1
414 && $item->{homebranch} ne $request->{borrowerbranch} );
416 next unless $item->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } );
418 my $local_holds_priority_item_branchcode =
419 $item->{$LocalHoldsPriorityItemControl};
421 my $local_holds_priority_patron_branchcode =
422 ( $LocalHoldsPriorityPatronControl eq 'PickupLibrary' )
423 ? $request->{branchcode}
424 : ( $LocalHoldsPriorityPatronControl eq 'HomeLibrary' )
425 ? $request->{borrowerbranch}
429 $local_holds_priority_item_branchcode eq
430 $local_holds_priority_patron_branchcode;
432 if ($local_hold_match) {
433 if ( exists $items_by_itemnumber{ $item->{itemnumber} }
434 and not exists $allocated_items{ $item->{itemnumber} }
435 and not $request->{allocated})
437 $item_map{ $item->{itemnumber} } = {
438 borrowernumber => $request->{borrowernumber},
439 biblionumber => $request->{biblionumber},
440 holdingbranch => $item->{holdingbranch},
441 pickup_branch => $request->{branchcode}
442 || $request->{borrowerbranch},
444 reservedate => $request->{reservedate},
445 reservenotes => $request->{reservenotes},
447 $allocated_items{ $item->{itemnumber} }++;
448 $request->{allocated} = 1;
449 $num_items_remaining--;
456 foreach my $request (@$hold_requests) {
457 last if $num_items_remaining == 0;
458 next if $request->{allocated};
460 # is this an item-level request?
461 if (defined($request->{itemnumber})) {
462 # fill it if possible; if not skip it
464 exists $items_by_itemnumber{ $request->{itemnumber} }
465 and not exists $allocated_items{ $request->{itemnumber} }
466 and ( # Don't fill item level holds that contravene the hold pickup policy at this time
467 ( $items_by_itemnumber{ $request->{itemnumber} }->{hold_fulfillment_policy} eq 'any' )
468 || ( $request->{branchcode} eq $items_by_itemnumber{ $request->{itemnumber} }->{ $items_by_itemnumber{ $request->{itemnumber} }->{hold_fulfillment_policy} } )
469 and ( !$request->{itemtype} # If hold itemtype is set, item's itemtype must match
470 || $items_by_itemnumber{ $request->{itemnumber} }->{itype} eq $request->{itemtype} )
472 and $items_by_itemnumber{ $request->{itemnumber} }->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } )
477 $item_map{ $request->{itemnumber} } = {
478 borrowernumber => $request->{borrowernumber},
479 biblionumber => $request->{biblionumber},
480 holdingbranch => $items_by_itemnumber{ $request->{itemnumber} }->{holdingbranch},
481 pickup_branch => $request->{branchcode} || $request->{borrowerbranch},
483 reservedate => $request->{reservedate},
484 reservenotes => $request->{reservenotes},
486 $allocated_items{ $request->{itemnumber} }++;
487 $num_items_remaining--;
490 # it's title-level request that will take up one item
491 $num_items_remaining--;
495 # group available items by branch
496 my %items_by_branch = ();
497 foreach my $item (@$available_items) {
498 next unless $item->{holdallowed};
500 push @{ $items_by_branch{ $item->{holdingbranch} } }, $item
501 unless exists $allocated_items{ $item->{itemnumber} };
503 return \%item_map unless keys %items_by_branch;
505 # now handle the title-level requests
506 $num_items_remaining = scalar(@$available_items) - scalar(keys %allocated_items);
508 foreach my $request (@$hold_requests) {
509 last if $num_items_remaining == 0;
510 next if $request->{allocated};
511 next if defined($request->{itemnumber}); # already handled these
513 # look for local match first
514 my $pickup_branch = $request->{branchcode} || $request->{borrowerbranch};
515 my ($itemnumber, $holdingbranch);
517 my $holding_branch_items = $items_by_branch{$pickup_branch};
518 if ( $holding_branch_items ) {
519 foreach my $item (@$holding_branch_items) {
520 next unless $items_by_itemnumber{ $item->{itemnumber} }->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } );
523 $request->{borrowerbranch} eq $item->{homebranch}
524 && ( ( $item->{hold_fulfillment_policy} eq 'any' ) # Don't fill item level holds that contravene the hold pickup policy at this time
525 || $request->{branchcode} eq $item->{ $item->{hold_fulfillment_policy} } )
526 && ( !$request->{itemtype} # If hold itemtype is set, item's itemtype must match
527 || $items_by_itemnumber{ $request->{itemnumber} }->{itype} eq $request->{itemtype} )
530 $itemnumber = $item->{itemnumber};
534 $holdingbranch = $pickup_branch;
536 elsif ($transport_cost_matrix) {
537 $pull_branches = [keys %items_by_branch];
538 $holdingbranch = least_cost_branch( $pickup_branch, $pull_branches, $transport_cost_matrix );
539 if ( $holdingbranch ) {
541 my $holding_branch_items = $items_by_branch{$holdingbranch};
542 foreach my $item (@$holding_branch_items) {
543 next if $request->{borrowerbranch} ne $item->{homebranch};
544 next unless $items_by_itemnumber{ $item->{itemnumber} }->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } );
546 # Don't fill item level holds that contravene the hold pickup policy at this time
547 next unless $item->{hold_fulfillment_policy} eq 'any'
548 || $request->{branchcode} eq $item->{ $item->{hold_fulfillment_policy} };
550 # If hold itemtype is set, item's itemtype must match
551 next unless ( !$request->{itemtype}
552 || $item->{itype} eq $request->{itemtype} );
554 $itemnumber = $item->{itemnumber};
563 unless ($itemnumber) {
564 # not found yet, fall back to basics
565 if ($branches_to_use) {
566 $pull_branches = $branches_to_use;
568 $pull_branches = [keys %items_by_branch];
571 # Try picking items where the home and pickup branch match first
573 foreach my $branch (@$pull_branches) {
574 my $holding_branch_items = $items_by_branch{$branch}
577 $holdingbranch ||= $branch;
578 foreach my $item (@$holding_branch_items) {
579 next if $pickup_branch ne $item->{homebranch};
580 next if ( $item->{holdallowed} == 1 && $item->{homebranch} ne $request->{borrowerbranch} );
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 $item->{hold_fulfillment_policy} eq 'any'
585 || $request->{branchcode} eq $item->{ $item->{hold_fulfillment_policy} };
587 # If hold itemtype is set, item's itemtype must match
588 next unless ( !$request->{itemtype}
589 || $item->{itype} eq $request->{itemtype} );
591 $itemnumber = $item->{itemnumber};
592 $holdingbranch = $branch;
597 # Now try items from the least cost branch based on the transport cost matrix or StaticHoldsQueueWeight
598 unless ( $itemnumber ) {
599 foreach my $current_item ( @{ $items_by_branch{$holdingbranch} } ) {
600 if ( $holdingbranch && ( $current_item->{holdallowed} == 2 || $request->{borrowerbranch} eq $current_item->{homebranch} ) ) {
602 # Don't fill item level holds that contravene the hold pickup policy at this time
603 next unless $current_item->{hold_fulfillment_policy} eq 'any'
604 || $request->{branchcode} eq $current_item->{ $current_item->{hold_fulfillment_policy} };
606 # If hold itemtype is set, item's itemtype must match
607 next unless ( !$request->{itemtype}
608 || $current_item->{itype} eq $request->{itemtype} );
610 next unless $items_by_itemnumber{ $current_item->{itemnumber} }->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } );
612 $itemnumber = $current_item->{itemnumber};
613 last; # quit this loop as soon as we have a suitable item
618 # Now try for items for any item that can fill this hold
619 unless ( $itemnumber ) {
621 foreach my $branch (@$pull_branches) {
622 my $holding_branch_items = $items_by_branch{$branch}
625 foreach my $item (@$holding_branch_items) {
626 next if ( $item->{holdallowed} == 1 && $item->{homebranch} ne $request->{borrowerbranch} );
628 # Don't fill item level holds that contravene the hold pickup policy at this time
629 next unless $item->{hold_fulfillment_policy} eq 'any'
630 || $request->{branchcode} eq $item->{ $item->{hold_fulfillment_policy} };
632 # If hold itemtype is set, item's itemtype must match
633 next unless ( !$request->{itemtype}
634 || $item->{itype} eq $request->{itemtype} );
636 next unless $items_by_itemnumber{ $item->{itemnumber} }->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } );
638 $itemnumber = $item->{itemnumber};
639 $holdingbranch = $branch;
647 my $holding_branch_items = $items_by_branch{$holdingbranch}
648 or die "Have $itemnumber, $holdingbranch, but no items!";
649 @$holding_branch_items = grep { $_->{itemnumber} != $itemnumber } @$holding_branch_items;
650 delete $items_by_branch{$holdingbranch} unless @$holding_branch_items;
652 $item_map{$itemnumber} = {
653 borrowernumber => $request->{borrowernumber},
654 biblionumber => $request->{biblionumber},
655 holdingbranch => $holdingbranch,
656 pickup_branch => $pickup_branch,
658 reservedate => $request->{reservedate},
659 reservenotes => $request->{reservenotes},
661 $num_items_remaining--;
667 =head2 CreatePickListFromItemMap
671 sub CreatePicklistFromItemMap {
672 my $item_map = shift;
674 my $dbh = C4::Context->dbh;
676 my $sth_load=$dbh->prepare("
677 INSERT INTO tmp_holdsqueue (biblionumber,itemnumber,barcode,surname,firstname,phone,borrowernumber,
678 cardnumber,reservedate,title, itemcallnumber,
679 holdingbranch,pickbranch,notes, item_level_request)
680 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
683 foreach my $itemnumber (sort keys %$item_map) {
684 my $mapped_item = $item_map->{$itemnumber};
685 my $biblionumber = $mapped_item->{biblionumber};
686 my $borrowernumber = $mapped_item->{borrowernumber};
687 my $pickbranch = $mapped_item->{pickup_branch};
688 my $holdingbranch = $mapped_item->{holdingbranch};
689 my $reservedate = $mapped_item->{reservedate};
690 my $reservenotes = $mapped_item->{reservenotes};
691 my $item_level = $mapped_item->{item_level};
693 my $item = Koha::Items->find($itemnumber);
694 my $barcode = $item->barcode;
695 my $itemcallnumber = $item->itemcallnumber;
697 my $patron = Koha::Patrons->find( $borrowernumber );
698 my $cardnumber = $patron->cardnumber;
699 my $surname = $patron->surname;
700 my $firstname = $patron->firstname;
701 my $phone = $patron->phone;
703 my $biblio = Koha::Biblios->find( $biblionumber );
704 my $title = $biblio->title;
706 $sth_load->execute($biblionumber, $itemnumber, $barcode, $surname, $firstname, $phone, $borrowernumber,
707 $cardnumber, $reservedate, $title, $itemcallnumber,
708 $holdingbranch, $pickbranch, $reservenotes, $item_level);
712 =head2 AddToHoldTargetMap
716 sub AddToHoldTargetMap {
717 my $item_map = shift;
719 my $dbh = C4::Context->dbh;
722 INSERT INTO hold_fill_targets (borrowernumber, biblionumber, itemnumber, source_branchcode, item_level_request)
723 VALUES (?, ?, ?, ?, ?)
725 my $sth_insert = $dbh->prepare($insert_sql);
727 foreach my $itemnumber (keys %$item_map) {
728 my $mapped_item = $item_map->{$itemnumber};
729 $sth_insert->execute($mapped_item->{borrowernumber}, $mapped_item->{biblionumber}, $itemnumber,
730 $mapped_item->{holdingbranch}, $mapped_item->{item_level});
734 # Helper functions, not part of any interface
737 return $_[0] unless $_[0];
743 sub load_branches_to_pull_from {
746 my $static_branch_list = C4::Context->preference("StaticHoldsQueueWeight");
747 @branches_to_use = map { _trim($_) } split( /,/, $static_branch_list )
748 if $static_branch_list;
751 Koha::Database->new()->schema()->resultset('Branch')
752 ->get_column('branchcode')->all()
753 unless (@branches_to_use);
755 @branches_to_use = shuffle(@branches_to_use)
756 if C4::Context->preference("RandomizeHoldsQueueWeight");
758 my $today = dt_from_string();
759 if ( C4::Context->preference('HoldsQueueSkipClosed') ) {
760 @branches_to_use = grep {
761 !Koha::Calendar->new( branchcode => $_ )
762 ->is_holiday( $today )
766 return \@branches_to_use;
769 sub least_cost_branch {
772 my ($to, $from, $transport_cost_matrix) = @_;
774 # Nothing really spectacular: supply to branch, a list of potential from branches
775 # and find the minimum from - to value from the transport_cost_matrix
776 return $from->[0] if ( @$from == 1 && $transport_cost_matrix->{$to}{$from->[0]}->{disable_transfer} != 1 );
778 # If the pickup library is in the list of libraries to pull from,
779 # return that library right away, it is obviously the least costly
780 return ($to) if any { $_ eq $to } @$from;
782 my ($least_cost, @branch);
784 my $cell = $transport_cost_matrix->{$to}{$_};
785 next if $cell->{disable_transfer};
787 my $cost = $cell->{cost};
788 next unless defined $cost; # XXX should this be reported?
790 unless (defined $least_cost) {
796 next if $cost > $least_cost;
798 if ($cost == $least_cost) {
809 # XXX return a random @branch with minimum cost instead of the first one;
810 # return $branch[0] if @branch == 1;