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 => 38;
22 use t::lib::TestBuilder;
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;
37 my $builder = t::lib::TestBuilder->new;
39 my $library1 = $builder->build({
42 my $library2 = $builder->build({
45 my $library3 = $builder->build({
49 my $TITLE = "Test Holds Queue XXX";
51 my $borrower = $builder->build({
54 branchcode => $library1->{branchcode},
58 my $borrowernumber = $borrower->{borrowernumber};
59 # Set special (for this test) branches
60 my $borrower_branchcode = $borrower->{branchcode};
61 my @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode} );
62 my @other_branches = ( $library2->{branchcode}, $library3->{branchcode} );
63 my $least_cost_branch_code = pop @other_branches;
64 my $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
67 # Sysprefs and cost matrix
68 C4::Context->set_preference('HoldsQueueSkipClosed', 0);
69 $dbh->do("UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'", undef,
70 join( ',', @other_branches, $borrower_branchcode, $least_cost_branch_code));
71 $dbh->do("UPDATE systempreferences SET value = '0' WHERE variable = 'RandomizeHoldsQueueWeight'");
73 $dbh->do("DELETE FROM transport_cost");
74 my $transport_cost_insert_sth = $dbh->prepare("insert into transport_cost (frombranch, tobranch, cost) values (?, ?, ?)");
75 # Favour $least_cost_branch_code
76 $transport_cost_insert_sth->execute($borrower_branchcode, $least_cost_branch_code, 0.2);
77 $transport_cost_insert_sth->execute($least_cost_branch_code, $borrower_branchcode, 0.2);
78 my @b = @other_branches;
79 while ( my $b1 = shift @b ) {
80 foreach my $b2 ($borrower_branchcode, $least_cost_branch_code, @b) {
81 $transport_cost_insert_sth->execute($b1, $b2, 0.5);
82 $transport_cost_insert_sth->execute($b2, $b1, 0.5);
87 # Loanable items - all possible combinations of homebranch and holdingbranch
88 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated)
89 VALUES ('SER', 'Koha test', '$TITLE', '2011-02-01')");
90 my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
91 or BAIL_OUT("Cannot find newly created biblio record");
92 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype)
93 VALUES ($biblionumber, '', '$itemtype')");
94 my $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
95 or BAIL_OUT("Cannot find newly created biblioitems record");
97 my $items_insert_sth = $dbh->prepare("INSERT INTO items (biblionumber, biblioitemnumber, barcode, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
98 VALUES ($biblionumber, $biblioitemnumber, ?, ?, ?, 0, 0, 0, 0, NULL, '$itemtype')"); # CURRENT_DATE - 3)");
99 my $first_barcode = int(rand(1000000000000)); # XXX
100 my $barcode = $first_barcode;
101 foreach ( $borrower_branchcode, $least_cost_branch_code, @other_branches ) {
102 $items_insert_sth->execute($barcode++, $borrower_branchcode, $_);
103 $items_insert_sth->execute($barcode++, $_, $_);
104 $items_insert_sth->execute($barcode++, $_, $borrower_branchcode);
107 # Remove existing reserves, makes debugging easier
108 $dbh->do("DELETE FROM reserves");
109 my $bibitems = undef;
112 AddReserve ( $borrower_branchcode, $borrowernumber, $biblionumber, $bibitems, $priority );
113 # $resdate, $expdate, $notes, $title, $checkitem, $found
114 $dbh->do("UPDATE reserves SET reservedate = DATE_SUB( reservedate, INTERVAL 1 DAY )");
117 my $use_cost_matrix_sth = $dbh->prepare("UPDATE systempreferences SET value = ? WHERE variable = 'UseTransportCostMatrix'");
118 my $test_sth = $dbh->prepare("SELECT * FROM hold_fill_targets
119 JOIN tmp_holdsqueue USING (borrowernumber, biblionumber, itemnumber)
120 JOIN items USING (itemnumber)
121 WHERE borrowernumber = $borrowernumber");
123 # We have a book available homed in borrower branch, no point fiddling with AutomaticItemReturn
124 C4::Context->set_preference('AutomaticItemReturn', 0);
125 test_queue ('take from homebranch', 0, $borrower_branchcode, $borrower_branchcode);
126 test_queue ('take from homebranch', 1, $borrower_branchcode, $borrower_branchcode);
128 $dbh->do("DELETE FROM tmp_holdsqueue");
129 $dbh->do("DELETE FROM hold_fill_targets");
130 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode')");
131 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode'");
132 # test_queue will flush
133 C4::Context->set_preference('AutomaticItemReturn', 1);
134 # Not sure how to make this test more difficult - holding branch does not matter
136 $dbh->do("DELETE FROM tmp_holdsqueue");
137 $dbh->do("DELETE FROM hold_fill_targets");
138 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode')");
139 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode'");
140 C4::Context->set_preference('AutomaticItemReturn', 0);
141 # We have a book available held in borrower branch
142 test_queue ('take from holdingbranch', 0, $borrower_branchcode, $borrower_branchcode);
143 test_queue ('take from holdingbranch', 1, $borrower_branchcode, $borrower_branchcode);
145 $dbh->do("DELETE FROM tmp_holdsqueue");
146 $dbh->do("DELETE FROM hold_fill_targets");
147 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE holdingbranch = '$borrower_branchcode')");
148 $dbh->do("DELETE FROM items WHERE holdingbranch = '$borrower_branchcode'");
149 # No book available in borrower branch, pick according to the rules
150 # Frst branch from StaticHoldsQueueWeight
151 test_queue ('take from lowest cost branch', 0, $borrower_branchcode, $other_branches[0]);
152 test_queue ('take from lowest cost branch', 1, $borrower_branchcode, $least_cost_branch_code);
153 my $queue = C4::HoldsQueue::GetHoldsQueueItems($least_cost_branch_code) || [];
154 my $queue_item = $queue->[0];
156 && $queue_item->{pickbranch} eq $borrower_branchcode
157 && $queue_item->{holdingbranch} eq $least_cost_branch_code, "GetHoldsQueueItems" )
158 or diag( "Expected item for pick $borrower_branchcode, hold $least_cost_branch_code, got ".Dumper($queue_item) );
159 ok( exists($queue_item->{itype}), 'item type included in queued items list (bug 5825)' );
162 C4::HoldsQueue::least_cost_branch( 'B', [ 'A', 'B', 'C' ] ) eq 'B',
163 'C4::HoldsQueue::least_cost_branch returns the local branch if it is in the list of branches to pull from'
166 # XXX All this tests are for borrower branch pick-up.
167 # Maybe needs expanding to homebranch or holdingbranch pick-up.
169 $schema->txn_rollback;
172 ### Test holds queue builder does not violate holds policy ###
174 # Clear out existing rules relating to holdallowed
175 $dbh->do("DELETE FROM default_branch_circ_rules");
176 $dbh->do("DELETE FROM default_branch_item_rules");
177 $dbh->do("DELETE FROM default_circ_rules");
179 C4::Context->set_preference('UseTransportCostMatrix', 0);
181 $itemtype = Koha::ItemTypes->search->next->itemtype;
183 $library1 = $builder->build({
186 $library2 = $builder->build({
189 $library3 = $builder->build({
192 @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode} );
194 my $borrower1 = $builder->build({
195 source => 'Borrower',
197 branchcode => $branchcodes[0],
200 my $borrower2 = $builder->build({
201 source => 'Borrower',
203 branchcode => $branchcodes[1],
206 my $borrower3 = $builder->build({
207 source => 'Borrower',
209 branchcode => $branchcodes[2],
226 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
227 or BAIL_OUT("Cannot find newly created biblio record");
230 INSERT INTO biblioitems (
240 $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
241 or BAIL_OUT("Cannot find newly created biblioitems record");
243 $items_insert_sth = $dbh->prepare(qq{
270 # Create 3 items from 2 branches ( branches are for borrowers 1 and 2 respectively )
271 $barcode = int( rand(1000000000000) );
272 $items_insert_sth->execute( $barcode + 0, $branchcodes[0], $branchcodes[0] );
273 $items_insert_sth->execute( $barcode + 1, $branchcodes[1], $branchcodes[1] );
274 $items_insert_sth->execute( $barcode + 2, $branchcodes[1], $branchcodes[1] );
276 $dbh->do("DELETE FROM reserves");
277 my $sth = $dbh->prepare(q{
278 INSERT INTO reserves (
284 ) VALUES ( ?,?,?,?, CURRENT_DATE() )
286 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
287 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
288 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[0], 3 );
292 $dbh->do("DELETE FROM default_circ_rules");
293 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 1 )");
294 C4::HoldsQueue::CreateQueue();
295 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
296 is( @$holds_queue, 2, "Holds queue filling correct number for default holds policy 'from home library'" );
297 is( $holds_queue->[0]->{cardnumber}, $borrower1->{cardnumber}, "Holds queue filling 1st correct hold for default holds policy 'from home library'");
298 is( $holds_queue->[1]->{cardnumber}, $borrower2->{cardnumber}, "Holds queue filling 2nd correct hold for default holds policy 'from home library'");
300 # Test skipping hold picks for closed libraries.
301 # At this point in the test, we have 2 rows in the holds queue
302 # 1 of which is coming from MPL. Let's enable HoldsQueueSkipClosed
303 # and make today a holiday for MPL. When we run it again we should only
304 # have 1 row in the holds queue
305 C4::Context->set_preference('HoldsQueueSkipClosed', 1);
306 my $today = dt_from_string();
307 C4::Calendar->new( branchcode => $branchcodes[0] )->insert_single_holiday(
308 day => $today->day(),
309 month => $today->month(),
310 year => $today->year(),
312 description => "$today",
314 # If the test below is removed, aother tests using the holiday will fail. For some reason if we call is_holiday now
315 # the holiday will get set in cache correctly, but not if we let C4::HoldsQueue call is_holiday instead.
316 is( Koha::Calendar->new( branchcode => $branchcodes[0] )->is_holiday( $today ), 1, 'Is today a holiday for pickup branch' );
317 C4::HoldsQueue::CreateQueue();
318 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
319 is( scalar( @$holds_queue ), 1, "Holds not filled with items from closed libraries" );
320 C4::Context->set_preference('HoldsQueueSkipClosed', 0);
322 $dbh->do("DELETE FROM default_circ_rules");
323 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 2 )");
324 C4::HoldsQueue::CreateQueue();
325 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
326 is( @$holds_queue, 3, "Holds queue filling correct number for holds for default holds policy 'from any library'" );
328 # Test skipping hold picks for closed libraries without transport cost matrix
329 # At this point in the test, we have 3 rows in the holds queue
330 # one of which is coming from MPL. Let's enable HoldsQueueSkipClosed
331 # and use our previously created holiday for MPL
332 # When we run it again we should only have 2 rows in the holds queue
333 C4::Context->set_preference( 'HoldsQueueSkipClosed', 1 );
334 C4::HoldsQueue::CreateQueue();
335 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
336 is( scalar( @$holds_queue ), 2, "Holds not filled with items from closed libraries" );
337 C4::Context->set_preference( 'HoldsQueueSkipClosed', 0 );
340 $itemtype = Koha::ItemTypes->search->next->itemtype;
341 $borrowernumber = $borrower3->{borrowernumber};
342 my $library_A = $library1->{branchcode};
343 my $library_B = $library2->{branchcode};
344 my $library_C = $borrower3->{branchcode};
345 $dbh->do("DELETE FROM reserves");
346 $dbh->do("DELETE FROM issues");
347 $dbh->do("DELETE FROM items");
348 $dbh->do("DELETE FROM biblio");
349 $dbh->do("DELETE FROM biblioitems");
350 $dbh->do("DELETE FROM transport_cost");
351 $dbh->do("DELETE FROM tmp_holdsqueue");
352 $dbh->do("DELETE FROM hold_fill_targets");
353 $dbh->do("DELETE FROM default_branch_circ_rules");
354 $dbh->do("DELETE FROM default_branch_item_rules");
355 $dbh->do("DELETE FROM default_circ_rules");
356 $dbh->do("DELETE FROM branch_item_rules");
359 INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
362 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
363 or BAIL_OUT("Cannot find newly created biblio record");
365 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
368 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
369 or BAIL_OUT("Cannot find newly created biblioitems record");
372 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
373 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
377 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
378 VALUES ($biblionumber, $biblioitemnumber, '$library_B', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
382 INSERT INTO branch_item_rules ( branchcode, itemtype, holdallowed, returnbranch ) VALUES
383 ( '$library_A', '$itemtype', 2, 'homebranch' ), ( '$library_B', '$itemtype', 1, 'homebranch' );
386 $dbh->do( "UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'",
387 undef, join( ',', $library_B, $library_A, $library_C ) );
388 $dbh->do( "UPDATE systempreferences SET value = 0 WHERE variable = 'RandomizeHoldsQueueWeight'" );
390 my $reserve_id = AddReserve ( $library_C, $borrowernumber, $biblionumber, '', 1 );
391 C4::HoldsQueue::CreateQueue();
392 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
393 is( @$holds_queue, 1, "Bug 14297 - Holds Queue building ignoring holds where pickup & home branch don't match and item is not from le");
397 $itemtype = Koha::ItemTypes->search->next->itemtype;
398 $borrowernumber = $borrower2->{borrowernumber};
399 $library_A = $library1->{branchcode};
400 $library_B = $library2->{branchcode};
401 $dbh->do("DELETE FROM reserves");
402 $dbh->do("DELETE FROM issues");
403 $dbh->do("DELETE FROM items");
404 $dbh->do("DELETE FROM biblio");
405 $dbh->do("DELETE FROM biblioitems");
406 $dbh->do("DELETE FROM transport_cost");
407 $dbh->do("DELETE FROM tmp_holdsqueue");
408 $dbh->do("DELETE FROM hold_fill_targets");
409 $dbh->do("DELETE FROM default_branch_circ_rules");
410 $dbh->do("DELETE FROM default_branch_item_rules");
411 $dbh->do("DELETE FROM default_circ_rules");
412 $dbh->do("DELETE FROM branch_item_rules");
414 C4::Context->set_preference("UseTransportCostMatrix",1);
416 my $tc_rs = $schema->resultset('TransportCost');
417 $tc_rs->create({ frombranch => $library_A, tobranch => $library_B, cost => 0, disable_transfer => 1 });
418 $tc_rs->create({ frombranch => $library_B, tobranch => $library_A, cost => 0, disable_transfer => 1 });
421 INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
424 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
425 or BAIL_OUT("Cannot find newly created biblio record");
427 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
430 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
431 or BAIL_OUT("Cannot find newly created biblioitems record");
434 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
435 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
438 $reserve_id = AddReserve ( $library_B, $borrowernumber, $biblionumber, '', 1 );
439 C4::HoldsQueue::CreateQueue();
440 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
441 is( @$holds_queue, 0, "Bug 15062 - Holds queue with Transport Cost Matrix will transfer item even if transfers disabled");
444 # Test hold_fulfillment_policy
445 C4::Context->set_preference( "UseTransportCostMatrix", 0 );
446 $borrowernumber = $borrower3->{borrowernumber};
447 $library_A = $library1->{branchcode};
448 $library_B = $library2->{branchcode};
449 $library_C = $library3->{branchcode};
450 $dbh->do("DELETE FROM reserves");
451 $dbh->do("DELETE FROM issues");
452 $dbh->do("DELETE FROM items");
453 $dbh->do("DELETE FROM biblio");
454 $dbh->do("DELETE FROM biblioitems");
455 $dbh->do("DELETE FROM transport_cost");
456 $dbh->do("DELETE FROM tmp_holdsqueue");
457 $dbh->do("DELETE FROM hold_fill_targets");
458 $dbh->do("DELETE FROM default_branch_circ_rules");
459 $dbh->do("DELETE FROM default_branch_item_rules");
460 $dbh->do("DELETE FROM default_circ_rules");
461 $dbh->do("DELETE FROM branch_item_rules");
463 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
465 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
466 or BAIL_OUT("Cannot find newly created biblio record");
468 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
471 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
472 or BAIL_OUT("Cannot find newly created biblioitems record");
475 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
476 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
479 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
480 $dbh->do("DELETE FROM default_circ_rules");
481 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'homebranch' )");
483 # Home branch matches pickup branch
484 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
485 C4::HoldsQueue::CreateQueue();
486 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
487 is( @$holds_queue, 1, "Hold where pickup branch matches home branch targeted" );
488 CancelReserve( { reserve_id => $reserve_id } );
490 # Holding branch matches pickup branch
491 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
492 C4::HoldsQueue::CreateQueue();
493 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
494 is( @$holds_queue, 0, "Hold where pickup ne home, pickup eq home not targeted" );
495 CancelReserve( { reserve_id => $reserve_id } );
497 # Neither branch matches pickup branch
498 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
499 C4::HoldsQueue::CreateQueue();
500 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
501 is( @$holds_queue, 0, "Hold where pickup ne home, pickup ne holding not targeted" );
502 CancelReserve( { reserve_id => $reserve_id } );
504 # With hold_fulfillment_policy = holdingbranch, hold should only be picked up if pickup branch = holdingbranch
505 $dbh->do("DELETE FROM default_circ_rules");
506 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'holdingbranch' )");
508 # Home branch matches pickup branch
509 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
510 C4::HoldsQueue::CreateQueue();
511 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
512 is( @$holds_queue, 0, "Hold where pickup eq home, pickup ne holding not targeted" );
513 CancelReserve( { reserve_id => $reserve_id } );
515 # Holding branch matches pickup branch
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, 1, "Hold where pickup ne home, pickup eq holding targeted" );
520 CancelReserve( { reserve_id => $reserve_id } );
522 # Neither branch matches pickup branch
523 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
524 C4::HoldsQueue::CreateQueue();
525 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
526 is( @$holds_queue, 0, "Hold where pickup ne home, pickup ne holding not targeted" );
527 CancelReserve( { reserve_id => $reserve_id } );
529 # With hold_fulfillment_policy = any, hold should be pikcup up reguardless of matching home or holding branch
530 $dbh->do("DELETE FROM default_circ_rules");
531 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'any' )");
533 # Home branch matches pickup branch
534 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
535 C4::HoldsQueue::CreateQueue();
536 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
537 is( @$holds_queue, 1, "Hold where pickup eq home, pickup ne holding targeted" );
538 CancelReserve( { reserve_id => $reserve_id } );
540 # Holding branch matches pickup branch
541 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
542 C4::HoldsQueue::CreateQueue();
543 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
544 is( @$holds_queue, 1, "Hold where pickup ne home, pickup eq holding targeted" );
545 CancelReserve( { reserve_id => $reserve_id } );
547 # Neither branch matches pickup branch
548 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
549 C4::HoldsQueue::CreateQueue();
550 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
551 is( @$holds_queue, 1, "Hold where pickup ne home, pickup ne holding targeted" );
552 CancelReserve( { reserve_id => $reserve_id } );
554 # End testing hold_fulfillment_policy
556 # Test hold itemtype limit
557 C4::Context->set_preference( "UseTransportCostMatrix", 0 );
558 my @itemtypes = Koha::ItemTypes->search();
559 my $wrong_itemtype = $itemtypes[0]->itemtype;
560 my $right_itemtype = $itemtypes[1]->itemtype;
561 $borrowernumber = $borrower3->{borrowernumber};
562 my $branchcode = $library1->{branchcode};
563 $dbh->do("DELETE FROM reserves");
564 $dbh->do("DELETE FROM issues");
565 $dbh->do("DELETE FROM items");
566 $dbh->do("DELETE FROM biblio");
567 $dbh->do("DELETE FROM biblioitems");
568 $dbh->do("DELETE FROM transport_cost");
569 $dbh->do("DELETE FROM tmp_holdsqueue");
570 $dbh->do("DELETE FROM hold_fill_targets");
571 $dbh->do("DELETE FROM default_branch_circ_rules");
572 $dbh->do("DELETE FROM default_branch_item_rules");
573 $dbh->do("DELETE FROM default_circ_rules");
574 $dbh->do("DELETE FROM branch_item_rules");
576 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
578 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
579 or BAIL_OUT("Cannot find newly created biblio record");
581 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
584 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
585 or BAIL_OUT("Cannot find newly created biblioitems record");
588 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
589 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$right_itemtype')
592 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
593 $dbh->do("DELETE FROM default_circ_rules");
594 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'any' )");
596 # Home branch matches pickup branch
597 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, $wrong_itemtype );
598 C4::HoldsQueue::CreateQueue();
599 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
600 is( @$holds_queue, 0, "Item with incorrect itemtype not targeted" );
601 CancelReserve( { reserve_id => $reserve_id } );
603 # Holding branch matches pickup branch
604 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, $right_itemtype );
605 C4::HoldsQueue::CreateQueue();
606 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
607 is( @$holds_queue, 1, "Item with matching itemtype is targeted" );
608 CancelReserve( { reserve_id => $reserve_id } );
610 # Neither branch matches pickup branch
611 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, undef );
612 C4::HoldsQueue::CreateQueue();
613 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
614 is( @$holds_queue, 1, "Item targeted when hold itemtype is not set" );
615 CancelReserve( { reserve_id => $reserve_id } );
617 # End testing hold itemtype limit
620 $schema->storage->txn_rollback;
622 ### END Test holds queue builder does not violate holds policy ###
625 my ($test_name, $use_cost_matrix, $pick_branch, $hold_branch) = @_;
627 $test_name = "$test_name (".($use_cost_matrix ? "" : "don't ")."use cost matrix)";
629 $use_cost_matrix_sth->execute($use_cost_matrix);
630 C4::Context->clear_syspref_cache();
631 C4::HoldsQueue::CreateQueue();
633 my $results = $dbh->selectall_arrayref($test_sth, { Slice => {} }); # should be only one
634 my $r = $results->[0];
636 my $ok = is( $r->{pickbranch}, $pick_branch, "$test_name pick up branch");
637 $ok &&= is( $r->{holdingbranch}, $hold_branch, "$test_name holding branch")
640 diag( "Wrong pick-up/hold for first target (pick_branch, hold_branch, reserves, hold_fill_targets, tmp_holdsqueue): "
641 . Dumper ($pick_branch, $hold_branch, map dump_records($_), qw(reserves hold_fill_targets tmp_holdsqueue)) )
646 my ($tablename) = @_;
647 return $dbh->selectall_arrayref("SELECT * from $tablename where borrowernumber = ?", { Slice => {} }, $borrowernumber);