Line 0
Link Here
|
|
|
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 under the |
8 |
# terms of the GNU General Public License as published by the Free Software |
9 |
# Foundation; either version 2 of the License, or (at your option) any later |
10 |
# version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License along |
17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
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 C4::Dates qw/format_date/; |
33 |
|
34 |
use List::Util qw(shuffle); |
35 |
use Data::Dumper; |
36 |
|
37 |
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); |
38 |
BEGIN { |
39 |
$VERSION = 3.03; |
40 |
require Exporter; |
41 |
@ISA = qw(Exporter); |
42 |
@EXPORT_OK = qw( |
43 |
&CreateQueue |
44 |
&GetHoldsQueueItems |
45 |
|
46 |
&TransportCostMatrix |
47 |
&UpdateTransportCostMatrix |
48 |
); |
49 |
} |
50 |
|
51 |
# XXX This is not safe in a persistant environment |
52 |
my $dbh = C4::Context->dbh; |
53 |
|
54 |
=head1 FUNCTIONS |
55 |
|
56 |
=head2 TransportCostMatrix |
57 |
|
58 |
TransportCostMatrix(); |
59 |
|
60 |
Returns Transport Cost Matrix as a hashref <to branch code> => <from branch code> => cost |
61 |
|
62 |
=cut |
63 |
|
64 |
sub TransportCostMatrix { |
65 |
my $transport_costs = $dbh->selectall_arrayref("SELECT * FROM transport_cost",{ Slice => {} }); |
66 |
|
67 |
my %transport_cost_matrix; |
68 |
foreach (@$transport_costs) { |
69 |
my $from = $_->{frombranch}; |
70 |
my $to = $_->{tobranch}; |
71 |
my $cost = $_->{cost}; |
72 |
my $disabled = $_->{disable_transfer}; |
73 |
$transport_cost_matrix{$to}{$from} = { cost => $cost, disable_transfer => $disabled }; |
74 |
} |
75 |
return \%transport_cost_matrix; |
76 |
} |
77 |
|
78 |
=head2 UpdateTransportCostMatrix |
79 |
|
80 |
UpdateTransportCostMatrix($records); |
81 |
|
82 |
Updates full Transport Cost Matrix table. $records is an arrayref of records. |
83 |
Records: { frombranch => <code>, tobranch => <code>, cost => <figure>, disable_transfer => <0,1> } |
84 |
|
85 |
=cut |
86 |
|
87 |
sub UpdateTransportCostMatrix { |
88 |
my ($records) = @_; |
89 |
|
90 |
my $sth = $dbh->prepare("INSERT INTO transport_cost (frombranch, tobranch, cost, disable_transfer) VALUES (?, ?, ?, ?)"); |
91 |
|
92 |
$dbh->do("TRUNCATE TABLE transport_cost"); |
93 |
foreach (@$records) { |
94 |
my $cost = $_->{cost}; |
95 |
my $from = $_->{frombranch}; |
96 |
my $to = $_->{tobranch}; |
97 |
if ($_->{disable_transfer}) { |
98 |
$cost ||= 0; |
99 |
} |
100 |
elsif ( !defined ($cost) || ($cost !~ m/(0|[1-9][0-9]*)(\.[0-9]*)?/o) ) { |
101 |
warn "Invalid $from -> $to cost $cost - must be a number >= 0, disablig"; |
102 |
$cost = 0; |
103 |
$_->{disable_transfer} = 1; |
104 |
} |
105 |
$sth->execute( $from, $to, $cost, $_->{disable_transfer} ? 1 : 0 ); |
106 |
} |
107 |
} |
108 |
|
109 |
=head2 GetHoldsQueueItems |
110 |
|
111 |
GetHoldsQueueItems($branch); |
112 |
|
113 |
Returns hold queue for a holding branch. If branch is omitted, then whole queue is returned |
114 |
|
115 |
=cut |
116 |
|
117 |
sub GetHoldsQueueItems { |
118 |
my ($branchlimit) = @_; |
119 |
|
120 |
my @bind_params = (); |
121 |
my $query = q/SELECT tmp_holdsqueue.*, biblio.author, items.ccode, items.location, items.enumchron, items.cn_sort, biblioitems.publishercode,biblio.copyrightdate,biblioitems.publicationyear,biblioitems.pages,biblioitems.size,biblioitems.publicationyear,biblioitems.isbn,items.copynumber |
122 |
FROM tmp_holdsqueue |
123 |
JOIN biblio USING (biblionumber) |
124 |
LEFT JOIN biblioitems USING (biblionumber) |
125 |
LEFT JOIN items USING ( itemnumber) |
126 |
/; |
127 |
if ($branchlimit) { |
128 |
$query .=" WHERE tmp_holdsqueue.holdingbranch = ?"; |
129 |
push @bind_params, $branchlimit; |
130 |
} |
131 |
$query .= " ORDER BY ccode, location, cn_sort, author, title, pickbranch, reservedate"; |
132 |
my $sth = $dbh->prepare($query); |
133 |
$sth->execute(@bind_params); |
134 |
my $items = []; |
135 |
while ( my $row = $sth->fetchrow_hashref ){ |
136 |
$row->{reservedate} = format_date($row->{reservedate}); |
137 |
my $record = GetMarcBiblio($row->{biblionumber}); |
138 |
if ($record){ |
139 |
$row->{subtitle} = GetRecordValue('subtitle',$record,'')->[0]->{subfield}; |
140 |
$row->{parts} = GetRecordValue('parts',$record,'')->[0]->{subfield}; |
141 |
$row->{numbers} = GetRecordValue('numbers',$record,'')->[0]->{subfield}; |
142 |
} |
143 |
push @$items, $row; |
144 |
} |
145 |
return $items; |
146 |
} |
147 |
|
148 |
=head2 CreateQueue |
149 |
|
150 |
CreateQueue(); |
151 |
|
152 |
Top level function that turns reserves into tmp_holdsqueue and hold_fill_targets. |
153 |
|
154 |
=cut |
155 |
|
156 |
sub CreateQueue { |
157 |
|
158 |
$dbh->do("DELETE FROM tmp_holdsqueue"); # clear the old table for new info |
159 |
$dbh->do("DELETE FROM hold_fill_targets"); |
160 |
|
161 |
my $total_bibs = 0; |
162 |
my $total_requests = 0; |
163 |
my $total_available_items = 0; |
164 |
my $num_items_mapped = 0; |
165 |
|
166 |
my $branches_to_use; |
167 |
my $transport_cost_matrix; |
168 |
my $use_transport_cost_matrix = C4::Context->preference("UseTransportCostMatrix"); |
169 |
if ($use_transport_cost_matrix) { |
170 |
debug( "Using cost matrix" ); |
171 |
$transport_cost_matrix = TransportCostMatrix(); |
172 |
unless (keys %$transport_cost_matrix) { |
173 |
warn "UseTransportCostMatrix set to yes, but matrix not populated"; |
174 |
undef $transport_cost_matrix; |
175 |
} |
176 |
} |
177 |
unless ($transport_cost_matrix) { |
178 |
debug( "Not using cost matrix" ); |
179 |
$branches_to_use = load_branches_to_pull_from(); |
180 |
} |
181 |
|
182 |
my $bibs_with_pending_requests = GetBibsWithPendingHoldRequests(); |
183 |
debug( "bibs_with_pending_requests: ".Dumper($bibs_with_pending_requests) ); |
184 |
|
185 |
foreach my $biblionumber (@$bibs_with_pending_requests) { |
186 |
$total_bibs++; |
187 |
my $hold_requests = GetPendingHoldRequestsForBib($biblionumber); |
188 |
my $available_items = GetItemsAvailableToFillHoldRequestsForBib($biblionumber, $branches_to_use); |
189 |
$total_requests += scalar(@$hold_requests); |
190 |
$total_available_items += scalar(@$available_items); |
191 |
|
192 |
my $item_map = MapItemsToHoldRequests($hold_requests, $available_items, $branches_to_use, $transport_cost_matrix); |
193 |
$item_map or next; |
194 |
my $item_map_size = scalar(keys %$item_map) |
195 |
or next;; |
196 |
|
197 |
$num_items_mapped += $item_map_size; |
198 |
CreatePicklistFromItemMap($item_map); |
199 |
AddToHoldTargetMap($item_map); |
200 |
if (($item_map_size < scalar(@$hold_requests )) and |
201 |
($item_map_size < scalar(@$available_items))) { |
202 |
# DOUBLE CHECK, but this is probably OK - unfilled item-level requests |
203 |
# FIXME |
204 |
#warn "unfilled requests for $biblionumber"; |
205 |
#warn Dumper($hold_requests), Dumper($available_items), Dumper($item_map); |
206 |
} |
207 |
} |
208 |
} |
209 |
|
210 |
=head2 GetBibsWithPendingHoldRequests |
211 |
|
212 |
my $biblionumber_aref = GetBibsWithPendingHoldRequests(); |
213 |
|
214 |
Return an arrayref of the biblionumbers of all bibs |
215 |
that have one or more unfilled hold requests. |
216 |
|
217 |
=cut |
218 |
|
219 |
sub GetBibsWithPendingHoldRequests { |
220 |
my $dbh = C4::Context->dbh; |
221 |
|
222 |
my $bib_query = "SELECT DISTINCT biblionumber |
223 |
FROM reserves |
224 |
WHERE found IS NULL |
225 |
AND priority > 0 |
226 |
AND reservedate <= CURRENT_DATE()"; |
227 |
my $sth = $dbh->prepare($bib_query); |
228 |
|
229 |
$sth->execute(); |
230 |
my $biblionumbers = $sth->fetchall_arrayref(); |
231 |
|
232 |
return [ map { $_->[0] } @$biblionumbers ]; |
233 |
} |
234 |
|
235 |
=head2 GetPendingHoldRequestsForBib |
236 |
|
237 |
my $requests = GetPendingHoldRequestsForBib($biblionumber); |
238 |
|
239 |
Returns an arrayref of hashrefs to pending, unfilled hold requests |
240 |
on the bib identified by $biblionumber. The following keys |
241 |
are present in each hashref: |
242 |
|
243 |
biblionumber |
244 |
borrowernumber |
245 |
itemnumber |
246 |
priority |
247 |
branchcode |
248 |
reservedate |
249 |
reservenotes |
250 |
borrowerbranch |
251 |
|
252 |
The arrayref is sorted in order of increasing priority. |
253 |
|
254 |
=cut |
255 |
|
256 |
sub GetPendingHoldRequestsForBib { |
257 |
my $biblionumber = shift; |
258 |
|
259 |
my $dbh = C4::Context->dbh; |
260 |
|
261 |
my $request_query = "SELECT biblionumber, borrowernumber, itemnumber, priority, reserves.branchcode, |
262 |
reservedate, reservenotes, borrowers.branchcode AS borrowerbranch |
263 |
FROM reserves |
264 |
JOIN borrowers USING (borrowernumber) |
265 |
WHERE biblionumber = ? |
266 |
AND found IS NULL |
267 |
AND priority > 0 |
268 |
AND reservedate <= CURRENT_DATE() |
269 |
ORDER BY priority"; |
270 |
my $sth = $dbh->prepare($request_query); |
271 |
$sth->execute($biblionumber); |
272 |
|
273 |
my $requests = $sth->fetchall_arrayref({}); |
274 |
return $requests; |
275 |
|
276 |
} |
277 |
|
278 |
=head2 GetItemsAvailableToFillHoldRequestsForBib |
279 |
|
280 |
my $available_items = GetItemsAvailableToFillHoldRequestsForBib($biblionumber, $branches_ar); |
281 |
|
282 |
Returns an arrayref of items available to fill hold requests |
283 |
for the bib identified by C<$biblionumber>. An item is available |
284 |
to fill a hold request if and only if: |
285 |
|
286 |
* it is not on loan |
287 |
* it is not withdrawn |
288 |
* it is not marked notforloan |
289 |
* it is not currently in transit |
290 |
* it is not lost |
291 |
* it is not sitting on the hold shelf |
292 |
|
293 |
=cut |
294 |
|
295 |
sub GetItemsAvailableToFillHoldRequestsForBib { |
296 |
my ($biblionumber, $branches_to_use) = @_; |
297 |
|
298 |
my $dbh = C4::Context->dbh; |
299 |
my $items_query = "SELECT itemnumber, homebranch, holdingbranch, itemtypes.itemtype AS itype |
300 |
FROM items "; |
301 |
|
302 |
if (C4::Context->preference('item-level_itypes')) { |
303 |
$items_query .= "LEFT JOIN itemtypes ON (itemtypes.itemtype = items.itype) "; |
304 |
} else { |
305 |
$items_query .= "JOIN biblioitems USING (biblioitemnumber) |
306 |
LEFT JOIN itemtypes USING (itemtype) "; |
307 |
} |
308 |
$items_query .= "WHERE items.notforloan = 0 |
309 |
AND holdingbranch IS NOT NULL |
310 |
AND itemlost = 0 |
311 |
AND wthdrawn = 0 |
312 |
AND items.onloan IS NULL |
313 |
AND (itemtypes.notforloan IS NULL OR itemtypes.notforloan = 0) |
314 |
AND itemnumber NOT IN ( |
315 |
SELECT itemnumber |
316 |
FROM reserves |
317 |
WHERE biblionumber = ? |
318 |
AND itemnumber IS NOT NULL |
319 |
AND (found IS NOT NULL OR priority = 0) |
320 |
) |
321 |
AND items.biblionumber = ?"; |
322 |
$items_query .= " AND damaged = 0 " |
323 |
unless C4::Context->preference('AllowHoldsOnDamagedItems'); |
324 |
|
325 |
my @params = ($biblionumber, $biblionumber); |
326 |
if ($branches_to_use && @$branches_to_use) { |
327 |
$items_query .= " AND holdingbranch IN (" . join (",", map { "?" } @$branches_to_use) . ")"; |
328 |
push @params, @$branches_to_use; |
329 |
} |
330 |
my $sth = $dbh->prepare($items_query); |
331 |
$sth->execute(@params); |
332 |
|
333 |
my $itm = $sth->fetchall_arrayref({}); |
334 |
my @items = grep { ! scalar GetTransfers($_->{itemnumber}) } @$itm; |
335 |
return [ grep { |
336 |
my $rule = GetBranchItemRule($_->{homebranch}, $_->{itype}); |
337 |
$_->{holdallowed} = $rule->{holdallowed} != 0 |
338 |
} @items ]; |
339 |
} |
340 |
|
341 |
=head2 MapItemsToHoldRequests |
342 |
|
343 |
MapItemsToHoldRequests($hold_requests, $available_items, $branches, $transport_cost_matrix) |
344 |
|
345 |
=cut |
346 |
|
347 |
sub MapItemsToHoldRequests { |
348 |
my ($hold_requests, $available_items, $branches_to_use, $transport_cost_matrix) = @_; |
349 |
|
350 |
# handle trival cases |
351 |
return unless scalar(@$hold_requests) > 0; |
352 |
return unless scalar(@$available_items) > 0; |
353 |
|
354 |
debug( "MapItemsToHoldRequests() for ", Dumper($hold_requests, $available_items, $branches_to_use, $transport_cost_matrix) ); |
355 |
my $automatic_return = C4::Context->preference("AutomaticItemReturn"); |
356 |
|
357 |
# identify item-level requests |
358 |
my %specific_items_requested = map { $_->{itemnumber} => 1 } |
359 |
grep { defined($_->{itemnumber}) } |
360 |
@$hold_requests; |
361 |
|
362 |
# group available items by itemnumber |
363 |
my %items_by_itemnumber = map { $_->{itemnumber} => $_ } @$available_items; |
364 |
|
365 |
# items already allocated |
366 |
my %allocated_items = (); |
367 |
|
368 |
# map of items to hold requests |
369 |
my %item_map = (); |
370 |
|
371 |
# figure out which item-level requests can be filled |
372 |
my $num_items_remaining = scalar(@$available_items); |
373 |
foreach my $request (@$hold_requests) { |
374 |
last if $num_items_remaining == 0; |
375 |
|
376 |
# is this an item-level request? |
377 |
if (defined($request->{itemnumber})) { |
378 |
# fill it if possible; if not skip it |
379 |
if (exists $items_by_itemnumber{$request->{itemnumber}} and |
380 |
not exists $allocated_items{$request->{itemnumber}}) { |
381 |
$item_map{$request->{itemnumber}} = { |
382 |
borrowernumber => $request->{borrowernumber}, |
383 |
biblionumber => $request->{biblionumber}, |
384 |
holdingbranch => $items_by_itemnumber{$request->{itemnumber}}->{holdingbranch}, |
385 |
pickup_branch => $request->{branchcode} || $request->{borrowerbranch}, |
386 |
item_level => 1, |
387 |
reservedate => $request->{reservedate}, |
388 |
reservenotes => $request->{reservenotes}, |
389 |
}; |
390 |
$allocated_items{$request->{itemnumber}}++; |
391 |
$num_items_remaining--; |
392 |
} |
393 |
} else { |
394 |
# it's title-level request that will take up one item |
395 |
$num_items_remaining--; |
396 |
} |
397 |
} |
398 |
|
399 |
# group available items by branch |
400 |
my %items_by_branch = (); |
401 |
foreach my $item (@$available_items) { |
402 |
next unless $item->{holdallowed}; |
403 |
|
404 |
push @{ $items_by_branch{ $automatic_return ? $item->{homebranch} |
405 |
: $item->{holdingbranch} } }, $item |
406 |
unless exists $allocated_items{ $item->{itemnumber} }; |
407 |
} |
408 |
return unless keys %items_by_branch; |
409 |
|
410 |
# now handle the title-level requests |
411 |
$num_items_remaining = scalar(@$available_items) - scalar(keys %allocated_items); |
412 |
my $pull_branches; |
413 |
foreach my $request (@$hold_requests) { |
414 |
last if $num_items_remaining == 0; |
415 |
next if defined($request->{itemnumber}); # already handled these |
416 |
|
417 |
# look for local match first |
418 |
my $pickup_branch = $request->{branchcode} || $request->{borrowerbranch}; |
419 |
my ($itemnumber, $holdingbranch); |
420 |
|
421 |
my $holding_branch_items = $automatic_return ? undef : $items_by_branch{$pickup_branch}; |
422 |
if ( $holding_branch_items ) { |
423 |
foreach my $item (@$holding_branch_items) { |
424 |
if ( $request->{borrowerbranch} eq $item->{homebranch} ) { |
425 |
$itemnumber = $item->{itemnumber}; |
426 |
last; |
427 |
} |
428 |
} |
429 |
$holdingbranch = $pickup_branch; |
430 |
$itemnumber ||= $holding_branch_items->[0]->{itemnumber}; |
431 |
} |
432 |
elsif ($transport_cost_matrix) { |
433 |
$pull_branches = [keys %items_by_branch]; |
434 |
$holdingbranch = least_cost_branch( $pickup_branch, $pull_branches, $transport_cost_matrix ); |
435 |
if ( $holdingbranch ) { |
436 |
|
437 |
my $holding_branch_items = $items_by_branch{$holdingbranch}; |
438 |
foreach my $item (@$holding_branch_items) { |
439 |
next if $request->{borrowerbranch} ne $item->{homebranch}; |
440 |
|
441 |
$itemnumber = $item->{itemnumber}; |
442 |
last; |
443 |
} |
444 |
$itemnumber ||= $holding_branch_items->[0]->{itemnumber}; |
445 |
} |
446 |
else { |
447 |
warn "No transport costs for $pickup_branch"; |
448 |
} |
449 |
} |
450 |
|
451 |
unless ($itemnumber) { |
452 |
# not found yet, fall back to basics |
453 |
if ($branches_to_use) { |
454 |
$pull_branches = $branches_to_use; |
455 |
} else { |
456 |
$pull_branches = [keys %items_by_branch]; |
457 |
} |
458 |
PULL_BRANCHES: |
459 |
foreach my $branch (@$pull_branches) { |
460 |
my $holding_branch_items = $items_by_branch{$branch} |
461 |
or next; |
462 |
|
463 |
$holdingbranch ||= $branch; |
464 |
foreach my $item (@$holding_branch_items) { |
465 |
next if $pickup_branch ne $item->{homebranch}; |
466 |
|
467 |
$itemnumber = $item->{itemnumber}; |
468 |
$holdingbranch = $branch; |
469 |
last PULL_BRANCHES; |
470 |
} |
471 |
} |
472 |
$itemnumber ||= $items_by_branch{$holdingbranch}->[0]->{itemnumber} |
473 |
if $holdingbranch; |
474 |
} |
475 |
|
476 |
if ($itemnumber) { |
477 |
my $holding_branch_items = $items_by_branch{$holdingbranch} |
478 |
or die "Have $itemnumber, $holdingbranch, but no items!"; |
479 |
@$holding_branch_items = grep { $_->{itemnumber} != $itemnumber } @$holding_branch_items; |
480 |
delete $items_by_branch{$holdingbranch} unless @$holding_branch_items; |
481 |
|
482 |
$item_map{$itemnumber} = { |
483 |
borrowernumber => $request->{borrowernumber}, |
484 |
biblionumber => $request->{biblionumber}, |
485 |
holdingbranch => $holdingbranch, |
486 |
pickup_branch => $pickup_branch, |
487 |
item_level => 0, |
488 |
reservedate => $request->{reservedate}, |
489 |
reservenotes => $request->{reservenotes}, |
490 |
}; |
491 |
$num_items_remaining--; |
492 |
} |
493 |
} |
494 |
return \%item_map; |
495 |
} |
496 |
|
497 |
=head2 CreatePickListFromItemMap |
498 |
|
499 |
=cut |
500 |
|
501 |
sub CreatePicklistFromItemMap { |
502 |
my $item_map = shift; |
503 |
debug ("CreatePicklistFromItemMap for ", Dumper($item_map) ); |
504 |
|
505 |
my $dbh = C4::Context->dbh; |
506 |
|
507 |
my $sth_load=$dbh->prepare(" |
508 |
INSERT INTO tmp_holdsqueue (biblionumber,itemnumber,barcode,surname,firstname,phone,borrowernumber, |
509 |
cardnumber,reservedate,title, itemcallnumber, |
510 |
holdingbranch,pickbranch,notes, item_level_request) |
511 |
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) |
512 |
"); |
513 |
|
514 |
foreach my $itemnumber (sort keys %$item_map) { |
515 |
my $mapped_item = $item_map->{$itemnumber}; |
516 |
my $biblionumber = $mapped_item->{biblionumber}; |
517 |
my $borrowernumber = $mapped_item->{borrowernumber}; |
518 |
my $pickbranch = $mapped_item->{pickup_branch}; |
519 |
my $holdingbranch = $mapped_item->{holdingbranch}; |
520 |
my $reservedate = $mapped_item->{reservedate}; |
521 |
my $reservenotes = $mapped_item->{reservenotes}; |
522 |
my $item_level = $mapped_item->{item_level}; |
523 |
|
524 |
my $item = GetItem($itemnumber); |
525 |
my $barcode = $item->{barcode}; |
526 |
my $itemcallnumber = $item->{itemcallnumber}; |
527 |
|
528 |
my $borrower = GetMember('borrowernumber'=>$borrowernumber); |
529 |
my $cardnumber = $borrower->{'cardnumber'}; |
530 |
my $surname = $borrower->{'surname'}; |
531 |
my $firstname = $borrower->{'firstname'}; |
532 |
my $phone = $borrower->{'phone'}; |
533 |
|
534 |
my $bib = GetBiblioData($biblionumber); |
535 |
my $title = $bib->{title}; |
536 |
|
537 |
$sth_load->execute($biblionumber, $itemnumber, $barcode, $surname, $firstname, $phone, $borrowernumber, |
538 |
$cardnumber, $reservedate, $title, $itemcallnumber, |
539 |
$holdingbranch, $pickbranch, $reservenotes, $item_level); |
540 |
} |
541 |
} |
542 |
|
543 |
=head2 AddToHoldTargetMap |
544 |
|
545 |
=cut |
546 |
|
547 |
sub AddToHoldTargetMap { |
548 |
my $item_map = shift; |
549 |
|
550 |
my $dbh = C4::Context->dbh; |
551 |
|
552 |
my $insert_sql = q( |
553 |
INSERT INTO hold_fill_targets (borrowernumber, biblionumber, itemnumber, source_branchcode, item_level_request) |
554 |
VALUES (?, ?, ?, ?, ?) |
555 |
); |
556 |
my $sth_insert = $dbh->prepare($insert_sql); |
557 |
|
558 |
foreach my $itemnumber (keys %$item_map) { |
559 |
my $mapped_item = $item_map->{$itemnumber}; |
560 |
$sth_insert->execute($mapped_item->{borrowernumber}, $mapped_item->{biblionumber}, $itemnumber, |
561 |
$mapped_item->{holdingbranch}, $mapped_item->{item_level}); |
562 |
} |
563 |
} |
564 |
|
565 |
# Helper functions, not part of any interface |
566 |
|
567 |
sub debug { |
568 |
# warn @_; |
569 |
} |
570 |
|
571 |
sub _trim { |
572 |
return $_[0] unless $_[0]; |
573 |
$_[0] =~ s/^\s+//; |
574 |
$_[0] =~ s/\s+$//; |
575 |
$_[0]; |
576 |
} |
577 |
|
578 |
sub load_branches_to_pull_from { |
579 |
my $static_branch_list = C4::Context->preference("StaticHoldsQueueWeight") |
580 |
or return; |
581 |
|
582 |
my @branches_to_use = map _trim($_), split /,/, $static_branch_list; |
583 |
|
584 |
@branches_to_use = shuffle(@branches_to_use) if C4::Context->preference("RandomizeHoldsQueueWeight"); |
585 |
|
586 |
return \@branches_to_use; |
587 |
} |
588 |
|
589 |
sub least_cost_branch { |
590 |
|
591 |
#$from - arrayref |
592 |
my ($to, $from, $transport_cost_matrix) = @_; |
593 |
|
594 |
# Nothing really spectacular: supply to branch, a list of potential from branches |
595 |
# and find the minimum from - to value from the transport_cost_matrix |
596 |
return $from->[0] if @$from == 1; |
597 |
|
598 |
my ($least_cost, @branch); |
599 |
foreach (@$from) { |
600 |
my $cell = $transport_cost_matrix->{$to}{$_}; |
601 |
next if $cell->{disable_transfer}; |
602 |
|
603 |
my $cost = $cell->{cost}; |
604 |
next unless defined $cost; # XXX should this be reported? |
605 |
|
606 |
unless (defined $least_cost) { |
607 |
$least_cost = $cost; |
608 |
push @branch, $_; |
609 |
next; |
610 |
} |
611 |
|
612 |
next if $cost > $least_cost; |
613 |
|
614 |
if ($cost == $least_cost) { |
615 |
push @branch, $_; |
616 |
next; |
617 |
} |
618 |
|
619 |
@branch = ($_); |
620 |
$least_cost = $cost; |
621 |
} |
622 |
|
623 |
return $branch[0]; |
624 |
|
625 |
# XXX return a random @branch with minimum cost instead of the first one; |
626 |
# return $branch[0] if @branch == 1; |
627 |
} |
628 |
|
629 |
|
630 |
1; |