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
.medium
,
138 biblio
.part_number
, biblio
.part_name
,
139 biblioitems
.publicationyear
, biblioitems
.pages
, biblioitems
.size
,
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
};
170 Top level function that turns reserves into tmp_holdsqueue and hold_fill_targets.
175 my $dbh = C4
::Context
->dbh;
177 $dbh->do("DELETE FROM tmp_holdsqueue"); # clear the old table for new info
178 $dbh->do("DELETE FROM hold_fill_targets");
181 my $total_requests = 0;
182 my $total_available_items = 0;
183 my $num_items_mapped = 0;
186 my $transport_cost_matrix;
187 my $use_transport_cost_matrix = C4
::Context
->preference("UseTransportCostMatrix");
188 if ($use_transport_cost_matrix) {
189 $transport_cost_matrix = TransportCostMatrix
();
190 unless (keys %$transport_cost_matrix) {
191 warn "UseTransportCostMatrix set to yes, but matrix not populated";
192 undef $transport_cost_matrix;
195 unless ($transport_cost_matrix) {
196 $branches_to_use = load_branches_to_pull_from
();
199 my $bibs_with_pending_requests = GetBibsWithPendingHoldRequests
();
201 foreach my $biblionumber (@
$bibs_with_pending_requests) {
203 my $hold_requests = GetPendingHoldRequestsForBib
($biblionumber);
204 my $available_items = GetItemsAvailableToFillHoldRequestsForBib
($biblionumber, $branches_to_use);
205 $total_requests += scalar(@
$hold_requests);
206 $total_available_items += scalar(@
$available_items);
208 my $item_map = MapItemsToHoldRequests
($hold_requests, $available_items, $branches_to_use, $transport_cost_matrix);
210 my $item_map_size = scalar(keys %$item_map)
213 $num_items_mapped += $item_map_size;
214 CreatePicklistFromItemMap
($item_map);
215 AddToHoldTargetMap
($item_map);
216 if (($item_map_size < scalar(@
$hold_requests )) and
217 ($item_map_size < scalar(@
$available_items))) {
218 # DOUBLE CHECK, but this is probably OK - unfilled item-level requests
220 #warn "unfilled requests for $biblionumber";
221 #warn Dumper($hold_requests), Dumper($available_items), Dumper($item_map);
226 =head2 GetBibsWithPendingHoldRequests
228 my $biblionumber_aref = GetBibsWithPendingHoldRequests();
230 Return an arrayref of the biblionumbers of all bibs
231 that have one or more unfilled hold requests.
235 sub GetBibsWithPendingHoldRequests
{
236 my $dbh = C4
::Context
->dbh;
238 my $bib_query = "SELECT DISTINCT biblionumber
242 AND reservedate <= CURRENT_DATE()
245 my $sth = $dbh->prepare($bib_query);
248 my $biblionumbers = $sth->fetchall_arrayref();
250 return [ map { $_->[0] } @
$biblionumbers ];
253 =head2 GetPendingHoldRequestsForBib
255 my $requests = GetPendingHoldRequestsForBib($biblionumber);
257 Returns an arrayref of hashrefs to pending, unfilled hold requests
258 on the bib identified by $biblionumber. The following keys
259 are present in each hashref:
270 The arrayref is sorted in order of increasing priority.
274 sub GetPendingHoldRequestsForBib
{
275 my $biblionumber = shift;
277 my $dbh = C4
::Context
->dbh;
279 my $request_query = "SELECT biblionumber, borrowernumber, itemnumber, priority, reserves.branchcode,
280 reservedate, reservenotes, borrowers.branchcode AS borrowerbranch, itemtype
282 JOIN borrowers USING (borrowernumber)
283 WHERE biblionumber = ?
286 AND reservedate <= CURRENT_DATE()
289 my $sth = $dbh->prepare($request_query);
290 $sth->execute($biblionumber);
292 my $requests = $sth->fetchall_arrayref({});
297 =head2 GetItemsAvailableToFillHoldRequestsForBib
299 my $available_items = GetItemsAvailableToFillHoldRequestsForBib($biblionumber, $branches_ar);
301 Returns an arrayref of items available to fill hold requests
302 for the bib identified by C<$biblionumber>. An item is available
303 to fill a hold request if and only if:
306 * it is not withdrawn
307 * it is not marked notforloan
308 * it is not currently in transit
310 * it is not sitting on the hold shelf
311 * it is not damaged (unless AllowHoldsOnDamagedItems is on)
315 sub GetItemsAvailableToFillHoldRequestsForBib
{
316 my ($biblionumber, $branches_to_use) = @_;
318 my $dbh = C4
::Context
->dbh;
319 my $items_query = "SELECT itemnumber, homebranch, holdingbranch, itemtypes.itemtype AS itype
322 if (C4
::Context
->preference('item-level_itypes')) {
323 $items_query .= "LEFT JOIN itemtypes ON (itemtypes.itemtype = items.itype) ";
325 $items_query .= "JOIN biblioitems USING (biblioitemnumber)
326 LEFT JOIN itemtypes USING (itemtype) ";
328 $items_query .= "WHERE items.notforloan = 0
329 AND holdingbranch IS NOT NULL
332 $items_query .= " AND damaged = 0" unless C4
::Context
->preference('AllowHoldsOnDamagedItems');
333 $items_query .= " AND items.onloan IS NULL
334 AND (itemtypes.notforloan IS NULL OR itemtypes.notforloan = 0)
335 AND itemnumber NOT IN (
338 WHERE biblionumber = ?
339 AND itemnumber IS NOT NULL
340 AND (found IS NOT NULL OR priority = 0)
342 AND items.biblionumber = ?";
344 my @params = ($biblionumber, $biblionumber);
345 if ($branches_to_use && @
$branches_to_use) {
346 $items_query .= " AND holdingbranch IN (" . join (",", map { "?" } @
$branches_to_use) . ")";
347 push @params, @
$branches_to_use;
349 my $sth = $dbh->prepare($items_query);
350 $sth->execute(@params);
352 my $itm = $sth->fetchall_arrayref({});
353 my @items = grep { ! scalar GetTransfers
($_->{itemnumber
}) } @
$itm;
355 my $rule = GetBranchItemRule
($_->{homebranch
}, $_->{itype
});
356 $_->{holdallowed
} = $rule->{holdallowed
};
357 $_->{hold_fulfillment_policy
} = $rule->{hold_fulfillment_policy
};
361 =head2 MapItemsToHoldRequests
363 MapItemsToHoldRequests($hold_requests, $available_items, $branches, $transport_cost_matrix)
367 sub MapItemsToHoldRequests
{
368 my ($hold_requests, $available_items, $branches_to_use, $transport_cost_matrix) = @_;
370 # handle trival cases
371 return unless scalar(@
$hold_requests) > 0;
372 return unless scalar(@
$available_items) > 0;
374 # identify item-level requests
375 my %specific_items_requested = map { $_->{itemnumber
} => 1 }
376 grep { defined($_->{itemnumber
}) }
379 map { $_->{_object
} = Koha
::Items
->find( $_->{itemnumber
} ) } @
$available_items;
381 map { $libraries->{$_->id} = $_ } Koha
::Libraries
->search();
383 # group available items by itemnumber
384 my %items_by_itemnumber = map { $_->{itemnumber
} => $_ } @
$available_items;
386 # items already allocated
387 my %allocated_items = ();
389 # map of items to hold requests
392 # figure out which item-level requests can be filled
393 my $num_items_remaining = scalar(@
$available_items);
395 # Look for Local Holds Priority matches first
396 if ( C4
::Context
->preference('LocalHoldsPriority') ) {
397 my $LocalHoldsPriorityPatronControl =
398 C4
::Context
->preference('LocalHoldsPriorityPatronControl');
399 my $LocalHoldsPriorityItemControl =
400 C4
::Context
->preference('LocalHoldsPriorityItemControl');
402 foreach my $request (@
$hold_requests) {
403 next if (defined($request->{itemnumber
})); #skip item level holds in local priority checking
404 last if $num_items_remaining == 0;
406 my $local_hold_match;
407 foreach my $item (@
$available_items) {
409 if ( !$item->{holdallowed
} )
410 || ( $item->{holdallowed
} == 1
411 && $item->{homebranch
} ne $request->{borrowerbranch
} );
413 next unless $item->{_object
}->can_be_transferred( { to
=> $libraries->{ $request->{branchcode
} } } );
415 my $local_holds_priority_item_branchcode =
416 $item->{$LocalHoldsPriorityItemControl};
418 my $local_holds_priority_patron_branchcode =
419 ( $LocalHoldsPriorityPatronControl eq 'PickupLibrary' )
420 ?
$request->{branchcode
}
421 : ( $LocalHoldsPriorityPatronControl eq 'HomeLibrary' )
422 ?
$request->{borrowerbranch
}
426 $local_holds_priority_item_branchcode eq
427 $local_holds_priority_patron_branchcode;
429 if ($local_hold_match) {
430 if ( exists $items_by_itemnumber{ $item->{itemnumber
} }
431 and not exists $allocated_items{ $item->{itemnumber
} }
432 and not $request->{allocated
})
434 $item_map{ $item->{itemnumber
} } = {
435 borrowernumber
=> $request->{borrowernumber
},
436 biblionumber
=> $request->{biblionumber
},
437 holdingbranch
=> $item->{holdingbranch
},
438 pickup_branch
=> $request->{branchcode
}
439 || $request->{borrowerbranch
},
441 reservedate
=> $request->{reservedate
},
442 reservenotes
=> $request->{reservenotes
},
444 $allocated_items{ $item->{itemnumber
} }++;
445 $request->{allocated
} = 1;
446 $num_items_remaining--;
453 foreach my $request (@
$hold_requests) {
454 last if $num_items_remaining == 0;
455 next if $request->{allocated
};
457 # is this an item-level request?
458 if (defined($request->{itemnumber
})) {
459 # fill it if possible; if not skip it
461 exists $items_by_itemnumber{ $request->{itemnumber
} }
462 and not exists $allocated_items{ $request->{itemnumber
} }
463 and ( # Don't fill item level holds that contravene the hold pickup policy at this time
464 ( $items_by_itemnumber{ $request->{itemnumber
} }->{hold_fulfillment_policy
} eq 'any' )
465 || ( $request->{branchcode
} eq $items_by_itemnumber{ $request->{itemnumber
} }->{ $items_by_itemnumber{ $request->{itemnumber
} }->{hold_fulfillment_policy
} } )
466 and ( !$request->{itemtype
} # If hold itemtype is set, item's itemtype must match
467 || $items_by_itemnumber{ $request->{itemnumber
} }->{itype
} eq $request->{itemtype
} )
469 and $items_by_itemnumber{ $request->{itemnumber
} }->{_object
}->can_be_transferred( { to
=> $libraries->{ $request->{branchcode
} } } )
474 $item_map{ $request->{itemnumber
} } = {
475 borrowernumber
=> $request->{borrowernumber
},
476 biblionumber
=> $request->{biblionumber
},
477 holdingbranch
=> $items_by_itemnumber{ $request->{itemnumber
} }->{holdingbranch
},
478 pickup_branch
=> $request->{branchcode
} || $request->{borrowerbranch
},
480 reservedate
=> $request->{reservedate
},
481 reservenotes
=> $request->{reservenotes
},
483 $allocated_items{ $request->{itemnumber
} }++;
484 $num_items_remaining--;
487 # it's title-level request that will take up one item
488 $num_items_remaining--;
492 # group available items by branch
493 my %items_by_branch = ();
494 foreach my $item (@
$available_items) {
495 next unless $item->{holdallowed
};
497 push @
{ $items_by_branch{ $item->{holdingbranch
} } }, $item
498 unless exists $allocated_items{ $item->{itemnumber
} };
500 return \
%item_map unless keys %items_by_branch;
502 # now handle the title-level requests
503 $num_items_remaining = scalar(@
$available_items) - scalar(keys %allocated_items);
505 foreach my $request (@
$hold_requests) {
506 last if $num_items_remaining == 0;
507 next if $request->{allocated
};
508 next if defined($request->{itemnumber
}); # already handled these
510 # look for local match first
511 my $pickup_branch = $request->{branchcode
} || $request->{borrowerbranch
};
512 my ($itemnumber, $holdingbranch);
514 my $holding_branch_items = $items_by_branch{$pickup_branch};
515 if ( $holding_branch_items ) {
516 foreach my $item (@
$holding_branch_items) {
517 next unless $items_by_itemnumber{ $item->{itemnumber
} }->{_object
}->can_be_transferred( { to
=> $libraries->{ $request->{branchcode
} } } );
520 $request->{borrowerbranch
} eq $item->{homebranch
}
521 && ( ( $item->{hold_fulfillment_policy
} eq 'any' ) # Don't fill item level holds that contravene the hold pickup policy at this time
522 || $request->{branchcode
} eq $item->{ $item->{hold_fulfillment_policy
} } )
523 && ( !$request->{itemtype
} # If hold itemtype is set, item's itemtype must match
524 || $items_by_itemnumber{ $request->{itemnumber
} }->{itype
} eq $request->{itemtype
} )
527 $itemnumber = $item->{itemnumber
};
531 $holdingbranch = $pickup_branch;
533 elsif ($transport_cost_matrix) {
534 $pull_branches = [keys %items_by_branch];
535 $holdingbranch = least_cost_branch
( $pickup_branch, $pull_branches, $transport_cost_matrix );
536 if ( $holdingbranch ) {
538 my $holding_branch_items = $items_by_branch{$holdingbranch};
539 foreach my $item (@
$holding_branch_items) {
540 next if $request->{borrowerbranch
} ne $item->{homebranch
};
541 next unless $items_by_itemnumber{ $item->{itemnumber
} }->{_object
}->can_be_transferred( { to
=> $libraries->{ $request->{branchcode
} } } );
543 # Don't fill item level holds that contravene the hold pickup policy at this time
544 next unless $item->{hold_fulfillment_policy
} eq 'any'
545 || $request->{branchcode
} eq $item->{ $item->{hold_fulfillment_policy
} };
547 # If hold itemtype is set, item's itemtype must match
548 next unless ( !$request->{itemtype
}
549 || $item->{itype
} eq $request->{itemtype
} );
551 $itemnumber = $item->{itemnumber
};
560 unless ($itemnumber) {
561 # not found yet, fall back to basics
562 if ($branches_to_use) {
563 $pull_branches = $branches_to_use;
565 $pull_branches = [keys %items_by_branch];
568 # Try picking items where the home and pickup branch match first
570 foreach my $branch (@
$pull_branches) {
571 my $holding_branch_items = $items_by_branch{$branch}
574 $holdingbranch ||= $branch;
575 foreach my $item (@
$holding_branch_items) {
576 next if $pickup_branch ne $item->{homebranch
};
577 next if ( $item->{holdallowed
} == 1 && $item->{homebranch
} ne $request->{borrowerbranch
} );
578 next unless $items_by_itemnumber{ $item->{itemnumber
} }->{_object
}->can_be_transferred( { to
=> $libraries->{ $request->{branchcode
} } } );
580 # Don't fill item level holds that contravene the hold pickup policy at this time
581 next unless $item->{hold_fulfillment_policy
} eq 'any'
582 || $request->{branchcode
} eq $item->{ $item->{hold_fulfillment_policy
} };
584 # If hold itemtype is set, item's itemtype must match
585 next unless ( !$request->{itemtype
}
586 || $item->{itype
} eq $request->{itemtype
} );
588 $itemnumber = $item->{itemnumber
};
589 $holdingbranch = $branch;
594 # Now try items from the least cost branch based on the transport cost matrix or StaticHoldsQueueWeight
595 unless ( $itemnumber ) {
596 foreach my $current_item ( @
{ $items_by_branch{$holdingbranch} } ) {
597 if ( $holdingbranch && ( $current_item->{holdallowed
} == 2 || $request->{borrowerbranch
} eq $current_item->{homebranch
} ) ) {
599 # Don't fill item level holds that contravene the hold pickup policy at this time
600 next unless $current_item->{hold_fulfillment_policy
} eq 'any'
601 || $request->{branchcode
} eq $current_item->{ $current_item->{hold_fulfillment_policy
} };
603 # If hold itemtype is set, item's itemtype must match
604 next unless ( !$request->{itemtype
}
605 || $current_item->{itype
} eq $request->{itemtype
} );
607 next unless $items_by_itemnumber{ $current_item->{itemnumber
} }->{_object
}->can_be_transferred( { to
=> $libraries->{ $request->{branchcode
} } } );
609 $itemnumber = $current_item->{itemnumber
};
610 last; # quit this loop as soon as we have a suitable item
615 # Now try for items for any item that can fill this hold
616 unless ( $itemnumber ) {
618 foreach my $branch (@
$pull_branches) {
619 my $holding_branch_items = $items_by_branch{$branch}
622 foreach my $item (@
$holding_branch_items) {
623 next if ( $item->{holdallowed
} == 1 && $item->{homebranch
} ne $request->{borrowerbranch
} );
625 # Don't fill item level holds that contravene the hold pickup policy at this time
626 next unless $item->{hold_fulfillment_policy
} eq 'any'
627 || $request->{branchcode
} eq $item->{ $item->{hold_fulfillment_policy
} };
629 # If hold itemtype is set, item's itemtype must match
630 next unless ( !$request->{itemtype
}
631 || $item->{itype
} eq $request->{itemtype
} );
633 next unless $items_by_itemnumber{ $item->{itemnumber
} }->{_object
}->can_be_transferred( { to
=> $libraries->{ $request->{branchcode
} } } );
635 $itemnumber = $item->{itemnumber
};
636 $holdingbranch = $branch;
644 my $holding_branch_items = $items_by_branch{$holdingbranch}
645 or die "Have $itemnumber, $holdingbranch, but no items!";
646 @
$holding_branch_items = grep { $_->{itemnumber
} != $itemnumber } @
$holding_branch_items;
647 delete $items_by_branch{$holdingbranch} unless @
$holding_branch_items;
649 $item_map{$itemnumber} = {
650 borrowernumber
=> $request->{borrowernumber
},
651 biblionumber
=> $request->{biblionumber
},
652 holdingbranch
=> $holdingbranch,
653 pickup_branch
=> $pickup_branch,
655 reservedate
=> $request->{reservedate
},
656 reservenotes
=> $request->{reservenotes
},
658 $num_items_remaining--;
664 =head2 CreatePickListFromItemMap
668 sub CreatePicklistFromItemMap
{
669 my $item_map = shift;
671 my $dbh = C4
::Context
->dbh;
673 my $sth_load=$dbh->prepare("
674 INSERT INTO tmp_holdsqueue (biblionumber,itemnumber,barcode,surname,firstname,phone,borrowernumber,
675 cardnumber,reservedate,title, itemcallnumber,
676 holdingbranch,pickbranch,notes, item_level_request)
677 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
680 foreach my $itemnumber (sort keys %$item_map) {
681 my $mapped_item = $item_map->{$itemnumber};
682 my $biblionumber = $mapped_item->{biblionumber
};
683 my $borrowernumber = $mapped_item->{borrowernumber
};
684 my $pickbranch = $mapped_item->{pickup_branch
};
685 my $holdingbranch = $mapped_item->{holdingbranch
};
686 my $reservedate = $mapped_item->{reservedate
};
687 my $reservenotes = $mapped_item->{reservenotes
};
688 my $item_level = $mapped_item->{item_level
};
690 my $item = Koha
::Items
->find($itemnumber);
691 my $barcode = $item->barcode;
692 my $itemcallnumber = $item->itemcallnumber;
694 my $patron = Koha
::Patrons
->find( $borrowernumber );
695 my $cardnumber = $patron->cardnumber;
696 my $surname = $patron->surname;
697 my $firstname = $patron->firstname;
698 my $phone = $patron->phone;
700 my $biblio = Koha
::Biblios
->find( $biblionumber );
701 my $title = $biblio->title;
703 $sth_load->execute($biblionumber, $itemnumber, $barcode, $surname, $firstname, $phone, $borrowernumber,
704 $cardnumber, $reservedate, $title, $itemcallnumber,
705 $holdingbranch, $pickbranch, $reservenotes, $item_level);
709 =head2 AddToHoldTargetMap
713 sub AddToHoldTargetMap
{
714 my $item_map = shift;
716 my $dbh = C4
::Context
->dbh;
719 INSERT INTO hold_fill_targets
(borrowernumber
, biblionumber
, itemnumber
, source_branchcode
, item_level_request
)
720 VALUES
(?
, ?
, ?
, ?
, ?
)
722 my $sth_insert = $dbh->prepare($insert_sql);
724 foreach my $itemnumber (keys %$item_map) {
725 my $mapped_item = $item_map->{$itemnumber};
726 $sth_insert->execute($mapped_item->{borrowernumber
}, $mapped_item->{biblionumber
}, $itemnumber,
727 $mapped_item->{holdingbranch
}, $mapped_item->{item_level
});
731 # Helper functions, not part of any interface
734 return $_[0] unless $_[0];
740 sub load_branches_to_pull_from
{
743 my $static_branch_list = C4
::Context
->preference("StaticHoldsQueueWeight");
744 @branches_to_use = map { _trim
($_) } split( /,/, $static_branch_list )
745 if $static_branch_list;
748 Koha
::Database
->new()->schema()->resultset('Branch')
749 ->get_column('branchcode')->all()
750 unless (@branches_to_use);
752 @branches_to_use = shuffle
(@branches_to_use)
753 if C4
::Context
->preference("RandomizeHoldsQueueWeight");
755 my $today = dt_from_string
();
756 if ( C4
::Context
->preference('HoldsQueueSkipClosed') ) {
757 @branches_to_use = grep {
758 !Koha
::Calendar
->new( branchcode
=> $_ )
759 ->is_holiday( $today )
763 return \
@branches_to_use;
766 sub least_cost_branch
{
769 my ($to, $from, $transport_cost_matrix) = @_;
771 # Nothing really spectacular: supply to branch, a list of potential from branches
772 # and find the minimum from - to value from the transport_cost_matrix
773 return $from->[0] if ( @
$from == 1 && $transport_cost_matrix->{$to}{$from->[0]}->{disable_transfer
} != 1 );
775 # If the pickup library is in the list of libraries to pull from,
776 # return that library right away, it is obviously the least costly
777 return ($to) if any
{ $_ eq $to } @
$from;
779 my ($least_cost, @branch);
781 my $cell = $transport_cost_matrix->{$to}{$_};
782 next if $cell->{disable_transfer
};
784 my $cost = $cell->{cost
};
785 next unless defined $cost; # XXX should this be reported?
787 unless (defined $least_cost) {
793 next if $cost > $least_cost;
795 if ($cost == $least_cost) {
806 # XXX return a random @branch with minimum cost instead of the first one;
807 # return $branch[0] if @branch == 1;