Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# Copyright 2023 ByWater Solutions |
4 |
# |
5 |
# This file is part of Koha |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it |
8 |
# under the terms of the GNU General Public License as published by |
9 |
# the Free Software Foundation; either version 3 of the License, or |
10 |
# (at your option) any later version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but |
13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
# GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License |
18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
19 |
|
20 |
use Modern::Perl; |
21 |
|
22 |
use Test::More tests => 7; |
23 |
|
24 |
use Koha::Database; |
25 |
use C4::Circulation qw(CreateBranchTransferLimit); |
26 |
use Koha::DateUtils qw( dt_from_string ); |
27 |
|
28 |
use t::lib::Mocks; |
29 |
use t::lib::TestBuilder; |
30 |
|
31 |
BEGIN { |
32 |
use_ok('Koha::Library::FloatLimit'); |
33 |
use_ok('Koha::Library::FloatLimits'); |
34 |
} |
35 |
|
36 |
my $schema = Koha::Database->new->schema; |
37 |
$schema->storage->txn_begin; |
38 |
|
39 |
my $builder = t::lib::TestBuilder->new; |
40 |
my $library1 = $builder->build( { source => 'Branch' } ); |
41 |
my $library2 = $builder->build( { source => 'Branch' } ); |
42 |
my $library3 = $builder->build( { source => 'Branch' } ); |
43 |
my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } ); |
44 |
|
45 |
my $biblio = $builder->build_sample_biblio(); |
46 |
|
47 |
for ( 1 .. 5 ) { |
48 |
$builder->build_sample_item( |
49 |
{ |
50 |
biblionumber => $biblio->biblionumber, |
51 |
homebranch => $library1->{branchcode}, |
52 |
holdingbranch => $library1->{branchcode}, |
53 |
itype => $itemtype->itemtype, |
54 |
} |
55 |
); |
56 |
} |
57 |
|
58 |
for ( 1 .. 10 ) { |
59 |
$builder->build_sample_item( |
60 |
{ |
61 |
biblionumber => $biblio->biblionumber, |
62 |
homebranch => $library2->{branchcode}, |
63 |
holdingbranch => $library2->{branchcode}, |
64 |
itype => $itemtype->itemtype, |
65 |
} |
66 |
); |
67 |
} |
68 |
|
69 |
for ( 1 .. 15 ) { |
70 |
$builder->build_sample_item( |
71 |
{ |
72 |
biblionumber => $biblio->biblionumber, |
73 |
homebranch => $library3->{branchcode}, |
74 |
holdingbranch => $library3->{branchcode}, |
75 |
itype => $itemtype->itemtype, |
76 |
} |
77 |
); |
78 |
} |
79 |
|
80 |
my $item = $builder->build_sample_item( |
81 |
{ |
82 |
biblionumber => $biblio->biblionumber, |
83 |
homebranch => $library1->{branchcode}, |
84 |
holdingbranch => $library1->{branchcode}, |
85 |
itype => $itemtype->itemtype, |
86 |
} |
87 |
); |
88 |
|
89 |
my $limit1 = Koha::Library::FloatLimit->new( |
90 |
{ |
91 |
branchcode => $library1->{branchcode}, |
92 |
itemtype => $itemtype->itemtype, |
93 |
float_limit => 1, |
94 |
} |
95 |
)->store(); |
96 |
|
97 |
my $float_limit2 = Koha::Library::FloatLimit->new( |
98 |
{ |
99 |
branchcode => $library2->{branchcode}, |
100 |
itemtype => $itemtype->itemtype, |
101 |
float_limit => 100, |
102 |
} |
103 |
)->store(); |
104 |
|
105 |
my $float_limit3 = Koha::Library::FloatLimit->new( |
106 |
{ |
107 |
branchcode => $library3->{branchcode}, |
108 |
itemtype => $itemtype->itemtype, |
109 |
float_limit => 1000, |
110 |
} |
111 |
)->store(); |
112 |
|
113 |
is( |
114 |
Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode, |
115 |
$library3->{branchcode}, |
116 |
"Correct library selected for float limit transfer" |
117 |
); |
118 |
|
119 |
t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '1' ); |
120 |
t::lib::Mocks::mock_preference( 'BranchTransferLimitsType', 'itemtype' ); |
121 |
|
122 |
my $to = $library3->{branchcode}; |
123 |
my $from = $item->holdingbranch; |
124 |
my $code = $itemtype->itemtype; |
125 |
CreateBranchTransferLimit( $to, $from, $code ); |
126 |
|
127 |
is( C4::Circulation::IsBranchTransferAllowed( $to, $from, $code ), 0, "Transfer to best library is no longer allowed" ); |
128 |
say "C4::Circulation::IsBranchTransferAllowed( $to, $from, $code )"; |
129 |
|
130 |
is( |
131 |
Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode, |
132 |
$library2->{branchcode}, |
133 |
"Correct library selected for float limit transfer when best cannot be used" |
134 |
); |
135 |
|
136 |
subtest 'onloan items excluded from ratio calculation' => sub { |
137 |
plan tests => 5; |
138 |
|
139 |
# Reset transfer limits for clean test |
140 |
t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '0' ); |
141 |
|
142 |
# Verify initial item counts |
143 |
my $library2_initial = Koha::Items->search( |
144 |
{ |
145 |
holdingbranch => $library2->{branchcode}, |
146 |
itype => $itemtype->itemtype, |
147 |
onloan => undef |
148 |
} |
149 |
)->count; |
150 |
|
151 |
my $library3_initial = Koha::Items->search( |
152 |
{ |
153 |
holdingbranch => $library3->{branchcode}, |
154 |
itype => $itemtype->itemtype, |
155 |
onloan => undef |
156 |
} |
157 |
)->count; |
158 |
|
159 |
is( $library2_initial, 10, "Library2 initially has 10 available items" ); |
160 |
is( $library3_initial, 15, "Library3 initially has 15 available items" ); |
161 |
|
162 |
# Initial library selection based on ratios |
163 |
is( |
164 |
Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode, |
165 |
$library3->{branchcode}, |
166 |
"Library3 initially selected (lowest ratio: 15/1000 = 0.015)" |
167 |
); |
168 |
|
169 |
# Check out enough items from library3 to change the selection |
170 |
my @library3_items = Koha::Items->search( |
171 |
{ |
172 |
holdingbranch => $library3->{branchcode}, |
173 |
itype => $itemtype->itemtype, |
174 |
onloan => undef |
175 |
} |
176 |
)->as_list; |
177 |
|
178 |
my $checkout_date = dt_from_string(); |
179 |
for my $i ( 0 .. 9 ) { |
180 |
$library3_items[$i]->onloan($checkout_date)->store; |
181 |
} |
182 |
|
183 |
# Verify the count decreased |
184 |
my $library3_after_checkout = Koha::Items->search( |
185 |
{ |
186 |
holdingbranch => $library3->{branchcode}, |
187 |
itype => $itemtype->itemtype, |
188 |
onloan => undef |
189 |
} |
190 |
)->count; |
191 |
|
192 |
is( $library3_after_checkout, 5, "Library3 now has 5 available items after 10 checkouts" ); |
193 |
|
194 |
# Library3 should still be selected |
195 |
is( |
196 |
Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode, |
197 |
$library3->{branchcode}, |
198 |
"Library3 still selected after checkouts (ratio now 5/1000 = 0.005, still better than library2's 10/100 = 0.1)" |
199 |
); |
200 |
}; |
201 |
|
202 |
subtest 'items in transit counted toward destination branch' => sub { |
203 |
plan tests => 6; |
204 |
|
205 |
# Reset transfer limits for clean test |
206 |
t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '0' ); |
207 |
|
208 |
Koha::Items->search( |
209 |
{ |
210 |
holdingbranch => $library3->{branchcode}, |
211 |
itype => $itemtype->itemtype, |
212 |
onloan => { '!=' => undef } |
213 |
} |
214 |
)->update( { onloan => undef } ); |
215 |
|
216 |
# Verify initial counts |
217 |
my $library2_initial = Koha::Items->search( |
218 |
{ |
219 |
holdingbranch => $library2->{branchcode}, |
220 |
itype => $itemtype->itemtype, |
221 |
onloan => undef |
222 |
} |
223 |
)->count; |
224 |
|
225 |
my $library3_initial = Koha::Items->search( |
226 |
{ |
227 |
holdingbranch => $library3->{branchcode}, |
228 |
itype => $itemtype->itemtype, |
229 |
onloan => undef |
230 |
} |
231 |
)->count; |
232 |
|
233 |
is( $library2_initial, 10, "Library2 initially has 10 available items" ); |
234 |
is( $library3_initial, 15, "Library3 initially has 15 available items" ); |
235 |
|
236 |
# Initial selection should be library3 |
237 |
is( |
238 |
Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode, |
239 |
$library3->{branchcode}, |
240 |
"Library3 initially selected (ratio: 15/1000 = 0.015)" |
241 |
); |
242 |
|
243 |
# Create items in transit TO library2 |
244 |
my @transit_items; |
245 |
for ( 1 .. 2 ) { |
246 |
my $transit_item = $builder->build_sample_item( |
247 |
{ |
248 |
biblionumber => $biblio->biblionumber, |
249 |
homebranch => $library1->{branchcode}, |
250 |
holdingbranch => $library1->{branchcode}, |
251 |
itype => $itemtype->itemtype, |
252 |
} |
253 |
); |
254 |
push @transit_items, $transit_item; |
255 |
} |
256 |
|
257 |
# Create active transfers to library2 |
258 |
my $transfer_date = dt_from_string(); |
259 |
for my $transit_item (@transit_items) { |
260 |
Koha::Item::Transfer->new( |
261 |
{ |
262 |
itemnumber => $transit_item->itemnumber, |
263 |
frombranch => $library1->{branchcode}, |
264 |
tobranch => $library2->{branchcode}, |
265 |
daterequested => $transfer_date, |
266 |
datesent => $transfer_date, |
267 |
reason => 'LibraryFloatLimit' |
268 |
} |
269 |
)->store; |
270 |
} |
271 |
|
272 |
# Verify that items in transit are counted toward destination |
273 |
my $in_transit_count = Koha::Items->search( |
274 |
{ |
275 |
itype => $itemtype->itemtype, |
276 |
}, |
277 |
{ |
278 |
join => 'branchtransfers', |
279 |
where => { |
280 |
'branchtransfers.tobranch' => $library2->{branchcode}, |
281 |
'branchtransfers.datearrived' => undef, |
282 |
'branchtransfers.datecancelled' => undef, |
283 |
}, |
284 |
distinct => 1 |
285 |
} |
286 |
)->count; |
287 |
|
288 |
is( $in_transit_count, 2, "2 items are in transit to library2" ); |
289 |
|
290 |
# Test 5: Library2's physical count should still be 10 |
291 |
my $library2_physical = Koha::Items->search( |
292 |
{ |
293 |
holdingbranch => $library2->{branchcode}, |
294 |
itype => $itemtype->itemtype, |
295 |
onloan => undef |
296 |
} |
297 |
)->count; |
298 |
|
299 |
is( $library2_physical, 10, "Library2 still has 10 physical items" ); |
300 |
|
301 |
# Library2's count should now be 10 + 2 = 12 |
302 |
is( |
303 |
Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode, |
304 |
$library3->{branchcode}, |
305 |
"Library3 still selected after items in transit to library2" |
306 |
); |
307 |
}; |
308 |
|
309 |
$schema->storage->txn_rollback; |