Bug 15532: Add ability to allow only items whose home/holding branch matches the...
[koha.git] / C4 / HoldsQueue.pm
1 package C4::HoldsQueue;
2
3 # Copyright 2011 Catalyst IT
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 # FIXME: expand perldoc, explain intended logic
21
22 use strict;
23 use warnings;
24
25 use C4::Context;
26 use C4::Search;
27 use C4::Items;
28 use C4::Branch;
29 use C4::Circulation;
30 use C4::Members;
31 use C4::Biblio;
32 use Koha::DateUtils;
33
34 use List::Util qw(shuffle);
35 use List::MoreUtils qw(any);
36 use Data::Dumper;
37
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
45
46         &TransportCostMatrix
47         &UpdateTransportCostMatrix
48      );
49 }
50
51
52 =head1 FUNCTIONS
53
54 =head2 TransportCostMatrix
55
56   TransportCostMatrix();
57
58 Returns Transport Cost Matrix as a hashref <to branch code> => <from branch code> => cost
59
60 =cut
61
62 sub TransportCostMatrix {
63     my $dbh   = C4::Context->dbh;
64     my $transport_costs = $dbh->selectall_arrayref("SELECT * FROM transport_cost",{ Slice => {} });
65
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
77         };
78
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 );
83         }
84
85     }
86     return \%transport_cost_matrix;
87 }
88
89 =head2 UpdateTransportCostMatrix
90
91   UpdateTransportCostMatrix($records);
92
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> }
95
96 =cut
97
98 sub UpdateTransportCostMatrix {
99     my ($records) = @_;
100     my $dbh   = C4::Context->dbh;
101
102     my $sth = $dbh->prepare("INSERT INTO transport_cost (frombranch, tobranch, cost, disable_transfer) VALUES (?, ?, ?, ?)");
103
104     $dbh->do("TRUNCATE TABLE transport_cost");
105     foreach (@$records) {
106         my $cost = $_->{cost};
107         my $from = $_->{frombranch};
108         my $to = $_->{tobranch};
109         if ($_->{disable_transfer}) {
110             $cost ||= 0;
111         }
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, disablig";
114             $cost = 0;
115             $_->{disable_transfer} = 1;
116         }
117         $sth->execute( $from, $to, $cost, $_->{disable_transfer} ? 1 : 0 );
118     }
119 }
120
121 =head2 GetHoldsQueueItems
122
123   GetHoldsQueueItems($branch);
124
125 Returns hold queue for a holding branch. If branch is omitted, then whole queue is returned
126
127 =cut
128
129 sub GetHoldsQueueItems {
130     my ($branchlimit) = @_;
131     my $dbh   = C4::Context->dbh;
132
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)
139                 /;
140     if ($branchlimit) {
141         $query .=" WHERE tmp_holdsqueue.holdingbranch = ?";
142         push @bind_params, $branchlimit;
143     }
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($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};
154         }
155
156         # return the bib-level or item-level itype per syspref
157         if (!C4::Context->preference('item-level_itypes')) {
158             $row->{itype} = $row->{itemtype};
159         }
160         delete $row->{itemtype};
161
162         push @$items, $row;
163     }
164     return $items;
165 }
166
167 =head2 CreateQueue
168
169   CreateQueue();
170
171 Top level function that turns reserves into tmp_holdsqueue and hold_fill_targets.
172
173 =cut
174
175 sub CreateQueue {
176     my $dbh   = C4::Context->dbh;
177
178     $dbh->do("DELETE FROM tmp_holdsqueue");  # clear the old table for new info
179     $dbh->do("DELETE FROM hold_fill_targets");
180
181     my $total_bibs            = 0;
182     my $total_requests        = 0;
183     my $total_available_items = 0;
184     my $num_items_mapped      = 0;
185
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;
194         }
195     }
196     unless ($transport_cost_matrix) {
197         $branches_to_use = load_branches_to_pull_from();
198     }
199
200     my $bibs_with_pending_requests = GetBibsWithPendingHoldRequests();
201
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);
208
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;
213
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);
223         }
224     }
225 }
226
227 =head2 GetBibsWithPendingHoldRequests
228
229   my $biblionumber_aref = GetBibsWithPendingHoldRequests();
230
231 Return an arrayref of the biblionumbers of all bibs
232 that have one or more unfilled hold requests.
233
234 =cut
235
236 sub GetBibsWithPendingHoldRequests {
237     my $dbh = C4::Context->dbh;
238
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
245                      ";
246     my $sth = $dbh->prepare($bib_query);
247
248     $sth->execute();
249     my $biblionumbers = $sth->fetchall_arrayref();
250
251     return [ map { $_->[0] } @$biblionumbers ];
252 }
253
254 =head2 GetPendingHoldRequestsForBib
255
256   my $requests = GetPendingHoldRequestsForBib($biblionumber);
257
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:
261
262     biblionumber
263     borrowernumber
264     itemnumber
265     priority
266     branchcode
267     reservedate
268     reservenotes
269     borrowerbranch
270
271 The arrayref is sorted in order of increasing priority.
272
273 =cut
274
275 sub GetPendingHoldRequestsForBib {
276     my $biblionumber = shift;
277
278     my $dbh = C4::Context->dbh;
279
280     my $request_query = "SELECT biblionumber, borrowernumber, itemnumber, priority, reserves.branchcode,
281                                 reservedate, reservenotes, borrowers.branchcode AS borrowerbranch
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);
292
293     my $requests = $sth->fetchall_arrayref({});
294     return $requests;
295
296 }
297
298 =head2 GetItemsAvailableToFillHoldRequestsForBib
299
300   my $available_items = GetItemsAvailableToFillHoldRequestsForBib($biblionumber, $branches_ar);
301
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:
305
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)
313
314 =cut
315
316 sub GetItemsAvailableToFillHoldRequestsForBib {
317     my ($biblionumber, $branches_to_use) = @_;
318
319     my $dbh = C4::Context->dbh;
320     my $items_query = "SELECT itemnumber, homebranch, holdingbranch, itemtypes.itemtype AS itype
321                        FROM items ";
322
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) ";
328     }
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)
342                         )
343                        AND items.biblionumber = ?";
344
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;
349     }
350     my $sth = $dbh->prepare($items_query);
351     $sth->execute(@params);
352
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 ];
360 }
361
362 =head2 MapItemsToHoldRequests
363
364   MapItemsToHoldRequests($hold_requests, $available_items, $branches, $transport_cost_matrix)
365
366 =cut
367
368 sub MapItemsToHoldRequests {
369     my ($hold_requests, $available_items, $branches_to_use, $transport_cost_matrix) = @_;
370
371     # handle trival cases
372     return unless scalar(@$hold_requests) > 0;
373     return unless scalar(@$available_items) > 0;
374
375     # identify item-level requests
376     my %specific_items_requested = map { $_->{itemnumber} => 1 }
377                                    grep { defined($_->{itemnumber}) }
378                                    @$hold_requests;
379
380     # group available items by itemnumber
381     my %items_by_itemnumber = map { $_->{itemnumber} => $_ } @$available_items;
382
383     # items already allocated
384     my %allocated_items = ();
385
386     # map of items to hold requests
387     my %item_map = ();
388
389     # figure out which item-level requests can be filled
390     my $num_items_remaining = scalar(@$available_items);
391     foreach my $request (@$hold_requests) {
392         last if $num_items_remaining == 0;
393
394         # is this an item-level request?
395         if (defined($request->{itemnumber})) {
396             # fill it if possible; if not skip it
397             if (
398                     exists $items_by_itemnumber{ $request->{itemnumber} }
399                 and not exists $allocated_items{ $request->{itemnumber} }
400                 and ( # Don't fill item level holds that contravene the hold pickup policy at this time
401                     ( $items_by_itemnumber{ $request->{itemnumber} }->{hold_fulfillment_policy} eq 'any' )
402                     || ( $request->{branchcode} eq $items_by_itemnumber{ $request->{itemnumber} }->{ $items_by_itemnumber{ $request->{itemnumber} }->{hold_fulfillment_policy} }  )
403                 )
404
405               )
406             {
407
408                 $item_map{ $request->{itemnumber} } = {
409                     borrowernumber => $request->{borrowernumber},
410                     biblionumber   => $request->{biblionumber},
411                     holdingbranch  => $items_by_itemnumber{ $request->{itemnumber} }->{holdingbranch},
412                     pickup_branch  => $request->{branchcode} || $request->{borrowerbranch},
413                     item_level     => 1,
414                     reservedate    => $request->{reservedate},
415                     reservenotes   => $request->{reservenotes},
416                 };
417                 $allocated_items{ $request->{itemnumber} }++;
418                 $num_items_remaining--;
419             }
420         } else {
421             # it's title-level request that will take up one item
422             $num_items_remaining--;
423         }
424     }
425
426     # group available items by branch
427     my %items_by_branch = ();
428     foreach my $item (@$available_items) {
429         next unless $item->{holdallowed};
430
431         push @{ $items_by_branch{ $item->{holdingbranch} } }, $item
432           unless exists $allocated_items{ $item->{itemnumber} };
433     }
434     return \%item_map unless keys %items_by_branch;
435
436     # now handle the title-level requests
437     $num_items_remaining = scalar(@$available_items) - scalar(keys %allocated_items);
438     my $pull_branches;
439     foreach my $request (@$hold_requests) {
440         last if $num_items_remaining == 0;
441         next if defined($request->{itemnumber}); # already handled these
442
443         # look for local match first
444         my $pickup_branch = $request->{branchcode} || $request->{borrowerbranch};
445         my ($itemnumber, $holdingbranch);
446
447         my $holding_branch_items = $items_by_branch{$pickup_branch};
448         if ( $holding_branch_items ) {
449             foreach my $item (@$holding_branch_items) {
450                 if (
451                     $request->{borrowerbranch} eq $item->{homebranch}
452                     && ( ( $item->{hold_fulfillment_policy} eq 'any' ) # Don't fill item level holds that contravene the hold pickup policy at this time
453                         || $request->{branchcode} eq $item->{ $item->{hold_fulfillment_policy} } )
454                   )
455                 {
456                     $itemnumber = $item->{itemnumber};
457                     last;
458                 }
459             }
460             $holdingbranch = $pickup_branch;
461         }
462         elsif ($transport_cost_matrix) {
463             $pull_branches = [keys %items_by_branch];
464             $holdingbranch = least_cost_branch( $pickup_branch, $pull_branches, $transport_cost_matrix );
465             if ( $holdingbranch ) {
466
467                 my $holding_branch_items = $items_by_branch{$holdingbranch};
468                 foreach my $item (@$holding_branch_items) {
469                     next if $request->{borrowerbranch} ne $item->{homebranch};
470
471                     # Don't fill item level holds that contravene the hold pickup policy at this time
472                     next unless $item->{hold_fulfillment_policy} eq 'any'
473                         || $request->{branchcode} eq $item->{ $item->{hold_fulfillment_policy} };
474
475                     $itemnumber = $item->{itemnumber};
476                     last;
477                 }
478             }
479             else {
480                 next;
481             }
482         }
483
484         unless ($itemnumber) {
485             # not found yet, fall back to basics
486             if ($branches_to_use) {
487                 $pull_branches = $branches_to_use;
488             } else {
489                 $pull_branches = [keys %items_by_branch];
490             }
491
492             # Try picking items where the home and pickup branch match first
493             PULL_BRANCHES:
494             foreach my $branch (@$pull_branches) {
495                 my $holding_branch_items = $items_by_branch{$branch}
496                   or next;
497
498                 $holdingbranch ||= $branch;
499                 foreach my $item (@$holding_branch_items) {
500                     next if $pickup_branch ne $item->{homebranch};
501                     next if ( $item->{holdallowed} == 1 && $item->{homebranch} ne $request->{borrowerbranch} );
502
503                     # Don't fill item level holds that contravene the hold pickup policy at this time
504                     next unless $item->{hold_fulfillment_policy} eq 'any'
505                         || $request->{branchcode} eq $item->{ $item->{hold_fulfillment_policy} };
506
507                     $itemnumber = $item->{itemnumber};
508                     $holdingbranch = $branch;
509                     last PULL_BRANCHES;
510                 }
511             }
512
513             # Now try items from the least cost branch based on the transport cost matrix or StaticHoldsQueueWeight
514             unless ( $itemnumber ) {
515                 foreach my $current_item ( @{ $items_by_branch{$holdingbranch} } ) {
516                     if ( $holdingbranch && ( $current_item->{holdallowed} == 2 || $request->{borrowerbranch} eq $current_item->{homebranch} ) ) {
517
518                         # Don't fill item level holds that contravene the hold pickup policy at this time
519                         next unless $current_item->{hold_fulfillment_policy} eq 'any'
520                             || $request->{branchcode} eq $current_item->{ $current_item->{hold_fulfillment_policy} };
521
522                         $itemnumber = $current_item->{itemnumber};
523                         last; # quit this loop as soon as we have a suitable item
524                     }
525                 }
526             }
527
528             # Now try for items for any item that can fill this hold
529             unless ( $itemnumber ) {
530                 PULL_BRANCHES2:
531                 foreach my $branch (@$pull_branches) {
532                     my $holding_branch_items = $items_by_branch{$branch}
533                       or next;
534
535                     foreach my $item (@$holding_branch_items) {
536                         next if ( $item->{holdallowed} == 1 && $item->{homebranch} ne $request->{borrowerbranch} );
537
538                         # Don't fill item level holds that contravene the hold pickup policy at this time
539                         next unless $item->{hold_fulfillment_policy} eq 'any'
540                             || $request->{branchcode} eq $item->{ $item->{hold_fulfillment_policy} };
541
542                         $itemnumber = $item->{itemnumber};
543                         $holdingbranch = $branch;
544                         last PULL_BRANCHES2;
545                     }
546                 }
547             }
548         }
549
550         if ($itemnumber) {
551             my $holding_branch_items = $items_by_branch{$holdingbranch}
552               or die "Have $itemnumber, $holdingbranch, but no items!";
553             @$holding_branch_items = grep { $_->{itemnumber} != $itemnumber } @$holding_branch_items;
554             delete $items_by_branch{$holdingbranch} unless @$holding_branch_items;
555
556             $item_map{$itemnumber} = {
557                 borrowernumber => $request->{borrowernumber},
558                 biblionumber => $request->{biblionumber},
559                 holdingbranch => $holdingbranch,
560                 pickup_branch => $pickup_branch,
561                 item_level => 0,
562                 reservedate => $request->{reservedate},
563                 reservenotes => $request->{reservenotes},
564             };
565             $num_items_remaining--;
566         }
567     }
568     return \%item_map;
569 }
570
571 =head2 CreatePickListFromItemMap
572
573 =cut
574
575 sub CreatePicklistFromItemMap {
576     my $item_map = shift;
577
578     my $dbh = C4::Context->dbh;
579
580     my $sth_load=$dbh->prepare("
581         INSERT INTO tmp_holdsqueue (biblionumber,itemnumber,barcode,surname,firstname,phone,borrowernumber,
582                                     cardnumber,reservedate,title, itemcallnumber,
583                                     holdingbranch,pickbranch,notes, item_level_request)
584         VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
585     ");
586
587     foreach my $itemnumber  (sort keys %$item_map) {
588         my $mapped_item = $item_map->{$itemnumber};
589         my $biblionumber = $mapped_item->{biblionumber};
590         my $borrowernumber = $mapped_item->{borrowernumber};
591         my $pickbranch = $mapped_item->{pickup_branch};
592         my $holdingbranch = $mapped_item->{holdingbranch};
593         my $reservedate = $mapped_item->{reservedate};
594         my $reservenotes = $mapped_item->{reservenotes};
595         my $item_level = $mapped_item->{item_level};
596
597         my $item = GetItem($itemnumber);
598         my $barcode = $item->{barcode};
599         my $itemcallnumber = $item->{itemcallnumber};
600
601         my $borrower = GetMember('borrowernumber'=>$borrowernumber);
602         my $cardnumber = $borrower->{'cardnumber'};
603         my $surname = $borrower->{'surname'};
604         my $firstname = $borrower->{'firstname'};
605         my $phone = $borrower->{'phone'};
606
607         my $bib = GetBiblioData($biblionumber);
608         my $title = $bib->{title};
609
610         $sth_load->execute($biblionumber, $itemnumber, $barcode, $surname, $firstname, $phone, $borrowernumber,
611                            $cardnumber, $reservedate, $title, $itemcallnumber,
612                            $holdingbranch, $pickbranch, $reservenotes, $item_level);
613     }
614 }
615
616 =head2 AddToHoldTargetMap
617
618 =cut
619
620 sub AddToHoldTargetMap {
621     my $item_map = shift;
622
623     my $dbh = C4::Context->dbh;
624
625     my $insert_sql = q(
626         INSERT INTO hold_fill_targets (borrowernumber, biblionumber, itemnumber, source_branchcode, item_level_request)
627                                VALUES (?, ?, ?, ?, ?)
628     );
629     my $sth_insert = $dbh->prepare($insert_sql);
630
631     foreach my $itemnumber (keys %$item_map) {
632         my $mapped_item = $item_map->{$itemnumber};
633         $sth_insert->execute($mapped_item->{borrowernumber}, $mapped_item->{biblionumber}, $itemnumber,
634                              $mapped_item->{holdingbranch}, $mapped_item->{item_level});
635     }
636 }
637
638 # Helper functions, not part of any interface
639
640 sub _trim {
641     return $_[0] unless $_[0];
642     $_[0] =~ s/^\s+//;
643     $_[0] =~ s/\s+$//;
644     $_[0];
645 }
646
647 sub load_branches_to_pull_from {
648     my @branches_to_use;
649
650     my $static_branch_list = C4::Context->preference("StaticHoldsQueueWeight");
651     @branches_to_use = map { _trim($_) } split( /,/, $static_branch_list )
652       if $static_branch_list;
653
654     @branches_to_use =
655       Koha::Database->new()->schema()->resultset('Branch')
656       ->get_column('branchcode')->all()
657       unless (@branches_to_use);
658
659     @branches_to_use = shuffle(@branches_to_use)
660       if C4::Context->preference("RandomizeHoldsQueueWeight");
661
662     my $today = dt_from_string();
663     if ( C4::Context->preference('HoldsQueueSkipClosed') ) {
664         @branches_to_use = grep {
665             !Koha::Calendar->new( branchcode => $_ )
666               ->is_holiday( $today )
667         } @branches_to_use;
668     }
669
670     return \@branches_to_use;
671 }
672
673 sub least_cost_branch {
674
675     #$from - arrayref
676     my ($to, $from, $transport_cost_matrix) = @_;
677
678     # Nothing really spectacular: supply to branch, a list of potential from branches
679     # and find the minimum from - to value from the transport_cost_matrix
680     return $from->[0] if ( @$from == 1 && $transport_cost_matrix->{$to}{$from->[0]}->{disable_transfer} != 1 );
681
682     # If the pickup library is in the list of libraries to pull from,
683     # return that library right away, it is obviously the least costly
684     return ($to) if any { $_ eq $to } @$from;
685
686     my ($least_cost, @branch);
687     foreach (@$from) {
688         my $cell = $transport_cost_matrix->{$to}{$_};
689         next if $cell->{disable_transfer};
690
691         my $cost = $cell->{cost};
692         next unless defined $cost; # XXX should this be reported?
693
694         unless (defined $least_cost) {
695             $least_cost = $cost;
696             push @branch, $_;
697             next;
698         }
699
700         next if $cost > $least_cost;
701
702         if ($cost == $least_cost) {
703             push @branch, $_;
704             next;
705         }
706
707         @branch = ($_);
708         $least_cost = $cost;
709     }
710
711     return $branch[0];
712
713     # XXX return a random @branch with minimum cost instead of the first one;
714     # return $branch[0] if @branch == 1;
715 }
716
717
718 1;