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