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 => 54;
21 use Koha::CirculationRules;
23 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;
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({
46 my $library2 = $builder->build({
49 my $library3 = $builder->build({
53 my $TITLE = "Test Holds Queue XXX";
55 my $borrower = $builder->build({
58 branchcode => $library1->{branchcode},
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};
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;
119 branchcode => $borrower_branchcode,
120 borrowernumber => $borrowernumber,
121 biblionumber => $biblionumber,
122 priority => $priority,
125 # $resdate, $expdate, $notes, $title, $checkitem, $found
126 $dbh->do("UPDATE reserves SET reservedate = DATE_SUB( reservedate, INTERVAL 1 DAY )");
129 my $use_cost_matrix_sth = $dbh->prepare("UPDATE systempreferences SET value = ? WHERE variable = 'UseTransportCostMatrix'");
130 my $test_sth = $dbh->prepare("SELECT * FROM hold_fill_targets
131 JOIN tmp_holdsqueue USING (borrowernumber, biblionumber, itemnumber)
132 JOIN items USING (itemnumber)
133 WHERE borrowernumber = $borrowernumber");
135 # We have a book available homed in borrower branch, no point fiddling with AutomaticItemReturn
136 t::lib::Mocks::mock_preference('AutomaticItemReturn', 0);
137 test_queue ('take from homebranch', 0, $borrower_branchcode, $borrower_branchcode);
138 test_queue ('take from homebranch', 1, $borrower_branchcode, $borrower_branchcode);
140 $dbh->do("DELETE FROM tmp_holdsqueue");
141 $dbh->do("DELETE FROM hold_fill_targets");
142 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode')");
143 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode'");
144 # test_queue will flush
145 t::lib::Mocks::mock_preference('AutomaticItemReturn', 1);
146 # Not sure how to make this test more difficult - holding branch does not matter
148 $dbh->do("DELETE FROM tmp_holdsqueue");
149 $dbh->do("DELETE FROM hold_fill_targets");
150 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode')");
151 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode'");
152 t::lib::Mocks::mock_preference('AutomaticItemReturn', 0);
153 # We have a book available held in borrower branch
154 test_queue ('take from holdingbranch', 0, $borrower_branchcode, $borrower_branchcode);
155 test_queue ('take from holdingbranch', 1, $borrower_branchcode, $borrower_branchcode);
157 $dbh->do("DELETE FROM tmp_holdsqueue");
158 $dbh->do("DELETE FROM hold_fill_targets");
159 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE holdingbranch = '$borrower_branchcode')");
160 $dbh->do("DELETE FROM items WHERE holdingbranch = '$borrower_branchcode'");
161 # No book available in borrower branch, pick according to the rules
162 # Frst branch from StaticHoldsQueueWeight
163 test_queue ('take from lowest cost branch', 0, $borrower_branchcode, $other_branches[0]);
164 test_queue ('take from lowest cost branch', 1, $borrower_branchcode, $least_cost_branch_code);
165 my $queue = C4::HoldsQueue::GetHoldsQueueItems($least_cost_branch_code) || [];
166 my $queue_item = $queue->[0];
168 && $queue_item->{pickbranch} eq $borrower_branchcode
169 && $queue_item->{holdingbranch} eq $least_cost_branch_code, "GetHoldsQueueItems" )
170 or diag( "Expected item for pick $borrower_branchcode, hold $least_cost_branch_code, got ".Dumper($queue_item) );
171 ok( exists($queue_item->{itype}), 'item type included in queued items list (bug 5825)' );
174 C4::HoldsQueue::least_cost_branch( 'B', [ 'A', 'B', 'C' ] ) eq 'B',
175 'C4::HoldsQueue::least_cost_branch returns the local branch if it is in the list of branches to pull from'
178 # XXX All this tests are for borrower branch pick-up.
179 # Maybe needs expanding to homebranch or holdingbranch pick-up.
181 $schema->txn_rollback;
184 ### Test holds queue builder does not violate holds policy ###
186 # Clear out existing rules relating to holdallowed
187 $dbh->do("DELETE FROM circulation_rules");
189 t::lib::Mocks::mock_preference('UseTransportCostMatrix', 0);
191 $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
193 $library1 = $builder->build({
196 $library2 = $builder->build({
199 $library3 = $builder->build({
202 @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode} );
204 my $category = $builder->build({ source => 'Category', value => {exclude_from_local_holds_priority => 0} } );
206 my $borrower1 = $builder->build({
207 source => 'Borrower',
209 branchcode => $branchcodes[0],
210 categorycode => $category->{categorycode},
213 my $borrower2 = $builder->build({
214 source => 'Borrower',
216 branchcode => $branchcodes[1],
217 categorycode => $category->{categorycode},
220 my $borrower3 = $builder->build({
221 source => 'Borrower',
223 branchcode => $branchcodes[2],
224 categorycode => $category->{categorycode},
241 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
242 or BAIL_OUT("Cannot find newly created biblio record");
245 INSERT INTO biblioitems (
253 $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
254 or BAIL_OUT("Cannot find newly created biblioitems record");
256 $items_insert_sth = $dbh->prepare(qq{
269 exclude_from_local_holds_priority
285 # Create 3 items from 2 branches ( branches are for borrowers 1 and 2 respectively )
286 $barcode = int( rand(1000000000000) );
287 $items_insert_sth->execute( $barcode + 0, $branchcodes[0], $branchcodes[0] );
288 $items_insert_sth->execute( $barcode + 1, $branchcodes[1], $branchcodes[1] );
289 $items_insert_sth->execute( $barcode + 2, $branchcodes[1], $branchcodes[1] );
291 $dbh->do("DELETE FROM reserves");
292 my $sth = $dbh->prepare(q{
293 INSERT INTO reserves (
299 ) VALUES ( ?,?,?,?, CURRENT_DATE() )
301 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
302 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
303 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[0], 3 );
307 $dbh->do("DELETE FROM circulation_rules");
308 Koha::CirculationRules->set_rule(
310 rule_name => 'holdallowed',
316 C4::HoldsQueue::CreateQueue();
317 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
318 is( @$holds_queue, 2, "Holds queue filling correct number for default holds policy 'from home library'" );
319 is( $holds_queue->[0]->{cardnumber}, $borrower1->{cardnumber}, "Holds queue filling 1st correct hold for default holds policy 'from home library'");
320 is( $holds_queue->[1]->{cardnumber}, $borrower2->{cardnumber}, "Holds queue filling 2nd correct hold for default holds policy 'from home library'");
322 # Test skipping hold picks for closed libraries.
323 # At this point in the test, we have 2 rows in the holds queue
324 # 1 of which is coming from MPL. Let's enable HoldsQueueSkipClosed
325 # and make today a holiday for MPL. When we run it again we should only
326 # have 1 row in the holds queue
327 t::lib::Mocks::mock_preference('HoldsQueueSkipClosed', 1);
328 my $today = dt_from_string();
329 C4::Calendar->new( branchcode => $branchcodes[0] )->insert_single_holiday(
330 day => $today->day(),
331 month => $today->month(),
332 year => $today->year(),
334 description => "$today",
336 # If the test below is removed, aother tests using the holiday will fail. For some reason if we call is_holiday now
337 # the holiday will get set in cache correctly, but not if we let C4::HoldsQueue call is_holiday instead.
338 is( Koha::Calendar->new( branchcode => $branchcodes[0] )->is_holiday( $today ), 1, 'Is today a holiday for pickup branch' );
339 C4::HoldsQueue::CreateQueue();
340 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
341 is( scalar( @$holds_queue ), 1, "Holds not filled with items from closed libraries" );
342 t::lib::Mocks::mock_preference('HoldsQueueSkipClosed', 0);
344 $dbh->do("DELETE FROM circulation_rules");
345 Koha::CirculationRules->set_rule(
347 rule_name => 'holdallowed',
353 C4::HoldsQueue::CreateQueue();
354 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
355 is( @$holds_queue, 3, "Holds queue filling correct number for holds for default holds policy 'from any library'" );
357 # Test skipping hold picks for closed libraries without transport cost matrix
358 # At this point in the test, we have 3 rows in the holds queue
359 # one of which is coming from MPL. Let's enable HoldsQueueSkipClosed
360 # and use our previously created holiday for MPL
361 # When we run it again we should only have 2 rows in the holds queue
362 t::lib::Mocks::mock_preference( 'HoldsQueueSkipClosed', 1 );
363 C4::HoldsQueue::CreateQueue();
364 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
365 is( scalar( @$holds_queue ), 2, "Holds not filled with items from closed libraries" );
366 t::lib::Mocks::mock_preference( 'HoldsQueueSkipClosed', 0 );
368 ## Test LocalHoldsPriority
369 t::lib::Mocks::mock_preference('LocalHoldsPriority', 1);
371 $dbh->do("DELETE FROM circulation_rules");
372 Koha::CirculationRules->set_rule(
374 rule_name => 'holdallowed',
380 $dbh->do("DELETE FROM issues");
382 # Test homebranch = patron branch
383 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'HomeLibrary');
384 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'homebranch');
385 C4::Context->clear_syspref_cache();
386 $dbh->do("DELETE FROM reserves");
387 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
388 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
389 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[0], 3 );
391 $dbh->do("DELETE FROM items");
392 # barcode, homebranch, holdingbranch, itemtype
393 $items_insert_sth->execute( $barcode + 4, $branchcodes[2], $branchcodes[0] );
395 C4::HoldsQueue::CreateQueue();
396 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
397 is( $holds_queue->[0]->{cardnumber}, $borrower3->{cardnumber}, "Holds queue giving priority to patron who's home library matches item's home library");
399 ### Test branch transfer limits ###
400 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'HomeLibrary');
401 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'holdingbranch');
402 t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '1' );
403 C4::Context->clear_syspref_cache();
404 $dbh->do("DELETE FROM reserves");
405 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
406 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[1], 2 );
408 $dbh->do("DELETE FROM items");
409 # barcode, homebranch, holdingbranch, itemtype
410 $items_insert_sth->execute( $barcode, $branchcodes[2], $branchcodes[2] );
411 my $item = Koha::Items->find( { barcode => $barcode } );
413 my $limit1 = Koha::Item::Transfer::Limit->new(
415 toBranch => $branchcodes[0],
416 fromBranch => $branchcodes[2],
417 itemtype => $item->effective_itemtype,
421 C4::HoldsQueue::CreateQueue();
422 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
423 is( $holds_queue->[0]->{cardnumber}, $borrower2->{cardnumber}, "Holds queue skips hold transfer that would violate branch transfer limits");
425 my $limit2 = Koha::Item::Transfer::Limit->new(
427 toBranch => $branchcodes[1],
428 fromBranch => $branchcodes[2],
429 itemtype => $item->effective_itemtype,
433 C4::HoldsQueue::CreateQueue();
434 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
435 is( $holds_queue->[0]->{cardnumber}, undef, "Holds queue doesn't fill hold where all available items would violate branch transfer limits");
439 t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '0' );
440 ### END Test branch transfer limits ###
442 # Test holdingbranch = patron branch
443 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'HomeLibrary');
444 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'holdingbranch');
445 C4::Context->clear_syspref_cache();
446 $dbh->do("DELETE FROM reserves");
447 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
448 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
449 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[0], 3 );
451 $dbh->do("DELETE FROM items");
452 # barcode, homebranch, holdingbranch, itemtype
453 $items_insert_sth->execute( $barcode + 4, $branchcodes[0], $branchcodes[2] );
455 C4::HoldsQueue::CreateQueue();
456 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
457 is( $holds_queue->[0]->{cardnumber}, $borrower3->{cardnumber}, "Holds queue giving priority to patron who's home library matches item's holding library");
459 # Test holdingbranch = pickup branch
460 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'PickupLibrary');
461 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'holdingbranch');
462 C4::Context->clear_syspref_cache();
463 $dbh->do("DELETE FROM reserves");
464 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
465 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
466 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[2], 3 );
468 $dbh->do("DELETE FROM items");
469 # barcode, homebranch, holdingbranch, itemtype
470 $items_insert_sth->execute( $barcode + 4, $branchcodes[0], $branchcodes[2] );
472 C4::HoldsQueue::CreateQueue();
473 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
474 is( $holds_queue->[0]->{cardnumber}, $borrower3->{cardnumber}, "Holds queue giving priority to patron who's home library matches item's holding library");
476 # Test homebranch = pickup branch
477 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'PickupLibrary');
478 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'homebranch');
479 C4::Context->clear_syspref_cache();
480 $dbh->do("DELETE FROM reserves");
481 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
482 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
483 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[2], 3 );
485 $dbh->do("DELETE FROM items");
486 # barcode, homebranch, holdingbranch, itemtype
487 $items_insert_sth->execute( $barcode + 4, $branchcodes[2], $branchcodes[0] );
489 C4::HoldsQueue::CreateQueue();
490 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
491 is( $holds_queue->[0]->{cardnumber}, $borrower3->{cardnumber}, "Holds queue giving priority to patron who's home library matches item's holding library");
493 t::lib::Mocks::mock_preference('LocalHoldsPriority', 0);
494 ## End testing of LocalHoldsPriority
498 $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
499 $borrowernumber = $borrower3->{borrowernumber};
500 my $library_A = $library1->{branchcode};
501 my $library_B = $library2->{branchcode};
502 my $library_C = $borrower3->{branchcode};
503 $dbh->do("DELETE FROM reserves");
504 $dbh->do("DELETE FROM issues");
505 $dbh->do("DELETE FROM items");
506 $dbh->do("DELETE FROM biblio");
507 $dbh->do("DELETE FROM biblioitems");
508 $dbh->do("DELETE FROM transport_cost");
509 $dbh->do("DELETE FROM tmp_holdsqueue");
510 $dbh->do("DELETE FROM hold_fill_targets");
513 INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
516 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
517 or BAIL_OUT("Cannot find newly created biblio record");
519 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
522 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
523 or BAIL_OUT("Cannot find newly created biblioitems record");
526 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype, exclude_from_local_holds_priority)
527 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype', 0)
531 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype, exclude_from_local_holds_priority)
532 VALUES ($biblionumber, $biblioitemnumber, '$library_B', '$library_B', 0, 0, 0, 0, NULL, '$itemtype', 0)
535 Koha::CirculationRules->set_rules(
537 branchcode => $library_A,
538 itemtype => $itemtype,
541 returnbranch => 'homebranch',
546 $dbh->do( "UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'",
547 undef, join( ',', $library_B, $library_A, $library_C ) );
548 $dbh->do( "UPDATE systempreferences SET value = 0 WHERE variable = 'RandomizeHoldsQueueWeight'" );
550 my $reserve_id = AddReserve(
552 branchcode => $library_C,
553 borrowernumber => $borrowernumber,
554 biblionumber => $biblionumber,
558 C4::HoldsQueue::CreateQueue();
559 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
560 is( @$holds_queue, 1, "Bug 14297 - Holds Queue building ignoring holds where pickup & home branch don't match and item is not from le");
564 $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
565 $borrowernumber = $borrower2->{borrowernumber};
566 $library_A = $library1->{branchcode};
567 $library_B = $library2->{branchcode};
568 $dbh->do("DELETE FROM reserves");
569 $dbh->do("DELETE FROM issues");
570 $dbh->do("DELETE FROM items");
571 $dbh->do("DELETE FROM biblio");
572 $dbh->do("DELETE FROM biblioitems");
573 $dbh->do("DELETE FROM transport_cost");
574 $dbh->do("DELETE FROM tmp_holdsqueue");
575 $dbh->do("DELETE FROM hold_fill_targets");
577 t::lib::Mocks::mock_preference("UseTransportCostMatrix",1);
579 my $tc_rs = $schema->resultset('TransportCost');
580 $tc_rs->create({ frombranch => $library_A, tobranch => $library_B, cost => 0, disable_transfer => 1 });
581 $tc_rs->create({ frombranch => $library_B, tobranch => $library_A, cost => 0, disable_transfer => 1 });
584 INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
587 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
588 or BAIL_OUT("Cannot find newly created biblio record");
590 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
593 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
594 or BAIL_OUT("Cannot find newly created biblioitems record");
597 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype, exclude_from_local_holds_priority)
598 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype', 0)
601 $reserve_id = AddReserve(
603 branchcode => $library_B,
604 borrowernumber => $borrowernumber,
605 biblionumber => $biblionumber,
610 C4::HoldsQueue::CreateQueue();
611 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
612 is( @$holds_queue, 0, "Bug 15062 - Holds queue with Transport Cost Matrix will transfer item even if transfers disabled");
615 # Test hold_fulfillment_policy
616 t::lib::Mocks::mock_preference( "UseTransportCostMatrix", 0 );
617 $borrowernumber = $borrower3->{borrowernumber};
618 $library_A = $library1->{branchcode};
619 $library_B = $library2->{branchcode};
620 $library_C = $library3->{branchcode};
621 $dbh->do("DELETE FROM reserves");
622 $dbh->do("DELETE FROM issues");
623 $dbh->do("DELETE FROM items");
624 $dbh->do("DELETE FROM biblio");
625 $dbh->do("DELETE FROM biblioitems");
626 $dbh->do("DELETE FROM transport_cost");
627 $dbh->do("DELETE FROM tmp_holdsqueue");
628 $dbh->do("DELETE FROM hold_fill_targets");
630 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
632 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
633 or BAIL_OUT("Cannot find newly created biblio record");
635 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
638 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
639 or BAIL_OUT("Cannot find newly created biblioitems record");
642 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype, exclude_from_local_holds_priority)
643 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$itemtype', 0)
646 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
647 $dbh->do("DELETE FROM circulation_rules");
648 Koha::CirculationRules->set_rules(
654 hold_fulfillment_policy => 'homebranch',
659 # Home branch matches pickup branch
660 $reserve_id = AddReserve(
662 branchcode => $library_A,
663 borrowernumber => $borrowernumber,
664 biblionumber => $biblionumber,
669 C4::HoldsQueue::CreateQueue();
670 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
671 is( @$holds_queue, 1, "Hold where pickup branch matches home branch targeted" );
672 Koha::Holds->find( $reserve_id )->cancel;
674 # Holding branch matches pickup branch
675 $reserve_id = AddReserve(
677 branchcode => $library_B,
678 borrowernumber => $borrowernumber,
679 biblionumber => $biblionumber,
685 C4::HoldsQueue::CreateQueue();
686 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
687 is( @$holds_queue, 0, "Hold where pickup ne home, pickup eq home not targeted" );
688 Koha::Holds->find( $reserve_id )->cancel;
690 # Neither branch matches pickup branch
691 $reserve_id = AddReserve(
693 branchcode => $library_C,
694 borrowernumber => $borrowernumber,
695 biblionumber => $biblionumber,
700 C4::HoldsQueue::CreateQueue();
701 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
702 is( @$holds_queue, 0, "Hold where pickup ne home, pickup ne holding not targeted" );
703 Koha::Holds->find( $reserve_id )->cancel;
705 # With hold_fulfillment_policy = holdingbranch, hold should only be picked up if pickup branch = holdingbranch
706 $dbh->do("DELETE FROM circulation_rules");
707 Koha::CirculationRules->set_rules(
713 hold_fulfillment_policy => 'holdingbranch',
718 # Home branch matches pickup branch
719 $reserve_id = AddReserve(
721 branchcode => $library_A,
722 borrowernumber => $borrowernumber,
723 biblionumber => $biblionumber,
728 C4::HoldsQueue::CreateQueue();
729 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
730 is( @$holds_queue, 0, "Hold where pickup eq home, pickup ne holding not targeted" );
731 Koha::Holds->find( $reserve_id )->cancel;
733 # Holding branch matches pickup branch
734 $reserve_id = AddReserve(
736 branchcode => $library_B,
737 borrowernumber => $borrowernumber,
738 biblionumber => $biblionumber,
743 C4::HoldsQueue::CreateQueue();
744 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
745 is( @$holds_queue, 1, "Hold where pickup ne home, pickup eq holding targeted" );
746 Koha::Holds->find( $reserve_id )->cancel;
748 # Neither branch matches pickup branch
749 $reserve_id = AddReserve(
751 branchcode => $library_C,
752 borrowernumber => $borrowernumber,
753 biblionumber => $biblionumber,
758 C4::HoldsQueue::CreateQueue();
759 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
760 is( @$holds_queue, 0, "Hold where pickup ne home, pickup ne holding not targeted" );
761 Koha::Holds->find( $reserve_id )->cancel;
763 # With hold_fulfillment_policy = any, hold should be pikcup up reguardless of matching home or holding branch
764 $dbh->do("DELETE FROM circulation_rules");
765 Koha::CirculationRules->set_rules(
771 hold_fulfillment_policy => 'any',
776 # Home branch matches pickup branch
777 $reserve_id = AddReserve(
779 branchcode => $library_A,
780 borrowernumber => $borrowernumber,
781 biblionumber => $biblionumber,
786 C4::HoldsQueue::CreateQueue();
787 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
788 is( @$holds_queue, 1, "Hold where pickup eq home, pickup ne holding targeted" );
789 Koha::Holds->find( $reserve_id )->cancel;
791 # Holding branch matches pickup branch
792 $reserve_id = AddReserve(
794 branchcode => $library_B,
795 borrowernumber => $borrowernumber,
796 biblionumber => $biblionumber,
801 C4::HoldsQueue::CreateQueue();
802 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
803 is( @$holds_queue, 1, "Hold where pickup ne home, pickup eq holding targeted" );
804 Koha::Holds->find( $reserve_id )->cancel;
806 # Neither branch matches pickup branch
807 $reserve_id = AddReserve(
809 branchcode => $library_C,
810 borrowernumber => $borrowernumber,
811 biblionumber => $biblionumber,
816 C4::HoldsQueue::CreateQueue();
817 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
818 is( @$holds_queue, 1, "Hold where pickup ne home, pickup ne holding targeted" );
819 Koha::Holds->find( $reserve_id )->cancel;
821 # End testing hold_fulfillment_policy
823 # Test hold itemtype limit
824 t::lib::Mocks::mock_preference( "UseTransportCostMatrix", 0 );
825 my $wrong_itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
826 my $right_itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
827 $borrowernumber = $borrower3->{borrowernumber};
828 my $branchcode = $library1->{branchcode};
829 $dbh->do("DELETE FROM reserves");
830 $dbh->do("DELETE FROM issues");
831 $dbh->do("DELETE FROM items");
832 $dbh->do("DELETE FROM biblio");
833 $dbh->do("DELETE FROM biblioitems");
834 $dbh->do("DELETE FROM transport_cost");
835 $dbh->do("DELETE FROM tmp_holdsqueue");
836 $dbh->do("DELETE FROM hold_fill_targets");
838 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
840 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
841 or BAIL_OUT("Cannot find newly created biblio record");
843 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
846 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
847 or BAIL_OUT("Cannot find newly created biblioitems record");
850 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype, exclude_from_local_holds_priority)
851 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$right_itemtype', 0)
854 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
855 $dbh->do("DELETE FROM circulation_rules");
856 Koha::CirculationRules->set_rules(
862 hold_fulfillment_policy => 'any',
867 # Home branch matches pickup branch
868 $reserve_id = AddReserve(
870 branchcode => $library_A,
871 borrowernumber => $borrowernumber,
872 biblionumber => $biblionumber,
874 itemtype => $wrong_itemtype,
878 C4::HoldsQueue::CreateQueue();
879 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
880 is( @$holds_queue, 0, "Item with incorrect itemtype not targeted" );
881 Koha::Holds->find( $reserve_id )->cancel;
883 # Holding branch matches pickup branch
884 $reserve_id = AddReserve(
886 branchcode => $library_A,
887 borrowernumber => $borrowernumber,
888 biblionumber => $biblionumber,
890 itemtype => $right_itemtype,
894 C4::HoldsQueue::CreateQueue();
895 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
896 is( @$holds_queue, 1, "Item with matching itemtype is targeted" );
897 Koha::Holds->find( $reserve_id )->cancel;
899 # Neither branch matches pickup branch
900 $reserve_id = AddReserve(
902 branchcode => $library_A,
903 borrowernumber => $borrowernumber,
904 biblionumber => $biblionumber,
909 C4::HoldsQueue::CreateQueue();
910 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
911 is( @$holds_queue, 1, "Item targeted when hold itemtype is not set" );
912 Koha::Holds->find( $reserve_id )->cancel;
914 # End testing hold itemtype limit
917 subtest "Test Local Holds Priority - Bib level" => sub {
920 Koha::Biblios->delete();
921 t::lib::Mocks::mock_preference( 'LocalHoldsPriority', 1 );
922 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'PickupLibrary' );
923 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'homebranch' );
924 my $branch = $builder->build_object( { class => 'Koha::Libraries' } );
925 my $branch2 = $builder->build_object( { class => 'Koha::Libraries' } );
926 my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => {exclude_from_local_holds_priority => 0} });
927 my $local_patron = $builder->build_object(
929 class => "Koha::Patrons",
931 branchcode => $branch->branchcode,
932 categorycode => $category->categorycode
936 my $other_patron = $builder->build_object(
938 class => "Koha::Patrons",
940 branchcode => $branch2->branchcode,
941 categorycode => $category->categorycode
945 my $biblio = $builder->build_sample_biblio();
946 my $item = $builder->build_sample_item(
948 biblionumber => $biblio->biblionumber,
949 library => $branch->branchcode,
950 exclude_from_local_holds_priority => 0,
954 my $reserve_id = AddReserve(
956 branchcode => $branch2->branchcode,
957 borrowernumber => $other_patron->borrowernumber,
958 biblionumber => $biblio->biblionumber,
962 my $reserve_id2 = AddReserve(
964 branchcode => $item->homebranch,
965 borrowernumber => $local_patron->borrowernumber,
966 biblionumber => $biblio->biblionumber,
971 C4::HoldsQueue::CreateQueue();
973 my $queue_rs = $schema->resultset('TmpHoldsqueue');
974 is( $queue_rs->count(), 1,
975 "Hold queue contains one hold" );
977 $queue_rs->next->borrowernumber,
978 $local_patron->borrowernumber,
979 "We should pick the local hold over the next available"
983 subtest "Test Local Holds Priority - Item level" => sub {
986 Koha::Biblios->delete();
987 t::lib::Mocks::mock_preference( 'LocalHoldsPriority', 1 );
988 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'PickupLibrary' );
989 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'homebranch' );
990 my $branch = $builder->build_object( { class => 'Koha::Libraries' } );
991 my $branch2 = $builder->build_object( { class => 'Koha::Libraries' } );
992 my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => {exclude_from_local_holds_priority => 0} });
993 my $local_patron = $builder->build_object(
995 class => "Koha::Patrons",
997 branchcode => $branch->branchcode,
998 categorycode => $category->categorycode
1002 my $other_patron = $builder->build_object(
1004 class => "Koha::Patrons",
1006 branchcode => $branch2->branchcode,
1007 categorycode => $category->categorycode
1011 my $biblio = $builder->build_sample_biblio();
1012 my $item = $builder->build_sample_item(
1014 biblionumber => $biblio->biblionumber,
1015 library => $branch->branchcode,
1016 exclude_from_local_holds_priority => 0,
1020 my $reserve_id = AddReserve(
1022 branchcode => $branch2->branchcode,
1023 borrowernumber => $other_patron->borrowernumber,
1024 biblionumber => $biblio->biblionumber,
1026 itemnumber => $item->id,
1029 my $reserve_id2 = AddReserve(
1031 branchcode => $item->homebranch,
1032 borrowernumber => $local_patron->borrowernumber,
1033 biblionumber => $biblio->biblionumber,
1035 itemnumber => $item->id,
1039 C4::HoldsQueue::CreateQueue();
1041 my $queue_rs = $schema->resultset('TmpHoldsqueue');
1042 my $q = $queue_rs->next;
1043 is( $queue_rs->count(), 1,
1044 "Hold queue contains one hold" );
1047 $local_patron->borrowernumber,
1048 "We should pick the local hold over the next available"
1052 subtest "Test Local Holds Priority - Item level hold over Record level hold (Bug 23934)" => sub {
1055 Koha::Biblios->delete();
1056 t::lib::Mocks::mock_preference( 'LocalHoldsPriority', 1 );
1057 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'PickupLibrary' );
1058 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'homebranch' );
1059 my $branch = $builder->build_object( { class => 'Koha::Libraries' } );
1060 my $branch2 = $builder->build_object( { class => 'Koha::Libraries' } );
1061 my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => {exclude_from_local_holds_priority => 0} });
1062 my $local_patron = $builder->build_object(
1064 class => "Koha::Patrons",
1066 branchcode => $branch->branchcode,
1067 categorycode => $category->categorycode,
1071 my $other_patron = $builder->build_object(
1073 class => "Koha::Patrons",
1075 branchcode => $branch2->branchcode,
1076 categorycode => $category->categorycode,
1080 my $biblio = $builder->build_sample_biblio();
1081 my $item = $builder->build_sample_item(
1083 biblionumber => $biblio->biblionumber,
1084 library => $branch->branchcode,
1085 exclude_from_local_holds_priority => 0,
1089 my $reserve_id = AddReserve(
1091 branchcode => $branch2->branchcode,
1092 borrowernumber => $other_patron->borrowernumber,
1093 biblionumber => $biblio->biblionumber,
1097 my $reserve_id2 = AddReserve(
1099 branchcode => $item->homebranch,
1100 borrowernumber => $local_patron->borrowernumber,
1101 biblionumber => $biblio->biblionumber,
1103 itemnumber => $item->id,
1107 C4::HoldsQueue::CreateQueue();
1109 my $queue_rs = $schema->resultset('TmpHoldsqueue');
1110 my $q = $queue_rs->next;
1111 is( $queue_rs->count(), 1,
1112 "Hold queue contains one hold" );
1115 $local_patron->borrowernumber,
1116 "We should pick the local hold over the next available"
1120 subtest "Test Local Holds Priority - Get correct item for item level hold" => sub {
1123 Koha::Biblios->delete();
1124 t::lib::Mocks::mock_preference( 'LocalHoldsPriority', 1 );
1125 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'PickupLibrary' );
1126 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'homebranch' );
1127 my $branch = $builder->build_object( { class => 'Koha::Libraries' } );
1128 my $branch2 = $builder->build_object( { class => 'Koha::Libraries' } );
1129 my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => {exclude_from_local_holds_priority => 0} });
1130 my $local_patron = $builder->build_object(
1132 class => "Koha::Patrons",
1134 branchcode => $branch->branchcode,
1135 categorycode => $category->categorycode,
1139 my $other_patron = $builder->build_object(
1141 class => "Koha::Patrons",
1143 branchcode => $branch2->branchcode,
1144 categorycode => $category->categorycode,
1148 my $biblio = $builder->build_sample_biblio();
1150 my $item1 = $builder->build_sample_item(
1152 biblionumber => $biblio->biblionumber,
1153 library => $branch->branchcode,
1154 exclude_from_local_holds_priority => 0,
1157 my $item2 = $builder->build_sample_item(
1159 biblionumber => $biblio->biblionumber,
1160 library => $branch->branchcode,
1161 exclude_from_local_holds_priority => 0,
1164 my $item3 = $builder->build_sample_item(
1166 biblionumber => $biblio->biblionumber,
1167 library => $branch->branchcode,
1168 exclude_from_local_holds_priority => 0,
1175 branchcode => $item2->homebranch,
1176 borrowernumber => $local_patron->borrowernumber,
1177 biblionumber => $biblio->biblionumber,
1179 itemnumber => $item2->id,
1184 C4::HoldsQueue::CreateQueue();
1186 my $queue_rs = $schema->resultset('TmpHoldsqueue');
1187 my $q = $queue_rs->next;
1188 is( $queue_rs->count(), 1,
1189 "Hold queue contains one hold" );
1192 $local_patron->borrowernumber,
1193 "We should pick the local hold over the next available"
1195 is( $q->itemnumber->id, $item2->id, "Got the correct item for item level local holds priority" );
1198 subtest "Test Local Holds Priority - Ensure no duplicate requests in holds queue (Bug 18001)" => sub {
1201 $dbh->do("DELETE FROM tmp_holdsqueue");
1202 $dbh->do("DELETE FROM hold_fill_targets");
1203 $dbh->do("DELETE FROM reserves");
1204 $dbh->do("DELETE FROM circulation_rules");
1205 Koha::Biblios->delete();
1207 t::lib::Mocks::mock_preference( 'LocalHoldsPriority', 1 );
1208 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'PickupLibrary' );
1209 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'homebranch' );
1210 my $branch = $builder->build_object( { class => 'Koha::Libraries' } );
1211 my $branch2 = $builder->build_object( { class => 'Koha::Libraries' } );
1212 my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => {exclude_from_local_holds_priority => 0} });
1213 my $patron = $builder->build_object(
1215 class => "Koha::Patrons",
1217 branchcode => $branch->branchcode,
1218 categorycode => $category->categorycode,
1222 my $biblio = $builder->build_sample_biblio();
1223 my $item1 = $builder->build_sample_item(
1225 biblionumber => $biblio->biblionumber,
1226 library => $branch->branchcode,
1227 exclude_from_local_holds_priority => 0,
1230 my $item2 = $builder->build_sample_item(
1232 biblionumber => $biblio->biblionumber,
1233 library => $branch->branchcode,
1234 exclude_from_local_holds_priority => 0,
1238 my $item3 = $builder->build_sample_item(
1240 biblionumber => $biblio->biblionumber,
1241 library => $branch->branchcode,
1242 exclude_from_local_holds_priority => 0,
1246 $reserve_id = AddReserve(
1248 branchcode => $item1->homebranch,
1249 borrowernumber => $patron->borrowernumber,
1250 biblionumber => $biblio->id,
1255 C4::HoldsQueue::CreateQueue();
1257 my $queue_rs = $schema->resultset('TmpHoldsqueue');
1259 is( $queue_rs->count(), 1,
1260 "Hold queue contains one hold from chosen from three possible items" );
1264 subtest "Item level holds info is preserved (Bug 25738)" => sub {
1268 $dbh->do("DELETE FROM tmp_holdsqueue");
1269 $dbh->do("DELETE FROM hold_fill_targets");
1270 $dbh->do("DELETE FROM reserves");
1271 $dbh->do("DELETE FROM circulation_rules");
1273 my $library = $builder->build_object({ class => 'Koha::Libraries' });
1274 my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => {exclude_from_local_holds_priority => 0} });
1275 my $patron_1 = $builder->build_object(
1277 class => "Koha::Patrons",
1279 branchcode => $library->branchcode,
1280 categorycode => $category->categorycode
1285 my $patron_2 = $builder->build_object(
1287 class => "Koha::Patrons",
1289 branchcode => $library->branchcode,
1290 categorycode => $category->categorycode
1295 my $biblio = $builder->build_sample_biblio();
1296 my $item_1 = $builder->build_sample_item(
1298 biblionumber => $biblio->biblionumber,
1299 library => $library->branchcode,
1300 exclude_from_local_holds_priority => 0,
1303 my $item_2 = $builder->build_sample_item(
1305 biblionumber => $biblio->biblionumber,
1306 library => $library->branchcode,
1307 exclude_from_local_holds_priority => 0,
1311 # Add item-level hold for patron_1
1312 my $reserve_id_1 = AddReserve(
1314 branchcode => $library->branchcode,
1315 borrowernumber => $patron_1->borrowernumber,
1316 biblionumber => $biblio->id,
1317 itemnumber => $item_1->itemnumber,
1322 my $reserve_id_2 = AddReserve(
1324 branchcode => $library->branchcode,
1325 borrowernumber => $patron_2->borrowernumber,
1326 biblionumber => $biblio->id,
1331 C4::HoldsQueue::CreateQueue();
1333 my $queue_rs = $schema->resultset('TmpHoldsqueue');
1335 is( $queue_rs->count(), 2, "Hold queue contains two holds" );
1337 my $queue_line_1 = $queue_rs->next;
1338 is( $queue_line_1->item_level_request, 1, 'Request is correctly advertised as item-level' );
1340 my $queue_line_2 = $queue_rs->next;
1341 is( $queue_line_2->item_level_request, 0, 'Request is correctly advertised as biblio-level' );
1345 subtest 'Trivial test for UpdateTransportCostMatrix' => sub {
1348 { frombranch => $library1->{branchcode}, tobranch => $library2->{branchcode}, cost => 1, disable_transfer => 0 },
1349 { frombranch => $library2->{branchcode}, tobranch => $library3->{branchcode}, cost => 0, disable_transfer => 1 },
1351 C4::HoldsQueue::UpdateTransportCostMatrix( $recs );
1352 is( $schema->resultset('TransportCost')->count, 2, 'UpdateTransportCostMatrix added two records' );
1355 subtest 'Excludes from local holds priority' => sub {
1358 Koha::Holds->delete;
1360 t::lib::Mocks::mock_preference( 'LocalHoldsPriority', 1 );
1361 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'PickupLibrary' );
1362 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'homebranch' );
1364 my $branch = $builder->build_object( { class => 'Koha::Libraries' } );
1365 my $branch2 = $builder->build_object( { class => 'Koha::Libraries' } );
1366 my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => {exclude_from_local_holds_priority => 0} });
1367 my $excluded_category = $builder->build_object( { class => 'Koha::Patron::Categories', value => {exclude_from_local_holds_priority => 1} });
1368 my $local_patron_excluded = $builder->build_object(
1370 class => "Koha::Patrons",
1372 branchcode => $branch->branchcode,
1373 categorycode => $excluded_category->categorycode
1378 my $local_patron_not_excluded = $builder->build_object(
1380 class => "Koha::Patrons",
1382 branchcode => $branch->branchcode,
1383 categorycode => $category->categorycode
1388 my $other_patron = $builder->build_object(
1390 class => "Koha::Patrons",
1392 branchcode => $branch2->branchcode,
1393 categorycode => $category->categorycode
1398 my $item1 = $builder->build_sample_item(
1400 library => $branch->branchcode,
1401 exclude_from_local_holds_priority => 0,
1407 branchcode => $other_patron->branchcode,
1408 borrowernumber => $other_patron->borrowernumber,
1409 biblionumber => $item1->biblionumber,
1416 branchcode => $local_patron_excluded->branchcode,
1417 borrowernumber => $local_patron_excluded->borrowernumber,
1418 biblionumber => $item1->biblionumber,
1425 branchcode => $local_patron_not_excluded->branchcode,
1426 borrowernumber => $local_patron_not_excluded->borrowernumber,
1427 biblionumber => $item1->biblionumber,
1432 C4::HoldsQueue::CreateQueue();
1434 my $queue_rs = $schema->resultset('TmpHoldsqueue');
1435 my $next = $queue_rs->next;
1436 is($queue_rs->count, 1, 'Only 1 patron queueud' );
1437 is($next->borrowernumber, $local_patron_not_excluded->borrowernumber, 'Not excluded local patron is queued');
1439 my $item2 = $builder->build_sample_item(
1441 biblionumber => $item1->biblionumber,
1442 library => $branch->branchcode,
1443 exclude_from_local_holds_priority => 1,
1446 C4::HoldsQueue::CreateQueue();
1448 $queue_rs = $schema->resultset('TmpHoldsqueue');
1449 is( $queue_rs->count, 2, '2 patrons queued' );
1450 $next = $queue_rs->next;
1451 is($next->borrowernumber, $local_patron_not_excluded->borrowernumber, 'Not excluded local patron is queued');
1452 $next = $queue_rs->next;
1453 is($next->borrowernumber, $other_patron->borrowernumber, 'Other patron is queued');
1455 $item1->exclude_from_local_holds_priority(1)->store;
1457 C4::HoldsQueue::CreateQueue();
1459 $queue_rs = $schema->resultset('TmpHoldsqueue');
1460 is( $queue_rs->count, 2, '2 patrons queued' );
1461 $next = $queue_rs->next;
1462 is($next->borrowernumber, $other_patron->borrowernumber, 'Other patron is queued');
1463 $next = $queue_rs->next;
1464 is($next->borrowernumber, $local_patron_excluded->borrowernumber, 'Excluded local patron is queued');
1467 $schema->storage->txn_rollback;
1469 ### END Test holds queue builder does not violate holds policy ###
1472 my ($test_name, $use_cost_matrix, $pick_branch, $hold_branch) = @_;
1474 $test_name = "$test_name (".($use_cost_matrix ? "" : "don't ")."use cost matrix)";
1476 $use_cost_matrix_sth->execute($use_cost_matrix);
1477 C4::Context->clear_syspref_cache();
1478 C4::HoldsQueue::CreateQueue();
1480 my $results = $dbh->selectall_arrayref($test_sth, { Slice => {} }); # should be only one
1481 my $r = $results->[0];
1483 my $ok = is( $r->{pickbranch}, $pick_branch, "$test_name pick up branch");
1484 $ok &&= is( $r->{holdingbranch}, $hold_branch, "$test_name holding branch")
1487 diag( "Wrong pick-up/hold for first target (pick_branch, hold_branch, reserves, hold_fill_targets, tmp_holdsqueue): "
1488 . Dumper ($pick_branch, $hold_branch, map dump_records($_), qw(reserves hold_fill_targets tmp_holdsqueue)) )
1491 # Test enforcement of branch transfer limit
1492 if ( $r->{pickbranch} ne $r->{holdingbranch} ) {
1493 t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '1' );
1494 my $limit = Koha::Item::Transfer::Limit->new(
1496 toBranch => $r->{pickbranch},
1497 fromBranch => $r->{holdingbranch},
1498 itemtype => $r->{itype},
1501 C4::Context->clear_syspref_cache();
1502 C4::HoldsQueue::CreateQueue();
1503 $results = $dbh->selectall_arrayref( $test_sth, { Slice => {} } )
1504 ; # should be only one
1505 my $s = $results->[0];
1506 isnt( $r->{holdingbranch}, $s->{holdingbranch}, 'Hold is not trapped for pickup at a branch that cannot be transferred to');
1509 t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '0' );
1510 C4::Context->clear_syspref_cache();
1511 C4::HoldsQueue::CreateQueue();
1517 my ($tablename) = @_;
1518 return $dbh->selectall_arrayref("SELECT * from $tablename where borrowernumber = ?", { Slice => {} }, $borrowernumber);