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
11 use Test::More tests => 43;
21 use t::lib::TestBuilder;
26 use lib $FindBin::Bin;
27 use_ok('C4::Reserves');
28 use_ok('C4::HoldsQueue');
31 my $schema = Koha::Database->schema;
32 $schema->storage->txn_begin;
33 my $dbh = C4::Context->dbh;
35 my $builder = t::lib::TestBuilder->new;
37 my $library1 = $builder->build({
40 my $library2 = $builder->build({
43 my $library3 = $builder->build({
47 my $TITLE = "Test Holds Queue XXX";
49 my $borrower = $builder->build({
52 branchcode => $library1->{branchcode},
56 my $borrowernumber = $borrower->{borrowernumber};
57 # Set special (for this test) branches
58 my $borrower_branchcode = $borrower->{branchcode};
59 my @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode} );
60 my @other_branches = ( $library2->{branchcode}, $library3->{branchcode} );
61 my $least_cost_branch_code = pop @other_branches;
62 my $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
65 # Sysprefs and cost matrix
66 t::lib::Mocks::mock_preference('HoldsQueueSkipClosed', 0);
67 t::lib::Mocks::mock_preference('LocalHoldsPriority', 0);
68 $dbh->do("UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'", undef,
69 join( ',', @other_branches, $borrower_branchcode, $least_cost_branch_code));
70 $dbh->do("UPDATE systempreferences SET value = '0' WHERE variable = 'RandomizeHoldsQueueWeight'");
72 $dbh->do("DELETE FROM transport_cost");
73 my $transport_cost_insert_sth = $dbh->prepare("insert into transport_cost (frombranch, tobranch, cost) values (?, ?, ?)");
74 # Favour $least_cost_branch_code
75 $transport_cost_insert_sth->execute($borrower_branchcode, $least_cost_branch_code, 0.2);
76 $transport_cost_insert_sth->execute($least_cost_branch_code, $borrower_branchcode, 0.2);
77 my @b = @other_branches;
78 while ( my $b1 = shift @b ) {
79 foreach my $b2 ($borrower_branchcode, $least_cost_branch_code, @b) {
80 $transport_cost_insert_sth->execute($b1, $b2, 0.5);
81 $transport_cost_insert_sth->execute($b2, $b1, 0.5);
86 # Loanable items - all possible combinations of homebranch and holdingbranch
87 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated)
88 VALUES ('SER', 'Koha test', '$TITLE', '2011-02-01')");
89 my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
90 or BAIL_OUT("Cannot find newly created biblio record");
91 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype)
92 VALUES ($biblionumber, '', '$itemtype')");
93 my $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
94 or BAIL_OUT("Cannot find newly created biblioitems record");
96 my $items_insert_sth = $dbh->prepare("INSERT INTO items (biblionumber, biblioitemnumber, barcode, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
97 VALUES ($biblionumber, $biblioitemnumber, ?, ?, ?, 0, 0, 0, 0, NULL, '$itemtype')"); # CURRENT_DATE - 3)");
98 my $first_barcode = int(rand(1000000000000)); # XXX
99 my $barcode = $first_barcode;
100 foreach ( $borrower_branchcode, $least_cost_branch_code, @other_branches ) {
101 $items_insert_sth->execute($barcode++, $borrower_branchcode, $_);
102 $items_insert_sth->execute($barcode++, $_, $_);
103 $items_insert_sth->execute($barcode++, $_, $borrower_branchcode);
106 # Remove existing reserves, makes debugging easier
107 $dbh->do("DELETE FROM reserves");
108 my $bibitems = undef;
111 AddReserve ( $borrower_branchcode, $borrowernumber, $biblionumber, $bibitems, $priority );
112 # $resdate, $expdate, $notes, $title, $checkitem, $found
113 $dbh->do("UPDATE reserves SET reservedate = DATE_SUB( reservedate, INTERVAL 1 DAY )");
116 my $use_cost_matrix_sth = $dbh->prepare("UPDATE systempreferences SET value = ? WHERE variable = 'UseTransportCostMatrix'");
117 my $test_sth = $dbh->prepare("SELECT * FROM hold_fill_targets
118 JOIN tmp_holdsqueue USING (borrowernumber, biblionumber, itemnumber)
119 JOIN items USING (itemnumber)
120 WHERE borrowernumber = $borrowernumber");
122 # We have a book available homed in borrower branch, no point fiddling with AutomaticItemReturn
123 t::lib::Mocks::mock_preference('AutomaticItemReturn', 0);
124 test_queue ('take from homebranch', 0, $borrower_branchcode, $borrower_branchcode);
125 test_queue ('take from homebranch', 1, $borrower_branchcode, $borrower_branchcode);
127 $dbh->do("DELETE FROM tmp_holdsqueue");
128 $dbh->do("DELETE FROM hold_fill_targets");
129 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode')");
130 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode'");
131 # test_queue will flush
132 t::lib::Mocks::mock_preference('AutomaticItemReturn', 1);
133 # Not sure how to make this test more difficult - holding branch does not matter
135 $dbh->do("DELETE FROM tmp_holdsqueue");
136 $dbh->do("DELETE FROM hold_fill_targets");
137 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode')");
138 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode'");
139 t::lib::Mocks::mock_preference('AutomaticItemReturn', 0);
140 # We have a book available held in borrower branch
141 test_queue ('take from holdingbranch', 0, $borrower_branchcode, $borrower_branchcode);
142 test_queue ('take from holdingbranch', 1, $borrower_branchcode, $borrower_branchcode);
144 $dbh->do("DELETE FROM tmp_holdsqueue");
145 $dbh->do("DELETE FROM hold_fill_targets");
146 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE holdingbranch = '$borrower_branchcode')");
147 $dbh->do("DELETE FROM items WHERE holdingbranch = '$borrower_branchcode'");
148 # No book available in borrower branch, pick according to the rules
149 # Frst branch from StaticHoldsQueueWeight
150 test_queue ('take from lowest cost branch', 0, $borrower_branchcode, $other_branches[0]);
151 test_queue ('take from lowest cost branch', 1, $borrower_branchcode, $least_cost_branch_code);
152 my $queue = C4::HoldsQueue::GetHoldsQueueItems($least_cost_branch_code) || [];
153 my $queue_item = $queue->[0];
155 && $queue_item->{pickbranch} eq $borrower_branchcode
156 && $queue_item->{holdingbranch} eq $least_cost_branch_code, "GetHoldsQueueItems" )
157 or diag( "Expected item for pick $borrower_branchcode, hold $least_cost_branch_code, got ".Dumper($queue_item) );
158 ok( exists($queue_item->{itype}), 'item type included in queued items list (bug 5825)' );
161 C4::HoldsQueue::least_cost_branch( 'B', [ 'A', 'B', 'C' ] ) eq 'B',
162 'C4::HoldsQueue::least_cost_branch returns the local branch if it is in the list of branches to pull from'
165 # XXX All this tests are for borrower branch pick-up.
166 # Maybe needs expanding to homebranch or holdingbranch pick-up.
168 $schema->txn_rollback;
171 ### Test holds queue builder does not violate holds policy ###
173 # Clear out existing rules relating to holdallowed
174 $dbh->do("DELETE FROM default_branch_circ_rules");
175 $dbh->do("DELETE FROM default_branch_item_rules");
176 $dbh->do("DELETE FROM default_circ_rules");
178 t::lib::Mocks::mock_preference('UseTransportCostMatrix', 0);
180 $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
182 $library1 = $builder->build({
185 $library2 = $builder->build({
188 $library3 = $builder->build({
191 @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode} );
193 my $borrower1 = $builder->build({
194 source => 'Borrower',
196 branchcode => $branchcodes[0],
199 my $borrower2 = $builder->build({
200 source => 'Borrower',
202 branchcode => $branchcodes[1],
205 my $borrower3 = $builder->build({
206 source => 'Borrower',
208 branchcode => $branchcodes[2],
225 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
226 or BAIL_OUT("Cannot find newly created biblio record");
229 INSERT INTO biblioitems (
239 $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
240 or BAIL_OUT("Cannot find newly created biblioitems record");
242 $items_insert_sth = $dbh->prepare(qq{
269 # Create 3 items from 2 branches ( branches are for borrowers 1 and 2 respectively )
270 $barcode = int( rand(1000000000000) );
271 $items_insert_sth->execute( $barcode + 0, $branchcodes[0], $branchcodes[0] );
272 $items_insert_sth->execute( $barcode + 1, $branchcodes[1], $branchcodes[1] );
273 $items_insert_sth->execute( $barcode + 2, $branchcodes[1], $branchcodes[1] );
275 $dbh->do("DELETE FROM reserves");
276 my $sth = $dbh->prepare(q{
277 INSERT INTO reserves (
283 ) VALUES ( ?,?,?,?, CURRENT_DATE() )
285 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
286 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
287 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[0], 3 );
291 $dbh->do("DELETE FROM default_circ_rules");
292 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 1 )");
293 C4::HoldsQueue::CreateQueue();
294 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
295 is( @$holds_queue, 2, "Holds queue filling correct number for default holds policy 'from home library'" );
296 is( $holds_queue->[0]->{cardnumber}, $borrower1->{cardnumber}, "Holds queue filling 1st correct hold for default holds policy 'from home library'");
297 is( $holds_queue->[1]->{cardnumber}, $borrower2->{cardnumber}, "Holds queue filling 2nd correct hold for default holds policy 'from home library'");
299 # Test skipping hold picks for closed libraries.
300 # At this point in the test, we have 2 rows in the holds queue
301 # 1 of which is coming from MPL. Let's enable HoldsQueueSkipClosed
302 # and make today a holiday for MPL. When we run it again we should only
303 # have 1 row in the holds queue
304 t::lib::Mocks::mock_preference('HoldsQueueSkipClosed', 1);
305 my $today = dt_from_string();
306 C4::Calendar->new( branchcode => $branchcodes[0] )->insert_single_holiday(
307 day => $today->day(),
308 month => $today->month(),
309 year => $today->year(),
311 description => "$today",
313 # If the test below is removed, aother tests using the holiday will fail. For some reason if we call is_holiday now
314 # the holiday will get set in cache correctly, but not if we let C4::HoldsQueue call is_holiday instead.
315 is( Koha::Calendar->new( branchcode => $branchcodes[0] )->is_holiday( $today ), 1, 'Is today a holiday for pickup branch' );
316 C4::HoldsQueue::CreateQueue();
317 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
318 is( scalar( @$holds_queue ), 1, "Holds not filled with items from closed libraries" );
319 t::lib::Mocks::mock_preference('HoldsQueueSkipClosed', 0);
321 $dbh->do("DELETE FROM default_circ_rules");
322 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 2 )");
323 C4::HoldsQueue::CreateQueue();
324 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
325 is( @$holds_queue, 3, "Holds queue filling correct number for holds for default holds policy 'from any library'" );
327 # Test skipping hold picks for closed libraries without transport cost matrix
328 # At this point in the test, we have 3 rows in the holds queue
329 # one of which is coming from MPL. Let's enable HoldsQueueSkipClosed
330 # and use our previously created holiday for MPL
331 # When we run it again we should only have 2 rows in the holds queue
332 t::lib::Mocks::mock_preference( 'HoldsQueueSkipClosed', 1 );
333 C4::HoldsQueue::CreateQueue();
334 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
335 is( scalar( @$holds_queue ), 2, "Holds not filled with items from closed libraries" );
336 t::lib::Mocks::mock_preference( 'HoldsQueueSkipClosed', 0 );
338 ## Test LocalHoldsPriority
339 t::lib::Mocks::mock_preference('LocalHoldsPriority', 1);
341 $dbh->do("DELETE FROM default_circ_rules");
342 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 2 )");
343 $dbh->do("DELETE FROM issues");
345 # Test homebranch = patron branch
346 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'HomeLibrary');
347 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'homebranch');
348 C4::Context->clear_syspref_cache();
349 $dbh->do("DELETE FROM reserves");
350 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
351 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
352 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[0], 3 );
354 $dbh->do("DELETE FROM items");
355 # barcode, homebranch, holdingbranch, itemtype
356 $items_insert_sth->execute( $barcode + 4, $branchcodes[2], $branchcodes[0] );
358 C4::HoldsQueue::CreateQueue();
359 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
360 is( $holds_queue->[0]->{cardnumber}, $borrower3->{cardnumber}, "Holds queue giving priority to patron who's home library matches item's home library");
362 # Test holdingbranch = patron branch
363 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'HomeLibrary');
364 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'holdingbranch');
365 C4::Context->clear_syspref_cache();
366 $dbh->do("DELETE FROM reserves");
367 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
368 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
369 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[0], 3 );
371 $dbh->do("DELETE FROM items");
372 # barcode, homebranch, holdingbranch, itemtype
373 $items_insert_sth->execute( $barcode + 4, $branchcodes[0], $branchcodes[2] );
375 C4::HoldsQueue::CreateQueue();
376 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
377 is( $holds_queue->[0]->{cardnumber}, $borrower3->{cardnumber}, "Holds queue giving priority to patron who's home library matches item's holding library");
379 # Test holdingbranch = pickup branch
380 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'PickupLibrary');
381 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'holdingbranch');
382 C4::Context->clear_syspref_cache();
383 $dbh->do("DELETE FROM reserves");
384 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
385 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
386 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[2], 3 );
388 $dbh->do("DELETE FROM items");
389 # barcode, homebranch, holdingbranch, itemtype
390 $items_insert_sth->execute( $barcode + 4, $branchcodes[0], $branchcodes[2] );
392 C4::HoldsQueue::CreateQueue();
393 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
394 is( $holds_queue->[0]->{cardnumber}, $borrower3->{cardnumber}, "Holds queue giving priority to patron who's home library matches item's holding library");
396 # Test homebranch = pickup branch
397 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'PickupLibrary');
398 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'homebranch');
399 C4::Context->clear_syspref_cache();
400 $dbh->do("DELETE FROM reserves");
401 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
402 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
403 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[2], 3 );
405 $dbh->do("DELETE FROM items");
406 # barcode, homebranch, holdingbranch, itemtype
407 $items_insert_sth->execute( $barcode + 4, $branchcodes[2], $branchcodes[0] );
409 C4::HoldsQueue::CreateQueue();
410 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
411 is( $holds_queue->[0]->{cardnumber}, $borrower3->{cardnumber}, "Holds queue giving priority to patron who's home library matches item's holding library");
413 t::lib::Mocks::mock_preference('LocalHoldsPriority', 0);
414 ## End testing of LocalHoldsPriority
418 $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
419 $borrowernumber = $borrower3->{borrowernumber};
420 my $library_A = $library1->{branchcode};
421 my $library_B = $library2->{branchcode};
422 my $library_C = $borrower3->{branchcode};
423 $dbh->do("DELETE FROM reserves");
424 $dbh->do("DELETE FROM issues");
425 $dbh->do("DELETE FROM items");
426 $dbh->do("DELETE FROM biblio");
427 $dbh->do("DELETE FROM biblioitems");
428 $dbh->do("DELETE FROM transport_cost");
429 $dbh->do("DELETE FROM tmp_holdsqueue");
430 $dbh->do("DELETE FROM hold_fill_targets");
431 $dbh->do("DELETE FROM default_branch_circ_rules");
432 $dbh->do("DELETE FROM default_branch_item_rules");
433 $dbh->do("DELETE FROM default_circ_rules");
434 $dbh->do("DELETE FROM branch_item_rules");
437 INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
440 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
441 or BAIL_OUT("Cannot find newly created biblio record");
443 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
446 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
447 or BAIL_OUT("Cannot find newly created biblioitems record");
450 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
451 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
455 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
456 VALUES ($biblionumber, $biblioitemnumber, '$library_B', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
460 INSERT INTO branch_item_rules ( branchcode, itemtype, holdallowed, returnbranch ) VALUES
461 ( '$library_A', '$itemtype', 2, 'homebranch' ), ( '$library_B', '$itemtype', 1, 'homebranch' );
464 $dbh->do( "UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'",
465 undef, join( ',', $library_B, $library_A, $library_C ) );
466 $dbh->do( "UPDATE systempreferences SET value = 0 WHERE variable = 'RandomizeHoldsQueueWeight'" );
468 my $reserve_id = AddReserve ( $library_C, $borrowernumber, $biblionumber, '', 1 );
469 C4::HoldsQueue::CreateQueue();
470 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
471 is( @$holds_queue, 1, "Bug 14297 - Holds Queue building ignoring holds where pickup & home branch don't match and item is not from le");
475 $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
476 $borrowernumber = $borrower2->{borrowernumber};
477 $library_A = $library1->{branchcode};
478 $library_B = $library2->{branchcode};
479 $dbh->do("DELETE FROM reserves");
480 $dbh->do("DELETE FROM issues");
481 $dbh->do("DELETE FROM items");
482 $dbh->do("DELETE FROM biblio");
483 $dbh->do("DELETE FROM biblioitems");
484 $dbh->do("DELETE FROM transport_cost");
485 $dbh->do("DELETE FROM tmp_holdsqueue");
486 $dbh->do("DELETE FROM hold_fill_targets");
487 $dbh->do("DELETE FROM default_branch_circ_rules");
488 $dbh->do("DELETE FROM default_branch_item_rules");
489 $dbh->do("DELETE FROM default_circ_rules");
490 $dbh->do("DELETE FROM branch_item_rules");
492 t::lib::Mocks::mock_preference("UseTransportCostMatrix",1);
494 my $tc_rs = $schema->resultset('TransportCost');
495 $tc_rs->create({ frombranch => $library_A, tobranch => $library_B, cost => 0, disable_transfer => 1 });
496 $tc_rs->create({ frombranch => $library_B, tobranch => $library_A, cost => 0, disable_transfer => 1 });
499 INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
502 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
503 or BAIL_OUT("Cannot find newly created biblio record");
505 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
508 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
509 or BAIL_OUT("Cannot find newly created biblioitems record");
512 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
513 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
516 $reserve_id = AddReserve ( $library_B, $borrowernumber, $biblionumber, '', 1 );
517 C4::HoldsQueue::CreateQueue();
518 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
519 is( @$holds_queue, 0, "Bug 15062 - Holds queue with Transport Cost Matrix will transfer item even if transfers disabled");
522 # Test hold_fulfillment_policy
523 t::lib::Mocks::mock_preference( "UseTransportCostMatrix", 0 );
524 $borrowernumber = $borrower3->{borrowernumber};
525 $library_A = $library1->{branchcode};
526 $library_B = $library2->{branchcode};
527 $library_C = $library3->{branchcode};
528 $dbh->do("DELETE FROM reserves");
529 $dbh->do("DELETE FROM issues");
530 $dbh->do("DELETE FROM items");
531 $dbh->do("DELETE FROM biblio");
532 $dbh->do("DELETE FROM biblioitems");
533 $dbh->do("DELETE FROM transport_cost");
534 $dbh->do("DELETE FROM tmp_holdsqueue");
535 $dbh->do("DELETE FROM hold_fill_targets");
536 $dbh->do("DELETE FROM default_branch_circ_rules");
537 $dbh->do("DELETE FROM default_branch_item_rules");
538 $dbh->do("DELETE FROM default_circ_rules");
539 $dbh->do("DELETE FROM branch_item_rules");
541 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
543 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
544 or BAIL_OUT("Cannot find newly created biblio record");
546 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
549 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
550 or BAIL_OUT("Cannot find newly created biblioitems record");
553 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
554 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
557 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
558 $dbh->do("DELETE FROM default_circ_rules");
559 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'homebranch' )");
561 # Home branch matches pickup branch
562 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
563 C4::HoldsQueue::CreateQueue();
564 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
565 is( @$holds_queue, 1, "Hold where pickup branch matches home branch targeted" );
566 CancelReserve( { reserve_id => $reserve_id } );
568 # Holding branch matches pickup branch
569 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
570 C4::HoldsQueue::CreateQueue();
571 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
572 is( @$holds_queue, 0, "Hold where pickup ne home, pickup eq home not targeted" );
573 CancelReserve( { reserve_id => $reserve_id } );
575 # Neither branch matches pickup branch
576 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
577 C4::HoldsQueue::CreateQueue();
578 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
579 is( @$holds_queue, 0, "Hold where pickup ne home, pickup ne holding not targeted" );
580 CancelReserve( { reserve_id => $reserve_id } );
582 # With hold_fulfillment_policy = holdingbranch, hold should only be picked up if pickup branch = holdingbranch
583 $dbh->do("DELETE FROM default_circ_rules");
584 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'holdingbranch' )");
586 # Home branch matches pickup branch
587 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
588 C4::HoldsQueue::CreateQueue();
589 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
590 is( @$holds_queue, 0, "Hold where pickup eq home, pickup ne holding not targeted" );
591 CancelReserve( { reserve_id => $reserve_id } );
593 # Holding branch matches pickup branch
594 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
595 C4::HoldsQueue::CreateQueue();
596 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
597 is( @$holds_queue, 1, "Hold where pickup ne home, pickup eq holding targeted" );
598 CancelReserve( { reserve_id => $reserve_id } );
600 # Neither branch matches pickup branch
601 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
602 C4::HoldsQueue::CreateQueue();
603 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
604 is( @$holds_queue, 0, "Hold where pickup ne home, pickup ne holding not targeted" );
605 CancelReserve( { reserve_id => $reserve_id } );
607 # With hold_fulfillment_policy = any, hold should be pikcup up reguardless of matching home or holding branch
608 $dbh->do("DELETE FROM default_circ_rules");
609 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'any' )");
611 # Home branch matches pickup branch
612 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
613 C4::HoldsQueue::CreateQueue();
614 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
615 is( @$holds_queue, 1, "Hold where pickup eq home, pickup ne holding targeted" );
616 CancelReserve( { reserve_id => $reserve_id } );
618 # Holding branch matches pickup branch
619 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
620 C4::HoldsQueue::CreateQueue();
621 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
622 is( @$holds_queue, 1, "Hold where pickup ne home, pickup eq holding targeted" );
623 CancelReserve( { reserve_id => $reserve_id } );
625 # Neither branch matches pickup branch
626 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
627 C4::HoldsQueue::CreateQueue();
628 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
629 is( @$holds_queue, 1, "Hold where pickup ne home, pickup ne holding targeted" );
630 CancelReserve( { reserve_id => $reserve_id } );
632 # End testing hold_fulfillment_policy
634 # Test hold itemtype limit
635 t::lib::Mocks::mock_preference( "UseTransportCostMatrix", 0 );
636 my $wrong_itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
637 my $right_itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
638 $borrowernumber = $borrower3->{borrowernumber};
639 my $branchcode = $library1->{branchcode};
640 $dbh->do("DELETE FROM reserves");
641 $dbh->do("DELETE FROM issues");
642 $dbh->do("DELETE FROM items");
643 $dbh->do("DELETE FROM biblio");
644 $dbh->do("DELETE FROM biblioitems");
645 $dbh->do("DELETE FROM transport_cost");
646 $dbh->do("DELETE FROM tmp_holdsqueue");
647 $dbh->do("DELETE FROM hold_fill_targets");
648 $dbh->do("DELETE FROM default_branch_circ_rules");
649 $dbh->do("DELETE FROM default_branch_item_rules");
650 $dbh->do("DELETE FROM default_circ_rules");
651 $dbh->do("DELETE FROM branch_item_rules");
653 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
655 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
656 or BAIL_OUT("Cannot find newly created biblio record");
658 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
661 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
662 or BAIL_OUT("Cannot find newly created biblioitems record");
665 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
666 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$right_itemtype')
669 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
670 $dbh->do("DELETE FROM default_circ_rules");
671 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'any' )");
673 # Home branch matches pickup branch
674 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, $wrong_itemtype );
675 C4::HoldsQueue::CreateQueue();
676 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
677 is( @$holds_queue, 0, "Item with incorrect itemtype not targeted" );
678 CancelReserve( { reserve_id => $reserve_id } );
680 # Holding branch matches pickup branch
681 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, $right_itemtype );
682 C4::HoldsQueue::CreateQueue();
683 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
684 is( @$holds_queue, 1, "Item with matching itemtype is targeted" );
685 CancelReserve( { reserve_id => $reserve_id } );
687 # Neither branch matches pickup branch
688 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, undef );
689 C4::HoldsQueue::CreateQueue();
690 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
691 is( @$holds_queue, 1, "Item targeted when hold itemtype is not set" );
692 CancelReserve( { reserve_id => $reserve_id } );
694 # End testing hold itemtype limit
697 # Test Local Holds Priority - Bug 18001
698 t::lib::Mocks::mock_preference('LocalHoldsPriority', 1);
699 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'PickupLibrary');
700 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'homebranch');
702 $dbh->do("DELETE FROM tmp_holdsqueue");
703 $dbh->do("DELETE FROM hold_fill_targets");
704 $dbh->do("DELETE FROM reserves");
705 $dbh->do("DELETE FROM default_branch_circ_rules");
706 $dbh->do("DELETE FROM default_branch_item_rules");
707 $dbh->do("DELETE FROM default_circ_rules");
708 $dbh->do("DELETE FROM branch_item_rules");
710 my $item = Koha::Items->find( { biblionumber => $biblionumber } );
711 $item->holdingbranch( $item->homebranch );
714 my $item2 = Koha::Item->new( $item->unblessed );
715 $item2->itemnumber( undef );
718 my $item3 = Koha::Item->new( $item->unblessed );
719 $item3->itemnumber( undef );
722 $reserve_id = AddReserve( $item->homebranch, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, undef );
724 C4::HoldsQueue::CreateQueue();
726 my $queue_rs = $schema->resultset('TmpHoldsqueue');
727 is( $queue_rs->count(), 1, "Hold queue contains one hold from chosen from three possible items" );
730 $schema->storage->txn_rollback;
732 ### END Test holds queue builder does not violate holds policy ###
735 my ($test_name, $use_cost_matrix, $pick_branch, $hold_branch) = @_;
737 $test_name = "$test_name (".($use_cost_matrix ? "" : "don't ")."use cost matrix)";
739 $use_cost_matrix_sth->execute($use_cost_matrix);
740 C4::Context->clear_syspref_cache();
741 C4::HoldsQueue::CreateQueue();
743 my $results = $dbh->selectall_arrayref($test_sth, { Slice => {} }); # should be only one
744 my $r = $results->[0];
746 my $ok = is( $r->{pickbranch}, $pick_branch, "$test_name pick up branch");
747 $ok &&= is( $r->{holdingbranch}, $hold_branch, "$test_name holding branch")
750 diag( "Wrong pick-up/hold for first target (pick_branch, hold_branch, reserves, hold_fill_targets, tmp_holdsqueue): "
751 . Dumper ($pick_branch, $hold_branch, map dump_records($_), qw(reserves hold_fill_targets tmp_holdsqueue)) )
756 my ($tablename) = @_;
757 return $dbh->selectall_arrayref("SELECT * from $tablename where borrowernumber = ?", { Slice => {} }, $borrowernumber);