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