Bug 23448: Clean up subscription detail template
[koha.git] / t / db_dependent / HoldsQueue.t
blob8c8bb430ddadacd355b5d7f554cd0db8513e07dd
1 #!/usr/bin/perl
3 # Test C4::HoldsQueue::CreateQueue() for both transport cost matrix
4 # and StaticHoldsQueueWeight array (no RandomizeHoldsQueueWeight, no point)
5 # Wraps tests in transaction that's rolled back, so no data is destroyed
6 # MySQL WARNING: This makes sense only if your tables are InnoDB, otherwise
7 # transactions are not supported and mess is left behind
9 use Modern::Perl;
11 use Test::More tests => 48;
12 use Data::Dumper;
14 use C4::Calendar;
15 use C4::Context;
16 use C4::Members;
17 use Koha::Database;
18 use Koha::DateUtils;
19 use Koha::Items;
20 use Koha::Holds;
21 use Koha::CirculationRules;
23 use t::lib::TestBuilder;
24 use t::lib::Mocks;
26 BEGIN {
27 use FindBin;
28 use lib $FindBin::Bin;
29 use_ok('C4::Reserves');
30 use_ok('C4::HoldsQueue');
33 my $schema = Koha::Database->schema;
34 $schema->storage->txn_begin;
35 my $dbh = C4::Context->dbh;
36 $dbh->do("DELETE FROM circulation_rules");
38 my $builder = t::lib::TestBuilder->new;
40 t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '0' );
41 t::lib::Mocks::mock_preference( 'BranchTransferLimitsType', 'itemtype' );
43 my $library1 = $builder->build({
44 source => 'Branch',
45 });
46 my $library2 = $builder->build({
47 source => 'Branch',
48 });
49 my $library3 = $builder->build({
50 source => 'Branch',
51 });
53 my $TITLE = "Test Holds Queue XXX";
55 my $borrower = $builder->build({
56 source => 'Borrower',
57 value => {
58 branchcode => $library1->{branchcode},
60 });
62 my $borrowernumber = $borrower->{borrowernumber};
63 # Set special (for this test) branches
64 my $borrower_branchcode = $borrower->{branchcode};
65 my @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode} );
66 my @other_branches = ( $library2->{branchcode}, $library3->{branchcode} );
67 my $least_cost_branch_code = pop @other_branches;
68 my $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
70 #Set up the stage
71 # Sysprefs and cost matrix
72 t::lib::Mocks::mock_preference('HoldsQueueSkipClosed', 0);
73 t::lib::Mocks::mock_preference('LocalHoldsPriority', 0);
74 $dbh->do("UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'", undef,
75 join( ',', @other_branches, $borrower_branchcode, $least_cost_branch_code));
76 $dbh->do("UPDATE systempreferences SET value = '0' WHERE variable = 'RandomizeHoldsQueueWeight'");
78 $dbh->do("DELETE FROM transport_cost");
79 my $transport_cost_insert_sth = $dbh->prepare("insert into transport_cost (frombranch, tobranch, cost) values (?, ?, ?)");
80 # Favour $least_cost_branch_code
81 $transport_cost_insert_sth->execute($borrower_branchcode, $least_cost_branch_code, 0.2);
82 $transport_cost_insert_sth->execute($least_cost_branch_code, $borrower_branchcode, 0.2);
83 my @b = @other_branches;
84 while ( my $b1 = shift @b ) {
85 foreach my $b2 ($borrower_branchcode, $least_cost_branch_code, @b) {
86 $transport_cost_insert_sth->execute($b1, $b2, 0.5);
87 $transport_cost_insert_sth->execute($b2, $b1, 0.5);
92 # Loanable items - all possible combinations of homebranch and holdingbranch
93 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated)
94 VALUES ('SER', 'Koha test', '$TITLE', '2011-02-01')");
95 my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
96 or BAIL_OUT("Cannot find newly created biblio record");
97 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype)
98 VALUES ($biblionumber, '$itemtype')");
99 my $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
100 or BAIL_OUT("Cannot find newly created biblioitems record");
102 my $items_insert_sth = $dbh->prepare("INSERT INTO items (biblionumber, biblioitemnumber, barcode, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
103 VALUES ($biblionumber, $biblioitemnumber, ?, ?, ?, 0, 0, 0, 0, NULL, '$itemtype')"); # CURRENT_DATE - 3)");
104 my $first_barcode = int(rand(1000000000000)); # XXX
105 my $barcode = $first_barcode;
106 foreach ( $borrower_branchcode, $least_cost_branch_code, @other_branches ) {
107 $items_insert_sth->execute($barcode++, $borrower_branchcode, $_);
108 $items_insert_sth->execute($barcode++, $_, $_);
109 $items_insert_sth->execute($barcode++, $_, $borrower_branchcode);
112 # Remove existing reserves, makes debugging easier
113 $dbh->do("DELETE FROM reserves");
114 my $bibitems = undef;
115 my $priority = 1;
116 # Make a reserve
117 AddReserve ( $borrower_branchcode, $borrowernumber, $biblionumber, $bibitems, $priority );
118 # $resdate, $expdate, $notes, $title, $checkitem, $found
119 $dbh->do("UPDATE reserves SET reservedate = DATE_SUB( reservedate, INTERVAL 1 DAY )");
121 # Tests
122 my $use_cost_matrix_sth = $dbh->prepare("UPDATE systempreferences SET value = ? WHERE variable = 'UseTransportCostMatrix'");
123 my $test_sth = $dbh->prepare("SELECT * FROM hold_fill_targets
124 JOIN tmp_holdsqueue USING (borrowernumber, biblionumber, itemnumber)
125 JOIN items USING (itemnumber)
126 WHERE borrowernumber = $borrowernumber");
128 # We have a book available homed in borrower branch, no point fiddling with AutomaticItemReturn
129 t::lib::Mocks::mock_preference('AutomaticItemReturn', 0);
130 test_queue ('take from homebranch', 0, $borrower_branchcode, $borrower_branchcode);
131 test_queue ('take from homebranch', 1, $borrower_branchcode, $borrower_branchcode);
133 $dbh->do("DELETE FROM tmp_holdsqueue");
134 $dbh->do("DELETE FROM hold_fill_targets");
135 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode')");
136 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode'");
137 # test_queue will flush
138 t::lib::Mocks::mock_preference('AutomaticItemReturn', 1);
139 # Not sure how to make this test more difficult - holding branch does not matter
141 $dbh->do("DELETE FROM tmp_holdsqueue");
142 $dbh->do("DELETE FROM hold_fill_targets");
143 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode')");
144 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode'");
145 t::lib::Mocks::mock_preference('AutomaticItemReturn', 0);
146 # We have a book available held in borrower branch
147 test_queue ('take from holdingbranch', 0, $borrower_branchcode, $borrower_branchcode);
148 test_queue ('take from holdingbranch', 1, $borrower_branchcode, $borrower_branchcode);
150 $dbh->do("DELETE FROM tmp_holdsqueue");
151 $dbh->do("DELETE FROM hold_fill_targets");
152 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE holdingbranch = '$borrower_branchcode')");
153 $dbh->do("DELETE FROM items WHERE holdingbranch = '$borrower_branchcode'");
154 # No book available in borrower branch, pick according to the rules
155 # Frst branch from StaticHoldsQueueWeight
156 test_queue ('take from lowest cost branch', 0, $borrower_branchcode, $other_branches[0]);
157 test_queue ('take from lowest cost branch', 1, $borrower_branchcode, $least_cost_branch_code);
158 my $queue = C4::HoldsQueue::GetHoldsQueueItems($least_cost_branch_code) || [];
159 my $queue_item = $queue->[0];
160 ok( $queue_item
161 && $queue_item->{pickbranch} eq $borrower_branchcode
162 && $queue_item->{holdingbranch} eq $least_cost_branch_code, "GetHoldsQueueItems" )
163 or diag( "Expected item for pick $borrower_branchcode, hold $least_cost_branch_code, got ".Dumper($queue_item) );
164 ok( exists($queue_item->{itype}), 'item type included in queued items list (bug 5825)' );
167 C4::HoldsQueue::least_cost_branch( 'B', [ 'A', 'B', 'C' ] ) eq 'B',
168 'C4::HoldsQueue::least_cost_branch returns the local branch if it is in the list of branches to pull from'
171 # XXX All this tests are for borrower branch pick-up.
172 # Maybe needs expanding to homebranch or holdingbranch pick-up.
174 $schema->txn_rollback;
175 $schema->txn_begin;
177 ### Test holds queue builder does not violate holds policy ###
179 # Clear out existing rules relating to holdallowed
180 $dbh->do("DELETE FROM circulation_rules");
182 t::lib::Mocks::mock_preference('UseTransportCostMatrix', 0);
184 $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
186 $library1 = $builder->build({
187 source => 'Branch',
189 $library2 = $builder->build({
190 source => 'Branch',
192 $library3 = $builder->build({
193 source => 'Branch',
195 @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode} );
197 my $borrower1 = $builder->build({
198 source => 'Borrower',
199 value => {
200 branchcode => $branchcodes[0],
203 my $borrower2 = $builder->build({
204 source => 'Borrower',
205 value => {
206 branchcode => $branchcodes[1],
209 my $borrower3 = $builder->build({
210 source => 'Borrower',
211 value => {
212 branchcode => $branchcodes[2],
216 $dbh->do(qq{
217 INSERT INTO biblio (
218 frameworkcode,
219 author,
220 title,
221 datecreated
222 ) VALUES (
223 'SER',
224 'Koha test',
225 '$TITLE',
226 '2011-02-01'
229 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
230 or BAIL_OUT("Cannot find newly created biblio record");
232 $dbh->do(qq{
233 INSERT INTO biblioitems (
234 biblionumber,
235 itemtype
236 ) VALUES (
237 $biblionumber,
238 '$itemtype'
241 $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
242 or BAIL_OUT("Cannot find newly created biblioitems record");
244 $items_insert_sth = $dbh->prepare(qq{
245 INSERT INTO items (
246 biblionumber,
247 biblioitemnumber,
248 barcode,
249 homebranch,
250 holdingbranch,
251 notforloan,
252 damaged,
253 itemlost,
254 withdrawn,
255 onloan,
256 itype
257 ) VALUES (
258 $biblionumber,
259 $biblioitemnumber,
267 NULL,
268 '$itemtype'
271 # Create 3 items from 2 branches ( branches are for borrowers 1 and 2 respectively )
272 $barcode = int( rand(1000000000000) );
273 $items_insert_sth->execute( $barcode + 0, $branchcodes[0], $branchcodes[0] );
274 $items_insert_sth->execute( $barcode + 1, $branchcodes[1], $branchcodes[1] );
275 $items_insert_sth->execute( $barcode + 2, $branchcodes[1], $branchcodes[1] );
277 $dbh->do("DELETE FROM reserves");
278 my $sth = $dbh->prepare(q{
279 INSERT INTO reserves (
280 borrowernumber,
281 biblionumber,
282 branchcode,
283 priority,
284 reservedate
285 ) VALUES ( ?,?,?,?, CURRENT_DATE() )
287 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
288 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
289 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[0], 3 );
291 my $holds_queue;
293 $dbh->do("DELETE FROM circulation_rules");
294 Koha::CirculationRules->set_rule(
296 rule_name => 'holdallowed',
297 rule_value => 1,
298 branchcode => undef,
299 categorycode => undef,
300 itemtype => undef,
303 C4::HoldsQueue::CreateQueue();
304 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
305 is( @$holds_queue, 2, "Holds queue filling correct number for default holds policy 'from home library'" );
306 is( $holds_queue->[0]->{cardnumber}, $borrower1->{cardnumber}, "Holds queue filling 1st correct hold for default holds policy 'from home library'");
307 is( $holds_queue->[1]->{cardnumber}, $borrower2->{cardnumber}, "Holds queue filling 2nd correct hold for default holds policy 'from home library'");
309 # Test skipping hold picks for closed libraries.
310 # At this point in the test, we have 2 rows in the holds queue
311 # 1 of which is coming from MPL. Let's enable HoldsQueueSkipClosed
312 # and make today a holiday for MPL. When we run it again we should only
313 # have 1 row in the holds queue
314 t::lib::Mocks::mock_preference('HoldsQueueSkipClosed', 1);
315 my $today = dt_from_string();
316 C4::Calendar->new( branchcode => $branchcodes[0] )->insert_single_holiday(
317 day => $today->day(),
318 month => $today->month(),
319 year => $today->year(),
320 title => "$today",
321 description => "$today",
323 # If the test below is removed, aother tests using the holiday will fail. For some reason if we call is_holiday now
324 # the holiday will get set in cache correctly, but not if we let C4::HoldsQueue call is_holiday instead.
325 is( Koha::Calendar->new( branchcode => $branchcodes[0] )->is_holiday( $today ), 1, 'Is today a holiday for pickup branch' );
326 C4::HoldsQueue::CreateQueue();
327 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
328 is( scalar( @$holds_queue ), 1, "Holds not filled with items from closed libraries" );
329 t::lib::Mocks::mock_preference('HoldsQueueSkipClosed', 0);
331 $dbh->do("DELETE FROM circulation_rules");
332 Koha::CirculationRules->set_rule(
334 rule_name => 'holdallowed',
335 rule_value => 2,
336 branchcode => undef,
337 categorycode => undef,
338 itemtype => undef,
341 C4::HoldsQueue::CreateQueue();
342 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
343 is( @$holds_queue, 3, "Holds queue filling correct number for holds for default holds policy 'from any library'" );
345 # Test skipping hold picks for closed libraries without transport cost matrix
346 # At this point in the test, we have 3 rows in the holds queue
347 # one of which is coming from MPL. Let's enable HoldsQueueSkipClosed
348 # and use our previously created holiday for MPL
349 # When we run it again we should only have 2 rows in the holds queue
350 t::lib::Mocks::mock_preference( 'HoldsQueueSkipClosed', 1 );
351 C4::HoldsQueue::CreateQueue();
352 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
353 is( scalar( @$holds_queue ), 2, "Holds not filled with items from closed libraries" );
354 t::lib::Mocks::mock_preference( 'HoldsQueueSkipClosed', 0 );
356 ## Test LocalHoldsPriority
357 t::lib::Mocks::mock_preference('LocalHoldsPriority', 1);
359 $dbh->do("DELETE FROM circulation_rules");
360 Koha::CirculationRules->set_rule(
362 rule_name => 'holdallowed',
363 rule_value => 2,
364 branchcode => undef,
365 categorycode => undef,
366 itemtype => undef,
369 $dbh->do("DELETE FROM issues");
371 # Test homebranch = patron branch
372 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'HomeLibrary');
373 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'homebranch');
374 C4::Context->clear_syspref_cache();
375 $dbh->do("DELETE FROM reserves");
376 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
377 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
378 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[0], 3 );
380 $dbh->do("DELETE FROM items");
381 # barcode, homebranch, holdingbranch, itemtype
382 $items_insert_sth->execute( $barcode + 4, $branchcodes[2], $branchcodes[0] );
384 C4::HoldsQueue::CreateQueue();
385 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
386 is( $holds_queue->[0]->{cardnumber}, $borrower3->{cardnumber}, "Holds queue giving priority to patron who's home library matches item's home library");
388 ### Test branch transfer limits ###
389 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'HomeLibrary');
390 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'holdingbranch');
391 t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '1' );
392 C4::Context->clear_syspref_cache();
393 $dbh->do("DELETE FROM reserves");
394 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
395 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[1], 2 );
397 $dbh->do("DELETE FROM items");
398 # barcode, homebranch, holdingbranch, itemtype
399 $items_insert_sth->execute( $barcode, $branchcodes[2], $branchcodes[2] );
400 my $item = Koha::Items->find( { barcode => $barcode } );
402 my $limit1 = Koha::Item::Transfer::Limit->new(
404 toBranch => $branchcodes[0],
405 fromBranch => $branchcodes[2],
406 itemtype => $item->effective_itemtype,
408 )->store();
410 C4::HoldsQueue::CreateQueue();
411 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
412 is( $holds_queue->[0]->{cardnumber}, $borrower2->{cardnumber}, "Holds queue skips hold transfer that would violate branch transfer limits");
414 my $limit2 = Koha::Item::Transfer::Limit->new(
416 toBranch => $branchcodes[1],
417 fromBranch => $branchcodes[2],
418 itemtype => $item->effective_itemtype,
420 )->store();
422 C4::HoldsQueue::CreateQueue();
423 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
424 is( $holds_queue->[0]->{cardnumber}, undef, "Holds queue doesn't fill hold where all available items would violate branch transfer limits");
426 $limit1->delete();
427 $limit2->delete();
428 t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '0' );
429 ### END Test branch transfer limits ###
431 # Test holdingbranch = patron branch
432 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'HomeLibrary');
433 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'holdingbranch');
434 C4::Context->clear_syspref_cache();
435 $dbh->do("DELETE FROM reserves");
436 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
437 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
438 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[0], 3 );
440 $dbh->do("DELETE FROM items");
441 # barcode, homebranch, holdingbranch, itemtype
442 $items_insert_sth->execute( $barcode + 4, $branchcodes[0], $branchcodes[2] );
444 C4::HoldsQueue::CreateQueue();
445 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
446 is( $holds_queue->[0]->{cardnumber}, $borrower3->{cardnumber}, "Holds queue giving priority to patron who's home library matches item's holding library");
448 # Test holdingbranch = pickup branch
449 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'PickupLibrary');
450 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'holdingbranch');
451 C4::Context->clear_syspref_cache();
452 $dbh->do("DELETE FROM reserves");
453 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
454 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
455 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[2], 3 );
457 $dbh->do("DELETE FROM items");
458 # barcode, homebranch, holdingbranch, itemtype
459 $items_insert_sth->execute( $barcode + 4, $branchcodes[0], $branchcodes[2] );
461 C4::HoldsQueue::CreateQueue();
462 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
463 is( $holds_queue->[0]->{cardnumber}, $borrower3->{cardnumber}, "Holds queue giving priority to patron who's home library matches item's holding library");
465 # Test homebranch = pickup branch
466 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'PickupLibrary');
467 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'homebranch');
468 C4::Context->clear_syspref_cache();
469 $dbh->do("DELETE FROM reserves");
470 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
471 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
472 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[2], 3 );
474 $dbh->do("DELETE FROM items");
475 # barcode, homebranch, holdingbranch, itemtype
476 $items_insert_sth->execute( $barcode + 4, $branchcodes[2], $branchcodes[0] );
478 C4::HoldsQueue::CreateQueue();
479 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
480 is( $holds_queue->[0]->{cardnumber}, $borrower3->{cardnumber}, "Holds queue giving priority to patron who's home library matches item's holding library");
482 t::lib::Mocks::mock_preference('LocalHoldsPriority', 0);
483 ## End testing of LocalHoldsPriority
486 # Bug 14297
487 $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
488 $borrowernumber = $borrower3->{borrowernumber};
489 my $library_A = $library1->{branchcode};
490 my $library_B = $library2->{branchcode};
491 my $library_C = $borrower3->{branchcode};
492 $dbh->do("DELETE FROM reserves");
493 $dbh->do("DELETE FROM issues");
494 $dbh->do("DELETE FROM items");
495 $dbh->do("DELETE FROM biblio");
496 $dbh->do("DELETE FROM biblioitems");
497 $dbh->do("DELETE FROM transport_cost");
498 $dbh->do("DELETE FROM tmp_holdsqueue");
499 $dbh->do("DELETE FROM hold_fill_targets");
501 $dbh->do("
502 INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
505 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
506 or BAIL_OUT("Cannot find newly created biblio record");
508 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
510 $biblioitemnumber =
511 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
512 or BAIL_OUT("Cannot find newly created biblioitems record");
514 $dbh->do("
515 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
516 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
519 $dbh->do("
520 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
521 VALUES ($biblionumber, $biblioitemnumber, '$library_B', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
524 Koha::CirculationRules->set_rules(
526 branchcode => $library_A,
527 itemtype => $itemtype,
528 categorycode => undef,
529 rules => {
530 holdallowed => 2,
531 returnbranch => 'homebranch',
536 $dbh->do( "UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'",
537 undef, join( ',', $library_B, $library_A, $library_C ) );
538 $dbh->do( "UPDATE systempreferences SET value = 0 WHERE variable = 'RandomizeHoldsQueueWeight'" );
540 my $reserve_id = AddReserve ( $library_C, $borrowernumber, $biblionumber, '', 1 );
541 C4::HoldsQueue::CreateQueue();
542 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
543 is( @$holds_queue, 1, "Bug 14297 - Holds Queue building ignoring holds where pickup & home branch don't match and item is not from le");
544 # End Bug 14297
546 # Bug 15062
547 $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
548 $borrowernumber = $borrower2->{borrowernumber};
549 $library_A = $library1->{branchcode};
550 $library_B = $library2->{branchcode};
551 $dbh->do("DELETE FROM reserves");
552 $dbh->do("DELETE FROM issues");
553 $dbh->do("DELETE FROM items");
554 $dbh->do("DELETE FROM biblio");
555 $dbh->do("DELETE FROM biblioitems");
556 $dbh->do("DELETE FROM transport_cost");
557 $dbh->do("DELETE FROM tmp_holdsqueue");
558 $dbh->do("DELETE FROM hold_fill_targets");
560 t::lib::Mocks::mock_preference("UseTransportCostMatrix",1);
562 my $tc_rs = $schema->resultset('TransportCost');
563 $tc_rs->create({ frombranch => $library_A, tobranch => $library_B, cost => 0, disable_transfer => 1 });
564 $tc_rs->create({ frombranch => $library_B, tobranch => $library_A, cost => 0, disable_transfer => 1 });
566 $dbh->do("
567 INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
570 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
571 or BAIL_OUT("Cannot find newly created biblio record");
573 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
575 $biblioitemnumber =
576 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
577 or BAIL_OUT("Cannot find newly created biblioitems record");
579 $dbh->do("
580 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
581 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
584 $reserve_id = AddReserve ( $library_B, $borrowernumber, $biblionumber, '', 1 );
585 C4::HoldsQueue::CreateQueue();
586 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
587 is( @$holds_queue, 0, "Bug 15062 - Holds queue with Transport Cost Matrix will transfer item even if transfers disabled");
588 # End Bug 15062
590 # Test hold_fulfillment_policy
591 t::lib::Mocks::mock_preference( "UseTransportCostMatrix", 0 );
592 $borrowernumber = $borrower3->{borrowernumber};
593 $library_A = $library1->{branchcode};
594 $library_B = $library2->{branchcode};
595 $library_C = $library3->{branchcode};
596 $dbh->do("DELETE FROM reserves");
597 $dbh->do("DELETE FROM issues");
598 $dbh->do("DELETE FROM items");
599 $dbh->do("DELETE FROM biblio");
600 $dbh->do("DELETE FROM biblioitems");
601 $dbh->do("DELETE FROM transport_cost");
602 $dbh->do("DELETE FROM tmp_holdsqueue");
603 $dbh->do("DELETE FROM hold_fill_targets");
605 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
607 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
608 or BAIL_OUT("Cannot find newly created biblio record");
610 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
612 $biblioitemnumber =
613 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
614 or BAIL_OUT("Cannot find newly created biblioitems record");
616 $dbh->do("
617 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
618 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
621 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
622 $dbh->do("DELETE FROM circulation_rules");
623 Koha::CirculationRules->set_rules(
625 branchcode => undef,
626 itemtype => undef,
627 categorycode => undef,
628 rules => {
629 holdallowed => 2,
630 hold_fulfillment_policy => 'homebranch',
635 # Home branch matches pickup branch
636 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
637 C4::HoldsQueue::CreateQueue();
638 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
639 is( @$holds_queue, 1, "Hold where pickup branch matches home branch targeted" );
640 Koha::Holds->find( $reserve_id )->cancel;
642 # Holding branch matches pickup branch
643 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
644 C4::HoldsQueue::CreateQueue();
645 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
646 is( @$holds_queue, 0, "Hold where pickup ne home, pickup eq home not targeted" );
647 Koha::Holds->find( $reserve_id )->cancel;
649 # Neither branch matches pickup branch
650 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
651 C4::HoldsQueue::CreateQueue();
652 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
653 is( @$holds_queue, 0, "Hold where pickup ne home, pickup ne holding not targeted" );
654 Koha::Holds->find( $reserve_id )->cancel;
656 # With hold_fulfillment_policy = holdingbranch, hold should only be picked up if pickup branch = holdingbranch
657 $dbh->do("DELETE FROM circulation_rules");
658 Koha::CirculationRules->set_rules(
660 branchcode => undef,
661 itemtype => undef,
662 categorycode => undef,
663 rules => {
664 holdallowed => 2,
665 hold_fulfillment_policy => 'holdingbranch',
670 # Home branch matches pickup branch
671 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
672 C4::HoldsQueue::CreateQueue();
673 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
674 is( @$holds_queue, 0, "Hold where pickup eq home, pickup ne holding not targeted" );
675 Koha::Holds->find( $reserve_id )->cancel;
677 # Holding branch matches pickup branch
678 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
679 C4::HoldsQueue::CreateQueue();
680 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
681 is( @$holds_queue, 1, "Hold where pickup ne home, pickup eq holding targeted" );
682 Koha::Holds->find( $reserve_id )->cancel;
684 # Neither branch matches pickup branch
685 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
686 C4::HoldsQueue::CreateQueue();
687 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
688 is( @$holds_queue, 0, "Hold where pickup ne home, pickup ne holding not targeted" );
689 Koha::Holds->find( $reserve_id )->cancel;
691 # With hold_fulfillment_policy = any, hold should be pikcup up reguardless of matching home or holding branch
692 $dbh->do("DELETE FROM circulation_rules");
693 Koha::CirculationRules->set_rules(
695 branchcode => undef,
696 itemtype => undef,
697 categorycode => undef,
698 rules => {
699 holdallowed => 2,
700 hold_fulfillment_policy => 'any',
705 # Home branch matches pickup branch
706 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
707 C4::HoldsQueue::CreateQueue();
708 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
709 is( @$holds_queue, 1, "Hold where pickup eq home, pickup ne holding targeted" );
710 Koha::Holds->find( $reserve_id )->cancel;
712 # Holding branch matches pickup branch
713 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
714 C4::HoldsQueue::CreateQueue();
715 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
716 is( @$holds_queue, 1, "Hold where pickup ne home, pickup eq holding targeted" );
717 Koha::Holds->find( $reserve_id )->cancel;
719 # Neither branch matches pickup branch
720 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
721 C4::HoldsQueue::CreateQueue();
722 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
723 is( @$holds_queue, 1, "Hold where pickup ne home, pickup ne holding targeted" );
724 Koha::Holds->find( $reserve_id )->cancel;
726 # End testing hold_fulfillment_policy
728 # Test hold itemtype limit
729 t::lib::Mocks::mock_preference( "UseTransportCostMatrix", 0 );
730 my $wrong_itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
731 my $right_itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
732 $borrowernumber = $borrower3->{borrowernumber};
733 my $branchcode = $library1->{branchcode};
734 $dbh->do("DELETE FROM reserves");
735 $dbh->do("DELETE FROM issues");
736 $dbh->do("DELETE FROM items");
737 $dbh->do("DELETE FROM biblio");
738 $dbh->do("DELETE FROM biblioitems");
739 $dbh->do("DELETE FROM transport_cost");
740 $dbh->do("DELETE FROM tmp_holdsqueue");
741 $dbh->do("DELETE FROM hold_fill_targets");
743 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
745 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
746 or BAIL_OUT("Cannot find newly created biblio record");
748 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
750 $biblioitemnumber =
751 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
752 or BAIL_OUT("Cannot find newly created biblioitems record");
754 $dbh->do("
755 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
756 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$right_itemtype')
759 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
760 $dbh->do("DELETE FROM circulation_rules");
761 Koha::CirculationRules->set_rules(
763 branchcode => undef,
764 itemtype => undef,
765 categorycode => undef,
766 rules => {
767 holdallowed => 2,
768 hold_fulfillment_policy => 'any',
773 # Home branch matches pickup branch
774 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, $wrong_itemtype );
775 C4::HoldsQueue::CreateQueue();
776 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
777 is( @$holds_queue, 0, "Item with incorrect itemtype not targeted" );
778 Koha::Holds->find( $reserve_id )->cancel;
780 # Holding branch matches pickup branch
781 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, $right_itemtype );
782 C4::HoldsQueue::CreateQueue();
783 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
784 is( @$holds_queue, 1, "Item with matching itemtype is targeted" );
785 Koha::Holds->find( $reserve_id )->cancel;
787 # Neither branch matches pickup branch
788 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, undef );
789 C4::HoldsQueue::CreateQueue();
790 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
791 is( @$holds_queue, 1, "Item targeted when hold itemtype is not set" );
792 Koha::Holds->find( $reserve_id )->cancel;
794 # End testing hold itemtype limit
797 # Test Local Holds Priority - Bug 18001
798 t::lib::Mocks::mock_preference('LocalHoldsPriority', 1);
799 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'PickupLibrary');
800 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'homebranch');
802 $dbh->do("DELETE FROM tmp_holdsqueue");
803 $dbh->do("DELETE FROM hold_fill_targets");
804 $dbh->do("DELETE FROM reserves");
806 $item = Koha::Items->find( { biblionumber => $biblionumber } );
807 $item->holdingbranch( $item->homebranch );
808 $item->store();
810 my $item2 = Koha::Item->new( $item->unblessed );
811 $item2->itemnumber( undef );
812 $item2->store();
814 my $item3 = Koha::Item->new( $item->unblessed );
815 $item3->itemnumber( undef );
816 $item3->store();
818 $reserve_id = AddReserve( $item->homebranch, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, undef );
820 C4::HoldsQueue::CreateQueue();
822 my $queue_rs = $schema->resultset('TmpHoldsqueue');
823 is( $queue_rs->count(), 1, "Hold queue contains one hold from chosen from three possible items" );
825 subtest 'Trivial test for UpdateTransportCostMatrix' => sub {
826 plan tests => 1;
827 my $recs = [
828 { frombranch => $library1->{branchcode}, tobranch => $library2->{branchcode}, cost => 1, disable_transfer => 0 },
829 { frombranch => $library2->{branchcode}, tobranch => $library3->{branchcode}, cost => 0, disable_transfer => 1 },
831 C4::HoldsQueue::UpdateTransportCostMatrix( $recs );
832 is( $schema->resultset('TransportCost')->count, 2, 'UpdateTransportCostMatrix added two records' );
835 # Cleanup
836 $schema->storage->txn_rollback;
838 ### END Test holds queue builder does not violate holds policy ###
840 sub test_queue {
841 my ($test_name, $use_cost_matrix, $pick_branch, $hold_branch) = @_;
843 $test_name = "$test_name (".($use_cost_matrix ? "" : "don't ")."use cost matrix)";
845 $use_cost_matrix_sth->execute($use_cost_matrix);
846 C4::Context->clear_syspref_cache();
847 C4::HoldsQueue::CreateQueue();
849 my $results = $dbh->selectall_arrayref($test_sth, { Slice => {} }); # should be only one
850 my $r = $results->[0];
852 my $ok = is( $r->{pickbranch}, $pick_branch, "$test_name pick up branch");
853 $ok &&= is( $r->{holdingbranch}, $hold_branch, "$test_name holding branch")
854 if $hold_branch;
856 diag( "Wrong pick-up/hold for first target (pick_branch, hold_branch, reserves, hold_fill_targets, tmp_holdsqueue): "
857 . Dumper ($pick_branch, $hold_branch, map dump_records($_), qw(reserves hold_fill_targets tmp_holdsqueue)) )
858 unless $ok;
860 # Test enforcement of branch transfer limit
861 if ( $r->{pickbranch} ne $r->{holdingbranch} ) {
862 t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '1' );
863 my $limit = Koha::Item::Transfer::Limit->new(
865 toBranch => $r->{pickbranch},
866 fromBranch => $r->{holdingbranch},
867 itemtype => $r->{itype},
869 )->store();
870 C4::Context->clear_syspref_cache();
871 C4::HoldsQueue::CreateQueue();
872 $results = $dbh->selectall_arrayref( $test_sth, { Slice => {} } )
873 ; # should be only one
874 my $s = $results->[0];
875 isnt( $r->{holdingbranch}, $s->{holdingbranch}, 'Hold is not trapped for pickup at a branch that cannot be transferred to');
877 $limit->delete();
878 t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '0' );
879 C4::Context->clear_syspref_cache();
880 C4::HoldsQueue::CreateQueue();
885 sub dump_records {
886 my ($tablename) = @_;
887 return $dbh->selectall_arrayref("SELECT * from $tablename where borrowernumber = ?", { Slice => {} }, $borrowernumber);