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;
21 use t
::lib
::TestBuilder
;
25 use lib
$FindBin::Bin
;
26 use_ok
('C4::Reserves');
27 use_ok
('C4::HoldsQueue');
30 my $schema = Koha
::Database
->schema;
31 $schema->storage->txn_begin;
32 my $dbh = C4
::Context
->dbh;
34 my $builder = t
::lib
::TestBuilder
->new;
36 my $library1 = $builder->build({
39 my $library2 = $builder->build({
42 my $library3 = $builder->build({
46 my $TITLE = "Test Holds Queue XXX";
48 my $borrower = $builder->build({
51 branchcode
=> $library1->{branchcode
},
55 my $borrowernumber = $borrower->{borrowernumber
};
56 # Set special (for this test) branches
57 my $borrower_branchcode = $borrower->{branchcode
};
58 my @branchcodes = ( $library1->{branchcode
}, $library2->{branchcode
}, $library3->{branchcode
} );
59 my @other_branches = ( $library2->{branchcode
}, $library3->{branchcode
} );
60 my $least_cost_branch_code = pop @other_branches;
61 my $itemtype = $builder->build({ source
=> 'Itemtype', value
=> { notforloan
=> 0 } })->{itemtype
};
64 # Sysprefs and cost matrix
65 C4
::Context
->set_preference('HoldsQueueSkipClosed', 0);
66 $dbh->do("UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'", undef,
67 join( ',', @other_branches, $borrower_branchcode, $least_cost_branch_code));
68 $dbh->do("UPDATE systempreferences SET value = '0' WHERE variable = 'RandomizeHoldsQueueWeight'");
70 $dbh->do("DELETE FROM transport_cost");
71 my $transport_cost_insert_sth = $dbh->prepare("insert into transport_cost (frombranch, tobranch, cost) values (?, ?, ?)");
72 # Favour $least_cost_branch_code
73 $transport_cost_insert_sth->execute($borrower_branchcode, $least_cost_branch_code, 0.2);
74 $transport_cost_insert_sth->execute($least_cost_branch_code, $borrower_branchcode, 0.2);
75 my @b = @other_branches;
76 while ( my $b1 = shift @b ) {
77 foreach my $b2 ($borrower_branchcode, $least_cost_branch_code, @b) {
78 $transport_cost_insert_sth->execute($b1, $b2, 0.5);
79 $transport_cost_insert_sth->execute($b2, $b1, 0.5);
84 # Loanable items - all possible combinations of homebranch and holdingbranch
85 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated)
86 VALUES ('SER', 'Koha test', '$TITLE', '2011-02-01')");
87 my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
88 or BAIL_OUT
("Cannot find newly created biblio record");
89 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype)
90 VALUES ($biblionumber, '', '$itemtype')");
91 my $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
92 or BAIL_OUT
("Cannot find newly created biblioitems record");
94 my $items_insert_sth = $dbh->prepare("INSERT INTO items (biblionumber, biblioitemnumber, barcode, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
95 VALUES ($biblionumber, $biblioitemnumber, ?, ?, ?, 0, 0, 0, 0, NULL, '$itemtype')"); # CURRENT_DATE - 3)");
96 my $first_barcode = int(rand(1000000000000)); # XXX
97 my $barcode = $first_barcode;
98 foreach ( $borrower_branchcode, $least_cost_branch_code, @other_branches ) {
99 $items_insert_sth->execute($barcode++, $borrower_branchcode, $_);
100 $items_insert_sth->execute($barcode++, $_, $_);
101 $items_insert_sth->execute($barcode++, $_, $borrower_branchcode);
104 # Remove existing reserves, makes debugging easier
105 $dbh->do("DELETE FROM reserves");
106 my $bibitems = undef;
109 AddReserve
( $borrower_branchcode, $borrowernumber, $biblionumber, $bibitems, $priority );
110 # $resdate, $expdate, $notes, $title, $checkitem, $found
111 $dbh->do("UPDATE reserves SET reservedate = DATE_SUB( reservedate, INTERVAL 1 DAY )");
114 my $use_cost_matrix_sth = $dbh->prepare("UPDATE systempreferences SET value = ? WHERE variable = 'UseTransportCostMatrix'");
115 my $test_sth = $dbh->prepare("SELECT * FROM hold_fill_targets
116 JOIN tmp_holdsqueue USING (borrowernumber, biblionumber, itemnumber)
117 JOIN items USING (itemnumber)
118 WHERE borrowernumber = $borrowernumber");
120 # We have a book available homed in borrower branch, no point fiddling with AutomaticItemReturn
121 C4
::Context
->set_preference('AutomaticItemReturn', 0);
122 test_queue
('take from homebranch', 0, $borrower_branchcode, $borrower_branchcode);
123 test_queue
('take from homebranch', 1, $borrower_branchcode, $borrower_branchcode);
125 $dbh->do("DELETE FROM tmp_holdsqueue");
126 $dbh->do("DELETE FROM hold_fill_targets");
127 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode')");
128 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode'");
129 # test_queue will flush
130 C4
::Context
->set_preference('AutomaticItemReturn', 1);
131 # Not sure how to make this test more difficult - holding branch does not matter
133 $dbh->do("DELETE FROM tmp_holdsqueue");
134 $dbh->do("DELETE FROM hold_fill_targets");
135 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode')");
136 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode'");
137 C4
::Context
->set_preference('AutomaticItemReturn', 0);
138 # We have a book available held in borrower branch
139 test_queue
('take from holdingbranch', 0, $borrower_branchcode, $borrower_branchcode);
140 test_queue
('take from holdingbranch', 1, $borrower_branchcode, $borrower_branchcode);
142 $dbh->do("DELETE FROM tmp_holdsqueue");
143 $dbh->do("DELETE FROM hold_fill_targets");
144 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE holdingbranch = '$borrower_branchcode')");
145 $dbh->do("DELETE FROM items WHERE holdingbranch = '$borrower_branchcode'");
146 # No book available in borrower branch, pick according to the rules
147 # Frst branch from StaticHoldsQueueWeight
148 test_queue
('take from lowest cost branch', 0, $borrower_branchcode, $other_branches[0]);
149 test_queue
('take from lowest cost branch', 1, $borrower_branchcode, $least_cost_branch_code);
150 my $queue = C4
::HoldsQueue
::GetHoldsQueueItems
($least_cost_branch_code) || [];
151 my $queue_item = $queue->[0];
153 && $queue_item->{pickbranch
} eq $borrower_branchcode
154 && $queue_item->{holdingbranch
} eq $least_cost_branch_code, "GetHoldsQueueItems" )
155 or diag
( "Expected item for pick $borrower_branchcode, hold $least_cost_branch_code, got ".Dumper
($queue_item) );
156 ok
( exists($queue_item->{itype
}), 'item type included in queued items list (bug 5825)' );
159 C4
::HoldsQueue
::least_cost_branch
( 'B', [ 'A', 'B', 'C' ] ) eq 'B',
160 'C4::HoldsQueue::least_cost_branch returns the local branch if it is in the list of branches to pull from'
163 # XXX All this tests are for borrower branch pick-up.
164 # Maybe needs expanding to homebranch or holdingbranch pick-up.
166 $schema->txn_rollback;
169 ### Test holds queue builder does not violate holds policy ###
171 # Clear out existing rules relating to holdallowed
172 $dbh->do("DELETE FROM default_branch_circ_rules");
173 $dbh->do("DELETE FROM default_branch_item_rules");
174 $dbh->do("DELETE FROM default_circ_rules");
176 C4
::Context
->set_preference('UseTransportCostMatrix', 0);
178 $itemtype = $builder->build({ source
=> 'Itemtype', value
=> { notforloan
=> 0 } })->{itemtype
};
180 $library1 = $builder->build({
183 $library2 = $builder->build({
186 $library3 = $builder->build({
189 @branchcodes = ( $library1->{branchcode
}, $library2->{branchcode
}, $library3->{branchcode
} );
191 my $borrower1 = $builder->build({
192 source
=> 'Borrower',
194 branchcode
=> $branchcodes[0],
197 my $borrower2 = $builder->build({
198 source
=> 'Borrower',
200 branchcode
=> $branchcodes[1],
203 my $borrower3 = $builder->build({
204 source
=> 'Borrower',
206 branchcode
=> $branchcodes[2],
223 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
224 or BAIL_OUT
("Cannot find newly created biblio record");
227 INSERT INTO biblioitems
(
237 $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
238 or BAIL_OUT
("Cannot find newly created biblioitems record");
240 $items_insert_sth = $dbh->prepare(qq{
267 # Create 3 items from 2 branches ( branches are for borrowers 1 and 2 respectively )
268 $barcode = int( rand(1000000000000) );
269 $items_insert_sth->execute( $barcode + 0, $branchcodes[0], $branchcodes[0] );
270 $items_insert_sth->execute( $barcode + 1, $branchcodes[1], $branchcodes[1] );
271 $items_insert_sth->execute( $barcode + 2, $branchcodes[1], $branchcodes[1] );
273 $dbh->do("DELETE FROM reserves");
274 my $sth = $dbh->prepare(q{
275 INSERT INTO reserves (
281 ) VALUES ( ?,?,?,?, CURRENT_DATE() )
283 $sth->execute( $borrower1->{borrowernumber
}, $biblionumber, $branchcodes[0], 1 );
284 $sth->execute( $borrower2->{borrowernumber
}, $biblionumber, $branchcodes[0], 2 );
285 $sth->execute( $borrower3->{borrowernumber
}, $biblionumber, $branchcodes[0], 3 );
289 $dbh->do("DELETE FROM default_circ_rules");
290 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 1 )");
291 C4
::HoldsQueue
::CreateQueue
();
292 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice
=> {} });
293 is
( @
$holds_queue, 2, "Holds queue filling correct number for default holds policy 'from home library'" );
294 is
( $holds_queue->[0]->{cardnumber
}, $borrower1->{cardnumber
}, "Holds queue filling 1st correct hold for default holds policy 'from home library'");
295 is
( $holds_queue->[1]->{cardnumber
}, $borrower2->{cardnumber
}, "Holds queue filling 2nd correct hold for default holds policy 'from home library'");
297 # Test skipping hold picks for closed libraries.
298 # At this point in the test, we have 2 rows in the holds queue
299 # 1 of which is coming from MPL. Let's enable HoldsQueueSkipClosed
300 # and make today a holiday for MPL. When we run it again we should only
301 # have 1 row in the holds queue
302 C4
::Context
->set_preference('HoldsQueueSkipClosed', 1);
303 my $today = dt_from_string
();
304 C4
::Calendar
->new( branchcode
=> $branchcodes[0] )->insert_single_holiday(
305 day
=> $today->day(),
306 month
=> $today->month(),
307 year
=> $today->year(),
309 description
=> "$today",
311 # If the test below is removed, aother tests using the holiday will fail. For some reason if we call is_holiday now
312 # the holiday will get set in cache correctly, but not if we let C4::HoldsQueue call is_holiday instead.
313 is
( Koha
::Calendar
->new( branchcode
=> $branchcodes[0] )->is_holiday( $today ), 1, 'Is today a holiday for pickup branch' );
314 C4
::HoldsQueue
::CreateQueue
();
315 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice
=> {} });
316 is
( scalar( @
$holds_queue ), 1, "Holds not filled with items from closed libraries" );
317 C4
::Context
->set_preference('HoldsQueueSkipClosed', 0);
319 $dbh->do("DELETE FROM default_circ_rules");
320 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 2 )");
321 C4
::HoldsQueue
::CreateQueue
();
322 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice
=> {} });
323 is
( @
$holds_queue, 3, "Holds queue filling correct number for holds for default holds policy 'from any library'" );
325 # Test skipping hold picks for closed libraries without transport cost matrix
326 # At this point in the test, we have 3 rows in the holds queue
327 # one of which is coming from MPL. Let's enable HoldsQueueSkipClosed
328 # and use our previously created holiday for MPL
329 # When we run it again we should only have 2 rows in the holds queue
330 C4
::Context
->set_preference( 'HoldsQueueSkipClosed', 1 );
331 C4
::HoldsQueue
::CreateQueue
();
332 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice
=> {} });
333 is
( scalar( @
$holds_queue ), 2, "Holds not filled with items from closed libraries" );
334 C4
::Context
->set_preference( 'HoldsQueueSkipClosed', 0 );
337 $itemtype = $builder->build({ source
=> 'Itemtype', value
=> { notforloan
=> 0 } })->{itemtype
};
338 $borrowernumber = $borrower3->{borrowernumber
};
339 my $library_A = $library1->{branchcode
};
340 my $library_B = $library2->{branchcode
};
341 my $library_C = $borrower3->{branchcode
};
342 $dbh->do("DELETE FROM reserves");
343 $dbh->do("DELETE FROM issues");
344 $dbh->do("DELETE FROM items");
345 $dbh->do("DELETE FROM biblio");
346 $dbh->do("DELETE FROM biblioitems");
347 $dbh->do("DELETE FROM transport_cost");
348 $dbh->do("DELETE FROM tmp_holdsqueue");
349 $dbh->do("DELETE FROM hold_fill_targets");
350 $dbh->do("DELETE FROM default_branch_circ_rules");
351 $dbh->do("DELETE FROM default_branch_item_rules");
352 $dbh->do("DELETE FROM default_circ_rules");
353 $dbh->do("DELETE FROM branch_item_rules");
356 INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
359 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
360 or BAIL_OUT
("Cannot find newly created biblio record");
362 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
365 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
366 or BAIL_OUT
("Cannot find newly created biblioitems record");
369 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
370 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
374 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
375 VALUES ($biblionumber, $biblioitemnumber, '$library_B', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
379 INSERT INTO branch_item_rules ( branchcode, itemtype, holdallowed, returnbranch ) VALUES
380 ( '$library_A', '$itemtype', 2, 'homebranch' ), ( '$library_B', '$itemtype', 1, 'homebranch' );
383 $dbh->do( "UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'",
384 undef, join( ',', $library_B, $library_A, $library_C ) );
385 $dbh->do( "UPDATE systempreferences SET value = 0 WHERE variable = 'RandomizeHoldsQueueWeight'" );
387 my $reserve_id = AddReserve
( $library_C, $borrowernumber, $biblionumber, '', 1 );
388 C4
::HoldsQueue
::CreateQueue
();
389 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice
=> {} });
390 is
( @
$holds_queue, 1, "Bug 14297 - Holds Queue building ignoring holds where pickup & home branch don't match and item is not from le");
394 $itemtype = $builder->build({ source
=> 'Itemtype', value
=> { notforloan
=> 0 } })->{itemtype
};
395 $borrowernumber = $borrower2->{borrowernumber
};
396 $library_A = $library1->{branchcode
};
397 $library_B = $library2->{branchcode
};
398 $dbh->do("DELETE FROM reserves");
399 $dbh->do("DELETE FROM issues");
400 $dbh->do("DELETE FROM items");
401 $dbh->do("DELETE FROM biblio");
402 $dbh->do("DELETE FROM biblioitems");
403 $dbh->do("DELETE FROM transport_cost");
404 $dbh->do("DELETE FROM tmp_holdsqueue");
405 $dbh->do("DELETE FROM hold_fill_targets");
406 $dbh->do("DELETE FROM default_branch_circ_rules");
407 $dbh->do("DELETE FROM default_branch_item_rules");
408 $dbh->do("DELETE FROM default_circ_rules");
409 $dbh->do("DELETE FROM branch_item_rules");
411 C4
::Context
->set_preference("UseTransportCostMatrix",1);
413 my $tc_rs = $schema->resultset('TransportCost');
414 $tc_rs->create({ frombranch
=> $library_A, tobranch
=> $library_B, cost
=> 0, disable_transfer
=> 1 });
415 $tc_rs->create({ frombranch
=> $library_B, tobranch
=> $library_A, cost
=> 0, disable_transfer
=> 1 });
418 INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
421 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
422 or BAIL_OUT
("Cannot find newly created biblio record");
424 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
427 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
428 or BAIL_OUT
("Cannot find newly created biblioitems record");
431 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
432 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
435 $reserve_id = AddReserve
( $library_B, $borrowernumber, $biblionumber, '', 1 );
436 C4
::HoldsQueue
::CreateQueue
();
437 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice
=> {} });
438 is
( @
$holds_queue, 0, "Bug 15062 - Holds queue with Transport Cost Matrix will transfer item even if transfers disabled");
441 # Test hold_fulfillment_policy
442 C4
::Context
->set_preference( "UseTransportCostMatrix", 0 );
443 $borrowernumber = $borrower3->{borrowernumber
};
444 $library_A = $library1->{branchcode
};
445 $library_B = $library2->{branchcode
};
446 $library_C = $library3->{branchcode
};
447 $dbh->do("DELETE FROM reserves");
448 $dbh->do("DELETE FROM issues");
449 $dbh->do("DELETE FROM items");
450 $dbh->do("DELETE FROM biblio");
451 $dbh->do("DELETE FROM biblioitems");
452 $dbh->do("DELETE FROM transport_cost");
453 $dbh->do("DELETE FROM tmp_holdsqueue");
454 $dbh->do("DELETE FROM hold_fill_targets");
455 $dbh->do("DELETE FROM default_branch_circ_rules");
456 $dbh->do("DELETE FROM default_branch_item_rules");
457 $dbh->do("DELETE FROM default_circ_rules");
458 $dbh->do("DELETE FROM branch_item_rules");
460 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
462 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
463 or BAIL_OUT
("Cannot find newly created biblio record");
465 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
468 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
469 or BAIL_OUT
("Cannot find newly created biblioitems record");
472 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
473 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
476 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
477 $dbh->do("DELETE FROM default_circ_rules");
478 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'homebranch' )");
480 # Home branch matches pickup branch
481 $reserve_id = AddReserve
( $library_A, $borrowernumber, $biblionumber, '', 1 );
482 C4
::HoldsQueue
::CreateQueue
();
483 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice
=> {} } );
484 is
( @
$holds_queue, 1, "Hold where pickup branch matches home branch targeted" );
485 CancelReserve
( { reserve_id
=> $reserve_id } );
487 # Holding branch matches pickup branch
488 $reserve_id = AddReserve
( $library_B, $borrowernumber, $biblionumber, '', 1 );
489 C4
::HoldsQueue
::CreateQueue
();
490 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice
=> {} } );
491 is
( @
$holds_queue, 0, "Hold where pickup ne home, pickup eq home not targeted" );
492 CancelReserve
( { reserve_id
=> $reserve_id } );
494 # Neither branch matches pickup branch
495 $reserve_id = AddReserve
( $library_C, $borrowernumber, $biblionumber, '', 1 );
496 C4
::HoldsQueue
::CreateQueue
();
497 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice
=> {} } );
498 is
( @
$holds_queue, 0, "Hold where pickup ne home, pickup ne holding not targeted" );
499 CancelReserve
( { reserve_id
=> $reserve_id } );
501 # With hold_fulfillment_policy = holdingbranch, hold should only be picked up if pickup branch = holdingbranch
502 $dbh->do("DELETE FROM default_circ_rules");
503 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'holdingbranch' )");
505 # Home branch matches pickup branch
506 $reserve_id = AddReserve
( $library_A, $borrowernumber, $biblionumber, '', 1 );
507 C4
::HoldsQueue
::CreateQueue
();
508 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice
=> {} } );
509 is
( @
$holds_queue, 0, "Hold where pickup eq home, pickup ne holding not targeted" );
510 CancelReserve
( { reserve_id
=> $reserve_id } );
512 # Holding branch matches pickup branch
513 $reserve_id = AddReserve
( $library_B, $borrowernumber, $biblionumber, '', 1 );
514 C4
::HoldsQueue
::CreateQueue
();
515 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice
=> {} } );
516 is
( @
$holds_queue, 1, "Hold where pickup ne home, pickup eq holding targeted" );
517 CancelReserve
( { reserve_id
=> $reserve_id } );
519 # Neither branch matches pickup branch
520 $reserve_id = AddReserve
( $library_C, $borrowernumber, $biblionumber, '', 1 );
521 C4
::HoldsQueue
::CreateQueue
();
522 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice
=> {} } );
523 is
( @
$holds_queue, 0, "Hold where pickup ne home, pickup ne holding not targeted" );
524 CancelReserve
( { reserve_id
=> $reserve_id } );
526 # With hold_fulfillment_policy = any, hold should be pikcup up reguardless of matching home or holding branch
527 $dbh->do("DELETE FROM default_circ_rules");
528 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'any' )");
530 # Home branch matches pickup branch
531 $reserve_id = AddReserve
( $library_A, $borrowernumber, $biblionumber, '', 1 );
532 C4
::HoldsQueue
::CreateQueue
();
533 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice
=> {} } );
534 is
( @
$holds_queue, 1, "Hold where pickup eq home, pickup ne holding targeted" );
535 CancelReserve
( { reserve_id
=> $reserve_id } );
537 # Holding branch matches pickup branch
538 $reserve_id = AddReserve
( $library_B, $borrowernumber, $biblionumber, '', 1 );
539 C4
::HoldsQueue
::CreateQueue
();
540 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice
=> {} } );
541 is
( @
$holds_queue, 1, "Hold where pickup ne home, pickup eq holding targeted" );
542 CancelReserve
( { reserve_id
=> $reserve_id } );
544 # Neither branch matches pickup branch
545 $reserve_id = AddReserve
( $library_C, $borrowernumber, $biblionumber, '', 1 );
546 C4
::HoldsQueue
::CreateQueue
();
547 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice
=> {} } );
548 is
( @
$holds_queue, 1, "Hold where pickup ne home, pickup ne holding targeted" );
549 CancelReserve
( { reserve_id
=> $reserve_id } );
551 # End testing hold_fulfillment_policy
553 # Test hold itemtype limit
554 C4
::Context
->set_preference( "UseTransportCostMatrix", 0 );
555 my $wrong_itemtype = $builder->build({ source
=> 'Itemtype', value
=> { notforloan
=> 0 } })->{itemtype
};
556 my $right_itemtype = $builder->build({ source
=> 'Itemtype', value
=> { notforloan
=> 0 } })->{itemtype
};
557 $borrowernumber = $borrower3->{borrowernumber
};
558 my $branchcode = $library1->{branchcode
};
559 $dbh->do("DELETE FROM reserves");
560 $dbh->do("DELETE FROM issues");
561 $dbh->do("DELETE FROM items");
562 $dbh->do("DELETE FROM biblio");
563 $dbh->do("DELETE FROM biblioitems");
564 $dbh->do("DELETE FROM transport_cost");
565 $dbh->do("DELETE FROM tmp_holdsqueue");
566 $dbh->do("DELETE FROM hold_fill_targets");
567 $dbh->do("DELETE FROM default_branch_circ_rules");
568 $dbh->do("DELETE FROM default_branch_item_rules");
569 $dbh->do("DELETE FROM default_circ_rules");
570 $dbh->do("DELETE FROM branch_item_rules");
572 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
574 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
575 or BAIL_OUT
("Cannot find newly created biblio record");
577 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
580 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
581 or BAIL_OUT
("Cannot find newly created biblioitems record");
584 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
585 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$right_itemtype')
588 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
589 $dbh->do("DELETE FROM default_circ_rules");
590 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'any' )");
592 # Home branch matches pickup branch
593 $reserve_id = AddReserve
( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, $wrong_itemtype );
594 C4
::HoldsQueue
::CreateQueue
();
595 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice
=> {} } );
596 is
( @
$holds_queue, 0, "Item with incorrect itemtype not targeted" );
597 CancelReserve
( { reserve_id
=> $reserve_id } );
599 # Holding branch matches pickup branch
600 $reserve_id = AddReserve
( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, $right_itemtype );
601 C4
::HoldsQueue
::CreateQueue
();
602 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice
=> {} } );
603 is
( @
$holds_queue, 1, "Item with matching itemtype is targeted" );
604 CancelReserve
( { reserve_id
=> $reserve_id } );
606 # Neither branch matches pickup branch
607 $reserve_id = AddReserve
( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, undef );
608 C4
::HoldsQueue
::CreateQueue
();
609 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice
=> {} } );
610 is
( @
$holds_queue, 1, "Item targeted when hold itemtype is not set" );
611 CancelReserve
( { reserve_id
=> $reserve_id } );
613 # End testing hold itemtype limit
616 $schema->storage->txn_rollback;
618 ### END Test holds queue builder does not violate holds policy ###
621 my ($test_name, $use_cost_matrix, $pick_branch, $hold_branch) = @_;
623 $test_name = "$test_name (".($use_cost_matrix ?
"" : "don't ")."use cost matrix)";
625 $use_cost_matrix_sth->execute($use_cost_matrix);
626 C4
::Context
->clear_syspref_cache();
627 C4
::HoldsQueue
::CreateQueue
();
629 my $results = $dbh->selectall_arrayref($test_sth, { Slice
=> {} }); # should be only one
630 my $r = $results->[0];
632 my $ok = is
( $r->{pickbranch
}, $pick_branch, "$test_name pick up branch");
633 $ok &&= is
( $r->{holdingbranch
}, $hold_branch, "$test_name holding branch")
636 diag
( "Wrong pick-up/hold for first target (pick_branch, hold_branch, reserves, hold_fill_targets, tmp_holdsqueue): "
637 . Dumper
($pick_branch, $hold_branch, map dump_records
($_), qw(reserves hold_fill_targets tmp_holdsqueue)) )
642 my ($tablename) = @_;
643 return $dbh->selectall_arrayref("SELECT * from $tablename where borrowernumber = ?", { Slice
=> {} }, $borrowernumber);