Bug 24683: Fix for take smart rules into account in "if all unavailable"
[koha.git] / t / db_dependent / Holds / DisallowHoldIfItemsAvailable.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use C4::Context;
6 use C4::Circulation;
7 use C4::Items;
8 use Koha::Items;
9 use Koha::CirculationRules;
10
11 use Test::More tests => 11;
12
13 use t::lib::TestBuilder;
14 use t::lib::Mocks;
15
16 BEGIN {
17     use_ok('C4::Reserves');
18 }
19
20 my $schema = Koha::Database->schema;
21 $schema->storage->txn_begin;
22 my $dbh = C4::Context->dbh;
23
24 my $builder = t::lib::TestBuilder->new;
25
26 my $library1 = $builder->build({
27     source => 'Branch',
28 });
29 my $library2 = $builder->build({
30     source => 'Branch',
31 });
32 my $itemtype = $builder->build({
33     source => 'Itemtype',
34     value  => { notforloan => 0 }
35 })->{itemtype};
36
37 t::lib::Mocks::mock_userenv({ branchcode => $library1->{branchcode} });
38
39
40 my $patron1 = $builder->build_object({
41     class => 'Koha::Patrons',
42     value => {
43         branchcode => $library1->{branchcode},
44         dateexpiry => '3000-01-01',
45     }
46 });
47 my $borrower1 = $patron1->unblessed;
48
49 my $patron2 = $builder->build_object({
50     class => 'Koha::Patrons',
51     value => {
52         branchcode => $library1->{branchcode},
53         dateexpiry => '3000-01-01',
54     }
55 });
56
57 my $patron3 = $builder->build_object({
58     class => 'Koha::Patrons',
59     value => {
60         branchcode => $library2->{branchcode},
61         dateexpiry => '3000-01-01',
62     }
63 });
64
65 my $library_A = $library1->{branchcode};
66 my $library_B = $library2->{branchcode};
67
68 my $biblio = $builder->build_sample_biblio({itemtype=>$itemtype});
69 my $biblionumber = $biblio->biblionumber;
70 my $item1  = $builder->build_sample_item({
71     biblionumber=>$biblionumber,
72     itype=>$itemtype,
73     homebranch => $library_A,
74     holdingbranch => $library_A
75 });
76 my $item2  = $builder->build_sample_item({
77     biblionumber=>$biblionumber,
78     itype=>$itemtype,
79     homebranch => $library_A,
80     holdingbranch => $library_A
81 });
82
83 # Test hold_fulfillment_policy
84 $dbh->do("DELETE FROM circulation_rules");
85 Koha::CirculationRules->set_rules(
86     {
87         categorycode => undef,
88         itemtype     => $itemtype,
89         branchcode   => undef,
90         rules        => {
91             issuelength     => 7,
92             lengthunit      => 8,
93             reservesallowed => 99,
94             onshelfholds    => 2,
95         }
96     }
97 );
98
99 my $is;
100
101 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 });
102 is( $is, 1, "Items availability: both of 2 items are available" );
103
104 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
105 is( $is, 0, "Item cannot be held, 2 items available" );
106
107 my $issue1 = AddIssue( $patron2->unblessed, $item1->barcode );
108
109 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 });
110 is( $is, 1, "Items availability: one item is available" );
111
112 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
113 is( $is, 0, "Item cannot be held, 1 item available" );
114
115 AddIssue( $patron2->unblessed, $item2->barcode );
116
117 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 });
118 is( $is, 0, "Items availability: none of items are available" );
119
120 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
121 is( $is, 1, "Item can be held, no items available" );
122
123 AddReturn( $item1->barcode );
124
125 { # Remove the issue for the first patron, and modify the branch for item1
126     subtest 'IsAvailableForItemLevelRequest behaviours depending on ReservesControlBranch + holdallowed' => sub {
127         plan tests => 2;
128
129         my $hold_allowed_from_home_library = 1;
130         my $hold_allowed_from_any_libraries = 2;
131
132         subtest 'Item is available at a different library' => sub {
133             plan tests => 13;
134
135             $item1->set({homebranch => $library_B, holdingbranch => $library_B })->store;
136             #Scenario is:
137             #One shelf holds is 'If all unavailable'/2
138             #Item 1 homebranch library B is available
139             #Item 2 homebranch library A is checked out
140             #Borrower1 is from library A
141
142             {
143                 set_holdallowed_rule( $hold_allowed_from_home_library );
144
145                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
146
147                 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 }); # patron1 in library A, library A 0 items, library B 1 item
148                 is( $is, 0, "Items availability: hold allowed from home + ReservesControlBranch=ItemHomeLibrary + one item is available at different library" );
149
150                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
151                 is( $is, 1, "Hold allowed from home library + ReservesControlBranch=ItemHomeLibrary, One item is available at different library, not holdable = none available => the hold is allowed at item level" );
152                 $is = IsAvailableForItemLevelRequest( $item1, $patron2);
153                 is( $is, 1, "Hold allowed from home library + ReservesControlBranch=ItemHomeLibrary, One item is available at home library, holdable = one available => the hold is not allowed at item level" );
154                 set_holdallowed_rule( $hold_allowed_from_any_libraries, $library_B );
155                 #Adding a rule for the item's home library affects the availability for a borrower from another library because ReservesControlBranch is set to ItemHomeLibrary
156
157                 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 }); # patron1 in library A, library A 0 items, library B 1 item
158                 is( $is, 1, "Items availability: hold allowed from any library for library B + ReservesControlBranch=ItemHomeLibrary + one item is available at different library" );
159
160                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
161                 is( $is, 0, "Hold allowed from home library + ReservesControlBranch=ItemHomeLibrary, One item is available at different library, holdable = one available => the hold is not allowed at item level" );
162
163                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
164
165                 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 }); # patron1 in library A, library A 0 items, library B 1 item
166                 is( $is, 0, "Items availability: hold allowed from any library for library B + ReservesControlBranch=PatronLibrary + one item is available at different library" );
167
168                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
169                 is( $is, 1, "Hold allowed from home library + ReservesControlBranch=PatronLibrary, One item is available at different library, not holdable = none available => the hold is allowed at item level" );
170                 #Adding a rule for the patron's home library affects the availability for an item from another library because ReservesControlBranch is set to PatronLibrary
171                 set_holdallowed_rule( $hold_allowed_from_any_libraries, $library_A );
172
173                 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 }); # patron1 in library A, library A 0 items, library B 1 item
174                 is( $is, 1, "Items availability: hold allowed from any library for library A + ReservesControlBranch=PatronLibrary + one item is available at different library" );
175
176                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
177                 is( $is, 0, "Hold allowed from home library + ReservesControlBranch=PatronLibrary, One item is available at different library, holdable = one available => the hold is not allowed at item level" );
178             }
179
180             {
181                 set_holdallowed_rule( $hold_allowed_from_any_libraries );
182
183                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
184
185                 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 }); # patron1 in library A, library A 0 items, library B 1 item
186                 is( $is, 1, "Items availability: hold allowed from any library + ReservesControlBranch=ItemHomeLibrary + one item is available at different library" );
187
188                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
189                 is( $is, 0, "Hold allowed from any library + ReservesControlBranch=ItemHomeLibrary, One item is available at the diff library, holdable = 1 available => the hold is not allowed at item level" );
190
191                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
192
193                 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 }); # patron1 in library A, library A 0 items, library B 1 item
194                 is( $is, 1, "Items availability: hold allowed from any library + ReservesControlBranch=PatronLibrary + one item is available at different library" );
195
196                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
197                 is( $is, 0, "Hold allowed from any library + ReservesControlBranch=PatronLibrary, One item is available at the diff library, holdable = 1 available => the hold is not allowed at item level" );
198             }
199         };
200
201         subtest 'Item is available at the same library' => sub {
202             plan tests => 8;
203
204             $item1->set({homebranch => $library_A, holdingbranch => $library_A })->store;
205             #Scenario is:
206             #One shelf holds is 'If all unavailable'/2
207             #Item 1 homebranch library A is available
208             #Item 2 homebranch library A is checked out
209             #Borrower1 is from library A
210             #CircControl has no effect - same rule for all branches as set at line 96
211             #ReservesControlBranch is not checked in these subs we are testing?
212
213             {
214                 set_holdallowed_rule( $hold_allowed_from_home_library );
215
216                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
217
218                 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 }); # patron1 in library A, library A 1 item
219                 is( $is, 1, "Items availability: hold allowed from home library + ReservesControlBranch=ItemHomeLibrary + one item is available at home library" );
220
221                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
222                 is( $is, 0, "Hold allowed from home library + ReservesControlBranch=ItemHomeLibrary, One item is available at the same library, holdable = 1 available  => the hold is not allowed at item level" );
223
224                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
225
226                 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 }); # patron1 in library A, library A 1 item
227                 is( $is, 1, "Items availability: hold allowed from home library + ReservesControlBranch=PatronLibrary + one item is available at home library" );
228
229                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
230                 is( $is, 0, "Hold allowed from home library + ReservesControlBranch=PatronLibrary, One item is available at the same library, holdable = 1 available  => the hold is not allowed at item level" );
231             }
232
233             {
234                 set_holdallowed_rule( $hold_allowed_from_any_libraries );
235
236                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
237
238                 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 }); # patron1 in library A, library A 1 item
239                 is( $is, 1, "Items availability: hold allowed from any library + ReservesControlBranch=ItemHomeLibrary + one item is available at home library" );
240
241                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
242                 is( $is, 0, "Hold allowed from any library + ReservesControlBranch=ItemHomeLibrary, One item is available at the same library, holdable = 1 available => the hold is not allowed at item level" );
243
244                 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
245
246                 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 }); # patron1 in library A, library A 1 item
247                 is( $is, 1, "Items availability: hold allowed from any library + ReservesControlBranch=PatronLibrary + one item is available at home library" );
248
249                 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
250                 is( $is, 0, "Hold allowed from any library + ReservesControlBranch=PatronLibrary, One item is available at the same library, holdable = 1 available  => the hold is not allowed at item level" );
251             }
252         };
253     };
254 }
255
256 my $itemtype2 = $builder->build({
257     source => 'Itemtype',
258     value  => { notforloan => 0 }
259 })->{itemtype};
260 my $item3 = $builder->build_sample_item({ itype => $itemtype2 });
261
262 my $hold = $builder->build({
263     source => 'Reserve',
264     value =>{
265         itemnumber => $item3->itemnumber,
266         found => 'T'
267     }
268 });
269
270 Koha::CirculationRules->set_rules(
271     {
272         categorycode => undef,
273         itemtype     => $itemtype2,
274         branchcode   => undef,
275         rules        => {
276             maxissueqty     => 99,
277             onshelfholds    => 0,
278         }
279     }
280 );
281
282 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 }); # patron1 in library A, library A 1 item
283 is( $is, 1, "Items availability: 1 item is available, 1 item held in T" );
284
285 $is = IsAvailableForItemLevelRequest( $item3, $patron1);
286 is( $is, 1, "Item can be held, items in transit are not available" );
287
288 subtest 'Check holds availability with different item types' => sub {
289     plan tests => 6;
290
291     # Check for holds availability when different item types have different
292     # smart rules assigned both with "if all unavailable" set,
293     # and $itemtype rule allows holds, $itemtype2 rule disallows holds.
294     # So, $item should be available for hold when checked out even if $item2
295     # is not checked out, because anyway $item2 unavailable for holds by rule
296     # (Bug 24683):
297
298     my $biblio2       = $builder->build_sample_biblio( { itemtype => $itemtype } );
299     my $biblionumber1 = $biblio2->biblionumber;
300     my $item4         = $builder->build_sample_item(
301         {   biblionumber  => $biblionumber1,
302             itype         => $itemtype,
303             homebranch    => $library_A,
304             holdingbranch => $library_A
305         }
306     );
307     my $item5 = $builder->build_sample_item(
308         {   biblionumber  => $biblionumber1,
309             itype         => $itemtype2,
310             homebranch    => $library_A,
311             holdingbranch => $library_A
312         }
313     );
314
315     # Test hold_fulfillment_policy
316     $dbh->do("DELETE FROM circulation_rules");
317     Koha::CirculationRules->set_rules(
318         {   categorycode => undef,
319             itemtype     => $itemtype,
320             branchcode   => undef,
321             rules        => {
322                 issuelength      => 7,
323                 lengthunit       => 8,
324                 reservesallowed  => 99,
325                 holds_per_record => 99,
326                 onshelfholds     => 2,
327             }
328         }
329     );
330     Koha::CirculationRules->set_rules(
331         {   categorycode => undef,
332             itemtype     => $itemtype2,
333             branchcode   => undef,
334             rules        => {
335                 issuelength      => 7,
336                 lengthunit       => 8,
337                 reservesallowed  => 0,
338                 holds_per_record => 0,
339                 onshelfholds     => 2,
340             }
341         }
342     );
343
344     $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber1, patron => $patron1 } );
345     is( $is, 1, "Items availability: 2 items, one allowed by smart rule but not checked out, another one not allowed to be held by smart rule" );
346
347     $is = IsAvailableForItemLevelRequest( $item4, $patron1 );
348     is( $is, 0, "Item4 cannot be requested to hold: 2 items, Item4 available, Item5 restricted" );
349
350     $is = IsAvailableForItemLevelRequest( $item5, $patron1 );
351     is( $is, 0, "Item5 cannot be requested to hold: 2 items, Item4 available, Item5 restricted" );
352
353     AddIssue( $patron2->unblessed, $item4->barcode );
354
355     $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber1, patron => $patron1 } );
356     is( $is, 0, "Items availability: 2 items, one allowed by smart rule and checked out, another one not allowed to be held by smart rule" );
357
358     $is = IsAvailableForItemLevelRequest( $item4, $patron1 );
359     is( $is, 1, "Item4 can be requested to hold, 2 items, Item4 checked out, Item5 restricted" );
360
361     $is = IsAvailableForItemLevelRequest( $item5, $patron1 );
362     # Note: read IsAvailableForItemLevelRequest sub description about CanItemBeReserved/CanBookBeReserved:
363     is( $is, 1, "Item5 can be requested to hold, 2 items, Item4 checked out, Item5 restricted" );
364 };
365
366
367 # Cleanup
368 $schema->storage->txn_rollback;
369
370 sub set_holdallowed_rule {
371     my ( $holdallowed, $branchcode ) = @_;
372     Koha::CirculationRules->set_rules(
373         {
374             branchcode   => $branchcode || undef,
375             itemtype     => undef,
376             rules        => {
377                 holdallowed              => $holdallowed,
378                 hold_fulfillment_policy  => 'any',
379                 returnbranch             => 'homebranch',
380             }
381         }
382     );
383 }