Bug 22071: (follow-up) Add POD for validate_query_parameters
[koha.git] / C4 / HoldsQueue.pm
blobce525ba64b6d6ade04ff4323769831806e6af633
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;
32 use Koha::Patrons;
34 use List::Util qw(shuffle);
35 use List::MoreUtils qw(any);
36 use Data::Dumper;
38 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
39 BEGIN {
40 require Exporter;
41 @ISA = qw(Exporter);
42 @EXPORT_OK = qw(
43 &CreateQueue
44 &GetHoldsQueueItems
46 &TransportCostMatrix
47 &UpdateTransportCostMatrix
52 =head1 FUNCTIONS
54 =head2 TransportCostMatrix
56 TransportCostMatrix();
58 Returns Transport Cost Matrix as a hashref <to branch code> => <from branch code> => cost
60 =cut
62 sub TransportCostMatrix {
63 my $dbh = C4::Context->dbh;
64 my $transport_costs = $dbh->selectall_arrayref("SELECT * FROM transport_cost",{ Slice => {} });
66 my $today = dt_from_string();
67 my $calendars;
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} = {
75 cost => $cost,
76 disable_transfer => $disabled
79 if ( C4::Context->preference("HoldsQueueSkipClosed") ) {
80 $calendars->{$from} ||= Koha::Calendar->new( branchcode => $from );
81 $transport_cost_matrix{$to}{$from}{disable_transfer} ||=
82 $calendars->{$from}->is_holiday( $today );
86 return \%transport_cost_matrix;
89 =head2 UpdateTransportCostMatrix
91 UpdateTransportCostMatrix($records);
93 Updates full Transport Cost Matrix table. $records is an arrayref of records.
94 Records: { frombranch => <code>, tobranch => <code>, cost => <figure>, disable_transfer => <0,1> }
96 =cut
98 sub UpdateTransportCostMatrix {
99 my ($records) = @_;
100 my $dbh = C4::Context->dbh;
102 my $sth = $dbh->prepare("INSERT INTO transport_cost (frombranch, tobranch, cost, disable_transfer) VALUES (?, ?, ?, ?)");
104 $dbh->do("DELETE FROM transport_cost");
105 foreach (@$records) {
106 my $cost = $_->{cost};
107 my $from = $_->{frombranch};
108 my $to = $_->{tobranch};
109 if ($_->{disable_transfer}) {
110 $cost ||= 0;
112 elsif ( !defined ($cost) || ($cost !~ m/(0|[1-9][0-9]*)(\.[0-9]*)?/o) ) {
113 warn "Invalid $from -> $to cost $cost - must be a number >= 0, disabling";
114 $cost = 0;
115 $_->{disable_transfer} = 1;
117 $sth->execute( $from, $to, $cost, $_->{disable_transfer} ? 1 : 0 );
121 =head2 GetHoldsQueueItems
123 GetHoldsQueueItems($branch);
125 Returns hold queue for a holding branch. If branch is omitted, then whole queue is returned
127 =cut
129 sub GetHoldsQueueItems {
130 my ($branchlimit) = @_;
131 my $dbh = C4::Context->dbh;
133 my @bind_params = ();
134 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
135 FROM tmp_holdsqueue
136 JOIN biblio USING (biblionumber)
137 LEFT JOIN biblioitems USING (biblionumber)
138 LEFT JOIN items USING ( itemnumber)
140 if ($branchlimit) {
141 $query .=" WHERE tmp_holdsqueue.holdingbranch = ?";
142 push @bind_params, $branchlimit;
144 $query .= " ORDER BY ccode, location, cn_sort, author, title, pickbranch, reservedate";
145 my $sth = $dbh->prepare($query);
146 $sth->execute(@bind_params);
147 my $items = [];
148 while ( my $row = $sth->fetchrow_hashref ){
149 my $record = GetMarcBiblio({ biblionumber => $row->{biblionumber} });
150 if ($record){
151 $row->{subtitle} = [ map { $_->{subfield} } @{ GetRecordValue( 'subtitle', $record, '' ) } ];
152 $row->{parts} = GetRecordValue('parts',$record,'')->[0]->{subfield};
153 $row->{numbers} = GetRecordValue('numbers',$record,'')->[0]->{subfield};
156 # return the bib-level or item-level itype per syspref
157 if (!C4::Context->preference('item-level_itypes')) {
158 $row->{itype} = $row->{itemtype};
160 delete $row->{itemtype};
162 push @$items, $row;
164 return $items;
167 =head2 CreateQueue
169 CreateQueue();
171 Top level function that turns reserves into tmp_holdsqueue and hold_fill_targets.
173 =cut
175 sub CreateQueue {
176 my $dbh = C4::Context->dbh;
178 $dbh->do("DELETE FROM tmp_holdsqueue"); # clear the old table for new info
179 $dbh->do("DELETE FROM hold_fill_targets");
181 my $total_bibs = 0;
182 my $total_requests = 0;
183 my $total_available_items = 0;
184 my $num_items_mapped = 0;
186 my $branches_to_use;
187 my $transport_cost_matrix;
188 my $use_transport_cost_matrix = C4::Context->preference("UseTransportCostMatrix");
189 if ($use_transport_cost_matrix) {
190 $transport_cost_matrix = TransportCostMatrix();
191 unless (keys %$transport_cost_matrix) {
192 warn "UseTransportCostMatrix set to yes, but matrix not populated";
193 undef $transport_cost_matrix;
196 unless ($transport_cost_matrix) {
197 $branches_to_use = load_branches_to_pull_from();
200 my $bibs_with_pending_requests = GetBibsWithPendingHoldRequests();
202 foreach my $biblionumber (@$bibs_with_pending_requests) {
203 $total_bibs++;
204 my $hold_requests = GetPendingHoldRequestsForBib($biblionumber);
205 my $available_items = GetItemsAvailableToFillHoldRequestsForBib($biblionumber, $branches_to_use);
206 $total_requests += scalar(@$hold_requests);
207 $total_available_items += scalar(@$available_items);
209 my $item_map = MapItemsToHoldRequests($hold_requests, $available_items, $branches_to_use, $transport_cost_matrix);
210 $item_map or next;
211 my $item_map_size = scalar(keys %$item_map)
212 or next;
214 $num_items_mapped += $item_map_size;
215 CreatePicklistFromItemMap($item_map);
216 AddToHoldTargetMap($item_map);
217 if (($item_map_size < scalar(@$hold_requests )) and
218 ($item_map_size < scalar(@$available_items))) {
219 # DOUBLE CHECK, but this is probably OK - unfilled item-level requests
220 # FIXME
221 #warn "unfilled requests for $biblionumber";
222 #warn Dumper($hold_requests), Dumper($available_items), Dumper($item_map);
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.
234 =cut
236 sub GetBibsWithPendingHoldRequests {
237 my $dbh = C4::Context->dbh;
239 my $bib_query = "SELECT DISTINCT biblionumber
240 FROM reserves
241 WHERE found IS NULL
242 AND priority > 0
243 AND reservedate <= CURRENT_DATE()
244 AND suspend = 0
246 my $sth = $dbh->prepare($bib_query);
248 $sth->execute();
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:
262 biblionumber
263 borrowernumber
264 itemnumber
265 priority
266 branchcode
267 reservedate
268 reservenotes
269 borrowerbranch
271 The arrayref is sorted in order of increasing priority.
273 =cut
275 sub GetPendingHoldRequestsForBib {
276 my $biblionumber = shift;
278 my $dbh = C4::Context->dbh;
280 my $request_query = "SELECT biblionumber, borrowernumber, itemnumber, priority, reserves.branchcode,
281 reservedate, reservenotes, borrowers.branchcode AS borrowerbranch, itemtype
282 FROM reserves
283 JOIN borrowers USING (borrowernumber)
284 WHERE biblionumber = ?
285 AND found IS NULL
286 AND priority > 0
287 AND reservedate <= CURRENT_DATE()
288 AND suspend = 0
289 ORDER BY priority";
290 my $sth = $dbh->prepare($request_query);
291 $sth->execute($biblionumber);
293 my $requests = $sth->fetchall_arrayref({});
294 return $requests;
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:
306 * it is not on loan
307 * it is not withdrawn
308 * it is not marked notforloan
309 * it is not currently in transit
310 * it is not lost
311 * it is not sitting on the hold shelf
312 * it is not damaged (unless AllowHoldsOnDamagedItems is on)
314 =cut
316 sub GetItemsAvailableToFillHoldRequestsForBib {
317 my ($biblionumber, $branches_to_use) = @_;
319 my $dbh = C4::Context->dbh;
320 my $items_query = "SELECT itemnumber, homebranch, holdingbranch, itemtypes.itemtype AS itype
321 FROM items ";
323 if (C4::Context->preference('item-level_itypes')) {
324 $items_query .= "LEFT JOIN itemtypes ON (itemtypes.itemtype = items.itype) ";
325 } else {
326 $items_query .= "JOIN biblioitems USING (biblioitemnumber)
327 LEFT JOIN itemtypes USING (itemtype) ";
329 $items_query .= "WHERE items.notforloan = 0
330 AND holdingbranch IS NOT NULL
331 AND itemlost = 0
332 AND withdrawn = 0";
333 $items_query .= " AND damaged = 0" unless C4::Context->preference('AllowHoldsOnDamagedItems');
334 $items_query .= " AND items.onloan IS NULL
335 AND (itemtypes.notforloan IS NULL OR itemtypes.notforloan = 0)
336 AND itemnumber NOT IN (
337 SELECT itemnumber
338 FROM reserves
339 WHERE biblionumber = ?
340 AND itemnumber IS NOT NULL
341 AND (found IS NOT NULL OR priority = 0)
343 AND items.biblionumber = ?";
345 my @params = ($biblionumber, $biblionumber);
346 if ($branches_to_use && @$branches_to_use) {
347 $items_query .= " AND holdingbranch IN (" . join (",", map { "?" } @$branches_to_use) . ")";
348 push @params, @$branches_to_use;
350 my $sth = $dbh->prepare($items_query);
351 $sth->execute(@params);
353 my $itm = $sth->fetchall_arrayref({});
354 my @items = grep { ! scalar GetTransfers($_->{itemnumber}) } @$itm;
355 return [ grep {
356 my $rule = GetBranchItemRule($_->{homebranch}, $_->{itype});
357 $_->{holdallowed} = $rule->{holdallowed};
358 $_->{hold_fulfillment_policy} = $rule->{hold_fulfillment_policy};
359 } @items ];
362 =head2 MapItemsToHoldRequests
364 MapItemsToHoldRequests($hold_requests, $available_items, $branches, $transport_cost_matrix)
366 =cut
368 sub MapItemsToHoldRequests {
369 my ($hold_requests, $available_items, $branches_to_use, $transport_cost_matrix) = @_;
372 # handle trival cases
373 return unless scalar(@$hold_requests) > 0;
374 return unless scalar(@$available_items) > 0;
376 # identify item-level requests
377 my %specific_items_requested = map { $_->{itemnumber} => 1 }
378 grep { defined($_->{itemnumber}) }
379 @$hold_requests;
381 # group available items by itemnumber
382 my %items_by_itemnumber = map { $_->{itemnumber} => $_ } @$available_items;
384 # items already allocated
385 my %allocated_items = ();
387 # map of items to hold requests
388 my %item_map = ();
390 # figure out which item-level requests can be filled
391 my $num_items_remaining = scalar(@$available_items);
393 # Look for Local Holds Priority matches first
394 if ( C4::Context->preference('LocalHoldsPriority') ) {
395 my $LocalHoldsPriorityPatronControl =
396 C4::Context->preference('LocalHoldsPriorityPatronControl');
397 my $LocalHoldsPriorityItemControl =
398 C4::Context->preference('LocalHoldsPriorityItemControl');
400 foreach my $request (@$hold_requests) {
401 next if (defined($request->{itemnumber})); #skip item level holds in local priority checking
402 last if $num_items_remaining == 0;
404 my $local_hold_match;
405 foreach my $item (@$available_items) {
406 next
407 if ( !$item->{holdallowed} )
408 || ( $item->{holdallowed} == 1
409 && $item->{homebranch} ne $request->{borrowerbranch} );
411 my $local_holds_priority_item_branchcode =
412 $item->{$LocalHoldsPriorityItemControl};
414 my $local_holds_priority_patron_branchcode =
415 ( $LocalHoldsPriorityPatronControl eq 'PickupLibrary' )
416 ? $request->{branchcode}
417 : ( $LocalHoldsPriorityPatronControl eq 'HomeLibrary' )
418 ? $request->{borrowerbranch}
419 : undef;
421 $local_hold_match =
422 $local_holds_priority_item_branchcode eq
423 $local_holds_priority_patron_branchcode;
425 if ($local_hold_match) {
426 if ( exists $items_by_itemnumber{ $item->{itemnumber} }
427 and not exists $allocated_items{ $item->{itemnumber} }
428 and not $request->{allocated})
430 $item_map{ $item->{itemnumber} } = {
431 borrowernumber => $request->{borrowernumber},
432 biblionumber => $request->{biblionumber},
433 holdingbranch => $item->{holdingbranch},
434 pickup_branch => $request->{branchcode}
435 || $request->{borrowerbranch},
436 item_level => 0,
437 reservedate => $request->{reservedate},
438 reservenotes => $request->{reservenotes},
440 $allocated_items{ $item->{itemnumber} }++;
441 $request->{allocated} = 1;
442 $num_items_remaining--;
449 foreach my $request (@$hold_requests) {
450 last if $num_items_remaining == 0;
451 next if $request->{allocated};
453 # is this an item-level request?
454 if (defined($request->{itemnumber})) {
455 # fill it if possible; if not skip it
456 if (
457 exists $items_by_itemnumber{ $request->{itemnumber} }
458 and not exists $allocated_items{ $request->{itemnumber} }
459 and ( # Don't fill item level holds that contravene the hold pickup policy at this time
460 ( $items_by_itemnumber{ $request->{itemnumber} }->{hold_fulfillment_policy} eq 'any' )
461 || ( $request->{branchcode} eq $items_by_itemnumber{ $request->{itemnumber} }->{ $items_by_itemnumber{ $request->{itemnumber} }->{hold_fulfillment_policy} } )
462 and ( !$request->{itemtype} # If hold itemtype is set, item's itemtype must match
463 || $items_by_itemnumber{ $request->{itemnumber} }->{itype} eq $request->{itemtype} )
469 $item_map{ $request->{itemnumber} } = {
470 borrowernumber => $request->{borrowernumber},
471 biblionumber => $request->{biblionumber},
472 holdingbranch => $items_by_itemnumber{ $request->{itemnumber} }->{holdingbranch},
473 pickup_branch => $request->{branchcode} || $request->{borrowerbranch},
474 item_level => 1,
475 reservedate => $request->{reservedate},
476 reservenotes => $request->{reservenotes},
478 $allocated_items{ $request->{itemnumber} }++;
479 $num_items_remaining--;
481 } else {
482 # it's title-level request that will take up one item
483 $num_items_remaining--;
487 # group available items by branch
488 my %items_by_branch = ();
489 foreach my $item (@$available_items) {
490 next unless $item->{holdallowed};
492 push @{ $items_by_branch{ $item->{holdingbranch} } }, $item
493 unless exists $allocated_items{ $item->{itemnumber} };
495 return \%item_map unless keys %items_by_branch;
497 # now handle the title-level requests
498 $num_items_remaining = scalar(@$available_items) - scalar(keys %allocated_items);
499 my $pull_branches;
500 foreach my $request (@$hold_requests) {
501 last if $num_items_remaining == 0;
502 next if $request->{allocated};
503 next if defined($request->{itemnumber}); # already handled these
505 # look for local match first
506 my $pickup_branch = $request->{branchcode} || $request->{borrowerbranch};
507 my ($itemnumber, $holdingbranch);
509 my $holding_branch_items = $items_by_branch{$pickup_branch};
510 if ( $holding_branch_items ) {
511 foreach my $item (@$holding_branch_items) {
512 if (
513 $request->{borrowerbranch} eq $item->{homebranch}
514 && ( ( $item->{hold_fulfillment_policy} eq 'any' ) # Don't fill item level holds that contravene the hold pickup policy at this time
515 || $request->{branchcode} eq $item->{ $item->{hold_fulfillment_policy} } )
516 && ( !$request->{itemtype} # If hold itemtype is set, item's itemtype must match
517 || $items_by_itemnumber{ $request->{itemnumber} }->{itype} eq $request->{itemtype} )
520 $itemnumber = $item->{itemnumber};
521 last;
524 $holdingbranch = $pickup_branch;
526 elsif ($transport_cost_matrix) {
527 $pull_branches = [keys %items_by_branch];
528 $holdingbranch = least_cost_branch( $pickup_branch, $pull_branches, $transport_cost_matrix );
529 if ( $holdingbranch ) {
531 my $holding_branch_items = $items_by_branch{$holdingbranch};
532 foreach my $item (@$holding_branch_items) {
533 next if $request->{borrowerbranch} ne $item->{homebranch};
535 # Don't fill item level holds that contravene the hold pickup policy at this time
536 next unless $item->{hold_fulfillment_policy} eq 'any'
537 || $request->{branchcode} eq $item->{ $item->{hold_fulfillment_policy} };
539 # If hold itemtype is set, item's itemtype must match
540 next unless ( !$request->{itemtype}
541 || $item->{itype} eq $request->{itemtype} );
543 $itemnumber = $item->{itemnumber};
544 last;
547 else {
548 next;
552 unless ($itemnumber) {
553 # not found yet, fall back to basics
554 if ($branches_to_use) {
555 $pull_branches = $branches_to_use;
556 } else {
557 $pull_branches = [keys %items_by_branch];
560 # Try picking items where the home and pickup branch match first
561 PULL_BRANCHES:
562 foreach my $branch (@$pull_branches) {
563 my $holding_branch_items = $items_by_branch{$branch}
564 or next;
566 $holdingbranch ||= $branch;
567 foreach my $item (@$holding_branch_items) {
568 next if $pickup_branch ne $item->{homebranch};
569 next if ( $item->{holdallowed} == 1 && $item->{homebranch} ne $request->{borrowerbranch} );
571 # Don't fill item level holds that contravene the hold pickup policy at this time
572 next unless $item->{hold_fulfillment_policy} eq 'any'
573 || $request->{branchcode} eq $item->{ $item->{hold_fulfillment_policy} };
575 # If hold itemtype is set, item's itemtype must match
576 next unless ( !$request->{itemtype}
577 || $item->{itype} eq $request->{itemtype} );
579 $itemnumber = $item->{itemnumber};
580 $holdingbranch = $branch;
581 last PULL_BRANCHES;
585 # Now try items from the least cost branch based on the transport cost matrix or StaticHoldsQueueWeight
586 unless ( $itemnumber ) {
587 foreach my $current_item ( @{ $items_by_branch{$holdingbranch} } ) {
588 if ( $holdingbranch && ( $current_item->{holdallowed} == 2 || $request->{borrowerbranch} eq $current_item->{homebranch} ) ) {
590 # Don't fill item level holds that contravene the hold pickup policy at this time
591 next unless $current_item->{hold_fulfillment_policy} eq 'any'
592 || $request->{branchcode} eq $current_item->{ $current_item->{hold_fulfillment_policy} };
594 # If hold itemtype is set, item's itemtype must match
595 next unless ( !$request->{itemtype}
596 || $current_item->{itype} eq $request->{itemtype} );
598 $itemnumber = $current_item->{itemnumber};
599 last; # quit this loop as soon as we have a suitable item
604 # Now try for items for any item that can fill this hold
605 unless ( $itemnumber ) {
606 PULL_BRANCHES2:
607 foreach my $branch (@$pull_branches) {
608 my $holding_branch_items = $items_by_branch{$branch}
609 or next;
611 foreach my $item (@$holding_branch_items) {
612 next if ( $item->{holdallowed} == 1 && $item->{homebranch} ne $request->{borrowerbranch} );
614 # Don't fill item level holds that contravene the hold pickup policy at this time
615 next unless $item->{hold_fulfillment_policy} eq 'any'
616 || $request->{branchcode} eq $item->{ $item->{hold_fulfillment_policy} };
618 # If hold itemtype is set, item's itemtype must match
619 next unless ( !$request->{itemtype}
620 || $item->{itype} eq $request->{itemtype} );
622 $itemnumber = $item->{itemnumber};
623 $holdingbranch = $branch;
624 last PULL_BRANCHES2;
630 if ($itemnumber) {
631 my $holding_branch_items = $items_by_branch{$holdingbranch}
632 or die "Have $itemnumber, $holdingbranch, but no items!";
633 @$holding_branch_items = grep { $_->{itemnumber} != $itemnumber } @$holding_branch_items;
634 delete $items_by_branch{$holdingbranch} unless @$holding_branch_items;
636 $item_map{$itemnumber} = {
637 borrowernumber => $request->{borrowernumber},
638 biblionumber => $request->{biblionumber},
639 holdingbranch => $holdingbranch,
640 pickup_branch => $pickup_branch,
641 item_level => 0,
642 reservedate => $request->{reservedate},
643 reservenotes => $request->{reservenotes},
645 $num_items_remaining--;
648 return \%item_map;
651 =head2 CreatePickListFromItemMap
653 =cut
655 sub CreatePicklistFromItemMap {
656 my $item_map = shift;
658 my $dbh = C4::Context->dbh;
660 my $sth_load=$dbh->prepare("
661 INSERT INTO tmp_holdsqueue (biblionumber,itemnumber,barcode,surname,firstname,phone,borrowernumber,
662 cardnumber,reservedate,title, itemcallnumber,
663 holdingbranch,pickbranch,notes, item_level_request)
664 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
667 foreach my $itemnumber (sort keys %$item_map) {
668 my $mapped_item = $item_map->{$itemnumber};
669 my $biblionumber = $mapped_item->{biblionumber};
670 my $borrowernumber = $mapped_item->{borrowernumber};
671 my $pickbranch = $mapped_item->{pickup_branch};
672 my $holdingbranch = $mapped_item->{holdingbranch};
673 my $reservedate = $mapped_item->{reservedate};
674 my $reservenotes = $mapped_item->{reservenotes};
675 my $item_level = $mapped_item->{item_level};
677 my $item = GetItem($itemnumber);
678 my $barcode = $item->{barcode};
679 my $itemcallnumber = $item->{itemcallnumber};
681 my $patron = Koha::Patrons->find( $borrowernumber );
682 my $cardnumber = $patron->cardnumber;
683 my $surname = $patron->surname;
684 my $firstname = $patron->firstname;
685 my $phone = $patron->phone;
687 my $biblio = Koha::Biblios->find( $biblionumber );
688 my $title = $biblio->title;
690 $sth_load->execute($biblionumber, $itemnumber, $barcode, $surname, $firstname, $phone, $borrowernumber,
691 $cardnumber, $reservedate, $title, $itemcallnumber,
692 $holdingbranch, $pickbranch, $reservenotes, $item_level);
696 =head2 AddToHoldTargetMap
698 =cut
700 sub AddToHoldTargetMap {
701 my $item_map = shift;
703 my $dbh = C4::Context->dbh;
705 my $insert_sql = q(
706 INSERT INTO hold_fill_targets (borrowernumber, biblionumber, itemnumber, source_branchcode, item_level_request)
707 VALUES (?, ?, ?, ?, ?)
709 my $sth_insert = $dbh->prepare($insert_sql);
711 foreach my $itemnumber (keys %$item_map) {
712 my $mapped_item = $item_map->{$itemnumber};
713 $sth_insert->execute($mapped_item->{borrowernumber}, $mapped_item->{biblionumber}, $itemnumber,
714 $mapped_item->{holdingbranch}, $mapped_item->{item_level});
718 # Helper functions, not part of any interface
720 sub _trim {
721 return $_[0] unless $_[0];
722 $_[0] =~ s/^\s+//;
723 $_[0] =~ s/\s+$//;
724 $_[0];
727 sub load_branches_to_pull_from {
728 my @branches_to_use;
730 my $static_branch_list = C4::Context->preference("StaticHoldsQueueWeight");
731 @branches_to_use = map { _trim($_) } split( /,/, $static_branch_list )
732 if $static_branch_list;
734 @branches_to_use =
735 Koha::Database->new()->schema()->resultset('Branch')
736 ->get_column('branchcode')->all()
737 unless (@branches_to_use);
739 @branches_to_use = shuffle(@branches_to_use)
740 if C4::Context->preference("RandomizeHoldsQueueWeight");
742 my $today = dt_from_string();
743 if ( C4::Context->preference('HoldsQueueSkipClosed') ) {
744 @branches_to_use = grep {
745 !Koha::Calendar->new( branchcode => $_ )
746 ->is_holiday( $today )
747 } @branches_to_use;
750 return \@branches_to_use;
753 sub least_cost_branch {
755 #$from - arrayref
756 my ($to, $from, $transport_cost_matrix) = @_;
758 # Nothing really spectacular: supply to branch, a list of potential from branches
759 # and find the minimum from - to value from the transport_cost_matrix
760 return $from->[0] if ( @$from == 1 && $transport_cost_matrix->{$to}{$from->[0]}->{disable_transfer} != 1 );
762 # If the pickup library is in the list of libraries to pull from,
763 # return that library right away, it is obviously the least costly
764 return ($to) if any { $_ eq $to } @$from;
766 my ($least_cost, @branch);
767 foreach (@$from) {
768 my $cell = $transport_cost_matrix->{$to}{$_};
769 next if $cell->{disable_transfer};
771 my $cost = $cell->{cost};
772 next unless defined $cost; # XXX should this be reported?
774 unless (defined $least_cost) {
775 $least_cost = $cost;
776 push @branch, $_;
777 next;
780 next if $cost > $least_cost;
782 if ($cost == $least_cost) {
783 push @branch, $_;
784 next;
787 @branch = ($_);
788 $least_cost = $cost;
791 return $branch[0];
793 # XXX return a random @branch with minimum cost instead of the first one;
794 # return $branch[0] if @branch == 1;