Lines 24-30
use DateTime::Duration;
Link Here
|
24 |
use Koha::Database; |
24 |
use Koha::Database; |
25 |
use Koha::DateUtils; |
25 |
use Koha::DateUtils; |
26 |
use Koha::Item::Transfer; |
26 |
use Koha::Item::Transfer; |
|
|
27 |
|
27 |
use t::lib::TestBuilder; |
28 |
use t::lib::TestBuilder; |
|
|
29 |
use t::lib::Mocks; |
28 |
|
30 |
|
29 |
use Test::More tests => 8; |
31 |
use Test::More tests => 8; |
30 |
|
32 |
|
Lines 135-160
subtest 'Tests for needs_repatriating' => sub {
Link Here
|
135 |
}; |
137 |
}; |
136 |
|
138 |
|
137 |
subtest "Tests for repatriate." => sub { |
139 |
subtest "Tests for repatriate." => sub { |
138 |
plan tests => 3; |
140 |
plan tests => 9; |
139 |
$schema->storage->txn_begin; |
141 |
$schema->storage->txn_begin; |
140 |
my $sritem = $builder->build( |
142 |
|
|
|
143 |
my $sritem_1 = $builder->build_object( |
141 |
{ |
144 |
{ |
142 |
source => 'Stockrotationitem', |
145 |
class => 'Koha::StockRotationItems', |
143 |
value => |
146 |
value => { |
144 |
{ itemnumber_id => $builder->build_sample_item->itemnumber } |
147 |
itemnumber_id => $builder->build_sample_item->itemnumber |
|
|
148 |
} |
145 |
} |
149 |
} |
146 |
); |
150 |
); |
147 |
my $dbitem = Koha::StockRotationItems->find($sritem->{itemnumber_id}); |
151 |
my $item_id = $sritem_1->itemnumber->itemnumber; |
148 |
$dbitem->stage->position(1); |
152 |
my $srstage_1 = $sritem_1->stage; |
149 |
$dbitem->stage->duration(50); |
153 |
$sritem_1->discard_changes; |
|
|
154 |
$sritem_1->stage->position(1); |
155 |
$sritem_1->stage->duration(50); |
150 |
my $branch = $builder->build({ source => 'Branch' }); |
156 |
my $branch = $builder->build({ source => 'Branch' }); |
151 |
$dbitem->itemnumber->holdingbranch($branch->{branchcode}); |
157 |
$sritem_1->itemnumber->holdingbranch($branch->{branchcode}); |
152 |
|
158 |
|
153 |
# Test a straight up repatriate |
159 |
# Test a straight up repatriate |
154 |
ok($dbitem->repatriate, "Repatriation done."); |
160 |
ok($sritem_1->repatriate, "Repatriation done."); |
155 |
my $intransfer = $dbitem->itemnumber->get_transfer; |
161 |
my $intransfer = $sritem_1->itemnumber->get_transfer; |
|
|
162 |
is($intransfer->frombranch, $branch->{branchcode}, "Origin correct."); |
163 |
is($intransfer->tobranch, $sritem_1->stage->branchcode_id, "Target Correct."); |
164 |
|
165 |
# Reset |
166 |
$intransfer->datearrived(dt_from_string())->store; |
167 |
$sritem_1->itemnumber->holdingbranch($branch->{branchcode}); |
168 |
|
169 |
# Setup a conflicting manual transfer |
170 |
my $item = Koha::Items->find($item_id); |
171 |
$item->request_transfer({ to => $srstage_1->branchcode, reason => "Manual" }); |
172 |
$intransfer = $item->get_transfer; |
173 |
is (ref($intransfer), 'Koha::Item::Transfer', "Conflicting transfer added"); |
174 |
is ($intransfer->reason, 'Manual', "Conflicting transfer reason is 'Manual'"); |
175 |
|
176 |
# Stockrotation should handle transfer clashes |
177 |
is($sritem_1->repatriate, 0, "Repatriation skipped if transfer in progress."); |
178 |
|
179 |
# Reset |
180 |
$intransfer->datearrived(dt_from_string())->store; |
181 |
$sritem_1->itemnumber->holdingbranch($branch->{branchcode}); |
182 |
|
183 |
# Confirm that stockrotation ignores transfer limits |
184 |
t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1); |
185 |
t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype'); |
186 |
my $limit = Koha::Item::Transfer::Limit->new( |
187 |
{ |
188 |
fromBranch => $branch->{branchcode}, |
189 |
toBranch => $srstage_1->branchcode_id, |
190 |
itemtype => $sritem_1->itemnumber->effective_itemtype, |
191 |
} |
192 |
)->store; |
193 |
|
194 |
# Stockrotation should overrule transfer limits |
195 |
ok($sritem_1->repatriate, "Repatriation done regardless of transfer limits."); |
196 |
$intransfer = $sritem_1->itemnumber->get_transfer; |
156 |
is($intransfer->frombranch, $branch->{branchcode}, "Origin correct."); |
197 |
is($intransfer->frombranch, $branch->{branchcode}, "Origin correct."); |
157 |
is($intransfer->tobranch, $dbitem->stage->branchcode_id, "Target Correct."); |
198 |
is($intransfer->tobranch, $sritem_1->stage->branchcode_id, "Target Correct."); |
158 |
|
199 |
|
159 |
$schema->storage->txn_rollback; |
200 |
$schema->storage->txn_rollback; |
160 |
}; |
201 |
}; |
Lines 229-340
subtest "Tests for needs_advancing." => sub {
Link Here
|
229 |
}; |
270 |
}; |
230 |
|
271 |
|
231 |
subtest "Tests for advance." => sub { |
272 |
subtest "Tests for advance." => sub { |
232 |
plan tests => 23; |
273 |
plan tests => 44; |
233 |
$schema->storage->txn_begin; |
274 |
$schema->storage->txn_begin; |
234 |
|
275 |
|
235 |
my $sritem = $builder->build( |
276 |
my $sritem_1 = $builder->build_object( |
236 |
{ |
277 |
{ |
237 |
source => 'Stockrotationitem', |
278 |
class => 'Koha::StockRotationItems', |
238 |
value => { |
279 |
value => { |
239 |
'fresh' => 1, |
280 |
'fresh' => 1, |
240 |
itemnumber_id => $builder->build_sample_item->itemnumber |
281 |
itemnumber_id => $builder->build_sample_item->itemnumber |
241 |
} |
282 |
} |
242 |
} |
283 |
} |
243 |
); |
284 |
); |
244 |
my $dbitem = Koha::StockRotationItems->find($sritem->{itemnumber_id}); |
285 |
$sritem_1->discard_changes; |
245 |
$dbitem->itemnumber->holdingbranch($dbitem->stage->branchcode_id); |
286 |
$sritem_1->itemnumber->holdingbranch($sritem_1->stage->branchcode_id); |
246 |
my $dbstage = $dbitem->stage; |
287 |
my $item_id = $sritem_1->itemnumber->itemnumber; |
247 |
$dbstage->position(1)->duration(50)->store; # Configure stage. |
288 |
my $srstage_1 = $sritem_1->stage; |
|
|
289 |
$srstage_1->position(1)->duration(50)->store; # Configure stage. |
248 |
# Configure item |
290 |
# Configure item |
249 |
$dbitem->itemnumber->holdingbranch($dbstage->branchcode_id)->store; |
291 |
$sritem_1->itemnumber->holdingbranch($srstage_1->branchcode_id)->store; |
250 |
$dbitem->itemnumber->homebranch($dbstage->branchcode_id)->store; |
292 |
$sritem_1->itemnumber->homebranch($srstage_1->branchcode_id)->store; |
251 |
# Sanity check |
293 |
# Sanity check |
252 |
is($dbitem->stage->stage_id, $dbstage->stage_id, "Stage sanity check."); |
294 |
is($sritem_1->stage->stage_id, $srstage_1->stage_id, "Stage sanity check."); |
253 |
|
295 |
|
254 |
# Test if an item is fresh, always move to first stage. |
296 |
# Test if an item is fresh, always move to first stage. |
255 |
is($dbitem->fresh, 1, "Fresh is correct."); |
297 |
is($sritem_1->fresh, 1, "Fresh is correct."); |
256 |
$dbitem->advance; |
298 |
$sritem_1->advance; |
257 |
is($dbitem->stage->stage_id, $dbstage->stage_id, "Stage is first stage after fresh advance."); |
299 |
is($sritem_1->stage->stage_id, $srstage_1->stage_id, "Stage is first stage after fresh advance."); |
258 |
is($dbitem->fresh, 0, "Fresh reset after advance."); |
300 |
is($sritem_1->fresh, 0, "Fresh reset after advance."); |
259 |
|
301 |
|
260 |
# Test cases of single stage |
302 |
# Test cases of single stage |
261 |
$dbstage->rota->cyclical(1)->store; # Set Rota to cyclical. |
303 |
$srstage_1->rota->cyclical(1)->store; # Set Rota to cyclical. |
262 |
ok($dbitem->advance, "Single stage cyclical advance done."); |
304 |
ok($sritem_1->advance, "Single stage cyclical advance done."); |
263 |
## Refetch dbitem |
305 |
## Refetch sritem_1 |
264 |
$dbitem = Koha::StockRotationItems->find($sritem->{itemnumber_id}); |
306 |
$sritem_1->discard_changes; |
265 |
is($dbitem->stage->stage_id, $dbstage->stage_id, "Single stage cyclical stage OK."); |
307 |
is($sritem_1->stage->stage_id, $srstage_1->stage_id, "Single stage cyclical stage OK."); |
266 |
|
308 |
|
267 |
# Test with indemand advance |
309 |
# Test with indemand advance |
268 |
$dbitem->indemand(1)->store; |
310 |
$sritem_1->indemand(1)->store; |
269 |
ok($dbitem->advance, "Indemand item advance done."); |
311 |
ok($sritem_1->advance, "Indemand item advance done."); |
270 |
## Refetch dbitem |
312 |
## Refetch sritem_1 |
271 |
$dbitem = Koha::StockRotationItems->find($sritem->{itemnumber_id}); |
313 |
$sritem_1->discard_changes; |
272 |
is($dbitem->indemand, 0, "Indemand OK."); |
314 |
is($sritem_1->indemand, 0, "Indemand OK."); |
273 |
is($dbitem->stage->stage_id, $dbstage->stage_id, "Indemand item advance stage OK."); |
315 |
is($sritem_1->stage->stage_id, $srstage_1->stage_id, "Indemand item advance stage OK."); |
274 |
|
316 |
|
275 |
# Multi stages |
317 |
# Multi stages |
276 |
my $srstage = $builder->build({ |
318 |
my $srstage_2 = $builder->build_object({ |
277 |
source => 'Stockrotationstage', |
319 |
class => 'Koha::StockRotationStages', |
278 |
value => { duration => 50 } |
320 |
value => { duration => 50 } |
279 |
}); |
321 |
}); |
280 |
my $dbstage2 = Koha::StockRotationStages->find($srstage->{stage_id}); |
322 |
$srstage_2->discard_changes; |
281 |
$dbstage2->move_to_group($dbitem->stage->rota_id); |
323 |
$srstage_2->move_to_group($sritem_1->stage->rota_id); |
282 |
$dbstage2->move_last; |
324 |
$srstage_2->move_last; |
283 |
|
325 |
|
284 |
# Test a straight up advance |
326 |
# Test a straight up advance |
285 |
ok($dbitem->advance, "Advancement done."); |
327 |
ok($sritem_1->advance, "Advancement done."); |
286 |
## Refetch dbitem |
328 |
## Refetch sritem_1 |
287 |
$dbitem = Koha::StockRotationItems->find($sritem->{itemnumber_id}); |
329 |
$sritem_1->discard_changes; |
288 |
## Test results |
330 |
## Test results |
289 |
is($dbitem->stage->stage_id, $dbstage2->stage_id, "Stage updated."); |
331 |
is($sritem_1->stage->stage_id, $srstage_2->stage_id, "Stage updated."); |
290 |
is( |
332 |
is( |
291 |
$dbitem->itemnumber->homebranch, |
333 |
$sritem_1->itemnumber->homebranch, |
292 |
$dbstage2->branchcode_id, |
334 |
$srstage_2->branchcode_id, |
293 |
"Item homebranch updated" |
335 |
"Item homebranch updated" |
294 |
); |
336 |
); |
295 |
my $intransfer = $dbitem->itemnumber->get_transfer; |
337 |
my $transfer_request = $sritem_1->itemnumber->get_transfer; |
296 |
is($intransfer->frombranch, $dbstage->branchcode_id, "Origin correct."); |
338 |
is($transfer_request->frombranch, $srstage_1->branchcode_id, "Origin correct."); |
297 |
is($intransfer->tobranch, $dbstage2->branchcode_id, "Target Correct."); |
339 |
is($transfer_request->tobranch, $srstage_2->branchcode_id, "Target Correct."); |
|
|
340 |
is($transfer_request->datesent, undef, "Transfer requested, but not sent."); |
298 |
|
341 |
|
299 |
# Arrive at new branch |
342 |
# Arrive at new branch |
300 |
$intransfer->datearrived(dt_from_string())->store; |
343 |
$transfer_request->datearrived(dt_from_string())->store; |
301 |
$dbitem->itemnumber->holdingbranch($srstage->{branchcode_id})->store; |
344 |
$sritem_1->itemnumber->holdingbranch($srstage_2->branchcode_id)->store; |
302 |
|
345 |
|
303 |
# Test a cyclical advance |
346 |
# Test a cyclical advance |
304 |
ok($dbitem->advance, "Cyclical advancement done."); |
347 |
ok($sritem_1->advance, "Cyclical advancement done."); |
305 |
## Refetch dbitem |
348 |
## Refetch sritem_1 |
306 |
$dbitem = Koha::StockRotationItems->find($sritem->{itemnumber_id}); |
349 |
$sritem_1->discard_changes; |
307 |
## Test results |
350 |
## Test results |
308 |
is($dbitem->stage->stage_id, $dbstage->stage_id, "Stage updated."); |
351 |
is($sritem_1->stage->stage_id, $srstage_1->stage_id, "Stage updated."); |
309 |
is( |
352 |
is( |
310 |
$dbitem->itemnumber->homebranch, |
353 |
$sritem_1->itemnumber->homebranch, |
311 |
$dbstage->branchcode_id, |
354 |
$srstage_1->branchcode_id, |
312 |
"Item homebranch updated" |
355 |
"Item homebranch updated" |
313 |
); |
356 |
); |
314 |
$intransfer = $dbitem->itemnumber->get_transfer; |
357 |
$transfer_request = $sritem_1->itemnumber->get_transfer; |
315 |
is($intransfer->frombranch, $dbstage2->branchcode_id, "Origin correct."); |
358 |
is($transfer_request->frombranch, $srstage_2->branchcode_id, "Origin correct."); |
316 |
is($intransfer->tobranch, $dbstage->branchcode_id, "Target correct."); |
359 |
is($transfer_request->tobranch, $srstage_1->branchcode_id, "Target correct."); |
317 |
|
360 |
|
318 |
# Arrive at new branch |
361 |
# Arrive at new branch |
319 |
$intransfer->datearrived(dt_from_string())->store; |
362 |
$transfer_request->datearrived(dt_from_string())->store; |
320 |
$dbitem->itemnumber->holdingbranch($srstage->{branchcode_id})->store; |
363 |
$sritem_1->itemnumber->holdingbranch($srstage_1->branchcode_id)->store; |
|
|
364 |
|
365 |
# Confirm that stockrotation ignores transfer limits |
366 |
t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1); |
367 |
t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype'); |
368 |
my $limit = Koha::Item::Transfer::Limit->new( |
369 |
{ |
370 |
fromBranch => $srstage_1->branchcode_id, |
371 |
toBranch => $srstage_2->branchcode_id, |
372 |
itemtype => $sritem_1->itemnumber->effective_itemtype, |
373 |
} |
374 |
)->store; |
375 |
|
376 |
ok($sritem_1->advance, "Advancement overrules transfer limits."); |
377 |
## Refetch sritem_1 |
378 |
$sritem_1->discard_changes; |
379 |
## Test results |
380 |
is($sritem_1->stage->stage_id, $srstage_2->stage_id, "Stage updated ignoring transfer limits."); |
381 |
is( |
382 |
$sritem_1->itemnumber->homebranch, |
383 |
$srstage_2->branchcode_id, |
384 |
"Item homebranch updated ignoring transfer limits" |
385 |
); |
386 |
$transfer_request = $sritem_1->itemnumber->get_transfer; |
387 |
is($transfer_request->frombranch, $srstage_1->branchcode_id, "Origin correct ignoring transfer limits."); |
388 |
is($transfer_request->tobranch, $srstage_2->branchcode_id, "Target correct ignoring transfer limits."); |
389 |
|
390 |
# Arrive at new branch |
391 |
$transfer_request->datearrived(dt_from_string())->store; |
392 |
$sritem_1->itemnumber->holdingbranch($srstage_2->branchcode_id)->store; |
393 |
|
394 |
# Setup a conflicting manual transfer |
395 |
my $item = Koha::Items->find($item_id); |
396 |
$item->request_transfer({ to => $srstage_1->branchcode, reason => "Manual" }); |
397 |
$transfer_request = $item->get_transfer; |
398 |
is (ref($transfer_request), 'Koha::Item::Transfer', "Conflicting transfer added"); |
399 |
is ($transfer_request->reason, 'Manual', "Conflicting transfer reason is 'Manual'"); |
400 |
|
401 |
# Advance item whilst conflicting manual transfer exists |
402 |
ok($sritem_1->advance, "Advancement done."); |
403 |
## Refetch sritem_1 |
404 |
$sritem_1->discard_changes; |
405 |
|
406 |
## Refetch conflicted transfer |
407 |
$transfer_request->discard_changes; |
408 |
|
409 |
# Conflicted transfer should have been cancelled |
410 |
isnt($transfer_request->datecancelled, undef, "Conflicting manual transfer was cancelled"); |
411 |
|
412 |
# StockRotationAdvance transfer added |
413 |
$transfer_request = $sritem_1->itemnumber->get_transfer; |
414 |
is($transfer_request->reason, 'StockrotationAdvance', "StockrotationAdvance transfer added"); |
415 |
is($transfer_request->frombranch, $srstage_2->branchcode_id, "Origin correct."); |
416 |
is($transfer_request->tobranch, $srstage_1->branchcode_id, "Target correct."); |
417 |
|
418 |
# Arrive at new branch |
419 |
$transfer_request->datearrived(dt_from_string())->store; |
420 |
$sritem_1->itemnumber->holdingbranch($srstage_1->branchcode_id)->store; |
321 |
|
421 |
|
322 |
$dbstage->rota->cyclical(0)->store; # Set Rota to non-cyclical. |
422 |
# Setup a conflicting reserve transfer |
|
|
423 |
$item->request_transfer({ to => $srstage_2->branchcode, reason => "Reserve" }); |
424 |
$transfer_request = $item->get_transfer; |
425 |
is (ref($transfer_request), 'Koha::Item::Transfer', "Conflicting transfer added"); |
426 |
is ($transfer_request->reason, 'Reserve', "Conflicting transfer reason is 'Reserve'"); |
427 |
|
428 |
# Advance item whilst conflicting reserve transfer exists |
429 |
ok($sritem_1->advance, "Advancement done."); |
430 |
## Refetch sritem_1 |
431 |
$sritem_1->discard_changes; |
432 |
|
433 |
## Refetch conflicted transfer |
434 |
$transfer_request->discard_changes; |
435 |
|
436 |
# Conflicted transfer should not been cancelled |
437 |
is($transfer_request->datecancelled, undef, "Conflicting reserve transfer was not cancelled"); |
438 |
|
439 |
# StockRotationAdvance transfer added |
440 |
my $transfer_requests = Koha::Item::Transfers->search( |
441 |
{ |
442 |
itemnumber => $sritem_1->itemnumber->itemnumber, |
443 |
datearrived => undef, |
444 |
datecancelled => undef |
445 |
} |
446 |
); |
447 |
is($transfer_requests->count, '2', "StockrotationAdvance transfer queued"); |
448 |
|
449 |
# Arrive at new branch |
450 |
$transfer_request->datearrived(dt_from_string())->store; |
451 |
$sritem_1->itemnumber->holdingbranch($srstage_2->branchcode_id)->store; |
452 |
|
453 |
# StockRotationAdvance transfer added |
454 |
$transfer_request = $sritem_1->itemnumber->get_transfer; |
455 |
is($transfer_request->reason, 'StockrotationAdvance', "StockrotationAdvance transfer remains after reserve is met"); |
456 |
is($transfer_request->frombranch, $srstage_1->branchcode_id, "Origin correct."); |
457 |
is($transfer_request->tobranch, $srstage_2->branchcode_id, "Target correct."); |
458 |
|
459 |
# Arrive at new branch |
460 |
$transfer_request->datearrived(dt_from_string())->store; |
461 |
$sritem_1->itemnumber->holdingbranch($srstage_2->branchcode_id)->store; |
462 |
|
463 |
$srstage_1->rota->cyclical(0)->store; # Set Rota to non-cyclical. |
464 |
|
465 |
my $srstage_3 = $builder->build_object({ |
466 |
class => 'Koha::StockRotationStages', |
467 |
value => { duration => 50 } |
468 |
}); |
469 |
$srstage_3->discard_changes; |
470 |
$srstage_3->move_to_group($sritem_1->stage->rota_id); |
471 |
$srstage_3->move_last; |
323 |
|
472 |
|
324 |
# Advance again, to end of rota. |
473 |
# Advance again, to end of rota. |
325 |
ok($dbitem->advance, "Non-cyclical advance to last stage."); |
474 |
ok($sritem_1->advance, "Non-cyclical advance to last stage."); |
326 |
|
475 |
|
327 |
# Arrive at new branch |
476 |
# Arrive at new branch |
328 |
$intransfer->datearrived(dt_from_string())->store; |
477 |
$transfer_request->datearrived(dt_from_string())->store; |
329 |
$dbitem->itemnumber->holdingbranch($srstage->{branchcode_id})->store; |
478 |
$sritem_1->itemnumber->holdingbranch($srstage_3->branchcode_id)->store; |
330 |
|
479 |
|
331 |
# Advance again, Remove from rota. |
480 |
# Advance again, Remove from rota. |
332 |
ok($dbitem->advance, "Non-cyclical advance."); |
481 |
ok($sritem_1->advance, "Non-cyclical advance."); |
333 |
## Refetch dbitem |
482 |
## Refetch sritem_1 |
334 |
$dbitem = Koha::StockRotationItems->find($sritem->{itemnumber_id}); |
483 |
$sritem_1 = Koha::StockRotationItems->find({ itemnumber_id => $item_id }); |
335 |
is($dbitem, undef, "StockRotationItem has been removed."); |
484 |
is($sritem_1, undef, "StockRotationItem has been removed."); |
336 |
my $item = Koha::Items->find($sritem->{itemnumber_id}); |
485 |
$item = Koha::Items->find($item_id); |
337 |
is($item->homebranch, $srstage->{branchcode_id}, "Item homebranch remains"); |
486 |
is($item->homebranch, $srstage_3->branchcode_id, "Item homebranch remains"); |
338 |
|
487 |
|
339 |
$schema->storage->txn_rollback; |
488 |
$schema->storage->txn_rollback; |
340 |
}; |
489 |
}; |
341 |
- |
|
|