Bug 15758: Koha::Libraries - Ultimate duel for C4::Branch
[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 => 38;
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
22 BEGIN {
23     use FindBin;
24     use lib $FindBin::Bin;
25     use_ok('C4::Reserves');
26     use_ok('C4::HoldsQueue');
27 }
28
29 my $schema = Koha::Database->schema;
30 $schema->storage->txn_begin;
31 my $dbh = C4::Context->dbh;
32
33 my $builder = t::lib::TestBuilder->new;
34
35 my $library1 = $builder->build({
36     source => 'Branch',
37 });
38 my $library2 = $builder->build({
39     source => 'Branch',
40 });
41 my $library3 = $builder->build({
42     source => 'Branch',
43 });
44
45 my $TITLE = "Test Holds Queue XXX";
46
47 my $borrower = $builder->build({
48     source => 'Borrower',
49     value => {
50         branchcode => $library1->{branchcode},
51     }
52 });
53
54 my $borrowernumber = $borrower->{borrowernumber};
55 # Set special (for this test) branches
56 my $borrower_branchcode = $borrower->{branchcode};
57 my @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode} );
58 my @other_branches = ( $library2->{branchcode}, $library3->{branchcode} );
59 my $least_cost_branch_code = pop @other_branches;
60 my $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
61
62 #Set up the stage
63 # Sysprefs and cost matrix
64 C4::Context->set_preference('HoldsQueueSkipClosed', 0);
65 $dbh->do("UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'", undef,
66          join( ',', @other_branches, $borrower_branchcode, $least_cost_branch_code));
67 $dbh->do("UPDATE systempreferences SET value = '0' WHERE variable = 'RandomizeHoldsQueueWeight'");
68
69 $dbh->do("DELETE FROM transport_cost");
70 my $transport_cost_insert_sth = $dbh->prepare("insert into transport_cost (frombranch, tobranch, cost) values (?, ?, ?)");
71 # Favour $least_cost_branch_code
72 $transport_cost_insert_sth->execute($borrower_branchcode, $least_cost_branch_code, 0.2);
73 $transport_cost_insert_sth->execute($least_cost_branch_code, $borrower_branchcode, 0.2);
74 my @b = @other_branches;
75 while ( my $b1 = shift @b ) {
76     foreach my $b2 ($borrower_branchcode, $least_cost_branch_code, @b) {
77         $transport_cost_insert_sth->execute($b1, $b2, 0.5);
78         $transport_cost_insert_sth->execute($b2, $b1, 0.5);
79     }
80 }
81
82
83 # Loanable items - all possible combinations of homebranch and holdingbranch
84 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated)
85           VALUES             ('SER', 'Koha test', '$TITLE', '2011-02-01')");
86 my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
87   or BAIL_OUT("Cannot find newly created biblio record");
88 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype)
89           VALUES                  ($biblionumber, '', '$itemtype')");
90 my $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
91   or BAIL_OUT("Cannot find newly created biblioitems record");
92
93 my $items_insert_sth = $dbh->prepare("INSERT INTO items (biblionumber, biblioitemnumber, barcode, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
94                                       VALUES            ($biblionumber, $biblioitemnumber, ?, ?, ?, 0, 0, 0, 0, NULL, '$itemtype')"); # CURRENT_DATE - 3)");
95 my $first_barcode = int(rand(1000000000000)); # XXX
96 my $barcode = $first_barcode;
97 foreach ( $borrower_branchcode, $least_cost_branch_code, @other_branches ) {
98     $items_insert_sth->execute($barcode++, $borrower_branchcode, $_);
99     $items_insert_sth->execute($barcode++, $_, $_);
100     $items_insert_sth->execute($barcode++, $_, $borrower_branchcode);
101 }
102
103 # Remove existing reserves, makes debugging easier
104 $dbh->do("DELETE FROM reserves");
105 my $bibitems = undef;
106 my $priority = 1;
107 # Make a reserve
108 AddReserve ( $borrower_branchcode, $borrowernumber, $biblionumber, $bibitems,  $priority );
109 #                           $resdate, $expdate, $notes, $title, $checkitem, $found
110 $dbh->do("UPDATE reserves SET reservedate = DATE_SUB( reservedate, INTERVAL 1 DAY )");
111
112 # Tests
113 my $use_cost_matrix_sth = $dbh->prepare("UPDATE systempreferences SET value = ? WHERE variable = 'UseTransportCostMatrix'");
114 my $test_sth = $dbh->prepare("SELECT * FROM hold_fill_targets
115                               JOIN tmp_holdsqueue USING (borrowernumber, biblionumber, itemnumber)
116                               JOIN items USING (itemnumber)
117                               WHERE borrowernumber = $borrowernumber");
118
119 # We have a book available homed in borrower branch, no point fiddling with AutomaticItemReturn
120 C4::Context->set_preference('AutomaticItemReturn', 0);
121 test_queue ('take from homebranch',  0, $borrower_branchcode, $borrower_branchcode);
122 test_queue ('take from homebranch',  1, $borrower_branchcode, $borrower_branchcode);
123
124 $dbh->do("DELETE FROM tmp_holdsqueue");
125 $dbh->do("DELETE FROM hold_fill_targets");
126 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode')");
127 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode'");
128 # test_queue will flush
129 C4::Context->set_preference('AutomaticItemReturn', 1);
130 # Not sure how to make this test more difficult - holding branch does not matter
131
132 $dbh->do("DELETE FROM tmp_holdsqueue");
133 $dbh->do("DELETE FROM hold_fill_targets");
134 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode')");
135 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode'");
136 C4::Context->set_preference('AutomaticItemReturn', 0);
137 # We have a book available held in borrower branch
138 test_queue ('take from holdingbranch', 0, $borrower_branchcode, $borrower_branchcode);
139 test_queue ('take from holdingbranch', 1, $borrower_branchcode, $borrower_branchcode);
140
141 $dbh->do("DELETE FROM tmp_holdsqueue");
142 $dbh->do("DELETE FROM hold_fill_targets");
143 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE holdingbranch = '$borrower_branchcode')");
144 $dbh->do("DELETE FROM items WHERE holdingbranch = '$borrower_branchcode'");
145 # No book available in borrower branch, pick according to the rules
146 # Frst branch from StaticHoldsQueueWeight
147 test_queue ('take from lowest cost branch', 0, $borrower_branchcode, $other_branches[0]);
148 test_queue ('take from lowest cost branch', 1, $borrower_branchcode, $least_cost_branch_code);
149 my $queue = C4::HoldsQueue::GetHoldsQueueItems($least_cost_branch_code) || [];
150 my $queue_item = $queue->[0];
151 ok( $queue_item
152  && $queue_item->{pickbranch} eq $borrower_branchcode
153  && $queue_item->{holdingbranch} eq $least_cost_branch_code, "GetHoldsQueueItems" )
154   or diag( "Expected item for pick $borrower_branchcode, hold $least_cost_branch_code, got ".Dumper($queue_item) );
155 ok( exists($queue_item->{itype}), 'item type included in queued items list (bug 5825)' );
156
157 ok(
158     C4::HoldsQueue::least_cost_branch( 'B', [ 'A', 'B', 'C' ] ) eq 'B',
159     'C4::HoldsQueue::least_cost_branch returns the local branch if it is in the list of branches to pull from'
160 );
161
162 # XXX All this tests are for borrower branch pick-up.
163 # Maybe needs expanding to homebranch or holdingbranch pick-up.
164
165 $schema->txn_rollback;
166 $schema->txn_begin;
167
168 ### Test holds queue builder does not violate holds policy ###
169
170 # Clear out existing rules relating to holdallowed
171 $dbh->do("DELETE FROM default_branch_circ_rules");
172 $dbh->do("DELETE FROM default_branch_item_rules");
173 $dbh->do("DELETE FROM default_circ_rules");
174
175 C4::Context->set_preference('UseTransportCostMatrix', 0);
176
177 $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
178
179 $library1 = $builder->build({
180     source => 'Branch',
181 });
182 $library2 = $builder->build({
183     source => 'Branch',
184 });
185 $library3 = $builder->build({
186     source => 'Branch',
187 });
188 @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode} );
189
190 my $borrower1 = $builder->build({
191     source => 'Borrower',
192     value => {
193         branchcode => $branchcodes[0],
194     },
195 });
196 my $borrower2 = $builder->build({
197     source => 'Borrower',
198     value => {
199         branchcode => $branchcodes[1],
200     },
201 });
202 my $borrower3 = $builder->build({
203     source => 'Borrower',
204     value => {
205         branchcode => $branchcodes[2],
206     },
207 });
208
209 $dbh->do(qq{
210     INSERT INTO biblio (
211         frameworkcode, 
212         author, 
213         title, 
214         datecreated
215     ) VALUES (
216         'SER', 
217         'Koha test', 
218         '$TITLE', 
219         '2011-02-01'
220     )
221 });
222 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
223   or BAIL_OUT("Cannot find newly created biblio record");
224
225 $dbh->do(qq{
226     INSERT INTO biblioitems (
227         biblionumber, 
228         marcxml, 
229         itemtype
230     ) VALUES (
231         $biblionumber, 
232         '', 
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 C4::Context->set_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 C4::Context->set_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 C4::Context->set_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 C4::Context->set_preference( 'HoldsQueueSkipClosed', 0 );
334
335 # Bug 14297
336 $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
337 $borrowernumber = $borrower3->{borrowernumber};
338 my $library_A = $library1->{branchcode};
339 my $library_B = $library2->{branchcode};
340 my $library_C = $borrower3->{branchcode};
341 $dbh->do("DELETE FROM reserves");
342 $dbh->do("DELETE FROM issues");
343 $dbh->do("DELETE FROM items");
344 $dbh->do("DELETE FROM biblio");
345 $dbh->do("DELETE FROM biblioitems");
346 $dbh->do("DELETE FROM transport_cost");
347 $dbh->do("DELETE FROM tmp_holdsqueue");
348 $dbh->do("DELETE FROM hold_fill_targets");
349 $dbh->do("DELETE FROM default_branch_circ_rules");
350 $dbh->do("DELETE FROM default_branch_item_rules");
351 $dbh->do("DELETE FROM default_circ_rules");
352 $dbh->do("DELETE FROM branch_item_rules");
353
354 $dbh->do("
355     INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
356 ");
357
358 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
359   or BAIL_OUT("Cannot find newly created biblio record");
360
361 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
362
363 $biblioitemnumber =
364   $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
365   or BAIL_OUT("Cannot find newly created biblioitems record");
366
367 $dbh->do("
368     INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
369     VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
370 ");
371
372 $dbh->do("
373     INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
374     VALUES ($biblionumber, $biblioitemnumber, '$library_B', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
375 ");
376
377 $dbh->do("
378     INSERT INTO branch_item_rules ( branchcode, itemtype, holdallowed, returnbranch ) VALUES
379     ( '$library_A', '$itemtype', 2, 'homebranch' ), ( '$library_B', '$itemtype', 1, 'homebranch' );
380 ");
381
382 $dbh->do( "UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'",
383     undef, join( ',', $library_B, $library_A, $library_C ) );
384 $dbh->do( "UPDATE systempreferences SET value = 0 WHERE variable = 'RandomizeHoldsQueueWeight'" );
385
386 my $reserve_id = AddReserve ( $library_C, $borrowernumber, $biblionumber, '', 1 );
387 C4::HoldsQueue::CreateQueue();
388 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
389 is( @$holds_queue, 1, "Bug 14297 - Holds Queue building ignoring holds where pickup & home branch don't match and item is not from le");
390 # End Bug 14297
391
392 # Bug 15062
393 $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
394 $borrowernumber = $borrower2->{borrowernumber};
395 $library_A = $library1->{branchcode};
396 $library_B = $library2->{branchcode};
397 $dbh->do("DELETE FROM reserves");
398 $dbh->do("DELETE FROM issues");
399 $dbh->do("DELETE FROM items");
400 $dbh->do("DELETE FROM biblio");
401 $dbh->do("DELETE FROM biblioitems");
402 $dbh->do("DELETE FROM transport_cost");
403 $dbh->do("DELETE FROM tmp_holdsqueue");
404 $dbh->do("DELETE FROM hold_fill_targets");
405 $dbh->do("DELETE FROM default_branch_circ_rules");
406 $dbh->do("DELETE FROM default_branch_item_rules");
407 $dbh->do("DELETE FROM default_circ_rules");
408 $dbh->do("DELETE FROM branch_item_rules");
409
410 C4::Context->set_preference("UseTransportCostMatrix",1);
411
412 my $tc_rs = $schema->resultset('TransportCost');
413 $tc_rs->create({ frombranch => $library_A, tobranch => $library_B, cost => 0, disable_transfer => 1 });
414 $tc_rs->create({ frombranch => $library_B, tobranch => $library_A, cost => 0, disable_transfer => 1 });
415
416 $dbh->do("
417     INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')
418 ");
419
420 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
421   or BAIL_OUT("Cannot find newly created biblio record");
422
423 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
424
425 $biblioitemnumber =
426   $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
427   or BAIL_OUT("Cannot find newly created biblioitems record");
428
429 $dbh->do("
430     INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
431     VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
432 ");
433
434 $reserve_id = AddReserve ( $library_B, $borrowernumber, $biblionumber, '', 1 );
435 C4::HoldsQueue::CreateQueue();
436 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
437 is( @$holds_queue, 0, "Bug 15062 - Holds queue with Transport Cost Matrix will transfer item even if transfers disabled");
438 # End Bug 15062
439
440 # Test hold_fulfillment_policy
441 C4::Context->set_preference( "UseTransportCostMatrix", 0 );
442 $borrowernumber = $borrower3->{borrowernumber};
443 $library_A = $library1->{branchcode};
444 $library_B = $library2->{branchcode};
445 $library_C = $library3->{branchcode};
446 $dbh->do("DELETE FROM reserves");
447 $dbh->do("DELETE FROM issues");
448 $dbh->do("DELETE FROM items");
449 $dbh->do("DELETE FROM biblio");
450 $dbh->do("DELETE FROM biblioitems");
451 $dbh->do("DELETE FROM transport_cost");
452 $dbh->do("DELETE FROM tmp_holdsqueue");
453 $dbh->do("DELETE FROM hold_fill_targets");
454 $dbh->do("DELETE FROM default_branch_circ_rules");
455 $dbh->do("DELETE FROM default_branch_item_rules");
456 $dbh->do("DELETE FROM default_circ_rules");
457 $dbh->do("DELETE FROM branch_item_rules");
458
459 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
460
461 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
462   or BAIL_OUT("Cannot find newly created biblio record");
463
464 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
465
466 $biblioitemnumber =
467   $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
468   or BAIL_OUT("Cannot find newly created biblioitems record");
469
470 $dbh->do("
471     INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
472     VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
473 ");
474
475 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
476 $dbh->do("DELETE FROM default_circ_rules");
477 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'homebranch' )");
478
479 # Home branch matches pickup branch
480 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
481 C4::HoldsQueue::CreateQueue();
482 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
483 is( @$holds_queue, 1, "Hold where pickup branch matches home branch targeted" );
484 CancelReserve( { reserve_id => $reserve_id } );
485
486 # Holding branch matches pickup branch
487 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
488 C4::HoldsQueue::CreateQueue();
489 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
490 is( @$holds_queue, 0, "Hold where pickup ne home, pickup eq home not targeted" );
491 CancelReserve( { reserve_id => $reserve_id } );
492
493 # Neither branch matches pickup branch
494 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
495 C4::HoldsQueue::CreateQueue();
496 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
497 is( @$holds_queue, 0, "Hold where pickup ne home, pickup ne holding not targeted" );
498 CancelReserve( { reserve_id => $reserve_id } );
499
500 # With hold_fulfillment_policy = holdingbranch, hold should only be picked up if pickup branch = holdingbranch
501 $dbh->do("DELETE FROM default_circ_rules");
502 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'holdingbranch' )");
503
504 # Home branch matches pickup branch
505 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
506 C4::HoldsQueue::CreateQueue();
507 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
508 is( @$holds_queue, 0, "Hold where pickup eq home, pickup ne holding not targeted" );
509 CancelReserve( { reserve_id => $reserve_id } );
510
511 # Holding branch matches pickup branch
512 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
513 C4::HoldsQueue::CreateQueue();
514 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
515 is( @$holds_queue, 1, "Hold where pickup ne home, pickup eq holding targeted" );
516 CancelReserve( { reserve_id => $reserve_id } );
517
518 # Neither branch matches pickup branch
519 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
520 C4::HoldsQueue::CreateQueue();
521 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
522 is( @$holds_queue, 0, "Hold where pickup ne home, pickup ne holding not targeted" );
523 CancelReserve( { reserve_id => $reserve_id } );
524
525 # With hold_fulfillment_policy = any, hold should be pikcup up reguardless of matching home or holding branch
526 $dbh->do("DELETE FROM default_circ_rules");
527 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'any' )");
528
529 # Home branch matches pickup branch
530 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
531 C4::HoldsQueue::CreateQueue();
532 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
533 is( @$holds_queue, 1, "Hold where pickup eq home, pickup ne holding targeted" );
534 CancelReserve( { reserve_id => $reserve_id } );
535
536 # Holding branch matches pickup branch
537 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
538 C4::HoldsQueue::CreateQueue();
539 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
540 is( @$holds_queue, 1, "Hold where pickup ne home, pickup eq holding targeted" );
541 CancelReserve( { reserve_id => $reserve_id } );
542
543 # Neither branch matches pickup branch
544 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
545 C4::HoldsQueue::CreateQueue();
546 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
547 is( @$holds_queue, 1, "Hold where pickup ne home, pickup ne holding targeted" );
548 CancelReserve( { reserve_id => $reserve_id } );
549
550 # End testing hold_fulfillment_policy
551
552 # Test hold itemtype limit
553 C4::Context->set_preference( "UseTransportCostMatrix", 0 );
554 my $wrong_itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
555 my $right_itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
556 $borrowernumber = $borrower3->{borrowernumber};
557 my $branchcode = $library1->{branchcode};
558 $dbh->do("DELETE FROM reserves");
559 $dbh->do("DELETE FROM issues");
560 $dbh->do("DELETE FROM items");
561 $dbh->do("DELETE FROM biblio");
562 $dbh->do("DELETE FROM biblioitems");
563 $dbh->do("DELETE FROM transport_cost");
564 $dbh->do("DELETE FROM tmp_holdsqueue");
565 $dbh->do("DELETE FROM hold_fill_targets");
566 $dbh->do("DELETE FROM default_branch_circ_rules");
567 $dbh->do("DELETE FROM default_branch_item_rules");
568 $dbh->do("DELETE FROM default_circ_rules");
569 $dbh->do("DELETE FROM branch_item_rules");
570
571 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
572
573 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
574   or BAIL_OUT("Cannot find newly created biblio record");
575
576 $dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
577
578 $biblioitemnumber =
579   $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
580   or BAIL_OUT("Cannot find newly created biblioitems record");
581
582 $dbh->do("
583     INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
584     VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$right_itemtype')
585 ");
586
587 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
588 $dbh->do("DELETE FROM default_circ_rules");
589 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'any' )");
590
591 # Home branch matches pickup branch
592 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, $wrong_itemtype );
593 C4::HoldsQueue::CreateQueue();
594 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
595 is( @$holds_queue, 0, "Item with incorrect itemtype not targeted" );
596 CancelReserve( { reserve_id => $reserve_id } );
597
598 # Holding branch matches pickup branch
599 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, $right_itemtype );
600 C4::HoldsQueue::CreateQueue();
601 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
602 is( @$holds_queue, 1, "Item with matching itemtype is targeted" );
603 CancelReserve( { reserve_id => $reserve_id } );
604
605 # Neither branch matches pickup branch
606 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, undef );
607 C4::HoldsQueue::CreateQueue();
608 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
609 is( @$holds_queue, 1, "Item targeted when hold itemtype is not set" );
610 CancelReserve( { reserve_id => $reserve_id } );
611
612 # End testing hold itemtype limit
613
614 # Cleanup
615 $schema->storage->txn_rollback;
616
617 ### END Test holds queue builder does not violate holds policy ###
618
619 sub test_queue {
620     my ($test_name, $use_cost_matrix, $pick_branch, $hold_branch) = @_;
621
622     $test_name = "$test_name (".($use_cost_matrix ? "" : "don't ")."use cost matrix)";
623
624     $use_cost_matrix_sth->execute($use_cost_matrix);
625     C4::Context->clear_syspref_cache();
626     C4::HoldsQueue::CreateQueue();
627
628     my $results = $dbh->selectall_arrayref($test_sth, { Slice => {} }); # should be only one
629     my $r = $results->[0];
630
631     my $ok = is( $r->{pickbranch}, $pick_branch, "$test_name pick up branch");
632     $ok &&=  is( $r->{holdingbranch}, $hold_branch, "$test_name holding branch")
633       if $hold_branch;
634
635     diag( "Wrong pick-up/hold for first target (pick_branch, hold_branch, reserves, hold_fill_targets, tmp_holdsqueue): "
636         . Dumper ($pick_branch, $hold_branch, map dump_records($_), qw(reserves hold_fill_targets tmp_holdsqueue)) )
637       unless $ok;
638 }
639
640 sub dump_records {
641     my ($tablename) = @_;
642     return $dbh->selectall_arrayref("SELECT * from $tablename where borrowernumber = ?", { Slice => {} }, $borrowernumber);
643 }