Bug 15774: Fix additional fields filters
[koha.git] / C4 / HoldsQueue.pm
blob1015763dd867875e5e59669307a5728af8968c31
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::Items;
33 use Koha::Patrons;
35 use List::Util qw(shuffle);
36 use List::MoreUtils qw(any);
37 use Data::Dumper;
39 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
40 BEGIN {
41 require Exporter;
42 @ISA = qw(Exporter);
43 @EXPORT_OK = qw(
44 &CreateQueue
45 &GetHoldsQueueItems
47 &TransportCostMatrix
48 &UpdateTransportCostMatrix
53 =head1 FUNCTIONS
55 =head2 TransportCostMatrix
57 TransportCostMatrix();
59 Returns Transport Cost Matrix as a hashref <to branch code> => <from branch code> => cost
61 =cut
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();
68 my $calendars;
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} = {
76 cost => $cost,
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> }
97 =cut
99 sub UpdateTransportCostMatrix {
100 my ($records) = @_;
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}) {
111 $cost ||= 0;
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";
115 $cost = 0;
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
128 =cut
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, items.enumchron, items.cn_sort, biblioitems.publishercode,biblio.copyrightdate,biblioitems.publicationyear,biblioitems.pages,biblioitems.size,biblioitems.publicationyear,biblioitems.isbn,items.copynumber
136 FROM tmp_holdsqueue
137 JOIN biblio USING (biblionumber)
138 LEFT JOIN biblioitems USING (biblionumber)
139 LEFT JOIN items USING ( itemnumber)
141 if ($branchlimit) {
142 $query .=" WHERE tmp_holdsqueue.holdingbranch = ?";
143 push @bind_params, $branchlimit;
145 $query .= " ORDER BY ccode, location, cn_sort, author, title, pickbranch, reservedate";
146 my $sth = $dbh->prepare($query);
147 $sth->execute(@bind_params);
148 my $items = [];
149 while ( my $row = $sth->fetchrow_hashref ){
150 my $record = GetMarcBiblio({ biblionumber => $row->{biblionumber} });
151 if ($record){
152 $row->{subtitle} = [ map { $_->{subfield} } @{ GetRecordValue( 'subtitle', $record, '' ) } ];
153 $row->{parts} = GetRecordValue('parts',$record,'')->[0]->{subfield};
154 $row->{numbers} = GetRecordValue('numbers',$record,'')->[0]->{subfield};
157 # return the bib-level or item-level itype per syspref
158 if (!C4::Context->preference('item-level_itypes')) {
159 $row->{itype} = $row->{itemtype};
161 delete $row->{itemtype};
163 push @$items, $row;
165 return $items;
168 =head2 CreateQueue
170 CreateQueue();
172 Top level function that turns reserves into tmp_holdsqueue and hold_fill_targets.
174 =cut
176 sub CreateQueue {
177 my $dbh = C4::Context->dbh;
179 $dbh->do("DELETE FROM tmp_holdsqueue"); # clear the old table for new info
180 $dbh->do("DELETE FROM hold_fill_targets");
182 my $total_bibs = 0;
183 my $total_requests = 0;
184 my $total_available_items = 0;
185 my $num_items_mapped = 0;
187 my $branches_to_use;
188 my $transport_cost_matrix;
189 my $use_transport_cost_matrix = C4::Context->preference("UseTransportCostMatrix");
190 if ($use_transport_cost_matrix) {
191 $transport_cost_matrix = TransportCostMatrix();
192 unless (keys %$transport_cost_matrix) {
193 warn "UseTransportCostMatrix set to yes, but matrix not populated";
194 undef $transport_cost_matrix;
197 unless ($transport_cost_matrix) {
198 $branches_to_use = load_branches_to_pull_from();
201 my $bibs_with_pending_requests = GetBibsWithPendingHoldRequests();
203 foreach my $biblionumber (@$bibs_with_pending_requests) {
204 $total_bibs++;
205 my $hold_requests = GetPendingHoldRequestsForBib($biblionumber);
206 my $available_items = GetItemsAvailableToFillHoldRequestsForBib($biblionumber, $branches_to_use);
207 $total_requests += scalar(@$hold_requests);
208 $total_available_items += scalar(@$available_items);
210 my $item_map = MapItemsToHoldRequests($hold_requests, $available_items, $branches_to_use, $transport_cost_matrix);
211 $item_map or next;
212 my $item_map_size = scalar(keys %$item_map)
213 or next;
215 $num_items_mapped += $item_map_size;
216 CreatePicklistFromItemMap($item_map);
217 AddToHoldTargetMap($item_map);
218 if (($item_map_size < scalar(@$hold_requests )) and
219 ($item_map_size < scalar(@$available_items))) {
220 # DOUBLE CHECK, but this is probably OK - unfilled item-level requests
221 # FIXME
222 #warn "unfilled requests for $biblionumber";
223 #warn Dumper($hold_requests), Dumper($available_items), Dumper($item_map);
228 =head2 GetBibsWithPendingHoldRequests
230 my $biblionumber_aref = GetBibsWithPendingHoldRequests();
232 Return an arrayref of the biblionumbers of all bibs
233 that have one or more unfilled hold requests.
235 =cut
237 sub GetBibsWithPendingHoldRequests {
238 my $dbh = C4::Context->dbh;
240 my $bib_query = "SELECT DISTINCT biblionumber
241 FROM reserves
242 WHERE found IS NULL
243 AND priority > 0
244 AND reservedate <= CURRENT_DATE()
245 AND suspend = 0
247 my $sth = $dbh->prepare($bib_query);
249 $sth->execute();
250 my $biblionumbers = $sth->fetchall_arrayref();
252 return [ map { $_->[0] } @$biblionumbers ];
255 =head2 GetPendingHoldRequestsForBib
257 my $requests = GetPendingHoldRequestsForBib($biblionumber);
259 Returns an arrayref of hashrefs to pending, unfilled hold requests
260 on the bib identified by $biblionumber. The following keys
261 are present in each hashref:
263 biblionumber
264 borrowernumber
265 itemnumber
266 priority
267 branchcode
268 reservedate
269 reservenotes
270 borrowerbranch
272 The arrayref is sorted in order of increasing priority.
274 =cut
276 sub GetPendingHoldRequestsForBib {
277 my $biblionumber = shift;
279 my $dbh = C4::Context->dbh;
281 my $request_query = "SELECT biblionumber, borrowernumber, itemnumber, priority, reserves.branchcode,
282 reservedate, reservenotes, borrowers.branchcode AS borrowerbranch, itemtype
283 FROM reserves
284 JOIN borrowers USING (borrowernumber)
285 WHERE biblionumber = ?
286 AND found IS NULL
287 AND priority > 0
288 AND reservedate <= CURRENT_DATE()
289 AND suspend = 0
290 ORDER BY priority";
291 my $sth = $dbh->prepare($request_query);
292 $sth->execute($biblionumber);
294 my $requests = $sth->fetchall_arrayref({});
295 return $requests;
299 =head2 GetItemsAvailableToFillHoldRequestsForBib
301 my $available_items = GetItemsAvailableToFillHoldRequestsForBib($biblionumber, $branches_ar);
303 Returns an arrayref of items available to fill hold requests
304 for the bib identified by C<$biblionumber>. An item is available
305 to fill a hold request if and only if:
307 * it is not on loan
308 * it is not withdrawn
309 * it is not marked notforloan
310 * it is not currently in transit
311 * it is not lost
312 * it is not sitting on the hold shelf
313 * it is not damaged (unless AllowHoldsOnDamagedItems is on)
315 =cut
317 sub GetItemsAvailableToFillHoldRequestsForBib {
318 my ($biblionumber, $branches_to_use) = @_;
320 my $dbh = C4::Context->dbh;
321 my $items_query = "SELECT itemnumber, homebranch, holdingbranch, itemtypes.itemtype AS itype
322 FROM items ";
324 if (C4::Context->preference('item-level_itypes')) {
325 $items_query .= "LEFT JOIN itemtypes ON (itemtypes.itemtype = items.itype) ";
326 } else {
327 $items_query .= "JOIN biblioitems USING (biblioitemnumber)
328 LEFT JOIN itemtypes USING (itemtype) ";
330 $items_query .= "WHERE items.notforloan = 0
331 AND holdingbranch IS NOT NULL
332 AND itemlost = 0
333 AND withdrawn = 0";
334 $items_query .= " AND damaged = 0" unless C4::Context->preference('AllowHoldsOnDamagedItems');
335 $items_query .= " AND items.onloan IS NULL
336 AND (itemtypes.notforloan IS NULL OR itemtypes.notforloan = 0)
337 AND itemnumber NOT IN (
338 SELECT itemnumber
339 FROM reserves
340 WHERE biblionumber = ?
341 AND itemnumber IS NOT NULL
342 AND (found IS NOT NULL OR priority = 0)
344 AND items.biblionumber = ?";
346 my @params = ($biblionumber, $biblionumber);
347 if ($branches_to_use && @$branches_to_use) {
348 $items_query .= " AND holdingbranch IN (" . join (",", map { "?" } @$branches_to_use) . ")";
349 push @params, @$branches_to_use;
351 my $sth = $dbh->prepare($items_query);
352 $sth->execute(@params);
354 my $itm = $sth->fetchall_arrayref({});
355 my @items = grep { ! scalar GetTransfers($_->{itemnumber}) } @$itm;
356 return [ grep {
357 my $rule = GetBranchItemRule($_->{homebranch}, $_->{itype});
358 $_->{holdallowed} = $rule->{holdallowed};
359 $_->{hold_fulfillment_policy} = $rule->{hold_fulfillment_policy};
360 } @items ];
363 =head2 MapItemsToHoldRequests
365 MapItemsToHoldRequests($hold_requests, $available_items, $branches, $transport_cost_matrix)
367 =cut
369 sub MapItemsToHoldRequests {
370 my ($hold_requests, $available_items, $branches_to_use, $transport_cost_matrix) = @_;
373 # handle trival cases
374 return unless scalar(@$hold_requests) > 0;
375 return unless scalar(@$available_items) > 0;
377 # identify item-level requests
378 my %specific_items_requested = map { $_->{itemnumber} => 1 }
379 grep { defined($_->{itemnumber}) }
380 @$hold_requests;
382 # group available items by itemnumber
383 my %items_by_itemnumber = map { $_->{itemnumber} => $_ } @$available_items;
385 # items already allocated
386 my %allocated_items = ();
388 # map of items to hold requests
389 my %item_map = ();
391 # figure out which item-level requests can be filled
392 my $num_items_remaining = scalar(@$available_items);
394 # Look for Local Holds Priority matches first
395 if ( C4::Context->preference('LocalHoldsPriority') ) {
396 my $LocalHoldsPriorityPatronControl =
397 C4::Context->preference('LocalHoldsPriorityPatronControl');
398 my $LocalHoldsPriorityItemControl =
399 C4::Context->preference('LocalHoldsPriorityItemControl');
401 foreach my $request (@$hold_requests) {
402 next if (defined($request->{itemnumber})); #skip item level holds in local priority checking
403 last if $num_items_remaining == 0;
405 my $local_hold_match;
406 foreach my $item (@$available_items) {
407 next
408 if ( !$item->{holdallowed} )
409 || ( $item->{holdallowed} == 1
410 && $item->{homebranch} ne $request->{borrowerbranch} );
412 my $local_holds_priority_item_branchcode =
413 $item->{$LocalHoldsPriorityItemControl};
415 my $local_holds_priority_patron_branchcode =
416 ( $LocalHoldsPriorityPatronControl eq 'PickupLibrary' )
417 ? $request->{branchcode}
418 : ( $LocalHoldsPriorityPatronControl eq 'HomeLibrary' )
419 ? $request->{borrowerbranch}
420 : undef;
422 $local_hold_match =
423 $local_holds_priority_item_branchcode eq
424 $local_holds_priority_patron_branchcode;
426 if ($local_hold_match) {
427 if ( exists $items_by_itemnumber{ $item->{itemnumber} }
428 and not exists $allocated_items{ $item->{itemnumber} }
429 and not $request->{allocated})
431 $item_map{ $item->{itemnumber} } = {
432 borrowernumber => $request->{borrowernumber},
433 biblionumber => $request->{biblionumber},
434 holdingbranch => $item->{holdingbranch},
435 pickup_branch => $request->{branchcode}
436 || $request->{borrowerbranch},
437 item_level => 0,
438 reservedate => $request->{reservedate},
439 reservenotes => $request->{reservenotes},
441 $allocated_items{ $item->{itemnumber} }++;
442 $request->{allocated} = 1;
443 $num_items_remaining--;
450 foreach my $request (@$hold_requests) {
451 last if $num_items_remaining == 0;
452 next if $request->{allocated};
454 # is this an item-level request?
455 if (defined($request->{itemnumber})) {
456 # fill it if possible; if not skip it
457 if (
458 exists $items_by_itemnumber{ $request->{itemnumber} }
459 and not exists $allocated_items{ $request->{itemnumber} }
460 and ( # Don't fill item level holds that contravene the hold pickup policy at this time
461 ( $items_by_itemnumber{ $request->{itemnumber} }->{hold_fulfillment_policy} eq 'any' )
462 || ( $request->{branchcode} eq $items_by_itemnumber{ $request->{itemnumber} }->{ $items_by_itemnumber{ $request->{itemnumber} }->{hold_fulfillment_policy} } )
463 and ( !$request->{itemtype} # If hold itemtype is set, item's itemtype must match
464 || $items_by_itemnumber{ $request->{itemnumber} }->{itype} eq $request->{itemtype} )
470 $item_map{ $request->{itemnumber} } = {
471 borrowernumber => $request->{borrowernumber},
472 biblionumber => $request->{biblionumber},
473 holdingbranch => $items_by_itemnumber{ $request->{itemnumber} }->{holdingbranch},
474 pickup_branch => $request->{branchcode} || $request->{borrowerbranch},
475 item_level => 1,
476 reservedate => $request->{reservedate},
477 reservenotes => $request->{reservenotes},
479 $allocated_items{ $request->{itemnumber} }++;
480 $num_items_remaining--;
482 } else {
483 # it's title-level request that will take up one item
484 $num_items_remaining--;
488 # group available items by branch
489 my %items_by_branch = ();
490 foreach my $item (@$available_items) {
491 next unless $item->{holdallowed};
493 push @{ $items_by_branch{ $item->{holdingbranch} } }, $item
494 unless exists $allocated_items{ $item->{itemnumber} };
496 return \%item_map unless keys %items_by_branch;
498 # now handle the title-level requests
499 $num_items_remaining = scalar(@$available_items) - scalar(keys %allocated_items);
500 my $pull_branches;
501 foreach my $request (@$hold_requests) {
502 last if $num_items_remaining == 0;
503 next if $request->{allocated};
504 next if defined($request->{itemnumber}); # already handled these
506 # look for local match first
507 my $pickup_branch = $request->{branchcode} || $request->{borrowerbranch};
508 my ($itemnumber, $holdingbranch);
510 my $holding_branch_items = $items_by_branch{$pickup_branch};
511 if ( $holding_branch_items ) {
512 foreach my $item (@$holding_branch_items) {
513 if (
514 $request->{borrowerbranch} eq $item->{homebranch}
515 && ( ( $item->{hold_fulfillment_policy} eq 'any' ) # Don't fill item level holds that contravene the hold pickup policy at this time
516 || $request->{branchcode} eq $item->{ $item->{hold_fulfillment_policy} } )
517 && ( !$request->{itemtype} # If hold itemtype is set, item's itemtype must match
518 || $items_by_itemnumber{ $request->{itemnumber} }->{itype} eq $request->{itemtype} )
521 $itemnumber = $item->{itemnumber};
522 last;
525 $holdingbranch = $pickup_branch;
527 elsif ($transport_cost_matrix) {
528 $pull_branches = [keys %items_by_branch];
529 $holdingbranch = least_cost_branch( $pickup_branch, $pull_branches, $transport_cost_matrix );
530 if ( $holdingbranch ) {
532 my $holding_branch_items = $items_by_branch{$holdingbranch};
533 foreach my $item (@$holding_branch_items) {
534 next if $request->{borrowerbranch} ne $item->{homebranch};
536 # Don't fill item level holds that contravene the hold pickup policy at this time
537 next unless $item->{hold_fulfillment_policy} eq 'any'
538 || $request->{branchcode} eq $item->{ $item->{hold_fulfillment_policy} };
540 # If hold itemtype is set, item's itemtype must match
541 next unless ( !$request->{itemtype}
542 || $item->{itype} eq $request->{itemtype} );
544 $itemnumber = $item->{itemnumber};
545 last;
548 else {
549 next;
553 unless ($itemnumber) {
554 # not found yet, fall back to basics
555 if ($branches_to_use) {
556 $pull_branches = $branches_to_use;
557 } else {
558 $pull_branches = [keys %items_by_branch];
561 # Try picking items where the home and pickup branch match first
562 PULL_BRANCHES:
563 foreach my $branch (@$pull_branches) {
564 my $holding_branch_items = $items_by_branch{$branch}
565 or next;
567 $holdingbranch ||= $branch;
568 foreach my $item (@$holding_branch_items) {
569 next if $pickup_branch ne $item->{homebranch};
570 next if ( $item->{holdallowed} == 1 && $item->{homebranch} ne $request->{borrowerbranch} );
572 # Don't fill item level holds that contravene the hold pickup policy at this time
573 next unless $item->{hold_fulfillment_policy} eq 'any'
574 || $request->{branchcode} eq $item->{ $item->{hold_fulfillment_policy} };
576 # If hold itemtype is set, item's itemtype must match
577 next unless ( !$request->{itemtype}
578 || $item->{itype} eq $request->{itemtype} );
580 $itemnumber = $item->{itemnumber};
581 $holdingbranch = $branch;
582 last PULL_BRANCHES;
586 # Now try items from the least cost branch based on the transport cost matrix or StaticHoldsQueueWeight
587 unless ( $itemnumber ) {
588 foreach my $current_item ( @{ $items_by_branch{$holdingbranch} } ) {
589 if ( $holdingbranch && ( $current_item->{holdallowed} == 2 || $request->{borrowerbranch} eq $current_item->{homebranch} ) ) {
591 # Don't fill item level holds that contravene the hold pickup policy at this time
592 next unless $current_item->{hold_fulfillment_policy} eq 'any'
593 || $request->{branchcode} eq $current_item->{ $current_item->{hold_fulfillment_policy} };
595 # If hold itemtype is set, item's itemtype must match
596 next unless ( !$request->{itemtype}
597 || $current_item->{itype} eq $request->{itemtype} );
599 $itemnumber = $current_item->{itemnumber};
600 last; # quit this loop as soon as we have a suitable item
605 # Now try for items for any item that can fill this hold
606 unless ( $itemnumber ) {
607 PULL_BRANCHES2:
608 foreach my $branch (@$pull_branches) {
609 my $holding_branch_items = $items_by_branch{$branch}
610 or next;
612 foreach my $item (@$holding_branch_items) {
613 next if ( $item->{holdallowed} == 1 && $item->{homebranch} ne $request->{borrowerbranch} );
615 # Don't fill item level holds that contravene the hold pickup policy at this time
616 next unless $item->{hold_fulfillment_policy} eq 'any'
617 || $request->{branchcode} eq $item->{ $item->{hold_fulfillment_policy} };
619 # If hold itemtype is set, item's itemtype must match
620 next unless ( !$request->{itemtype}
621 || $item->{itype} eq $request->{itemtype} );
623 $itemnumber = $item->{itemnumber};
624 $holdingbranch = $branch;
625 last PULL_BRANCHES2;
631 if ($itemnumber) {
632 my $holding_branch_items = $items_by_branch{$holdingbranch}
633 or die "Have $itemnumber, $holdingbranch, but no items!";
634 @$holding_branch_items = grep { $_->{itemnumber} != $itemnumber } @$holding_branch_items;
635 delete $items_by_branch{$holdingbranch} unless @$holding_branch_items;
637 $item_map{$itemnumber} = {
638 borrowernumber => $request->{borrowernumber},
639 biblionumber => $request->{biblionumber},
640 holdingbranch => $holdingbranch,
641 pickup_branch => $pickup_branch,
642 item_level => 0,
643 reservedate => $request->{reservedate},
644 reservenotes => $request->{reservenotes},
646 $num_items_remaining--;
649 return \%item_map;
652 =head2 CreatePickListFromItemMap
654 =cut
656 sub CreatePicklistFromItemMap {
657 my $item_map = shift;
659 my $dbh = C4::Context->dbh;
661 my $sth_load=$dbh->prepare("
662 INSERT INTO tmp_holdsqueue (biblionumber,itemnumber,barcode,surname,firstname,phone,borrowernumber,
663 cardnumber,reservedate,title, itemcallnumber,
664 holdingbranch,pickbranch,notes, item_level_request)
665 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
668 foreach my $itemnumber (sort keys %$item_map) {
669 my $mapped_item = $item_map->{$itemnumber};
670 my $biblionumber = $mapped_item->{biblionumber};
671 my $borrowernumber = $mapped_item->{borrowernumber};
672 my $pickbranch = $mapped_item->{pickup_branch};
673 my $holdingbranch = $mapped_item->{holdingbranch};
674 my $reservedate = $mapped_item->{reservedate};
675 my $reservenotes = $mapped_item->{reservenotes};
676 my $item_level = $mapped_item->{item_level};
678 my $item = Koha::Items->find($itemnumber);
679 my $barcode = $item->barcode;
680 my $itemcallnumber = $item->itemcallnumber;
682 my $patron = Koha::Patrons->find( $borrowernumber );
683 my $cardnumber = $patron->cardnumber;
684 my $surname = $patron->surname;
685 my $firstname = $patron->firstname;
686 my $phone = $patron->phone;
688 my $biblio = Koha::Biblios->find( $biblionumber );
689 my $title = $biblio->title;
691 $sth_load->execute($biblionumber, $itemnumber, $barcode, $surname, $firstname, $phone, $borrowernumber,
692 $cardnumber, $reservedate, $title, $itemcallnumber,
693 $holdingbranch, $pickbranch, $reservenotes, $item_level);
697 =head2 AddToHoldTargetMap
699 =cut
701 sub AddToHoldTargetMap {
702 my $item_map = shift;
704 my $dbh = C4::Context->dbh;
706 my $insert_sql = q(
707 INSERT INTO hold_fill_targets (borrowernumber, biblionumber, itemnumber, source_branchcode, item_level_request)
708 VALUES (?, ?, ?, ?, ?)
710 my $sth_insert = $dbh->prepare($insert_sql);
712 foreach my $itemnumber (keys %$item_map) {
713 my $mapped_item = $item_map->{$itemnumber};
714 $sth_insert->execute($mapped_item->{borrowernumber}, $mapped_item->{biblionumber}, $itemnumber,
715 $mapped_item->{holdingbranch}, $mapped_item->{item_level});
719 # Helper functions, not part of any interface
721 sub _trim {
722 return $_[0] unless $_[0];
723 $_[0] =~ s/^\s+//;
724 $_[0] =~ s/\s+$//;
725 $_[0];
728 sub load_branches_to_pull_from {
729 my @branches_to_use;
731 my $static_branch_list = C4::Context->preference("StaticHoldsQueueWeight");
732 @branches_to_use = map { _trim($_) } split( /,/, $static_branch_list )
733 if $static_branch_list;
735 @branches_to_use =
736 Koha::Database->new()->schema()->resultset('Branch')
737 ->get_column('branchcode')->all()
738 unless (@branches_to_use);
740 @branches_to_use = shuffle(@branches_to_use)
741 if C4::Context->preference("RandomizeHoldsQueueWeight");
743 my $today = dt_from_string();
744 if ( C4::Context->preference('HoldsQueueSkipClosed') ) {
745 @branches_to_use = grep {
746 !Koha::Calendar->new( branchcode => $_ )
747 ->is_holiday( $today )
748 } @branches_to_use;
751 return \@branches_to_use;
754 sub least_cost_branch {
756 #$from - arrayref
757 my ($to, $from, $transport_cost_matrix) = @_;
759 # Nothing really spectacular: supply to branch, a list of potential from branches
760 # and find the minimum from - to value from the transport_cost_matrix
761 return $from->[0] if ( @$from == 1 && $transport_cost_matrix->{$to}{$from->[0]}->{disable_transfer} != 1 );
763 # If the pickup library is in the list of libraries to pull from,
764 # return that library right away, it is obviously the least costly
765 return ($to) if any { $_ eq $to } @$from;
767 my ($least_cost, @branch);
768 foreach (@$from) {
769 my $cell = $transport_cost_matrix->{$to}{$_};
770 next if $cell->{disable_transfer};
772 my $cost = $cell->{cost};
773 next unless defined $cost; # XXX should this be reported?
775 unless (defined $least_cost) {
776 $least_cost = $cost;
777 push @branch, $_;
778 next;
781 next if $cost > $least_cost;
783 if ($cost == $least_cost) {
784 push @branch, $_;
785 next;
788 @branch = ($_);
789 $least_cost = $cost;
792 return $branch[0];
794 # XXX return a random @branch with minimum cost instead of the first one;
795 # return $branch[0] if @branch == 1;