Bug 17196: Move marcxml out of the biblioitems table
[koha.git] / t / db_dependent / HoldsQueue.t
1 #!/usr/bin/perl
2
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
8
9 use Modern::Perl;
10
11 use Test::More tests => 42;
12 use Data::Dumper;
13
14 use C4::Calendar;
15 use C4::Context;
16 use C4::Members;
17 use Koha::Database;
18 use Koha::DateUtils;
19
20 use t::lib::TestBuilder;
21 use t::lib::Mocks;
22
23 BEGIN {
24     use FindBin;
25     use lib $FindBin::Bin;
26     use_ok('C4::Reserves');
27     use_ok('C4::HoldsQueue');
28 }
29
30 my $schema = Koha::Database->schema;
31 $schema->storage->txn_begin;
32 my $dbh = C4::Context->dbh;
33
34 my $builder = t::lib::TestBuilder->new;
35
36 my $library1 = $builder->build({
37     source => 'Branch',
38 });
39 my $library2 = $builder->build({
40     source => 'Branch',
41 });
42 my $library3 = $builder->build({
43     source => 'Branch',
44 });
45
46 my $TITLE = "Test Holds Queue XXX";
47
48 my $borrower = $builder->build({
49     source => 'Borrower',
50     value => {
51         branchcode => $library1->{branchcode},
52     }
53 });
54
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};
62
63 #Set up the stage
64 # Sysprefs and cost matrix
65 t::lib::Mocks::mock_preference('HoldsQueueSkipClosed', 0);
66 t::lib::Mocks::mock_preference('LocalHoldsPriority', 0);
67 $dbh->do("UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'", undef,
68          join( ',', @other_branches, $borrower_branchcode, $least_cost_branch_code));
69 $dbh->do("UPDATE systempreferences SET value = '0' WHERE variable = 'RandomizeHoldsQueueWeight'");
70
71 $dbh->do("DELETE FROM transport_cost");
72 my $transport_cost_insert_sth = $dbh->prepare("insert into transport_cost (frombranch, tobranch, cost) values (?, ?, ?)");
73 # Favour $least_cost_branch_code
74 $transport_cost_insert_sth->execute($borrower_branchcode, $least_cost_branch_code, 0.2);
75 $transport_cost_insert_sth->execute($least_cost_branch_code, $borrower_branchcode, 0.2);
76 my @b = @other_branches;
77 while ( my $b1 = shift @b ) {
78     foreach my $b2 ($borrower_branchcode, $least_cost_branch_code, @b) {
79         $transport_cost_insert_sth->execute($b1, $b2, 0.5);
80         $transport_cost_insert_sth->execute($b2, $b1, 0.5);
81     }
82 }
83
84
85 # Loanable items - all possible combinations of homebranch and holdingbranch
86 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated)
87           VALUES             ('SER', 'Koha test', '$TITLE', '2011-02-01')");
88 my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
89   or BAIL_OUT("Cannot find newly created biblio record");
90 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype)
91           VALUES                  ($biblionumber, '$itemtype')");
92 my $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
93   or BAIL_OUT("Cannot find newly created biblioitems record");
94
95 my $items_insert_sth = $dbh->prepare("INSERT INTO items (biblionumber, biblioitemnumber, barcode, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
96                                       VALUES            ($biblionumber, $biblioitemnumber, ?, ?, ?, 0, 0, 0, 0, NULL, '$itemtype')"); # CURRENT_DATE - 3)");
97 my $first_barcode = int(rand(1000000000000)); # XXX
98 my $barcode = $first_barcode;
99 foreach ( $borrower_branchcode, $least_cost_branch_code, @other_branches ) {
100     $items_insert_sth->execute($barcode++, $borrower_branchcode, $_);
101     $items_insert_sth->execute($barcode++, $_, $_);
102     $items_insert_sth->execute($barcode++, $_, $borrower_branchcode);
103 }
104
105 # Remove existing reserves, makes debugging easier
106 $dbh->do("DELETE FROM reserves");
107 my $bibitems = undef;
108 my $priority = 1;
109 # Make a reserve
110 AddReserve ( $borrower_branchcode, $borrowernumber, $biblionumber, $bibitems,  $priority );
111 #                           $resdate, $expdate, $notes, $title, $checkitem, $found
112 $dbh->do("UPDATE reserves SET reservedate = DATE_SUB( reservedate, INTERVAL 1 DAY )");
113
114 # Tests
115 my $use_cost_matrix_sth = $dbh->prepare("UPDATE systempreferences SET value = ? WHERE variable = 'UseTransportCostMatrix'");
116 my $test_sth = $dbh->prepare("SELECT * FROM hold_fill_targets
117                               JOIN tmp_holdsqueue USING (borrowernumber, biblionumber, itemnumber)
118                               JOIN items USING (itemnumber)
119                               WHERE borrowernumber = $borrowernumber");
120
121 # We have a book available homed in borrower branch, no point fiddling with AutomaticItemReturn
122 t::lib::Mocks::mock_preference('AutomaticItemReturn', 0);
123 test_queue ('take from homebranch',  0, $borrower_branchcode, $borrower_branchcode);
124 test_queue ('take from homebranch',  1, $borrower_branchcode, $borrower_branchcode);
125
126 $dbh->do("DELETE FROM tmp_holdsqueue");
127 $dbh->do("DELETE FROM hold_fill_targets");
128 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode')");
129 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode'");
130 # test_queue will flush
131 t::lib::Mocks::mock_preference('AutomaticItemReturn', 1);
132 # Not sure how to make this test more difficult - holding branch does not matter
133
134 $dbh->do("DELETE FROM tmp_holdsqueue");
135 $dbh->do("DELETE FROM hold_fill_targets");
136 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode')");
137 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode'");
138 t::lib::Mocks::mock_preference('AutomaticItemReturn', 0);
139 # We have a book available held in borrower branch
140 test_queue ('take from holdingbranch', 0, $borrower_branchcode, $borrower_branchcode);
141 test_queue ('take from holdingbranch', 1, $borrower_branchcode, $borrower_branchcode);
142
143 $dbh->do("DELETE FROM tmp_holdsqueue");
144 $dbh->do("DELETE FROM hold_fill_targets");
145 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE holdingbranch = '$borrower_branchcode')");
146 $dbh->do("DELETE FROM items WHERE holdingbranch = '$borrower_branchcode'");
147 # No book available in borrower branch, pick according to the rules
148 # Frst branch from StaticHoldsQueueWeight
149 test_queue ('take from lowest cost branch', 0, $borrower_branchcode, $other_branches[0]);
150 test_queue ('take from lowest cost branch', 1, $borrower_branchcode, $least_cost_branch_code);
151 my $queue = C4::HoldsQueue::GetHoldsQueueItems($least_cost_branch_code) || [];
152 my $queue_item = $queue->[0];
153 ok( $queue_item
154  && $queue_item->{pickbranch} eq $borrower_branchcode
155  && $queue_item->{holdingbranch} eq $least_cost_branch_code, "GetHoldsQueueItems" )
156   or diag( "Expected item for pick $borrower_branchcode, hold $least_cost_branch_code, got ".Dumper($queue_item) );
157 ok( exists($queue_item->{itype}), 'item type included in queued items list (bug 5825)' );
158
159 ok(
160     C4::HoldsQueue::least_cost_branch( 'B', [ 'A', 'B', 'C' ] ) eq 'B',
161     'C4::HoldsQueue::least_cost_branch returns the local branch if it is in the list of branches to pull from'
162 );
163
164 # XXX All this tests are for borrower branch pick-up.
165 # Maybe needs expanding to homebranch or holdingbranch pick-up.
166
167 $schema->txn_rollback;
168 $schema->txn_begin;
169
170 ### Test holds queue builder does not violate holds policy ###
171
172 # Clear out existing rules relating to holdallowed
173 $dbh->do("DELETE FROM default_branch_circ_rules");
174 $dbh->do("DELETE FROM default_branch_item_rules");
175 $dbh->do("DELETE FROM default_circ_rules");
176
177 t::lib::Mocks::mock_preference('UseTransportCostMatrix', 0);
178
179 $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
180
181 $library1 = $builder->build({
182     source => 'Branch',
183 });
184 $library2 = $builder->build({
185     source => 'Branch',
186 });
187 $library3 = $builder->build({
188     source => 'Branch',
189 });
190 @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode} );
191
192 my $borrower1 = $builder->build({
193     source => 'Borrower',
194     value => {
195         branchcode => $branchcodes[0],
196     },
197 });
198 my $borrower2 = $builder->build({
199     source => 'Borrower',
200     value => {
201         branchcode => $branchcodes[1],
202     },
203 });
204 my $borrower3 = $builder->build({
205     source => 'Borrower',
206     value => {
207         branchcode => $branchcodes[2],
208     },
209 });
210
211 $dbh->do(qq{
212     INSERT INTO biblio (
213         frameworkcode, 
214         author, 
215         title, 
216         datecreated
217     ) VALUES (
218         'SER', 
219         'Koha test', 
220         '$TITLE', 
221         '2011-02-01'
222     )
223 });
224 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
225   or BAIL_OUT("Cannot find newly created biblio record");
226
227 $dbh->do(qq{
228     INSERT INTO biblioitems (
229         biblionumber, 
230         itemtype
231     ) VALUES (
232         $biblionumber, 
233         '$itemtype'
234     )
235 });
236 $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
237   or BAIL_OUT("Cannot find newly created biblioitems record");
238
239 $items_insert_sth = $dbh->prepare(qq{
240     INSERT INTO items (
241         biblionumber, 
242         biblioitemnumber,
243         barcode,
244         homebranch,
245         holdingbranch,
246         notforloan,
247         damaged,
248         itemlost,
249         withdrawn,
250         onloan,
251         itype
252     ) VALUES (
253         $biblionumber,
254         $biblioitemnumber,
255         ?,
256         ?,
257         ?,
258         0,
259         0,
260         0,
261         0,
262         NULL,
263         '$itemtype'
264     )
265 });
266 # Create 3 items from 2 branches ( branches are for borrowers 1 and 2 respectively )
267 $barcode = int( rand(1000000000000) );
268 $items_insert_sth->execute( $barcode + 0, $branchcodes[0], $branchcodes[0] );
269 $items_insert_sth->execute( $barcode + 1, $branchcodes[1], $branchcodes[1] );
270 $items_insert_sth->execute( $barcode + 2, $branchcodes[1], $branchcodes[1] );
271
272 $dbh->do("DELETE FROM reserves");
273 my $sth = $dbh->prepare(q{
274     INSERT INTO reserves ( 
275         borrowernumber,
276         biblionumber,
277         branchcode,
278         priority,
279         reservedate
280     ) VALUES ( ?,?,?,?, CURRENT_DATE() )
281 });
282 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
283 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
284 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[0], 3 );
285
286 my $holds_queue;
287
288 $dbh->do("DELETE FROM default_circ_rules");
289 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 1 )");
290 C4::HoldsQueue::CreateQueue();
291 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
292 is( @$holds_queue, 2, "Holds queue filling correct number for default holds policy 'from home library'" );
293 is( $holds_queue->[0]->{cardnumber}, $borrower1->{cardnumber}, "Holds queue filling 1st correct hold for default holds policy 'from home library'");
294 is( $holds_queue->[1]->{cardnumber}, $borrower2->{cardnumber}, "Holds queue filling 2nd correct hold for default holds policy 'from home library'");
295
296 # Test skipping hold picks for closed libraries.
297 # At this point in the test, we have 2 rows in the holds queue
298 # 1 of which is coming from MPL. Let's enable HoldsQueueSkipClosed
299 # and make today a holiday for MPL. When we run it again we should only
300 # have 1 row in the holds queue
301 t::lib::Mocks::mock_preference('HoldsQueueSkipClosed', 1);
302 my $today = dt_from_string();
303 C4::Calendar->new( branchcode => $branchcodes[0] )->insert_single_holiday(
304     day         => $today->day(),
305     month       => $today->month(),
306     year        => $today->year(),
307     title       => "$today",
308     description => "$today",
309 );
310 # If the test below is removed, aother tests using the holiday will fail. For some reason if we call is_holiday now
311 # the holiday will get set in cache correctly, but not if we let C4::HoldsQueue call is_holiday instead.
312 is( Koha::Calendar->new( branchcode => $branchcodes[0] )->is_holiday( $today ), 1, 'Is today a holiday for pickup branch' );
313 C4::HoldsQueue::CreateQueue();
314 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
315 is( scalar( @$holds_queue ), 1, "Holds not filled with items from closed libraries" );
316 t::lib::Mocks::mock_preference('HoldsQueueSkipClosed', 0);
317
318 $dbh->do("DELETE FROM default_circ_rules");
319 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 2 )");
320 C4::HoldsQueue::CreateQueue();
321 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
322 is( @$holds_queue, 3, "Holds queue filling correct number for holds for default holds policy 'from any library'" );
323
324 # Test skipping hold picks for closed libraries without transport cost matrix
325 # At this point in the test, we have 3 rows in the holds queue
326 # one of which is coming from MPL. Let's enable HoldsQueueSkipClosed
327 # and use our previously created holiday for MPL
328 # When we run it again we should only have 2 rows in the holds queue
329 t::lib::Mocks::mock_preference( 'HoldsQueueSkipClosed', 1 );
330 C4::HoldsQueue::CreateQueue();
331 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
332 is( scalar( @$holds_queue ), 2, "Holds not filled with items from closed libraries" );
333 t::lib::Mocks::mock_preference( 'HoldsQueueSkipClosed', 0 );
334
335 ## Test LocalHoldsPriority
336 t::lib::Mocks::mock_preference('LocalHoldsPriority', 1);
337
338 $dbh->do("DELETE FROM default_circ_rules");
339 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 2 )");
340 $dbh->do("DELETE FROM issues");
341
342 # Test homebranch = patron branch
343 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'HomeLibrary');
344 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'homebranch');
345 C4::Context->clear_syspref_cache();
346 $dbh->do("DELETE FROM reserves");
347 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
348 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
349 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[0], 3 );
350
351 $dbh->do("DELETE FROM items");
352 # barcode, homebranch, holdingbranch, itemtype
353 $items_insert_sth->execute( $barcode + 4, $branchcodes[2], $branchcodes[0] );
354
355 C4::HoldsQueue::CreateQueue();
356 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
357 is( $holds_queue->[0]->{cardnumber}, $borrower3->{cardnumber}, "Holds queue giving priority to patron who's home library matches item's home library");
358
359 # Test holdingbranch = patron branch
360 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'HomeLibrary');
361 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'holdingbranch');
362 C4::Context->clear_syspref_cache();
363 $dbh->do("DELETE FROM reserves");
364 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
365 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
366 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[0], 3 );
367
368 $dbh->do("DELETE FROM items");
369 # barcode, homebranch, holdingbranch, itemtype
370 $items_insert_sth->execute( $barcode + 4, $branchcodes[0], $branchcodes[2] );
371
372 C4::HoldsQueue::CreateQueue();
373 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
374 is( $holds_queue->[0]->{cardnumber}, $borrower3->{cardnumber}, "Holds queue giving priority to patron who's home library matches item's holding library");
375
376 # Test holdingbranch = pickup branch
377 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'PickupLibrary');
378 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'holdingbranch');
379 C4::Context->clear_syspref_cache();
380 $dbh->do("DELETE FROM reserves");
381 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
382 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
383 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[2], 3 );
384
385 $dbh->do("DELETE FROM items");
386 # barcode, homebranch, holdingbranch, itemtype
387 $items_insert_sth->execute( $barcode + 4, $branchcodes[0], $branchcodes[2] );
388
389 C4::HoldsQueue::CreateQueue();
390 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
391 is( $holds_queue->[0]->{cardnumber}, $borrower3->{cardnumber}, "Holds queue giving priority to patron who's home library matches item's holding library");
392
393 # Test homebranch = pickup branch
394 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'PickupLibrary');
395 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'homebranch');
396 C4::Context->clear_syspref_cache();
397 $dbh->do("DELETE FROM reserves");
398 $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
399 $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
400 $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[2], 3 );
401
402 $dbh->do("DELETE FROM items");
403 # barcode, homebranch, holdingbranch, itemtype
404 $items_insert_sth->execute( $barcode + 4, $branchcodes[2], $branchcodes[0] );
405
406 C4::HoldsQueue::CreateQueue();
407 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
408 is( $holds_queue->[0]->{cardnumber}, $borrower3->{cardnumber}, "Holds queue giving priority to patron who's home library matches item's holding library");
409
410 t::lib::Mocks::mock_preference('LocalHoldsPriority', 0);
411 ## End testing of LocalHoldsPriority
412
413
414 # Bug 14297
415 $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
416 $borrowernumber = $borrower3->{borrowernumber};
417 my $library_A = $library1->{branchcode};
418 my $library_B = $library2->{branchcode};
419 my $library_C = $borrower3->{branchcode};
420 $dbh->do("DELETE FROM reserves");
421 $dbh->do("DELETE FROM issues");
422 $dbh->do("DELETE FROM items");
423 $dbh->do("DELETE FROM biblio");
424 $dbh->do("DELETE FROM biblioitems");
425 $dbh->do("DELETE FROM transport_cost");
426 $dbh->do("DELETE FROM tmp_holdsqueue");
427 $dbh->do("DELETE FROM hold_fill_targets");
428 $dbh->do("DELETE FROM default_branch_circ_rules");
429 $dbh->do("DELETE FROM default_branch_item_rules");
430 $dbh->do("DELETE FROM default_circ_rules");
431 $dbh->do("DELETE FROM branch_item_rules");
432
433 $dbh->do("
434     INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
435 ");
436
437 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
438   or BAIL_OUT("Cannot find newly created biblio record");
439
440 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
441
442 $biblioitemnumber =
443   $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
444   or BAIL_OUT("Cannot find newly created biblioitems record");
445
446 $dbh->do("
447     INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
448     VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
449 ");
450
451 $dbh->do("
452     INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
453     VALUES ($biblionumber, $biblioitemnumber, '$library_B', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
454 ");
455
456 $dbh->do("
457     INSERT INTO branch_item_rules ( branchcode, itemtype, holdallowed, returnbranch ) VALUES
458     ( '$library_A', '$itemtype', 2, 'homebranch' ), ( '$library_B', '$itemtype', 1, 'homebranch' );
459 ");
460
461 $dbh->do( "UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'",
462     undef, join( ',', $library_B, $library_A, $library_C ) );
463 $dbh->do( "UPDATE systempreferences SET value = 0 WHERE variable = 'RandomizeHoldsQueueWeight'" );
464
465 my $reserve_id = AddReserve ( $library_C, $borrowernumber, $biblionumber, '', 1 );
466 C4::HoldsQueue::CreateQueue();
467 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
468 is( @$holds_queue, 1, "Bug 14297 - Holds Queue building ignoring holds where pickup & home branch don't match and item is not from le");
469 # End Bug 14297
470
471 # Bug 15062
472 $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
473 $borrowernumber = $borrower2->{borrowernumber};
474 $library_A = $library1->{branchcode};
475 $library_B = $library2->{branchcode};
476 $dbh->do("DELETE FROM reserves");
477 $dbh->do("DELETE FROM issues");
478 $dbh->do("DELETE FROM items");
479 $dbh->do("DELETE FROM biblio");
480 $dbh->do("DELETE FROM biblioitems");
481 $dbh->do("DELETE FROM transport_cost");
482 $dbh->do("DELETE FROM tmp_holdsqueue");
483 $dbh->do("DELETE FROM hold_fill_targets");
484 $dbh->do("DELETE FROM default_branch_circ_rules");
485 $dbh->do("DELETE FROM default_branch_item_rules");
486 $dbh->do("DELETE FROM default_circ_rules");
487 $dbh->do("DELETE FROM branch_item_rules");
488
489 t::lib::Mocks::mock_preference("UseTransportCostMatrix",1);
490
491 my $tc_rs = $schema->resultset('TransportCost');
492 $tc_rs->create({ frombranch => $library_A, tobranch => $library_B, cost => 0, disable_transfer => 1 });
493 $tc_rs->create({ frombranch => $library_B, tobranch => $library_A, cost => 0, disable_transfer => 1 });
494
495 $dbh->do("
496     INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
497 ");
498
499 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
500   or BAIL_OUT("Cannot find newly created biblio record");
501
502 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
503
504 $biblioitemnumber =
505   $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
506   or BAIL_OUT("Cannot find newly created biblioitems record");
507
508 $dbh->do("
509     INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
510     VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
511 ");
512
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, 0, "Bug 15062 - Holds queue with Transport Cost Matrix will transfer item even if transfers disabled");
517 # End Bug 15062
518
519 # Test hold_fulfillment_policy
520 t::lib::Mocks::mock_preference( "UseTransportCostMatrix", 0 );
521 $borrowernumber = $borrower3->{borrowernumber};
522 $library_A = $library1->{branchcode};
523 $library_B = $library2->{branchcode};
524 $library_C = $library3->{branchcode};
525 $dbh->do("DELETE FROM reserves");
526 $dbh->do("DELETE FROM issues");
527 $dbh->do("DELETE FROM items");
528 $dbh->do("DELETE FROM biblio");
529 $dbh->do("DELETE FROM biblioitems");
530 $dbh->do("DELETE FROM transport_cost");
531 $dbh->do("DELETE FROM tmp_holdsqueue");
532 $dbh->do("DELETE FROM hold_fill_targets");
533 $dbh->do("DELETE FROM default_branch_circ_rules");
534 $dbh->do("DELETE FROM default_branch_item_rules");
535 $dbh->do("DELETE FROM default_circ_rules");
536 $dbh->do("DELETE FROM branch_item_rules");
537
538 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
539
540 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
541   or BAIL_OUT("Cannot find newly created biblio record");
542
543 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
544
545 $biblioitemnumber =
546   $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
547   or BAIL_OUT("Cannot find newly created biblioitems record");
548
549 $dbh->do("
550     INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
551     VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
552 ");
553
554 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
555 $dbh->do("DELETE FROM default_circ_rules");
556 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'homebranch' )");
557
558 # Home branch matches pickup branch
559 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
560 C4::HoldsQueue::CreateQueue();
561 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
562 is( @$holds_queue, 1, "Hold where pickup branch matches home branch targeted" );
563 CancelReserve( { reserve_id => $reserve_id } );
564
565 # Holding branch matches pickup branch
566 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
567 C4::HoldsQueue::CreateQueue();
568 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
569 is( @$holds_queue, 0, "Hold where pickup ne home, pickup eq home not targeted" );
570 CancelReserve( { reserve_id => $reserve_id } );
571
572 # Neither branch matches pickup branch
573 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
574 C4::HoldsQueue::CreateQueue();
575 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
576 is( @$holds_queue, 0, "Hold where pickup ne home, pickup ne holding not targeted" );
577 CancelReserve( { reserve_id => $reserve_id } );
578
579 # With hold_fulfillment_policy = holdingbranch, hold should only be picked up if pickup branch = holdingbranch
580 $dbh->do("DELETE FROM default_circ_rules");
581 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'holdingbranch' )");
582
583 # Home branch matches pickup branch
584 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
585 C4::HoldsQueue::CreateQueue();
586 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
587 is( @$holds_queue, 0, "Hold where pickup eq home, pickup ne holding not targeted" );
588 CancelReserve( { reserve_id => $reserve_id } );
589
590 # Holding branch matches pickup branch
591 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
592 C4::HoldsQueue::CreateQueue();
593 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
594 is( @$holds_queue, 1, "Hold where pickup ne home, pickup eq holding targeted" );
595 CancelReserve( { reserve_id => $reserve_id } );
596
597 # Neither branch matches pickup branch
598 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
599 C4::HoldsQueue::CreateQueue();
600 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
601 is( @$holds_queue, 0, "Hold where pickup ne home, pickup ne holding not targeted" );
602 CancelReserve( { reserve_id => $reserve_id } );
603
604 # With hold_fulfillment_policy = any, hold should be pikcup up reguardless of matching home or holding branch
605 $dbh->do("DELETE FROM default_circ_rules");
606 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'any' )");
607
608 # Home branch matches pickup branch
609 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
610 C4::HoldsQueue::CreateQueue();
611 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
612 is( @$holds_queue, 1, "Hold where pickup eq home, pickup ne holding targeted" );
613 CancelReserve( { reserve_id => $reserve_id } );
614
615 # Holding branch matches pickup branch
616 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
617 C4::HoldsQueue::CreateQueue();
618 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
619 is( @$holds_queue, 1, "Hold where pickup ne home, pickup eq holding targeted" );
620 CancelReserve( { reserve_id => $reserve_id } );
621
622 # Neither branch matches pickup branch
623 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
624 C4::HoldsQueue::CreateQueue();
625 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
626 is( @$holds_queue, 1, "Hold where pickup ne home, pickup ne holding targeted" );
627 CancelReserve( { reserve_id => $reserve_id } );
628
629 # End testing hold_fulfillment_policy
630
631 # Test hold itemtype limit
632 t::lib::Mocks::mock_preference( "UseTransportCostMatrix", 0 );
633 my $wrong_itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
634 my $right_itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
635 $borrowernumber = $borrower3->{borrowernumber};
636 my $branchcode = $library1->{branchcode};
637 $dbh->do("DELETE FROM reserves");
638 $dbh->do("DELETE FROM issues");
639 $dbh->do("DELETE FROM items");
640 $dbh->do("DELETE FROM biblio");
641 $dbh->do("DELETE FROM biblioitems");
642 $dbh->do("DELETE FROM transport_cost");
643 $dbh->do("DELETE FROM tmp_holdsqueue");
644 $dbh->do("DELETE FROM hold_fill_targets");
645 $dbh->do("DELETE FROM default_branch_circ_rules");
646 $dbh->do("DELETE FROM default_branch_item_rules");
647 $dbh->do("DELETE FROM default_circ_rules");
648 $dbh->do("DELETE FROM branch_item_rules");
649
650 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
651
652 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
653   or BAIL_OUT("Cannot find newly created biblio record");
654
655 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
656
657 $biblioitemnumber =
658   $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
659   or BAIL_OUT("Cannot find newly created biblioitems record");
660
661 $dbh->do("
662     INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
663     VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$right_itemtype')
664 ");
665
666 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
667 $dbh->do("DELETE FROM default_circ_rules");
668 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'any' )");
669
670 # Home branch matches pickup branch
671 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, $wrong_itemtype );
672 C4::HoldsQueue::CreateQueue();
673 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
674 is( @$holds_queue, 0, "Item with incorrect itemtype not targeted" );
675 CancelReserve( { reserve_id => $reserve_id } );
676
677 # Holding branch matches pickup branch
678 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, $right_itemtype );
679 C4::HoldsQueue::CreateQueue();
680 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
681 is( @$holds_queue, 1, "Item with matching itemtype is targeted" );
682 CancelReserve( { reserve_id => $reserve_id } );
683
684 # Neither branch matches pickup branch
685 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, undef );
686 C4::HoldsQueue::CreateQueue();
687 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
688 is( @$holds_queue, 1, "Item targeted when hold itemtype is not set" );
689 CancelReserve( { reserve_id => $reserve_id } );
690
691 # End testing hold itemtype limit
692
693 # Cleanup
694 $schema->storage->txn_rollback;
695
696 ### END Test holds queue builder does not violate holds policy ###
697
698 sub test_queue {
699     my ($test_name, $use_cost_matrix, $pick_branch, $hold_branch) = @_;
700
701     $test_name = "$test_name (".($use_cost_matrix ? "" : "don't ")."use cost matrix)";
702
703     $use_cost_matrix_sth->execute($use_cost_matrix);
704     C4::Context->clear_syspref_cache();
705     C4::HoldsQueue::CreateQueue();
706
707     my $results = $dbh->selectall_arrayref($test_sth, { Slice => {} }); # should be only one
708     my $r = $results->[0];
709
710     my $ok = is( $r->{pickbranch}, $pick_branch, "$test_name pick up branch");
711     $ok &&=  is( $r->{holdingbranch}, $hold_branch, "$test_name holding branch")
712       if $hold_branch;
713
714     diag( "Wrong pick-up/hold for first target (pick_branch, hold_branch, reserves, hold_fill_targets, tmp_holdsqueue): "
715         . Dumper ($pick_branch, $hold_branch, map dump_records($_), qw(reserves hold_fill_targets tmp_holdsqueue)) )
716       unless $ok;
717 }
718
719 sub dump_records {
720     my ($tablename) = @_;
721     return $dbh->selectall_arrayref("SELECT * from $tablename where borrowernumber = ?", { Slice => {} }, $borrowernumber);
722 }