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
27 use C4::Circulation qw( GetTransfers GetBranchItemRule );
28 use Koha::DateUtils qw( dt_from_string );
33 use List::Util qw( shuffle );
34 use List::MoreUtils qw( any );
36 our (@ISA, @EXPORT_OK);
45 UpdateTransportCostMatrix
46 GetPendingHoldRequestsForBib
53 =head2 TransportCostMatrix
55 TransportCostMatrix();
57 Returns Transport Cost Matrix as a hashref <to branch code> => <from branch code> => cost
61 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();
68 my %transport_cost_matrix;
69 foreach (@$transport_costs) {
70 my $from = $_->{frombranch};
71 my $to = $_->{tobranch};
72 my $cost = $_->{cost};
73 my $disabled = $_->{disable_transfer};
74 $transport_cost_matrix{$to}{$from} = {
76 disable_transfer => $disabled
80 return \%transport_cost_matrix;
83 =head2 UpdateTransportCostMatrix
85 UpdateTransportCostMatrix($records);
87 Updates full Transport Cost Matrix table. $records is an arrayref of records.
88 Records: { frombranch => <code>, tobranch => <code>, cost => <figure>, disable_transfer => <0,1> }
92 sub UpdateTransportCostMatrix {
94 my $dbh = C4::Context->dbh;
96 my $sth = $dbh->prepare("INSERT INTO transport_cost (frombranch, tobranch, cost, disable_transfer) VALUES (?, ?, ?, ?)");
98 $dbh->do("DELETE FROM transport_cost");
100 my $cost = $_->{cost};
101 my $from = $_->{frombranch};
102 my $to = $_->{tobranch};
103 if ($_->{disable_transfer}) {
106 elsif ( !defined ($cost) || ($cost !~ m/(0|[1-9][0-9]*)(\.[0-9]*)?/o) ) {
107 warn "Invalid $from -> $to cost $cost - must be a number >= 0, disabling";
109 $_->{disable_transfer} = 1;
111 $sth->execute( $from, $to, $cost, $_->{disable_transfer} ? 1 : 0 );
115 =head2 GetHoldsQueueItems
117 GetHoldsQueueItems($branch);
119 Returns hold queue for a holding branch. If branch is omitted, then whole queue is returned
123 sub GetHoldsQueueItems {
124 my ($branchlimit) = @_;
125 my $dbh = C4::Context->dbh;
127 my @bind_params = ();
128 my $query = q/SELECT tmp_holdsqueue.*, biblio.author, items.ccode, items.itype, biblioitems.itemtype, items.location,
129 items.enumchron, items.cn_sort, biblioitems.publishercode,
130 biblio.copyrightdate, biblio.subtitle, biblio.medium,
131 biblio.part_number, biblio.part_name,
132 biblioitems.publicationyear, biblioitems.pages, biblioitems.size,
133 biblioitems.isbn, biblioitems.editionstatement, items.copynumber
135 JOIN biblio USING (biblionumber)
136 LEFT JOIN biblioitems USING (biblionumber)
137 LEFT JOIN items USING ( itemnumber)
140 $query .=" WHERE tmp_holdsqueue.holdingbranch = ?";
141 push @bind_params, $branchlimit;
143 $query .= " ORDER BY ccode, location, cn_sort, author, title, pickbranch, reservedate";
144 my $sth = $dbh->prepare($query);
145 $sth->execute(@bind_params);
147 while ( my $row = $sth->fetchrow_hashref ){
148 # return the bib-level or item-level itype per syspref
149 if (!C4::Context->preference('item-level_itypes')) {
150 $row->{itype} = $row->{itemtype};
152 delete $row->{itemtype};
163 Top level function that turns reserves into tmp_holdsqueue and hold_fill_targets.
168 my $dbh = C4::Context->dbh;
170 $dbh->do("DELETE FROM tmp_holdsqueue"); # clear the old table for new info
171 $dbh->do("DELETE FROM hold_fill_targets");
174 my $total_requests = 0;
175 my $total_available_items = 0;
176 my $num_items_mapped = 0;
179 my $transport_cost_matrix;
180 my $use_transport_cost_matrix = C4::Context->preference("UseTransportCostMatrix");
181 if ($use_transport_cost_matrix) {
182 $transport_cost_matrix = TransportCostMatrix();
183 unless (keys %$transport_cost_matrix) {
184 warn "UseTransportCostMatrix set to yes, but matrix not populated";
185 undef $transport_cost_matrix;
189 $branches_to_use = load_branches_to_pull_from($use_transport_cost_matrix);
191 my $bibs_with_pending_requests = GetBibsWithPendingHoldRequests();
193 foreach my $biblionumber (@$bibs_with_pending_requests) {
195 my $hold_requests = GetPendingHoldRequestsForBib($biblionumber);
196 my $available_items = GetItemsAvailableToFillHoldRequestsForBib($biblionumber, $branches_to_use);
197 $total_requests += scalar(@$hold_requests);
198 $total_available_items += scalar(@$available_items);
200 my $item_map = MapItemsToHoldRequests($hold_requests, $available_items, $branches_to_use, $transport_cost_matrix);
202 my $item_map_size = scalar(keys %$item_map)
205 $num_items_mapped += $item_map_size;
206 CreatePicklistFromItemMap($item_map);
207 AddToHoldTargetMap($item_map);
208 if (($item_map_size < scalar(@$hold_requests )) and
209 ($item_map_size < scalar(@$available_items))) {
210 # DOUBLE CHECK, but this is probably OK - unfilled item-level requests
212 #warn "unfilled requests for $biblionumber";
213 #warn Dumper($hold_requests), Dumper($available_items), Dumper($item_map);
218 =head2 GetBibsWithPendingHoldRequests
220 my $biblionumber_aref = GetBibsWithPendingHoldRequests();
222 Return an arrayref of the biblionumbers of all bibs
223 that have one or more unfilled hold requests.
227 sub GetBibsWithPendingHoldRequests {
228 my $dbh = C4::Context->dbh;
230 my $bib_query = "SELECT DISTINCT biblionumber
234 AND reservedate <= CURRENT_DATE()
237 my $sth = $dbh->prepare($bib_query);
240 my $biblionumbers = $sth->fetchall_arrayref();
242 return [ map { $_->[0] } @$biblionumbers ];
245 =head2 GetPendingHoldRequestsForBib
247 my $requests = GetPendingHoldRequestsForBib($biblionumber);
249 Returns an arrayref of hashrefs to pending, unfilled hold requests
250 on the bib identified by $biblionumber. The following keys
251 are present in each hashref:
262 The arrayref is sorted in order of increasing priority.
266 sub GetPendingHoldRequestsForBib {
267 my $biblionumber = shift;
269 my $dbh = C4::Context->dbh;
271 my $request_query = "SELECT biblionumber, borrowernumber, itemnumber, priority, reserve_id, reserves.branchcode,
272 reservedate, reservenotes, borrowers.branchcode AS borrowerbranch, itemtype, item_level_hold
274 JOIN borrowers USING (borrowernumber)
275 WHERE biblionumber = ?
278 AND reservedate <= CURRENT_DATE()
281 my $sth = $dbh->prepare($request_query);
282 $sth->execute($biblionumber);
284 my $requests = $sth->fetchall_arrayref({});
289 =head2 GetItemsAvailableToFillHoldRequestsForBib
291 my $available_items = GetItemsAvailableToFillHoldRequestsForBib($biblionumber, $branches_ar);
293 Returns an arrayref of items available to fill hold requests
294 for the bib identified by C<$biblionumber>. An item is available
295 to fill a hold request if and only if:
298 * it is not withdrawn
299 * it is not marked notforloan
300 * it is not currently in transit
302 * it is not sitting on the hold shelf
303 * it is not damaged (unless AllowHoldsOnDamagedItems is on)
307 sub GetItemsAvailableToFillHoldRequestsForBib {
308 my ($biblionumber, $branches_to_use) = @_;
310 my $dbh = C4::Context->dbh;
311 my $items_query = "SELECT itemnumber, homebranch, holdingbranch, itemtypes.itemtype AS itype
314 if (C4::Context->preference('item-level_itypes')) {
315 $items_query .= "LEFT JOIN itemtypes ON (itemtypes.itemtype = items.itype) ";
317 $items_query .= "JOIN biblioitems USING (biblioitemnumber)
318 LEFT JOIN itemtypes USING (itemtype) ";
320 $items_query .= "WHERE items.notforloan = 0
321 AND holdingbranch IS NOT NULL
324 $items_query .= " AND damaged = 0" unless C4::Context->preference('AllowHoldsOnDamagedItems');
325 $items_query .= " AND items.onloan IS NULL
326 AND (itemtypes.notforloan IS NULL OR itemtypes.notforloan = 0)
327 AND itemnumber NOT IN (
330 WHERE biblionumber = ?
331 AND itemnumber IS NOT NULL
332 AND (found IS NOT NULL OR priority = 0)
334 AND items.biblionumber = ?";
336 my @params = ($biblionumber, $biblionumber);
337 if ($branches_to_use && @$branches_to_use) {
338 $items_query .= " AND holdingbranch IN (" . join (",", map { "?" } @$branches_to_use) . ")";
339 push @params, @$branches_to_use;
341 my $sth = $dbh->prepare($items_query);
342 $sth->execute(@params);
344 my $itm = $sth->fetchall_arrayref({});
345 my @items = grep { ! scalar GetTransfers($_->{itemnumber}) } @$itm;
347 my $rule = GetBranchItemRule($_->{homebranch}, $_->{itype});
348 $_->{holdallowed} = $rule->{holdallowed};
349 $_->{hold_fulfillment_policy} = $rule->{hold_fulfillment_policy};
353 =head2 _checkHoldPolicy
355 _checkHoldPolicy($item, $request)
357 check if item agrees with hold policies
361 sub _checkHoldPolicy {
362 my ( $item, $request ) = @_;
364 return 0 unless $item->{holdallowed} ne 'not_allowed';
367 if $item->{holdallowed} eq 'from_home_library'
368 && $item->{homebranch} ne $request->{borrowerbranch};
371 if $item->{'holdallowed'} eq 'from_local_hold_group'
372 && !Koha::Libraries->find( $item->{homebranch} )
373 ->validate_hold_sibling( { branchcode => $request->{borrowerbranch} } );
375 my $hold_fulfillment_policy = $item->{hold_fulfillment_policy};
378 if $hold_fulfillment_policy eq 'holdgroup'
379 && !Koha::Libraries->find( $item->{homebranch} )
380 ->validate_hold_sibling( { branchcode => $request->{branchcode} } );
383 if $hold_fulfillment_policy eq 'homebranch'
384 && $request->{branchcode} ne $item->{$hold_fulfillment_policy};
387 if $hold_fulfillment_policy eq 'holdingbranch'
388 && $request->{branchcode} ne $item->{$hold_fulfillment_policy};
391 if $hold_fulfillment_policy eq 'patrongroup'
392 && !Koha::Libraries->find( $request->{borrowerbranch} )
393 ->validate_hold_sibling( { branchcode => $request->{branchcode} } );
399 =head2 MapItemsToHoldRequests
401 MapItemsToHoldRequests($hold_requests, $available_items, $branches, $transport_cost_matrix)
405 sub MapItemsToHoldRequests {
406 my ($hold_requests, $available_items, $branches_to_use, $transport_cost_matrix) = @_;
408 # handle trival cases
409 return unless scalar(@$hold_requests) > 0;
410 return unless scalar(@$available_items) > 0;
412 # identify item-level requests
413 my %specific_items_requested = map { $_->{itemnumber} => 1 }
414 grep { defined($_->{itemnumber}) }
417 map { $_->{_object} = Koha::Items->find( $_->{itemnumber} ) } @$available_items;
419 map { $libraries->{$_->id} = $_ } Koha::Libraries->search();
421 # group available items by itemnumber
422 my %items_by_itemnumber = map { $_->{itemnumber} => $_ } @$available_items;
424 # items already allocated
425 my %allocated_items = ();
427 # map of items to hold requests
430 # figure out which item-level requests can be filled
431 my $num_items_remaining = scalar(@$available_items);
433 # Look for Local Holds Priority matches first
434 if ( C4::Context->preference('LocalHoldsPriority') ) {
435 my $LocalHoldsPriorityPatronControl =
436 C4::Context->preference('LocalHoldsPriorityPatronControl');
437 my $LocalHoldsPriorityItemControl =
438 C4::Context->preference('LocalHoldsPriorityItemControl');
440 foreach my $request (@$hold_requests) {
441 last if $num_items_remaining == 0;
442 my $patron = Koha::Patrons->find($request->{borrowernumber});
443 next if $patron->category->exclude_from_local_holds_priority;
445 my $local_hold_match;
446 foreach my $item (@$available_items) {
447 next if $item->{_object}->exclude_from_local_holds_priority;
449 next unless _checkHoldPolicy($item, $request);
451 next if $request->{itemnumber} && $request->{itemnumber} != $item->{itemnumber};
453 next unless $item->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } );
455 my $local_holds_priority_item_branchcode =
456 $item->{$LocalHoldsPriorityItemControl};
458 my $local_holds_priority_patron_branchcode =
459 ( $LocalHoldsPriorityPatronControl eq 'PickupLibrary' )
460 ? $request->{branchcode}
461 : ( $LocalHoldsPriorityPatronControl eq 'HomeLibrary' )
462 ? $request->{borrowerbranch}
466 $local_holds_priority_item_branchcode eq
467 $local_holds_priority_patron_branchcode;
469 if ($local_hold_match) {
470 if ( exists $items_by_itemnumber{ $item->{itemnumber} }
471 and not exists $allocated_items{ $item->{itemnumber} }
472 and not $request->{allocated})
474 $item_map{ $item->{itemnumber} } = {
475 borrowernumber => $request->{borrowernumber},
476 biblionumber => $request->{biblionumber},
477 holdingbranch => $item->{holdingbranch},
478 pickup_branch => $request->{branchcode}
479 || $request->{borrowerbranch},
480 reserve_id => $request->{reserve_id},
481 item_level => $request->{item_level_hold},
482 reservedate => $request->{reservedate},
483 reservenotes => $request->{reservenotes},
485 $allocated_items{ $item->{itemnumber} }++;
486 $request->{allocated} = 1;
487 $num_items_remaining--;
494 foreach my $request (@$hold_requests) {
495 last if $num_items_remaining == 0;
496 next if $request->{allocated};
498 # is this an item-level request?
499 if (defined($request->{itemnumber})) {
500 # fill it if possible; if not skip it
502 exists $items_by_itemnumber{ $request->{itemnumber} }
503 and not exists $allocated_items{ $request->{itemnumber} }
504 and _checkHoldPolicy($items_by_itemnumber{ $request->{itemnumber} }, $request) # Don't fill item level holds that contravene the hold pickup policy at this time
505 and ( !$request->{itemtype} # If hold itemtype is set, item's itemtype must match
506 || $items_by_itemnumber{ $request->{itemnumber} }->{itype} eq $request->{itemtype} )
508 and $items_by_itemnumber{ $request->{itemnumber} }->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } )
513 $item_map{ $request->{itemnumber} } = {
514 borrowernumber => $request->{borrowernumber},
515 biblionumber => $request->{biblionumber},
516 holdingbranch => $items_by_itemnumber{ $request->{itemnumber} }->{holdingbranch},
517 pickup_branch => $request->{branchcode} || $request->{borrowerbranch},
518 reserve_id => $request->{reserve_id},
519 item_level => $request->{item_level_hold},
520 reservedate => $request->{reservedate},
521 reservenotes => $request->{reservenotes},
523 $allocated_items{ $request->{itemnumber} }++;
524 $num_items_remaining--;
527 # it's title-level request that will take up one item
528 $num_items_remaining--;
532 # group available items by branch
533 my %items_by_branch = ();
534 foreach my $item (@$available_items) {
535 next unless $item->{holdallowed} ne 'not_allowed';
537 push @{ $items_by_branch{ $item->{holdingbranch} } }, $item
538 unless exists $allocated_items{ $item->{itemnumber} };
540 return \%item_map unless keys %items_by_branch;
542 # now handle the title-level requests
543 $num_items_remaining = scalar(@$available_items) - scalar(keys %allocated_items);
545 foreach my $request (@$hold_requests) {
546 last if $num_items_remaining == 0;
547 next if $request->{allocated};
548 next if defined($request->{itemnumber}); # already handled these
550 # look for local match first
551 my $pickup_branch = $request->{branchcode} || $request->{borrowerbranch};
552 my ($itemnumber, $holdingbranch);
554 my $holding_branch_items = $items_by_branch{$pickup_branch};
555 if ( $holding_branch_items ) {
556 foreach my $item (@$holding_branch_items) {
557 next unless $items_by_itemnumber{ $item->{itemnumber} }->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } );
560 $request->{borrowerbranch} eq $item->{homebranch}
561 && _checkHoldPolicy($item, $request) # Don't fill item level holds that contravene the hold pickup policy at this time
562 && ( !$request->{itemtype} # If hold itemtype is set, item's itemtype must match
563 || ( $request->{itemnumber} && ( $items_by_itemnumber{ $request->{itemnumber} }->{itype} eq $request->{itemtype} ) ) )
566 $itemnumber = $item->{itemnumber};
570 $holdingbranch = $pickup_branch;
572 elsif ($transport_cost_matrix) {
573 $pull_branches = [keys %items_by_branch];
574 $holdingbranch = least_cost_branch( $pickup_branch, $pull_branches, $transport_cost_matrix );
575 if ( $holdingbranch ) {
577 my $holding_branch_items = $items_by_branch{$holdingbranch};
578 foreach my $item (@$holding_branch_items) {
579 next if $request->{borrowerbranch} ne $item->{homebranch};
580 next unless $items_by_itemnumber{ $item->{itemnumber} }->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } );
582 # Don't fill item level holds that contravene the hold pickup policy at this time
583 next unless _checkHoldPolicy($item, $request);
585 # If hold itemtype is set, item's itemtype must match
586 next unless ( !$request->{itemtype}
587 || $item->{itype} eq $request->{itemtype} );
589 $itemnumber = $item->{itemnumber};
598 unless ($itemnumber) {
599 $pull_branches = [keys %items_by_branch];
601 # Try picking items where the home and pickup branch match first
603 foreach my $branch (@$pull_branches) {
604 my $holding_branch_items = $items_by_branch{$branch}
607 $holdingbranch ||= $branch;
608 foreach my $item (@$holding_branch_items) {
609 next if $pickup_branch ne $item->{homebranch};
610 next unless _checkHoldPolicy($item, $request);
611 next unless $items_by_itemnumber{ $item->{itemnumber} }->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } );
613 # Don't fill item level holds that contravene the hold pickup policy at this time
614 next unless $item->{hold_fulfillment_policy} eq 'any'
615 || $request->{branchcode} eq $item->{ $item->{hold_fulfillment_policy} };
617 # If hold itemtype is set, item's itemtype must match
618 next unless ( !$request->{itemtype}
619 || $item->{itype} eq $request->{itemtype} );
621 $itemnumber = $item->{itemnumber};
622 $holdingbranch = $branch;
627 # Now try items from the least cost branch based on the transport cost matrix or StaticHoldsQueueWeight
628 unless ( $itemnumber || !$holdingbranch) {
629 foreach my $current_item ( @{ $items_by_branch{$holdingbranch} } ) {
630 next unless _checkHoldPolicy($current_item, $request); # Don't fill item level holds that contravene the hold pickup policy at this time
632 # If hold itemtype is set, item's itemtype must match
633 next unless ( !$request->{itemtype}
634 || $current_item->{itype} eq $request->{itemtype} );
636 next unless $items_by_itemnumber{ $current_item->{itemnumber} }->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } );
638 $itemnumber = $current_item->{itemnumber};
639 last; # quit this loop as soon as we have a suitable item
643 # Now try for items for any item that can fill this hold
644 unless ( $itemnumber ) {
646 foreach my $branch (@$pull_branches) {
647 my $holding_branch_items = $items_by_branch{$branch}
650 foreach my $item (@$holding_branch_items) {
651 # Don't fill item level holds that contravene the hold pickup policy at this time
652 next unless _checkHoldPolicy($item, $request);
654 # If hold itemtype is set, item's itemtype must match
655 next unless ( !$request->{itemtype}
656 || $item->{itype} eq $request->{itemtype} );
658 next unless $items_by_itemnumber{ $item->{itemnumber} }->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } );
660 $itemnumber = $item->{itemnumber};
661 $holdingbranch = $branch;
669 my $holding_branch_items = $items_by_branch{$holdingbranch}
670 or die "Have $itemnumber, $holdingbranch, but no items!";
671 @$holding_branch_items = grep { $_->{itemnumber} != $itemnumber } @$holding_branch_items;
672 delete $items_by_branch{$holdingbranch} unless @$holding_branch_items;
674 $item_map{$itemnumber} = {
675 borrowernumber => $request->{borrowernumber},
676 biblionumber => $request->{biblionumber},
677 holdingbranch => $holdingbranch,
678 pickup_branch => $pickup_branch,
679 reserve_id => $request->{reserve_id},
680 item_level => $request->{item_level_hold},
681 reservedate => $request->{reservedate},
682 reservenotes => $request->{reservenotes},
684 $num_items_remaining--;
690 =head2 CreatePickListFromItemMap
694 sub CreatePicklistFromItemMap {
695 my $item_map = shift;
697 my $dbh = C4::Context->dbh;
699 my $sth_load=$dbh->prepare("
700 INSERT INTO tmp_holdsqueue (biblionumber,itemnumber,barcode,surname,firstname,phone,borrowernumber,
701 cardnumber,reservedate,title, itemcallnumber,
702 holdingbranch,pickbranch,notes, item_level_request)
703 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
706 foreach my $itemnumber (sort keys %$item_map) {
707 my $mapped_item = $item_map->{$itemnumber};
708 my $biblionumber = $mapped_item->{biblionumber};
709 my $borrowernumber = $mapped_item->{borrowernumber};
710 my $pickbranch = $mapped_item->{pickup_branch};
711 my $holdingbranch = $mapped_item->{holdingbranch};
712 my $reservedate = $mapped_item->{reservedate};
713 my $reservenotes = $mapped_item->{reservenotes};
714 my $item_level = $mapped_item->{item_level};
716 my $item = Koha::Items->find($itemnumber);
717 my $barcode = $item->barcode;
718 my $itemcallnumber = $item->itemcallnumber;
720 my $patron = Koha::Patrons->find( $borrowernumber );
721 my $cardnumber = $patron->cardnumber;
722 my $surname = $patron->surname;
723 my $firstname = $patron->firstname;
724 my $phone = $patron->phone;
726 my $biblio = Koha::Biblios->find( $biblionumber );
727 my $title = $biblio->title;
729 $sth_load->execute($biblionumber, $itemnumber, $barcode, $surname, $firstname, $phone, $borrowernumber,
730 $cardnumber, $reservedate, $title, $itemcallnumber,
731 $holdingbranch, $pickbranch, $reservenotes, $item_level);
735 =head2 AddToHoldTargetMap
739 sub AddToHoldTargetMap {
740 my $item_map = shift;
742 my $dbh = C4::Context->dbh;
745 INSERT INTO hold_fill_targets (borrowernumber, biblionumber, itemnumber, source_branchcode, item_level_request, reserve_id)
746 VALUES (?, ?, ?, ?, ?, ?)
748 my $sth_insert = $dbh->prepare($insert_sql);
750 foreach my $itemnumber (keys %$item_map) {
751 my $mapped_item = $item_map->{$itemnumber};
752 $sth_insert->execute($mapped_item->{borrowernumber}, $mapped_item->{biblionumber}, $itemnumber,
753 $mapped_item->{holdingbranch}, $mapped_item->{item_level}, $mapped_item->{reserve_id});
757 # Helper functions, not part of any interface
760 return $_[0] unless $_[0];
766 sub load_branches_to_pull_from {
767 my $use_transport_cost_matrix = shift;
771 unless ( $use_transport_cost_matrix ) {
772 my $static_branch_list = C4::Context->preference("StaticHoldsQueueWeight");
773 @branches_to_use = map { _trim($_) } split( /,/, $static_branch_list )
774 if $static_branch_list;
778 Koha::Database->new()->schema()->resultset('Branch')
779 ->get_column('branchcode')->all()
780 unless (@branches_to_use);
782 @branches_to_use = shuffle(@branches_to_use)
783 if C4::Context->preference("RandomizeHoldsQueueWeight");
785 my $today = dt_from_string();
786 if ( C4::Context->preference('HoldsQueueSkipClosed') ) {
787 @branches_to_use = grep {
788 !Koha::Calendar->new( branchcode => $_ )
789 ->is_holiday( $today )
793 return \@branches_to_use;
796 sub least_cost_branch {
799 my ($to, $from, $transport_cost_matrix) = @_;
801 # Nothing really spectacular: supply to branch, a list of potential from branches
802 # and find the minimum from - to value from the transport_cost_matrix
803 return $from->[0] if ( @$from == 1 && $transport_cost_matrix->{$to}{$from->[0]}->{disable_transfer} != 1 );
805 # If the pickup library is in the list of libraries to pull from,
806 # return that library right away, it is obviously the least costly
807 return ($to) if any { $_ eq $to } @$from;
809 my ($least_cost, @branch);
811 my $cell = $transport_cost_matrix->{$to}{$_};
812 next if $cell->{disable_transfer};
814 my $cost = $cell->{cost};
815 next unless defined $cost; # XXX should this be reported?
817 unless (defined $least_cost) {
823 next if $cost > $least_cost;
825 if ($cost == $least_cost) {
836 # XXX return a random @branch with minimum cost instead of the first one;
837 # return $branch[0] if @branch == 1;