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 = Koha::ItemTypes->search({ notforloan => 1 })->next;
65 $itemtype or BAIL_OUT("No adequate itemtype"); #FIXME Should be $itemtype = $itemtype->itemtype
68 # Sysprefs and cost matrix
69 C4::Context->set_preference('HoldsQueueSkipClosed', 0);
70 $dbh->do("UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'", undef,
71 join( ',', @other_branches, $borrower_branchcode, $least_cost_branch_code));
72 $dbh->do("UPDATE systempreferences SET value = '0' WHERE variable = 'RandomizeHoldsQueueWeight'");
74 $dbh->do("DELETE FROM transport_cost");
75 my $transport_cost_insert_sth = $dbh->prepare("insert into transport_cost (frombranch, tobranch, cost) values (?, ?, ?)");
76 # Favour $least_cost_branch_code
77 $transport_cost_insert_sth->execute($borrower_branchcode, $least_cost_branch_code, 0.2);
78 $transport_cost_insert_sth->execute($least_cost_branch_code, $borrower_branchcode, 0.2);
79 my @b = @other_branches;
80 while ( my $b1 = shift @b ) {
81 foreach my $b2 ($borrower_branchcode, $least_cost_branch_code, @b) {
82 $transport_cost_insert_sth->execute($b1, $b2, 0.5);
83 $transport_cost_insert_sth->execute($b2, $b1, 0.5);
88 # Loanable items - all possible combinations of homebranch and holdingbranch
89 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated)
90 VALUES ('SER', 'Koha test', '$TITLE', '2011-02-01')");
91 my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
92 or BAIL_OUT("Cannot find newly created biblio record");
93 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype)
94 VALUES ($biblionumber, '', '$itemtype')");
95 my $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
96 or BAIL_OUT("Cannot find newly created biblioitems record");
98 my $items_insert_sth = $dbh->prepare("INSERT INTO items (biblionumber, biblioitemnumber, barcode, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
99 VALUES ($biblionumber, $biblioitemnumber, ?, ?, ?, 0, 0, 0, 0, NULL, '$itemtype')"); # CURRENT_DATE - 3)");
100 my $first_barcode = int(rand(1000000000000)); # XXX
101 my $barcode = $first_barcode;
102 foreach ( $borrower_branchcode, $least_cost_branch_code, @other_branches ) {
103 $items_insert_sth->execute($barcode++, $borrower_branchcode, $_);
104 $items_insert_sth->execute($barcode++, $_, $_);
105 $items_insert_sth->execute($barcode++, $_, $borrower_branchcode);
108 # Remove existing reserves, makes debugging easier
109 $dbh->do("DELETE FROM reserves");
110 my $bibitems = undef;
113 AddReserve ( $borrower_branchcode, $borrowernumber, $biblionumber, $bibitems, $priority );
114 # $resdate, $expdate, $notes, $title, $checkitem, $found
115 $dbh->do("UPDATE reserves SET reservedate = DATE_SUB( reservedate, INTERVAL 1 DAY )");
118 my $use_cost_matrix_sth = $dbh->prepare("UPDATE systempreferences SET value = ? WHERE variable = 'UseTransportCostMatrix'");
119 my $test_sth = $dbh->prepare("SELECT * FROM hold_fill_targets
120 JOIN tmp_holdsqueue USING (borrowernumber, biblionumber, itemnumber)
121 JOIN items USING (itemnumber)
122 WHERE borrowernumber = $borrowernumber");
124 # We have a book available homed in borrower branch, no point fiddling with AutomaticItemReturn
125 C4::Context->set_preference('AutomaticItemReturn', 0);
126 test_queue ('take from homebranch', 0, $borrower_branchcode, $borrower_branchcode);
127 test_queue ('take from homebranch', 1, $borrower_branchcode, $borrower_branchcode);
129 $dbh->do("DELETE FROM tmp_holdsqueue");
130 $dbh->do("DELETE FROM hold_fill_targets");
131 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode')");
132 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode'");
133 # test_queue will flush
134 C4::Context->set_preference('AutomaticItemReturn', 1);
135 # Not sure how to make this test more difficult - holding branch does not matter
137 $dbh->do("DELETE FROM tmp_holdsqueue");
138 $dbh->do("DELETE FROM hold_fill_targets");
139 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode')");
140 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode'");
141 C4::Context->set_preference('AutomaticItemReturn', 0);
142 # We have a book available held in borrower branch
143 test_queue ('take from holdingbranch', 0, $borrower_branchcode, $borrower_branchcode);
144 test_queue ('take from holdingbranch', 1, $borrower_branchcode, $borrower_branchcode);
146 $dbh->do("DELETE FROM tmp_holdsqueue");
147 $dbh->do("DELETE FROM hold_fill_targets");
148 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE holdingbranch = '$borrower_branchcode')");
149 $dbh->do("DELETE FROM items WHERE holdingbranch = '$borrower_branchcode'");
150 # No book available in borrower branch, pick according to the rules
151 # Frst branch from StaticHoldsQueueWeight
152 test_queue ('take from lowest cost branch', 0, $borrower_branchcode, $other_branches[0]);
153 test_queue ('take from lowest cost branch', 1, $borrower_branchcode, $least_cost_branch_code);
154 my $queue = C4::HoldsQueue::GetHoldsQueueItems($least_cost_branch_code) || [];
155 my $queue_item = $queue->[0];
157 && $queue_item->{pickbranch} eq $borrower_branchcode
158 && $queue_item->{holdingbranch} eq $least_cost_branch_code, "GetHoldsQueueItems" )
159 or diag( "Expected item for pick $borrower_branchcode, hold $least_cost_branch_code, got ".Dumper($queue_item) );
160 ok( exists($queue_item->{itype}), 'item type included in queued items list (bug 5825)' );
163 C4::HoldsQueue::least_cost_branch( 'B', [ 'A', 'B', 'C' ] ) eq 'B',
164 'C4::HoldsQueue::least_cost_branch returns the local branch if it is in the list of branches to pull from'
167 # XXX All this tests are for borrower branch pick-up.
168 # Maybe needs expanding to homebranch or holdingbranch pick-up.
170 $schema->txn_rollback;
173 ### Test holds queue builder does not violate holds policy ###
175 # Clear out existing rules relating to holdallowed
176 $dbh->do("DELETE FROM default_branch_circ_rules");
177 $dbh->do("DELETE FROM default_branch_item_rules");
178 $dbh->do("DELETE FROM default_circ_rules");
180 C4::Context->set_preference('UseTransportCostMatrix', 0);
182 $itemtype = Koha::ItemTypes->search->next->itemtype;
184 $library1 = $builder->build({
187 $library2 = $builder->build({
190 $library3 = $builder->build({
193 @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode} );
195 my $borrower1 = $builder->build({
196 source => 'Borrower',
198 branchcode => $branchcodes[0],
201 my $borrower2 = $builder->build({
202 source => 'Borrower',
204 branchcode => $branchcodes[1],
207 my $borrower3 = $builder->build({
208 source => 'Borrower',
210 branchcode => $branchcodes[2],
227 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
228 or BAIL_OUT("Cannot find newly created biblio record");
231 INSERT INTO biblioitems (
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{
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 (
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 );
293 $dbh->do("DELETE FROM default_circ_rules");
294 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 1 )");
295 C4::HoldsQueue::CreateQueue();
296 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
297 is( @$holds_queue, 2, "Holds queue filling correct number for default holds policy 'from home library'" );
298 is( $holds_queue->[0]->{cardnumber}, $borrower1->{cardnumber}, "Holds queue filling 1st correct hold for default holds policy 'from home library'");
299 is( $holds_queue->[1]->{cardnumber}, $borrower2->{cardnumber}, "Holds queue filling 2nd correct hold for default holds policy 'from home library'");
301 # Test skipping hold picks for closed libraries.
302 # At this point in the test, we have 2 rows in the holds queue
303 # 1 of which is coming from MPL. Let's enable HoldsQueueSkipClosed
304 # and make today a holiday for MPL. When we run it again we should only
305 # have 1 row in the holds queue
306 C4::Context->set_preference('HoldsQueueSkipClosed', 1);
307 my $today = dt_from_string();
308 C4::Calendar->new( branchcode => $branchcodes[0] )->insert_single_holiday(
309 day => $today->day(),
310 month => $today->month(),
311 year => $today->year(),
313 description => "$today",
315 # If the test below is removed, aother tests using the holiday will fail. For some reason if we call is_holiday now
316 # the holiday will get set in cache correctly, but not if we let C4::HoldsQueue call is_holiday instead.
317 is( Koha::Calendar->new( branchcode => $branchcodes[0] )->is_holiday( $today ), 1, 'Is today a holiday for pickup branch' );
318 C4::HoldsQueue::CreateQueue();
319 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
320 is( scalar( @$holds_queue ), 1, "Holds not filled with items from closed libraries" );
321 C4::Context->set_preference('HoldsQueueSkipClosed', 0);
323 $dbh->do("DELETE FROM default_circ_rules");
324 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 2 )");
325 C4::HoldsQueue::CreateQueue();
326 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
327 is( @$holds_queue, 3, "Holds queue filling correct number for holds for default holds policy 'from any library'" );
329 # Test skipping hold picks for closed libraries without transport cost matrix
330 # At this point in the test, we have 3 rows in the holds queue
331 # one of which is coming from MPL. Let's enable HoldsQueueSkipClosed
332 # and use our previously created holiday for MPL
333 # When we run it again we should only have 2 rows in the holds queue
334 C4::Context->set_preference( 'HoldsQueueSkipClosed', 1 );
335 C4::HoldsQueue::CreateQueue();
336 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
337 is( scalar( @$holds_queue ), 2, "Holds not filled with items from closed libraries" );
338 C4::Context->set_preference( 'HoldsQueueSkipClosed', 0 );
341 $itemtype = Koha::ItemTypes->search->next->itemtype;
342 $borrowernumber = $borrower3->{borrowernumber};
343 my $library_A = $library1->{branchcode};
344 my $library_B = $library2->{branchcode};
345 my $library_C = $borrower3->{branchcode};
346 $dbh->do("DELETE FROM reserves");
347 $dbh->do("DELETE FROM issues");
348 $dbh->do("DELETE FROM items");
349 $dbh->do("DELETE FROM biblio");
350 $dbh->do("DELETE FROM biblioitems");
351 $dbh->do("DELETE FROM transport_cost");
352 $dbh->do("DELETE FROM tmp_holdsqueue");
353 $dbh->do("DELETE FROM hold_fill_targets");
354 $dbh->do("DELETE FROM default_branch_circ_rules");
355 $dbh->do("DELETE FROM default_branch_item_rules");
356 $dbh->do("DELETE FROM default_circ_rules");
357 $dbh->do("DELETE FROM branch_item_rules");
360 INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
363 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
364 or BAIL_OUT("Cannot find newly created biblio record");
366 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
369 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
370 or BAIL_OUT("Cannot find newly created biblioitems record");
373 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
374 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
378 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
379 VALUES ($biblionumber, $biblioitemnumber, '$library_B', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
383 INSERT INTO branch_item_rules ( branchcode, itemtype, holdallowed, returnbranch ) VALUES
384 ( '$library_A', '$itemtype', 2, 'homebranch' ), ( '$library_B', '$itemtype', 1, 'homebranch' );
387 $dbh->do( "UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'",
388 undef, join( ',', $library_B, $library_A, $library_C ) );
389 $dbh->do( "UPDATE systempreferences SET value = 0 WHERE variable = 'RandomizeHoldsQueueWeight'" );
391 my $reserve_id = AddReserve ( $library_C, $borrowernumber, $biblionumber, '', 1 );
392 C4::HoldsQueue::CreateQueue();
393 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
394 is( @$holds_queue, 1, "Bug 14297 - Holds Queue building ignoring holds where pickup & home branch don't match and item is not from le");
398 $itemtype = Koha::ItemTypes->search->next->itemtype;
399 $borrowernumber = $borrower2->{borrowernumber};
400 $library_A = $library1->{branchcode};
401 $library_B = $library2->{branchcode};
402 $dbh->do("DELETE FROM reserves");
403 $dbh->do("DELETE FROM issues");
404 $dbh->do("DELETE FROM items");
405 $dbh->do("DELETE FROM biblio");
406 $dbh->do("DELETE FROM biblioitems");
407 $dbh->do("DELETE FROM transport_cost");
408 $dbh->do("DELETE FROM tmp_holdsqueue");
409 $dbh->do("DELETE FROM hold_fill_targets");
410 $dbh->do("DELETE FROM default_branch_circ_rules");
411 $dbh->do("DELETE FROM default_branch_item_rules");
412 $dbh->do("DELETE FROM default_circ_rules");
413 $dbh->do("DELETE FROM branch_item_rules");
415 C4::Context->set_preference("UseTransportCostMatrix",1);
417 my $tc_rs = $schema->resultset('TransportCost');
418 $tc_rs->create({ frombranch => $library_A, tobranch => $library_B, cost => 0, disable_transfer => 1 });
419 $tc_rs->create({ frombranch => $library_B, tobranch => $library_A, cost => 0, disable_transfer => 1 });
422 INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
425 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
426 or BAIL_OUT("Cannot find newly created biblio record");
428 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
431 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
432 or BAIL_OUT("Cannot find newly created biblioitems record");
435 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
436 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
439 $reserve_id = AddReserve ( $library_B, $borrowernumber, $biblionumber, '', 1 );
440 C4::HoldsQueue::CreateQueue();
441 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
442 is( @$holds_queue, 0, "Bug 15062 - Holds queue with Transport Cost Matrix will transfer item even if transfers disabled");
445 # Test hold_fulfillment_policy
446 C4::Context->set_preference( "UseTransportCostMatrix", 0 );
447 $borrowernumber = $borrower3->{borrowernumber};
448 $library_A = $library1->{branchcode};
449 $library_B = $library2->{branchcode};
450 $library_C = $library3->{branchcode};
451 $dbh->do("DELETE FROM reserves");
452 $dbh->do("DELETE FROM issues");
453 $dbh->do("DELETE FROM items");
454 $dbh->do("DELETE FROM biblio");
455 $dbh->do("DELETE FROM biblioitems");
456 $dbh->do("DELETE FROM transport_cost");
457 $dbh->do("DELETE FROM tmp_holdsqueue");
458 $dbh->do("DELETE FROM hold_fill_targets");
459 $dbh->do("DELETE FROM default_branch_circ_rules");
460 $dbh->do("DELETE FROM default_branch_item_rules");
461 $dbh->do("DELETE FROM default_circ_rules");
462 $dbh->do("DELETE FROM branch_item_rules");
464 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
466 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
467 or BAIL_OUT("Cannot find newly created biblio record");
469 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
472 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
473 or BAIL_OUT("Cannot find newly created biblioitems record");
476 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
477 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
480 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
481 $dbh->do("DELETE FROM default_circ_rules");
482 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'homebranch' )");
484 # Home branch matches pickup branch
485 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
486 C4::HoldsQueue::CreateQueue();
487 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
488 is( @$holds_queue, 1, "Hold where pickup branch matches home branch targeted" );
489 CancelReserve( { reserve_id => $reserve_id } );
491 # Holding branch matches pickup branch
492 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
493 C4::HoldsQueue::CreateQueue();
494 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
495 is( @$holds_queue, 0, "Hold where pickup ne home, pickup eq home not targeted" );
496 CancelReserve( { reserve_id => $reserve_id } );
498 # Neither branch matches pickup branch
499 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
500 C4::HoldsQueue::CreateQueue();
501 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
502 is( @$holds_queue, 0, "Hold where pickup ne home, pickup ne holding not targeted" );
503 CancelReserve( { reserve_id => $reserve_id } );
505 # With hold_fulfillment_policy = holdingbranch, hold should only be picked up if pickup branch = holdingbranch
506 $dbh->do("DELETE FROM default_circ_rules");
507 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'holdingbranch' )");
509 # Home branch matches pickup branch
510 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
511 C4::HoldsQueue::CreateQueue();
512 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
513 is( @$holds_queue, 0, "Hold where pickup eq home, pickup ne holding not targeted" );
514 CancelReserve( { reserve_id => $reserve_id } );
516 # Holding branch matches pickup branch
517 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
518 C4::HoldsQueue::CreateQueue();
519 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
520 is( @$holds_queue, 1, "Hold where pickup ne home, pickup eq holding targeted" );
521 CancelReserve( { reserve_id => $reserve_id } );
523 # Neither branch matches pickup branch
524 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
525 C4::HoldsQueue::CreateQueue();
526 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
527 is( @$holds_queue, 0, "Hold where pickup ne home, pickup ne holding not targeted" );
528 CancelReserve( { reserve_id => $reserve_id } );
530 # With hold_fulfillment_policy = any, hold should be pikcup up reguardless of matching home or holding branch
531 $dbh->do("DELETE FROM default_circ_rules");
532 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'any' )");
534 # Home branch matches pickup branch
535 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
536 C4::HoldsQueue::CreateQueue();
537 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
538 is( @$holds_queue, 1, "Hold where pickup eq home, pickup ne holding targeted" );
539 CancelReserve( { reserve_id => $reserve_id } );
541 # Holding branch matches pickup branch
542 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
543 C4::HoldsQueue::CreateQueue();
544 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
545 is( @$holds_queue, 1, "Hold where pickup ne home, pickup eq holding targeted" );
546 CancelReserve( { reserve_id => $reserve_id } );
548 # Neither branch matches pickup branch
549 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
550 C4::HoldsQueue::CreateQueue();
551 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
552 is( @$holds_queue, 1, "Hold where pickup ne home, pickup ne holding targeted" );
553 CancelReserve( { reserve_id => $reserve_id } );
555 # End testing hold_fulfillment_policy
557 # Test hold itemtype limit
558 C4::Context->set_preference( "UseTransportCostMatrix", 0 );
559 my @itemtypes = Koha::ItemTypes->search();
560 my $wrong_itemtype = $itemtypes[0]->itemtype;
561 my $right_itemtype = $itemtypes[1]->itemtype;
562 $borrowernumber = $borrower3->{borrowernumber};
563 my $branchcode = $library1->{branchcode};
564 $dbh->do("DELETE FROM reserves");
565 $dbh->do("DELETE FROM issues");
566 $dbh->do("DELETE FROM items");
567 $dbh->do("DELETE FROM biblio");
568 $dbh->do("DELETE FROM biblioitems");
569 $dbh->do("DELETE FROM transport_cost");
570 $dbh->do("DELETE FROM tmp_holdsqueue");
571 $dbh->do("DELETE FROM hold_fill_targets");
572 $dbh->do("DELETE FROM default_branch_circ_rules");
573 $dbh->do("DELETE FROM default_branch_item_rules");
574 $dbh->do("DELETE FROM default_circ_rules");
575 $dbh->do("DELETE FROM branch_item_rules");
577 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
579 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
580 or BAIL_OUT("Cannot find newly created biblio record");
582 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
585 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
586 or BAIL_OUT("Cannot find newly created biblioitems record");
589 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
590 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$right_itemtype')
593 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
594 $dbh->do("DELETE FROM default_circ_rules");
595 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'any' )");
597 # Home branch matches pickup branch
598 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, $wrong_itemtype );
599 C4::HoldsQueue::CreateQueue();
600 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
601 is( @$holds_queue, 0, "Item with incorrect itemtype not targeted" );
602 CancelReserve( { reserve_id => $reserve_id } );
604 # Holding branch matches pickup branch
605 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, $right_itemtype );
606 C4::HoldsQueue::CreateQueue();
607 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
608 is( @$holds_queue, 1, "Item with matching itemtype is targeted" );
609 CancelReserve( { reserve_id => $reserve_id } );
611 # Neither branch matches pickup branch
612 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, undef );
613 C4::HoldsQueue::CreateQueue();
614 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
615 is( @$holds_queue, 1, "Item targeted when hold itemtype is not set" );
616 CancelReserve( { reserve_id => $reserve_id } );
618 # End testing hold itemtype limit
621 $schema->storage->txn_rollback;
623 ### END Test holds queue builder does not violate holds policy ###
626 my ($test_name, $use_cost_matrix, $pick_branch, $hold_branch) = @_;
628 $test_name = "$test_name (".($use_cost_matrix ? "" : "don't ")."use cost matrix)";
630 $use_cost_matrix_sth->execute($use_cost_matrix);
631 C4::Context->clear_syspref_cache();
632 C4::HoldsQueue::CreateQueue();
634 my $results = $dbh->selectall_arrayref($test_sth, { Slice => {} }); # should be only one
635 my $r = $results->[0];
637 my $ok = is( $r->{pickbranch}, $pick_branch, "$test_name pick up branch");
638 $ok &&= is( $r->{holdingbranch}, $hold_branch, "$test_name holding branch")
641 diag( "Wrong pick-up/hold for first target (pick_branch, hold_branch, reserves, hold_fill_targets, tmp_holdsqueue): "
642 . Dumper ($pick_branch, $hold_branch, map dump_records($_), qw(reserves hold_fill_targets tmp_holdsqueue)) )
647 my ($tablename) = @_;
648 return $dbh->selectall_arrayref("SELECT * from $tablename where borrowernumber = ?", { Slice => {} }, $borrowernumber);