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
26 use C4::Circulation qw( GetBranchItemRule );
27 use Koha::DateUtils qw( dt_from_string );
32 use List::Util qw( shuffle );
33 use List::MoreUtils qw( any );
35 our (@ISA, @EXPORT_OK);
44 UpdateTransportCostMatrix
45 GetPendingHoldRequestsForBib
46 load_branches_to_pull_from
47 update_queue_for_biblio
54 =head2 TransportCostMatrix
56 TransportCostMatrix();
58 Returns Transport Cost Matrix as a hashref <to branch code> => <from branch code> => cost
62 sub TransportCostMatrix {
65 my $dbh = C4::Context->dbh;
66 my $transport_costs = $dbh->selectall_arrayref("SELECT * FROM transport_cost",{ Slice => {} });
68 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
81 return \%transport_cost_matrix;
84 =head2 UpdateTransportCostMatrix
86 UpdateTransportCostMatrix($records);
88 Updates full Transport Cost Matrix table. $records is an arrayref of records.
89 Records: { frombranch => <code>, tobranch => <code>, cost => <figure>, disable_transfer => <0,1> }
93 sub UpdateTransportCostMatrix {
95 my $dbh = C4::Context->dbh;
97 my $sth = $dbh->prepare("INSERT INTO transport_cost (frombranch, tobranch, cost, disable_transfer) VALUES (?, ?, ?, ?)");
99 $dbh->do("DELETE FROM transport_cost");
100 foreach (@$records) {
101 my $cost = $_->{cost};
102 my $from = $_->{frombranch};
103 my $to = $_->{tobranch};
104 if ($_->{disable_transfer}) {
107 elsif ( !defined ($cost) || ($cost !~ m/(0|[1-9][0-9]*)(\.[0-9]*)?/o) ) {
108 warn "Invalid $from -> $to cost $cost - must be a number >= 0, disabling";
110 $_->{disable_transfer} = 1;
112 $sth->execute( $from, $to, $cost, $_->{disable_transfer} ? 1 : 0 );
116 =head2 GetHoldsQueueItems
118 GetHoldsQueueItems({ branchlimit => $branch, itemtypeslimit => $itype, ccodeslimit => $ccode, locationslimit => $location );
120 Returns hold queue for a holding branch. If branch is omitted, then whole queue is returned
124 sub GetHoldsQueueItems {
126 my $dbh = C4::Context->dbh;
128 my @bind_params = ();
129 my $query = q/SELECT tmp_holdsqueue.*, biblio.author, items.ccode, items.itype, biblioitems.itemtype, items.location,
130 items.enumchron, items.cn_sort, biblioitems.publishercode,
131 biblio.copyrightdate, biblio.subtitle, biblio.medium,
132 biblio.part_number, biblio.part_name,
133 biblioitems.publicationyear, biblioitems.pages, biblioitems.size,
134 biblioitems.isbn, biblioitems.editionstatement, items.copynumber,
135 item_groups.item_group_id, item_groups.description AS item_group_description
137 JOIN biblio USING (biblionumber)
138 LEFT JOIN biblioitems USING (biblionumber)
139 LEFT JOIN items USING ( itemnumber)
140 LEFT JOIN item_group_items ON ( items.itemnumber = item_group_items.item_id )
141 LEFT JOIN item_groups ON ( item_group_items.item_group_id = item_groups.item_group_id )
144 if ($params->{branchlimit}) {
145 $query .="AND tmp_holdsqueue.holdingbranch = ? ";
146 push @bind_params, $params->{branchlimit};
148 if( $params->{itemtypeslimit} ) {
149 $query .=" AND items.itype = ? ";
150 push @bind_params, $params->{itemtypeslimit};
152 if( $params->{ccodeslimit} ) {
153 $query .=" AND items.ccode = ? ";
154 push @bind_params, $params->{ccodeslimit};
156 if( $params->{locationslimit} ) {
157 $query .=" AND items.location = ? ";
158 push @bind_params, $params->{locationslimit};
160 $query .= " ORDER BY ccode, location, cn_sort, author, title, pickbranch, reservedate";
161 my $sth = $dbh->prepare($query);
162 $sth->execute(@bind_params);
164 while ( my $row = $sth->fetchrow_hashref ){
165 # return the bib-level or item-level itype per syspref
166 if (!C4::Context->preference('item-level_itypes')) {
167 $row->{itype} = $row->{itemtype};
169 delete $row->{itemtype};
180 Top level function that turns reserves into tmp_holdsqueue and hold_fill_targets.
185 my $dbh = C4::Context->dbh;
187 $dbh->do("DELETE FROM tmp_holdsqueue"); # clear the old table for new info
188 $dbh->do("DELETE FROM hold_fill_targets");
191 my $total_requests = 0;
192 my $total_available_items = 0;
193 my $num_items_mapped = 0;
196 my $transport_cost_matrix;
197 my $use_transport_cost_matrix = C4::Context->preference("UseTransportCostMatrix");
198 if ($use_transport_cost_matrix) {
199 $transport_cost_matrix = TransportCostMatrix();
200 unless (keys %$transport_cost_matrix) {
201 warn "UseTransportCostMatrix set to yes, but matrix not populated";
202 undef $transport_cost_matrix;
206 $branches_to_use = load_branches_to_pull_from($use_transport_cost_matrix);
208 my $bibs_with_pending_requests = GetBibsWithPendingHoldRequests();
210 foreach my $biblionumber (@$bibs_with_pending_requests) {
214 my $result = update_queue_for_biblio(
215 { biblio_id => $biblionumber,
216 branches_to_use => $branches_to_use,
217 transport_cost_matrix => $transport_cost_matrix,
221 $total_requests += $result->{requests};
222 $total_available_items += $result->{available_items};
223 $num_items_mapped += $result->{mapped_items};
227 =head2 GetBibsWithPendingHoldRequests
229 my $biblionumber_aref = GetBibsWithPendingHoldRequests();
231 Return an arrayref of the biblionumbers of all bibs
232 that have one or more unfilled hold requests.
236 sub GetBibsWithPendingHoldRequests {
237 my $dbh = C4::Context->dbh;
239 my $bib_query = "SELECT DISTINCT biblionumber
243 AND reservedate <= CURRENT_DATE()
246 my $sth = $dbh->prepare($bib_query);
249 my $biblionumbers = $sth->fetchall_arrayref();
251 return [ map { $_->[0] } @$biblionumbers ];
254 =head2 GetPendingHoldRequestsForBib
256 my $requests = GetPendingHoldRequestsForBib($biblionumber);
258 Returns an arrayref of hashrefs to pending, unfilled hold requests
259 on the bib identified by $biblionumber. The following keys
260 are present in each hashref:
271 The arrayref is sorted in order of increasing priority.
275 sub GetPendingHoldRequestsForBib {
276 my $biblionumber = shift;
278 my $dbh = C4::Context->dbh;
280 my $request_query = "SELECT biblionumber, borrowernumber, itemnumber, priority, reserve_id, reserves.branchcode,
281 reservedate, reservenotes, borrowers.branchcode AS borrowerbranch, itemtype, item_level_hold, item_group_id
283 JOIN borrowers USING (borrowernumber)
284 WHERE biblionumber = ?
287 AND reservedate <= CURRENT_DATE()
290 my $sth = $dbh->prepare($request_query);
291 $sth->execute($biblionumber);
293 my $requests = $sth->fetchall_arrayref({});
298 =head2 GetItemsAvailableToFillHoldRequestsForBib
300 my $available_items = GetItemsAvailableToFillHoldRequestsForBib($biblionumber, $branches_ar);
302 Returns an arrayref of items available to fill hold requests
303 for the bib identified by C<$biblionumber>. An item is available
304 to fill a hold request if and only if:
307 * it is not withdrawn
308 * it is not marked notforloan
309 * it is not currently in transit
311 * it is not sitting on the hold shelf
312 * it is not damaged (unless AllowHoldsOnDamagedItems is on)
316 sub GetItemsAvailableToFillHoldRequestsForBib {
317 my ($biblionumber, $branches_to_use) = @_;
319 my $dbh = C4::Context->dbh;
320 my $items_query = "SELECT items.itemnumber, homebranch, holdingbranch, itemtypes.itemtype AS itype
323 if (C4::Context->preference('item-level_itypes')) {
324 $items_query .= "LEFT JOIN itemtypes ON (itemtypes.itemtype = items.itype) ";
326 $items_query .= "JOIN biblioitems USING (biblioitemnumber)
327 LEFT JOIN itemtypes USING (itemtype) ";
329 $items_query .= " LEFT JOIN branchtransfers ON (
330 items.itemnumber = branchtransfers.itemnumber
331 AND branchtransfers.datearrived IS NULL AND branchtransfers.datecancelled IS NULL
333 $items_query .= " WHERE items.notforloan = 0
334 AND holdingbranch IS NOT NULL
337 $items_query .= " AND damaged = 0" unless C4::Context->preference('AllowHoldsOnDamagedItems');
338 $items_query .= " AND items.onloan IS NULL
339 AND (itemtypes.notforloan IS NULL OR itemtypes.notforloan = 0)
340 AND items.itemnumber NOT IN (
343 WHERE biblionumber = ?
344 AND itemnumber IS NOT NULL
345 AND (found IS NOT NULL OR priority = 0)
347 AND items.biblionumber = ?
348 AND branchtransfers.itemnumber IS NULL";
350 my @params = ($biblionumber, $biblionumber);
351 if ($branches_to_use && @$branches_to_use) {
352 $items_query .= " AND holdingbranch IN (" . join (",", map { "?" } @$branches_to_use) . ")";
353 push @params, @$branches_to_use;
355 my $sth = $dbh->prepare($items_query);
356 $sth->execute(@params);
358 my $itm = $sth->fetchall_arrayref({});
360 my $rule = C4::Circulation::GetBranchItemRule($_->{homebranch}, $_->{itype});
361 $_->{holdallowed} = $rule->{holdallowed};
362 $_->{hold_fulfillment_policy} = $rule->{hold_fulfillment_policy};
366 =head2 _checkHoldPolicy
368 _checkHoldPolicy($item, $request)
370 check if item agrees with hold policies
374 sub _checkHoldPolicy {
375 my ( $item, $request ) = @_;
377 return 0 unless $item->{holdallowed} ne 'not_allowed';
380 if $item->{holdallowed} eq 'from_home_library'
381 && $item->{homebranch} ne $request->{borrowerbranch};
384 if $item->{'holdallowed'} eq 'from_local_hold_group'
385 && !Koha::Libraries->find( $item->{homebranch} )
386 ->validate_hold_sibling( { branchcode => $request->{borrowerbranch} } );
388 my $hold_fulfillment_policy = $item->{hold_fulfillment_policy};
391 if $hold_fulfillment_policy eq 'holdgroup'
392 && !Koha::Libraries->find( $item->{homebranch} )
393 ->validate_hold_sibling( { branchcode => $request->{branchcode} } );
396 if $hold_fulfillment_policy eq 'homebranch'
397 && $request->{branchcode} ne $item->{$hold_fulfillment_policy};
400 if $hold_fulfillment_policy eq 'holdingbranch'
401 && $request->{branchcode} ne $item->{$hold_fulfillment_policy};
404 if $hold_fulfillment_policy eq 'patrongroup'
405 && !Koha::Libraries->find( $request->{borrowerbranch} )
406 ->validate_hold_sibling( { branchcode => $request->{branchcode} } );
412 =head2 MapItemsToHoldRequests
414 MapItemsToHoldRequests($hold_requests, $available_items, $branches, $transport_cost_matrix)
418 sub MapItemsToHoldRequests {
419 my ($hold_requests, $available_items, $branches_to_use, $transport_cost_matrix) = @_;
421 # handle trival cases
422 return unless scalar(@$hold_requests) > 0;
423 return unless scalar(@$available_items) > 0;
425 # identify item-level requests
426 my %specific_items_requested = map { $_->{itemnumber} => 1 }
427 grep { defined($_->{itemnumber}) }
430 map { $_->{_object} = Koha::Items->find( $_->{itemnumber} ) } @$available_items;
432 map { $libraries->{$_->id} = $_ } Koha::Libraries->search->as_list;
434 # group available items by itemnumber
435 my %items_by_itemnumber = map { $_->{itemnumber} => $_ } @$available_items;
437 # items already allocated
438 my %allocated_items = ();
440 # map of items to hold requests
443 # figure out which item-level requests can be filled
444 my $num_items_remaining = scalar(@$available_items);
446 # Look for Local Holds Priority matches first
447 if ( C4::Context->preference('LocalHoldsPriority') ) {
448 my $LocalHoldsPriorityPatronControl =
449 C4::Context->preference('LocalHoldsPriorityPatronControl');
450 my $LocalHoldsPriorityItemControl =
451 C4::Context->preference('LocalHoldsPriorityItemControl');
453 foreach my $request (@$hold_requests) {
454 last if $num_items_remaining == 0;
455 my $patron = Koha::Patrons->find($request->{borrowernumber});
456 next if $patron->category->exclude_from_local_holds_priority;
458 my $local_hold_match;
459 foreach my $item (@$available_items) {
460 next if $item->{_object}->exclude_from_local_holds_priority;
462 next unless _checkHoldPolicy($item, $request);
464 next if $request->{itemnumber} && $request->{itemnumber} != $item->{itemnumber};
466 next if $request->{item_group_id} && $item->{_object}->item_group && $item->{_object}->item_group->id ne $request->{item_group_id};
468 next unless $item->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } );
470 my $local_holds_priority_item_branchcode =
471 $item->{$LocalHoldsPriorityItemControl};
473 my $local_holds_priority_patron_branchcode =
474 ( $LocalHoldsPriorityPatronControl eq 'PickupLibrary' )
475 ? $request->{branchcode}
476 : ( $LocalHoldsPriorityPatronControl eq 'HomeLibrary' )
477 ? $request->{borrowerbranch}
481 $local_holds_priority_item_branchcode eq
482 $local_holds_priority_patron_branchcode;
484 if ($local_hold_match) {
485 if ( exists $items_by_itemnumber{ $item->{itemnumber} }
486 and not exists $allocated_items{ $item->{itemnumber} }
487 and not $request->{allocated})
489 $item_map{ $item->{itemnumber} } = {
490 borrowernumber => $request->{borrowernumber},
491 biblionumber => $request->{biblionumber},
492 holdingbranch => $item->{holdingbranch},
493 pickup_branch => $request->{branchcode}
494 || $request->{borrowerbranch},
495 reserve_id => $request->{reserve_id},
496 item_level => $request->{item_level_hold},
497 reservedate => $request->{reservedate},
498 reservenotes => $request->{reservenotes},
500 $allocated_items{ $item->{itemnumber} }++;
501 $request->{allocated} = 1;
502 $num_items_remaining--;
509 foreach my $request (@$hold_requests) {
510 last if $num_items_remaining == 0;
511 next if $request->{allocated};
513 # is this an item-level request?
514 if (defined($request->{itemnumber})) {
515 # fill it if possible; if not skip it
517 exists $items_by_itemnumber{ $request->{itemnumber} }
518 and not exists $allocated_items{ $request->{itemnumber} }
519 and _checkHoldPolicy($items_by_itemnumber{ $request->{itemnumber} }, $request) # Don't fill item level holds that contravene the hold pickup policy at this time
520 and ( !$request->{itemtype} # If hold itemtype is set, item's itemtype must match
521 || $items_by_itemnumber{ $request->{itemnumber} }->{itype} eq $request->{itemtype} )
522 and ( !$request->{item_group_id} # If hold item_group is set, item's item_group must match
523 || ( $items_by_itemnumber{ $request->{itemnumber} }->{_object}->item_group
524 && $items_by_itemnumber{ $request->{itemnumber} }->{_object}->item_group->id eq $request->{item_group_id} ) )
525 and $items_by_itemnumber{ $request->{itemnumber} }->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } )
530 $item_map{ $request->{itemnumber} } = {
531 borrowernumber => $request->{borrowernumber},
532 biblionumber => $request->{biblionumber},
533 holdingbranch => $items_by_itemnumber{ $request->{itemnumber} }->{holdingbranch},
534 pickup_branch => $request->{branchcode} || $request->{borrowerbranch},
535 reserve_id => $request->{reserve_id},
536 item_level => $request->{item_level_hold},
537 reservedate => $request->{reservedate},
538 reservenotes => $request->{reservenotes},
540 $allocated_items{ $request->{itemnumber} }++;
541 $num_items_remaining--;
544 # it's title-level request that will take up one item
545 $num_items_remaining--;
549 # group available items by branch
550 my %items_by_branch = ();
551 foreach my $item (@$available_items) {
552 next unless $item->{holdallowed} ne 'not_allowed';
554 push @{ $items_by_branch{ $item->{holdingbranch} } }, $item
555 unless exists $allocated_items{ $item->{itemnumber} };
557 return \%item_map unless keys %items_by_branch;
559 # now handle the title-level requests
560 $num_items_remaining = scalar(@$available_items) - scalar(keys %allocated_items);
562 foreach my $request (@$hold_requests) {
563 last if $num_items_remaining == 0;
564 next if $request->{allocated};
565 next if defined($request->{itemnumber}); # already handled these
567 # look for local match first
568 my $pickup_branch = $request->{branchcode} || $request->{borrowerbranch};
569 my ($itemnumber, $holdingbranch);
571 my $holding_branch_items = $items_by_branch{$pickup_branch};
572 if ( $holding_branch_items ) {
573 foreach my $item (@$holding_branch_items) {
574 next unless $items_by_itemnumber{ $item->{itemnumber} }->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } );
577 $request->{borrowerbranch} eq $item->{homebranch}
578 && _checkHoldPolicy($item, $request) # Don't fill item level holds that contravene the hold pickup policy at this time
579 && ( !$request->{itemtype} # If hold itemtype is set, item's itemtype must match
580 || ( $request->{itemnumber} && ( $items_by_itemnumber{ $request->{itemnumber} }->{itype} eq $request->{itemtype} ) ) )
581 && ( !$request->{item_group_id} # If hold item_group is set, item's item_group must match
582 || ( $item->{_object}->item_group && $item->{_object}->item_group->id eq $request->{item_group_id} ) )
585 $itemnumber = $item->{itemnumber};
589 $holdingbranch = $pickup_branch;
591 elsif ($transport_cost_matrix) {
592 $pull_branches = [keys %items_by_branch];
593 $holdingbranch = least_cost_branch( $pickup_branch, $pull_branches, $transport_cost_matrix );
594 if ( $holdingbranch ) {
596 my $holding_branch_items = $items_by_branch{$holdingbranch};
597 foreach my $item (@$holding_branch_items) {
598 next if $request->{borrowerbranch} ne $item->{homebranch};
599 next unless $items_by_itemnumber{ $item->{itemnumber} }->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } );
601 # Don't fill item level holds that contravene the hold pickup policy at this time
602 next unless _checkHoldPolicy($item, $request);
604 # If hold itemtype is set, item's itemtype must match
605 next unless ( !$request->{itemtype}
606 || $item->{itype} eq $request->{itemtype} );
608 # If hold item_group is set, item's item_group must match
610 !$request->{item_group_id}
611 || ( $item->{_object}->item_group
612 && $item->{_object}->item_group->id eq $request->{item_group_id} )
615 $itemnumber = $item->{itemnumber};
624 unless ($itemnumber) {
625 # not found yet, fall back to basics
626 if ($branches_to_use) {
627 $pull_branches = $branches_to_use;
629 $pull_branches = [keys %items_by_branch];
632 # Try picking items where the home and pickup branch match first
634 foreach my $branch (@$pull_branches) {
635 my $holding_branch_items = $items_by_branch{$branch}
638 $holdingbranch ||= $branch;
639 foreach my $item (@$holding_branch_items) {
640 next if $pickup_branch ne $item->{homebranch};
641 next unless _checkHoldPolicy($item, $request);
642 next unless $items_by_itemnumber{ $item->{itemnumber} }->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } );
644 # If hold itemtype is set, item's itemtype must match
645 next unless ( !$request->{itemtype}
646 || $item->{itype} eq $request->{itemtype} );
648 # If hold item_group is set, item's item_group must match
650 !$request->{item_group_id}
651 || ( $item->{_object}->item_group
652 && $item->{_object}->item_group->id eq $request->{item_group_id} )
655 $itemnumber = $item->{itemnumber};
656 $holdingbranch = $branch;
661 # Now try items from the least cost branch based on the transport cost matrix or StaticHoldsQueueWeight
662 unless ( $itemnumber || !$holdingbranch) {
663 foreach my $current_item ( @{ $items_by_branch{$holdingbranch} } ) {
664 next unless _checkHoldPolicy($current_item, $request); # Don't fill item level holds that contravene the hold pickup policy at this time
666 # If hold itemtype is set, item's itemtype must match
667 next unless ( !$request->{itemtype}
668 || $current_item->{itype} eq $request->{itemtype} );
670 next unless $items_by_itemnumber{ $current_item->{itemnumber} }->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } );
672 # If hold item_group is set, item's item_group must match
674 !$request->{item_group_id}
675 || ( $current_item->{_object}->item_group
676 && $current_item->{_object}->item_group->id eq $request->{item_group_id} )
680 $itemnumber = $current_item->{itemnumber};
681 last; # quit this loop as soon as we have a suitable item
685 # Now try for items for any item that can fill this hold
686 unless ( $itemnumber ) {
688 foreach my $branch (@$pull_branches) {
689 my $holding_branch_items = $items_by_branch{$branch}
692 foreach my $item (@$holding_branch_items) {
693 # Don't fill item level holds that contravene the hold pickup policy at this time
694 next unless _checkHoldPolicy($item, $request);
696 # If hold itemtype is set, item's itemtype must match
697 next unless ( !$request->{itemtype}
698 || $item->{itype} eq $request->{itemtype} );
700 # If hold item_group is set, item's item_group must match
702 !$request->{item_group_id}
703 || ( $item->{_object}->item_group
704 && $item->{_object}->item_group->id eq $request->{item_group_id} )
707 next unless $items_by_itemnumber{ $item->{itemnumber} }->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } );
709 $itemnumber = $item->{itemnumber};
710 $holdingbranch = $branch;
718 my $holding_branch_items = $items_by_branch{$holdingbranch}
719 or die "Have $itemnumber, $holdingbranch, but no items!";
720 @$holding_branch_items = grep { $_->{itemnumber} != $itemnumber } @$holding_branch_items;
721 delete $items_by_branch{$holdingbranch} unless @$holding_branch_items;
723 $item_map{$itemnumber} = {
724 borrowernumber => $request->{borrowernumber},
725 biblionumber => $request->{biblionumber},
726 holdingbranch => $holdingbranch,
727 pickup_branch => $pickup_branch,
728 reserve_id => $request->{reserve_id},
729 item_level => $request->{item_level_hold},
730 reservedate => $request->{reservedate},
731 reservenotes => $request->{reservenotes},
733 $num_items_remaining--;
739 =head2 CreatePickListFromItemMap
743 sub CreatePicklistFromItemMap {
744 my $item_map = shift;
746 my $dbh = C4::Context->dbh;
748 my $sth_load=$dbh->prepare("
749 INSERT INTO tmp_holdsqueue (biblionumber,itemnumber,barcode,surname,firstname,phone,borrowernumber,
750 cardnumber,reservedate,title, itemcallnumber,
751 holdingbranch,pickbranch,notes, item_level_request)
752 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
755 foreach my $itemnumber (sort keys %$item_map) {
756 my $mapped_item = $item_map->{$itemnumber};
757 my $biblionumber = $mapped_item->{biblionumber};
758 my $borrowernumber = $mapped_item->{borrowernumber};
759 my $pickbranch = $mapped_item->{pickup_branch};
760 my $holdingbranch = $mapped_item->{holdingbranch};
761 my $reservedate = $mapped_item->{reservedate};
762 my $reservenotes = $mapped_item->{reservenotes};
763 my $item_level = $mapped_item->{item_level};
765 my $item = Koha::Items->find($itemnumber);
766 my $barcode = $item->barcode;
767 my $itemcallnumber = $item->itemcallnumber;
769 my $patron = Koha::Patrons->find( $borrowernumber );
770 my $cardnumber = $patron->cardnumber;
771 my $surname = $patron->surname;
772 my $firstname = $patron->firstname;
773 my $phone = $patron->phone;
775 my $biblio = Koha::Biblios->find( $biblionumber );
776 my $title = $biblio->title;
778 $sth_load->execute($biblionumber, $itemnumber, $barcode, $surname, $firstname, $phone, $borrowernumber,
779 $cardnumber, $reservedate, $title, $itemcallnumber,
780 $holdingbranch, $pickbranch, $reservenotes, $item_level);
784 =head2 AddToHoldTargetMap
788 sub AddToHoldTargetMap {
789 my $item_map = shift;
791 my $dbh = C4::Context->dbh;
794 INSERT INTO hold_fill_targets (borrowernumber, biblionumber, itemnumber, source_branchcode, item_level_request, reserve_id)
795 VALUES (?, ?, ?, ?, ?, ?)
797 my $sth_insert = $dbh->prepare($insert_sql);
799 foreach my $itemnumber (keys %$item_map) {
800 my $mapped_item = $item_map->{$itemnumber};
801 $sth_insert->execute($mapped_item->{borrowernumber}, $mapped_item->{biblionumber}, $itemnumber,
802 $mapped_item->{holdingbranch}, $mapped_item->{item_level}, $mapped_item->{reserve_id});
806 # Helper functions, not part of any interface
809 return $_[0] unless $_[0];
815 sub load_branches_to_pull_from {
816 my $use_transport_cost_matrix = shift;
820 unless ( $use_transport_cost_matrix ) {
821 my $static_branch_list = C4::Context->preference("StaticHoldsQueueWeight");
822 @branches_to_use = map { _trim($_) } split( /,/, $static_branch_list )
823 if $static_branch_list;
827 Koha::Database->new()->schema()->resultset('Branch')
828 ->get_column('branchcode')->all()
829 unless (@branches_to_use);
831 @branches_to_use = shuffle(@branches_to_use)
832 if C4::Context->preference("RandomizeHoldsQueueWeight");
834 my $today = dt_from_string();
835 if ( C4::Context->preference('HoldsQueueSkipClosed') ) {
836 @branches_to_use = grep {
837 !Koha::Calendar->new( branchcode => $_ )
838 ->is_holiday( $today )
842 return \@branches_to_use;
845 sub least_cost_branch {
848 my ($to, $from, $transport_cost_matrix) = @_;
850 # Nothing really spectacular: supply to branch, a list of potential from branches
851 # and find the minimum from - to value from the transport_cost_matrix
852 return $from->[0] if ( @$from == 1 && $transport_cost_matrix->{$to}{$from->[0]}->{disable_transfer} != 1 );
854 # If the pickup library is in the list of libraries to pull from,
855 # return that library right away, it is obviously the least costly
856 return ($to) if any { $_ eq $to } @$from;
858 my ($least_cost, @branch);
860 my $cell = $transport_cost_matrix->{$to}{$_};
861 next if $cell->{disable_transfer};
863 my $cost = $cell->{cost};
864 next unless defined $cost; # XXX should this be reported?
866 unless (defined $least_cost) {
872 next if $cost > $least_cost;
874 if ($cost == $least_cost) {
885 # XXX return a random @branch with minimum cost instead of the first one;
886 # return $branch[0] if @branch == 1;
889 =head3 update_queue_for_biblio
891 my $result = update_queue_for_biblio(
893 biblio_id => $biblio_id,
894 [ branches_to_use => $branches_to_use,
895 transport_cost_matrix => $transport_cost_matrix,
900 Given a I<biblio_id>, this method calculates and sets the holds queue entries
901 for the biblio's holds, and the hold fill targets (items).
905 It return a hashref containing:
909 =item I<requests>: the pending holds count for the biblio.
911 =item I<available_items> the count of items that are available to fill holds for the biblio.
913 =item I<mapped_items> the total items that got mapped.
917 =head4 Optional parameters
921 =item I<branches_to_use> a list of branchcodes to be used to restrict which items can be used.
923 =item I<transport_cost_matrix> is the output of C<TransportCostMatrix>.
925 =item I<delete> tells the method to delete prior entries on the related tables for the biblio_id.
929 Note: All the optional parameters will be calculated in the method if omitted. They
930 are allowed to be passed to avoid calculating them many times inside loops.
934 sub update_queue_for_biblio {
936 my $biblio_id = $args->{biblio_id};
939 # We need to empty the queue for this biblio unless CreateQueue has emptied the entire queue for rebuilding
940 if ( $args->{delete} ) {
941 my $dbh = C4::Context->dbh;
943 $dbh->do("DELETE FROM tmp_holdsqueue WHERE biblionumber=$biblio_id");
944 $dbh->do("DELETE FROM hold_fill_targets WHERE biblionumber=$biblio_id");
947 my $hold_requests = GetPendingHoldRequestsForBib($biblio_id);
948 $result->{requests} = scalar( @{$hold_requests} );
949 # No need to check anything else if there are no holds to fill
950 return $result unless $result->{requests};
952 my $branches_to_use = $args->{branches_to_use} // load_branches_to_pull_from( C4::Context->preference('UseTransportCostMatrix') );
953 my $transport_cost_matrix;
955 if ( !exists $args->{transport_cost_matrix}
956 && C4::Context->preference('UseTransportCostMatrix') ) {
957 $transport_cost_matrix = TransportCostMatrix();
959 $transport_cost_matrix = $args->{transport_cost_matrix};
962 my $available_items = GetItemsAvailableToFillHoldRequestsForBib( $biblio_id, $branches_to_use );
964 $result->{available_items} = scalar( @{$available_items} );
966 my $item_map = MapItemsToHoldRequests( $hold_requests, $available_items, $branches_to_use, $transport_cost_matrix );
967 $result->{mapped_items} = scalar( keys %{$item_map} );
970 CreatePicklistFromItemMap($item_map);
971 AddToHoldTargetMap($item_map);