Bug 18361: Koha::Objects->find should accept composite primary keys
[koha.git] / C4 / HoldsQueue.pm
blob0f2ad221a249cbf7730a9ea2b5533feba2ddc13c
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
22 use strict;
23 use warnings;
25 use C4::Context;
26 use C4::Search;
27 use C4::Items;
28 use C4::Circulation;
29 use C4::Members;
30 use C4::Biblio;
31 use Koha::DateUtils;
33 use List::Util qw(shuffle);
34 use List::MoreUtils qw(any);
35 use Data::Dumper;
37 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
38 BEGIN {
39 require Exporter;
40 @ISA = qw(Exporter);
41 @EXPORT_OK = qw(
42 &CreateQueue
43 &GetHoldsQueueItems
45 &TransportCostMatrix
46 &UpdateTransportCostMatrix
51 =head1 FUNCTIONS
53 =head2 TransportCostMatrix
55 TransportCostMatrix();
57 Returns Transport Cost Matrix as a hashref <to branch code> => <from branch code> => cost
59 =cut
61 sub TransportCostMatrix {
62 my $dbh = C4::Context->dbh;
63 my $transport_costs = $dbh->selectall_arrayref("SELECT * FROM transport_cost",{ Slice => {} });
65 my $today = dt_from_string();
66 my $calendars;
67 my %transport_cost_matrix;
68 foreach (@$transport_costs) {
69 my $from = $_->{frombranch};
70 my $to = $_->{tobranch};
71 my $cost = $_->{cost};
72 my $disabled = $_->{disable_transfer};
73 $transport_cost_matrix{$to}{$from} = {
74 cost => $cost,
75 disable_transfer => $disabled
78 if ( C4::Context->preference("HoldsQueueSkipClosed") ) {
79 $calendars->{$from} ||= Koha::Calendar->new( branchcode => $from );
80 $transport_cost_matrix{$to}{$from}{disable_transfer} ||=
81 $calendars->{$from}->is_holiday( $today );
85 return \%transport_cost_matrix;
88 =head2 UpdateTransportCostMatrix
90 UpdateTransportCostMatrix($records);
92 Updates full Transport Cost Matrix table. $records is an arrayref of records.
93 Records: { frombranch => <code>, tobranch => <code>, cost => <figure>, disable_transfer => <0,1> }
95 =cut
97 sub UpdateTransportCostMatrix {
98 my ($records) = @_;
99 my $dbh = C4::Context->dbh;
101 my $sth = $dbh->prepare("INSERT INTO transport_cost (frombranch, tobranch, cost, disable_transfer) VALUES (?, ?, ?, ?)");
103 $dbh->do("TRUNCATE TABLE transport_cost");
104 foreach (@$records) {
105 my $cost = $_->{cost};
106 my $from = $_->{frombranch};
107 my $to = $_->{tobranch};
108 if ($_->{disable_transfer}) {
109 $cost ||= 0;
111 elsif ( !defined ($cost) || ($cost !~ m/(0|[1-9][0-9]*)(\.[0-9]*)?/o) ) {
112 warn "Invalid $from -> $to cost $cost - must be a number >= 0, disablig";
113 $cost = 0;
114 $_->{disable_transfer} = 1;
116 $sth->execute( $from, $to, $cost, $_->{disable_transfer} ? 1 : 0 );
120 =head2 GetHoldsQueueItems
122 GetHoldsQueueItems($branch);
124 Returns hold queue for a holding branch. If branch is omitted, then whole queue is returned
126 =cut
128 sub GetHoldsQueueItems {
129 my ($branchlimit) = @_;
130 my $dbh = C4::Context->dbh;
132 my @bind_params = ();
133 my $query = q/SELECT tmp_holdsqueue.*, biblio.author, items.ccode, items.itype, biblioitems.itemtype, items.location, items.enumchron, items.cn_sort, biblioitems.publishercode,biblio.copyrightdate,biblioitems.publicationyear,biblioitems.pages,biblioitems.size,biblioitems.publicationyear,biblioitems.isbn,items.copynumber
134 FROM tmp_holdsqueue
135 JOIN biblio USING (biblionumber)
136 LEFT JOIN biblioitems USING (biblionumber)
137 LEFT JOIN items USING ( itemnumber)
139 if ($branchlimit) {
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);
146 my $items = [];
147 while ( my $row = $sth->fetchrow_hashref ){
148 my $record = GetMarcBiblio($row->{biblionumber});
149 if ($record){
150 $row->{subtitle} = [ map { $_->{subfield} } @{ GetRecordValue( 'subtitle', $record, '' ) } ];
151 $row->{parts} = GetRecordValue('parts',$record,'')->[0]->{subfield};
152 $row->{numbers} = GetRecordValue('numbers',$record,'')->[0]->{subfield};
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 push @$items, $row;
163 return $items;
166 =head2 CreateQueue
168 CreateQueue();
170 Top level function that turns reserves into tmp_holdsqueue and hold_fill_targets.
172 =cut
174 sub CreateQueue {
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");
180 my $total_bibs = 0;
181 my $total_requests = 0;
182 my $total_available_items = 0;
183 my $num_items_mapped = 0;
185 my $branches_to_use;
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) {
202 $total_bibs++;
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);
209 $item_map or next;
210 my $item_map_size = scalar(keys %$item_map)
211 or next;
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
219 # FIXME
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.
233 =cut
235 sub GetBibsWithPendingHoldRequests {
236 my $dbh = C4::Context->dbh;
238 my $bib_query = "SELECT DISTINCT biblionumber
239 FROM reserves
240 WHERE found IS NULL
241 AND priority > 0
242 AND reservedate <= CURRENT_DATE()
243 AND suspend = 0
245 my $sth = $dbh->prepare($bib_query);
247 $sth->execute();
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:
261 biblionumber
262 borrowernumber
263 itemnumber
264 priority
265 branchcode
266 reservedate
267 reservenotes
268 borrowerbranch
270 The arrayref is sorted in order of increasing priority.
272 =cut
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
281 FROM reserves
282 JOIN borrowers USING (borrowernumber)
283 WHERE biblionumber = ?
284 AND found IS NULL
285 AND priority > 0
286 AND reservedate <= CURRENT_DATE()
287 AND suspend = 0
288 ORDER BY priority";
289 my $sth = $dbh->prepare($request_query);
290 $sth->execute($biblionumber);
292 my $requests = $sth->fetchall_arrayref({});
293 return $requests;
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:
305 * it is not on loan
306 * it is not withdrawn
307 * it is not marked notforloan
308 * it is not currently in transit
309 * it is not lost
310 * it is not sitting on the hold shelf
311 * it is not damaged (unless AllowHoldsOnDamagedItems is on)
313 =cut
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
320 FROM items ";
322 if (C4::Context->preference('item-level_itypes')) {
323 $items_query .= "LEFT JOIN itemtypes ON (itemtypes.itemtype = items.itype) ";
324 } else {
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
330 AND itemlost = 0
331 AND withdrawn = 0";
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 (
336 SELECT itemnumber
337 FROM reserves
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;
354 return [ grep {
355 my $rule = GetBranchItemRule($_->{homebranch}, $_->{itype});
356 $_->{holdallowed} = $rule->{holdallowed};
357 $_->{hold_fulfillment_policy} = $rule->{hold_fulfillment_policy};
358 } @items ];
361 =head2 MapItemsToHoldRequests
363 MapItemsToHoldRequests($hold_requests, $available_items, $branches, $transport_cost_matrix)
365 =cut
367 sub MapItemsToHoldRequests {
368 my ($hold_requests, $available_items, $branches_to_use, $transport_cost_matrix) = @_;
371 # handle trival cases
372 return unless scalar(@$hold_requests) > 0;
373 return unless scalar(@$available_items) > 0;
375 # identify item-level requests
376 my %specific_items_requested = map { $_->{itemnumber} => 1 }
377 grep { defined($_->{itemnumber}) }
378 @$hold_requests;
380 # group available items by itemnumber
381 my %items_by_itemnumber = map { $_->{itemnumber} => $_ } @$available_items;
383 # items already allocated
384 my %allocated_items = ();
386 # map of items to hold requests
387 my %item_map = ();
389 # figure out which item-level requests can be filled
390 my $num_items_remaining = scalar(@$available_items);
392 # Look for Local Holds Priority matches first
393 if ( C4::Context->preference('LocalHoldsPriority') ) {
394 my $LocalHoldsPriorityPatronControl =
395 C4::Context->preference('LocalHoldsPriorityPatronControl');
396 my $LocalHoldsPriorityItemControl =
397 C4::Context->preference('LocalHoldsPriorityItemControl');
399 foreach my $request (@$hold_requests) {
400 next if (defined($request->{itemnumber})); #skip item level holds in local priority checking
401 last if $num_items_remaining == 0;
403 my $local_hold_match;
404 foreach my $item (@$available_items) {
405 next
406 if ( !$item->{holdallowed} )
407 || ( $item->{holdallowed} == 1
408 && $item->{homebranch} ne $request->{borrowerbranch} );
410 my $local_holds_priority_item_branchcode =
411 $item->{$LocalHoldsPriorityItemControl};
413 my $local_holds_priority_patron_branchcode =
414 ( $LocalHoldsPriorityPatronControl eq 'PickupLibrary' )
415 ? $request->{branchcode}
416 : ( $LocalHoldsPriorityPatronControl eq 'HomeLibrary' )
417 ? $request->{borrowerbranch}
418 : undef;
420 $local_hold_match =
421 $local_holds_priority_item_branchcode eq
422 $local_holds_priority_patron_branchcode;
424 if ($local_hold_match) {
425 if ( exists $items_by_itemnumber{ $item->{itemnumber} }
426 and not exists $allocated_items{ $item->{itemnumber} }
427 and not $request->{allocated})
429 $item_map{ $item->{itemnumber} } = {
430 borrowernumber => $request->{borrowernumber},
431 biblionumber => $request->{biblionumber},
432 holdingbranch => $item->{holdingbranch},
433 pickup_branch => $request->{branchcode}
434 || $request->{borrowerbranch},
435 item_level => 0,
436 reservedate => $request->{reservedate},
437 reservenotes => $request->{reservenotes},
439 $allocated_items{ $item->{itemnumber} }++;
440 $request->{allocated} = 1;
441 $num_items_remaining--;
448 foreach my $request (@$hold_requests) {
449 last if $num_items_remaining == 0;
450 next if $request->{allocated};
452 # is this an item-level request?
453 if (defined($request->{itemnumber})) {
454 # fill it if possible; if not skip it
455 if (
456 exists $items_by_itemnumber{ $request->{itemnumber} }
457 and not exists $allocated_items{ $request->{itemnumber} }
458 and ( # Don't fill item level holds that contravene the hold pickup policy at this time
459 ( $items_by_itemnumber{ $request->{itemnumber} }->{hold_fulfillment_policy} eq 'any' )
460 || ( $request->{branchcode} eq $items_by_itemnumber{ $request->{itemnumber} }->{ $items_by_itemnumber{ $request->{itemnumber} }->{hold_fulfillment_policy} } )
461 and ( !$request->{itemtype} # If hold itemtype is set, item's itemtype must match
462 || $items_by_itemnumber{ $request->{itemnumber} }->{itype} eq $request->{itemtype} )
468 $item_map{ $request->{itemnumber} } = {
469 borrowernumber => $request->{borrowernumber},
470 biblionumber => $request->{biblionumber},
471 holdingbranch => $items_by_itemnumber{ $request->{itemnumber} }->{holdingbranch},
472 pickup_branch => $request->{branchcode} || $request->{borrowerbranch},
473 item_level => 1,
474 reservedate => $request->{reservedate},
475 reservenotes => $request->{reservenotes},
477 $allocated_items{ $request->{itemnumber} }++;
478 $num_items_remaining--;
480 } else {
481 # it's title-level request that will take up one item
482 $num_items_remaining--;
486 # group available items by branch
487 my %items_by_branch = ();
488 foreach my $item (@$available_items) {
489 next unless $item->{holdallowed};
491 push @{ $items_by_branch{ $item->{holdingbranch} } }, $item
492 unless exists $allocated_items{ $item->{itemnumber} };
494 return \%item_map unless keys %items_by_branch;
496 # now handle the title-level requests
497 $num_items_remaining = scalar(@$available_items) - scalar(keys %allocated_items);
498 my $pull_branches;
499 foreach my $request (@$hold_requests) {
500 last if $num_items_remaining == 0;
501 next if $request->{allocated};
502 next if defined($request->{itemnumber}); # already handled these
504 # look for local match first
505 my $pickup_branch = $request->{branchcode} || $request->{borrowerbranch};
506 my ($itemnumber, $holdingbranch);
508 my $holding_branch_items = $items_by_branch{$pickup_branch};
509 if ( $holding_branch_items ) {
510 foreach my $item (@$holding_branch_items) {
511 if (
512 $request->{borrowerbranch} eq $item->{homebranch}
513 && ( ( $item->{hold_fulfillment_policy} eq 'any' ) # Don't fill item level holds that contravene the hold pickup policy at this time
514 || $request->{branchcode} eq $item->{ $item->{hold_fulfillment_policy} } )
515 && ( !$request->{itemtype} # If hold itemtype is set, item's itemtype must match
516 || $items_by_itemnumber{ $request->{itemnumber} }->{itype} eq $request->{itemtype} )
519 $itemnumber = $item->{itemnumber};
520 last;
523 $holdingbranch = $pickup_branch;
525 elsif ($transport_cost_matrix) {
526 $pull_branches = [keys %items_by_branch];
527 $holdingbranch = least_cost_branch( $pickup_branch, $pull_branches, $transport_cost_matrix );
528 if ( $holdingbranch ) {
530 my $holding_branch_items = $items_by_branch{$holdingbranch};
531 foreach my $item (@$holding_branch_items) {
532 next if $request->{borrowerbranch} ne $item->{homebranch};
534 # Don't fill item level holds that contravene the hold pickup policy at this time
535 next unless $item->{hold_fulfillment_policy} eq 'any'
536 || $request->{branchcode} eq $item->{ $item->{hold_fulfillment_policy} };
538 # If hold itemtype is set, item's itemtype must match
539 next unless ( !$request->{itemtype}
540 || $item->{itype} eq $request->{itemtype} );
542 $itemnumber = $item->{itemnumber};
543 last;
546 else {
547 next;
551 unless ($itemnumber) {
552 # not found yet, fall back to basics
553 if ($branches_to_use) {
554 $pull_branches = $branches_to_use;
555 } else {
556 $pull_branches = [keys %items_by_branch];
559 # Try picking items where the home and pickup branch match first
560 PULL_BRANCHES:
561 foreach my $branch (@$pull_branches) {
562 my $holding_branch_items = $items_by_branch{$branch}
563 or next;
565 $holdingbranch ||= $branch;
566 foreach my $item (@$holding_branch_items) {
567 next if $pickup_branch ne $item->{homebranch};
568 next if ( $item->{holdallowed} == 1 && $item->{homebranch} ne $request->{borrowerbranch} );
570 # Don't fill item level holds that contravene the hold pickup policy at this time
571 next unless $item->{hold_fulfillment_policy} eq 'any'
572 || $request->{branchcode} eq $item->{ $item->{hold_fulfillment_policy} };
574 # If hold itemtype is set, item's itemtype must match
575 next unless ( !$request->{itemtype}
576 || $item->{itype} eq $request->{itemtype} );
578 $itemnumber = $item->{itemnumber};
579 $holdingbranch = $branch;
580 last PULL_BRANCHES;
584 # Now try items from the least cost branch based on the transport cost matrix or StaticHoldsQueueWeight
585 unless ( $itemnumber ) {
586 foreach my $current_item ( @{ $items_by_branch{$holdingbranch} } ) {
587 if ( $holdingbranch && ( $current_item->{holdallowed} == 2 || $request->{borrowerbranch} eq $current_item->{homebranch} ) ) {
589 # Don't fill item level holds that contravene the hold pickup policy at this time
590 next unless $current_item->{hold_fulfillment_policy} eq 'any'
591 || $request->{branchcode} eq $current_item->{ $current_item->{hold_fulfillment_policy} };
593 # If hold itemtype is set, item's itemtype must match
594 next unless ( !$request->{itemtype}
595 || $current_item->{itype} eq $request->{itemtype} );
597 $itemnumber = $current_item->{itemnumber};
598 last; # quit this loop as soon as we have a suitable item
603 # Now try for items for any item that can fill this hold
604 unless ( $itemnumber ) {
605 PULL_BRANCHES2:
606 foreach my $branch (@$pull_branches) {
607 my $holding_branch_items = $items_by_branch{$branch}
608 or next;
610 foreach my $item (@$holding_branch_items) {
611 next if ( $item->{holdallowed} == 1 && $item->{homebranch} ne $request->{borrowerbranch} );
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;
623 last PULL_BRANCHES2;
629 if ($itemnumber) {
630 my $holding_branch_items = $items_by_branch{$holdingbranch}
631 or die "Have $itemnumber, $holdingbranch, but no items!";
632 @$holding_branch_items = grep { $_->{itemnumber} != $itemnumber } @$holding_branch_items;
633 delete $items_by_branch{$holdingbranch} unless @$holding_branch_items;
635 $item_map{$itemnumber} = {
636 borrowernumber => $request->{borrowernumber},
637 biblionumber => $request->{biblionumber},
638 holdingbranch => $holdingbranch,
639 pickup_branch => $pickup_branch,
640 item_level => 0,
641 reservedate => $request->{reservedate},
642 reservenotes => $request->{reservenotes},
644 $num_items_remaining--;
647 return \%item_map;
650 =head2 CreatePickListFromItemMap
652 =cut
654 sub CreatePicklistFromItemMap {
655 my $item_map = shift;
657 my $dbh = C4::Context->dbh;
659 my $sth_load=$dbh->prepare("
660 INSERT INTO tmp_holdsqueue (biblionumber,itemnumber,barcode,surname,firstname,phone,borrowernumber,
661 cardnumber,reservedate,title, itemcallnumber,
662 holdingbranch,pickbranch,notes, item_level_request)
663 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
666 foreach my $itemnumber (sort keys %$item_map) {
667 my $mapped_item = $item_map->{$itemnumber};
668 my $biblionumber = $mapped_item->{biblionumber};
669 my $borrowernumber = $mapped_item->{borrowernumber};
670 my $pickbranch = $mapped_item->{pickup_branch};
671 my $holdingbranch = $mapped_item->{holdingbranch};
672 my $reservedate = $mapped_item->{reservedate};
673 my $reservenotes = $mapped_item->{reservenotes};
674 my $item_level = $mapped_item->{item_level};
676 my $item = GetItem($itemnumber);
677 my $barcode = $item->{barcode};
678 my $itemcallnumber = $item->{itemcallnumber};
680 my $borrower = GetMember('borrowernumber'=>$borrowernumber);
681 my $cardnumber = $borrower->{'cardnumber'};
682 my $surname = $borrower->{'surname'};
683 my $firstname = $borrower->{'firstname'};
684 my $phone = $borrower->{'phone'};
686 my $bib = GetBiblioData($biblionumber);
687 my $title = $bib->{title};
689 $sth_load->execute($biblionumber, $itemnumber, $barcode, $surname, $firstname, $phone, $borrowernumber,
690 $cardnumber, $reservedate, $title, $itemcallnumber,
691 $holdingbranch, $pickbranch, $reservenotes, $item_level);
695 =head2 AddToHoldTargetMap
697 =cut
699 sub AddToHoldTargetMap {
700 my $item_map = shift;
702 my $dbh = C4::Context->dbh;
704 my $insert_sql = q(
705 INSERT INTO hold_fill_targets (borrowernumber, biblionumber, itemnumber, source_branchcode, item_level_request)
706 VALUES (?, ?, ?, ?, ?)
708 my $sth_insert = $dbh->prepare($insert_sql);
710 foreach my $itemnumber (keys %$item_map) {
711 my $mapped_item = $item_map->{$itemnumber};
712 $sth_insert->execute($mapped_item->{borrowernumber}, $mapped_item->{biblionumber}, $itemnumber,
713 $mapped_item->{holdingbranch}, $mapped_item->{item_level});
717 # Helper functions, not part of any interface
719 sub _trim {
720 return $_[0] unless $_[0];
721 $_[0] =~ s/^\s+//;
722 $_[0] =~ s/\s+$//;
723 $_[0];
726 sub load_branches_to_pull_from {
727 my @branches_to_use;
729 my $static_branch_list = C4::Context->preference("StaticHoldsQueueWeight");
730 @branches_to_use = map { _trim($_) } split( /,/, $static_branch_list )
731 if $static_branch_list;
733 @branches_to_use =
734 Koha::Database->new()->schema()->resultset('Branch')
735 ->get_column('branchcode')->all()
736 unless (@branches_to_use);
738 @branches_to_use = shuffle(@branches_to_use)
739 if C4::Context->preference("RandomizeHoldsQueueWeight");
741 my $today = dt_from_string();
742 if ( C4::Context->preference('HoldsQueueSkipClosed') ) {
743 @branches_to_use = grep {
744 !Koha::Calendar->new( branchcode => $_ )
745 ->is_holiday( $today )
746 } @branches_to_use;
749 return \@branches_to_use;
752 sub least_cost_branch {
754 #$from - arrayref
755 my ($to, $from, $transport_cost_matrix) = @_;
757 # Nothing really spectacular: supply to branch, a list of potential from branches
758 # and find the minimum from - to value from the transport_cost_matrix
759 return $from->[0] if ( @$from == 1 && $transport_cost_matrix->{$to}{$from->[0]}->{disable_transfer} != 1 );
761 # If the pickup library is in the list of libraries to pull from,
762 # return that library right away, it is obviously the least costly
763 return ($to) if any { $_ eq $to } @$from;
765 my ($least_cost, @branch);
766 foreach (@$from) {
767 my $cell = $transport_cost_matrix->{$to}{$_};
768 next if $cell->{disable_transfer};
770 my $cost = $cell->{cost};
771 next unless defined $cost; # XXX should this be reported?
773 unless (defined $least_cost) {
774 $least_cost = $cost;
775 push @branch, $_;
776 next;
779 next if $cost > $least_cost;
781 if ($cost == $least_cost) {
782 push @branch, $_;
783 next;
786 @branch = ($_);
787 $least_cost = $cost;
790 return $branch[0];
792 # XXX return a random @branch with minimum cost instead of the first one;
793 # return $branch[0] if @branch == 1;