|
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 156-161
subtest "Tests for repatriate." => sub {
Link Here
|
| 156 |
is($intransfer->frombranch, $branch->{branchcode}, "Origin correct."); |
158 |
is($intransfer->frombranch, $branch->{branchcode}, "Origin correct."); |
| 157 |
is($intransfer->tobranch, $dbitem->stage->branchcode_id, "Target Correct."); |
159 |
is($intransfer->tobranch, $dbitem->stage->branchcode_id, "Target Correct."); |
| 158 |
|
160 |
|
|
|
161 |
# Stockrotation should overrule transfer limits |
| 162 |
|
| 163 |
# Stockrotation should handle transfer clashes |
| 164 |
|
| 159 |
$schema->storage->txn_rollback; |
165 |
$schema->storage->txn_rollback; |
| 160 |
}; |
166 |
}; |
| 161 |
|
167 |
|
|
Lines 229-340
subtest "Tests for needs_advancing." => sub {
Link Here
|
| 229 |
}; |
235 |
}; |
| 230 |
|
236 |
|
| 231 |
subtest "Tests for advance." => sub { |
237 |
subtest "Tests for advance." => sub { |
| 232 |
plan tests => 23; |
238 |
plan tests => 44; |
| 233 |
$schema->storage->txn_begin; |
239 |
$schema->storage->txn_begin; |
| 234 |
|
240 |
|
| 235 |
my $sritem = $builder->build( |
241 |
my $sritem_1 = $builder->build_object( |
| 236 |
{ |
242 |
{ |
| 237 |
source => 'Stockrotationitem', |
243 |
class => 'Koha::StockRotationItems', |
| 238 |
value => { |
244 |
value => { |
| 239 |
'fresh' => 1, |
245 |
'fresh' => 1, |
| 240 |
itemnumber_id => $builder->build_sample_item->itemnumber |
246 |
itemnumber_id => $builder->build_sample_item->itemnumber |
| 241 |
} |
247 |
} |
| 242 |
} |
248 |
} |
| 243 |
); |
249 |
); |
| 244 |
my $dbitem = Koha::StockRotationItems->find($sritem->{itemnumber_id}); |
250 |
$sritem_1->discard_changes; |
| 245 |
$dbitem->itemnumber->holdingbranch($dbitem->stage->branchcode_id); |
251 |
$sritem_1->itemnumber->holdingbranch($sritem_1->stage->branchcode_id); |
| 246 |
my $dbstage = $dbitem->stage; |
252 |
my $item_id = $sritem_1->itemnumber->itemnumber; |
| 247 |
$dbstage->position(1)->duration(50)->store; # Configure stage. |
253 |
my $srstage_1 = $sritem_1->stage; |
|
|
254 |
$srstage_1->position(1)->duration(50)->store; # Configure stage. |
| 248 |
# Configure item |
255 |
# Configure item |
| 249 |
$dbitem->itemnumber->holdingbranch($dbstage->branchcode_id)->store; |
256 |
$sritem_1->itemnumber->holdingbranch($srstage_1->branchcode_id)->store; |
| 250 |
$dbitem->itemnumber->homebranch($dbstage->branchcode_id)->store; |
257 |
$sritem_1->itemnumber->homebranch($srstage_1->branchcode_id)->store; |
| 251 |
# Sanity check |
258 |
# Sanity check |
| 252 |
is($dbitem->stage->stage_id, $dbstage->stage_id, "Stage sanity check."); |
259 |
is($sritem_1->stage->stage_id, $srstage_1->stage_id, "Stage sanity check."); |
| 253 |
|
260 |
|
| 254 |
# Test if an item is fresh, always move to first stage. |
261 |
# Test if an item is fresh, always move to first stage. |
| 255 |
is($dbitem->fresh, 1, "Fresh is correct."); |
262 |
is($sritem_1->fresh, 1, "Fresh is correct."); |
| 256 |
$dbitem->advance; |
263 |
$sritem_1->advance; |
| 257 |
is($dbitem->stage->stage_id, $dbstage->stage_id, "Stage is first stage after fresh advance."); |
264 |
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."); |
265 |
is($sritem_1->fresh, 0, "Fresh reset after advance."); |
| 259 |
|
266 |
|
| 260 |
# Test cases of single stage |
267 |
# Test cases of single stage |
| 261 |
$dbstage->rota->cyclical(1)->store; # Set Rota to cyclical. |
268 |
$srstage_1->rota->cyclical(1)->store; # Set Rota to cyclical. |
| 262 |
ok($dbitem->advance, "Single stage cyclical advance done."); |
269 |
ok($sritem_1->advance, "Single stage cyclical advance done."); |
| 263 |
## Refetch dbitem |
270 |
## Refetch sritem_1 |
| 264 |
$dbitem = Koha::StockRotationItems->find($sritem->{itemnumber_id}); |
271 |
$sritem_1->discard_changes; |
| 265 |
is($dbitem->stage->stage_id, $dbstage->stage_id, "Single stage cyclical stage OK."); |
272 |
is($sritem_1->stage->stage_id, $srstage_1->stage_id, "Single stage cyclical stage OK."); |
| 266 |
|
273 |
|
| 267 |
# Test with indemand advance |
274 |
# Test with indemand advance |
| 268 |
$dbitem->indemand(1)->store; |
275 |
$sritem_1->indemand(1)->store; |
| 269 |
ok($dbitem->advance, "Indemand item advance done."); |
276 |
ok($sritem_1->advance, "Indemand item advance done."); |
| 270 |
## Refetch dbitem |
277 |
## Refetch sritem_1 |
| 271 |
$dbitem = Koha::StockRotationItems->find($sritem->{itemnumber_id}); |
278 |
$sritem_1->discard_changes; |
| 272 |
is($dbitem->indemand, 0, "Indemand OK."); |
279 |
is($sritem_1->indemand, 0, "Indemand OK."); |
| 273 |
is($dbitem->stage->stage_id, $dbstage->stage_id, "Indemand item advance stage OK."); |
280 |
is($sritem_1->stage->stage_id, $srstage_1->stage_id, "Indemand item advance stage OK."); |
| 274 |
|
281 |
|
| 275 |
# Multi stages |
282 |
# Multi stages |
| 276 |
my $srstage = $builder->build({ |
283 |
my $srstage_2 = $builder->build_object({ |
| 277 |
source => 'Stockrotationstage', |
284 |
class => 'Koha::StockRotationStages', |
| 278 |
value => { duration => 50 } |
285 |
value => { duration => 50 } |
| 279 |
}); |
286 |
}); |
| 280 |
my $dbstage2 = Koha::StockRotationStages->find($srstage->{stage_id}); |
287 |
$srstage_2->discard_changes; |
| 281 |
$dbstage2->move_to_group($dbitem->stage->rota_id); |
288 |
$srstage_2->move_to_group($sritem_1->stage->rota_id); |
| 282 |
$dbstage2->move_last; |
289 |
$srstage_2->move_last; |
| 283 |
|
290 |
|
| 284 |
# Test a straight up advance |
291 |
# Test a straight up advance |
| 285 |
ok($dbitem->advance, "Advancement done."); |
292 |
ok($sritem_1->advance, "Advancement done."); |
| 286 |
## Refetch dbitem |
293 |
## Refetch sritem_1 |
| 287 |
$dbitem = Koha::StockRotationItems->find($sritem->{itemnumber_id}); |
294 |
$sritem_1->discard_changes; |
| 288 |
## Test results |
295 |
## Test results |
| 289 |
is($dbitem->stage->stage_id, $dbstage2->stage_id, "Stage updated."); |
296 |
is($sritem_1->stage->stage_id, $srstage_2->stage_id, "Stage updated."); |
| 290 |
is( |
297 |
is( |
| 291 |
$dbitem->itemnumber->homebranch, |
298 |
$sritem_1->itemnumber->homebranch, |
| 292 |
$dbstage2->branchcode_id, |
299 |
$srstage_2->branchcode_id, |
| 293 |
"Item homebranch updated" |
300 |
"Item homebranch updated" |
| 294 |
); |
301 |
); |
| 295 |
my $intransfer = $dbitem->itemnumber->get_transfer; |
302 |
my $transfer_request = $sritem_1->itemnumber->get_transfer; |
| 296 |
is($intransfer->frombranch, $dbstage->branchcode_id, "Origin correct."); |
303 |
is($transfer_request->frombranch, $srstage_1->branchcode_id, "Origin correct."); |
| 297 |
is($intransfer->tobranch, $dbstage2->branchcode_id, "Target Correct."); |
304 |
is($transfer_request->tobranch, $srstage_2->branchcode_id, "Target Correct."); |
|
|
305 |
is($transfer_request->datesent, undef, "Transfer requested, but not sent."); |
| 298 |
|
306 |
|
| 299 |
# Arrive at new branch |
307 |
# Arrive at new branch |
| 300 |
$intransfer->datearrived(dt_from_string())->store; |
308 |
$transfer_request->datearrived(dt_from_string())->store; |
| 301 |
$dbitem->itemnumber->holdingbranch($srstage->{branchcode_id})->store; |
309 |
$sritem_1->itemnumber->holdingbranch($srstage_2->branchcode_id)->store; |
| 302 |
|
310 |
|
| 303 |
# Test a cyclical advance |
311 |
# Test a cyclical advance |
| 304 |
ok($dbitem->advance, "Cyclical advancement done."); |
312 |
ok($sritem_1->advance, "Cyclical advancement done."); |
| 305 |
## Refetch dbitem |
313 |
## Refetch sritem_1 |
| 306 |
$dbitem = Koha::StockRotationItems->find($sritem->{itemnumber_id}); |
314 |
$sritem_1->discard_changes; |
| 307 |
## Test results |
315 |
## Test results |
| 308 |
is($dbitem->stage->stage_id, $dbstage->stage_id, "Stage updated."); |
316 |
is($sritem_1->stage->stage_id, $srstage_1->stage_id, "Stage updated."); |
| 309 |
is( |
317 |
is( |
| 310 |
$dbitem->itemnumber->homebranch, |
318 |
$sritem_1->itemnumber->homebranch, |
| 311 |
$dbstage->branchcode_id, |
319 |
$srstage_1->branchcode_id, |
| 312 |
"Item homebranch updated" |
320 |
"Item homebranch updated" |
| 313 |
); |
321 |
); |
| 314 |
$intransfer = $dbitem->itemnumber->get_transfer; |
322 |
$transfer_request = $sritem_1->itemnumber->get_transfer; |
| 315 |
is($intransfer->frombranch, $dbstage2->branchcode_id, "Origin correct."); |
323 |
is($transfer_request->frombranch, $srstage_2->branchcode_id, "Origin correct."); |
| 316 |
is($intransfer->tobranch, $dbstage->branchcode_id, "Target correct."); |
324 |
is($transfer_request->tobranch, $srstage_1->branchcode_id, "Target correct."); |
| 317 |
|
325 |
|
| 318 |
# Arrive at new branch |
326 |
# Arrive at new branch |
| 319 |
$intransfer->datearrived(dt_from_string())->store; |
327 |
$transfer_request->datearrived(dt_from_string())->store; |
| 320 |
$dbitem->itemnumber->holdingbranch($srstage->{branchcode_id})->store; |
328 |
$sritem_1->itemnumber->holdingbranch($srstage_1->branchcode_id)->store; |
|
|
329 |
|
| 330 |
# Confirm that stockrotation ignores transfer limits |
| 331 |
t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1); |
| 332 |
t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype'); |
| 333 |
my $limit = Koha::Item::Transfer::Limit->new( |
| 334 |
{ |
| 335 |
fromBranch => $srstage_1->branchcode_id, |
| 336 |
toBranch => $srstage_2->branchcode_id, |
| 337 |
itemtype => $sritem_1->itemnumber->effective_itemtype, |
| 338 |
} |
| 339 |
)->store; |
| 321 |
|
340 |
|
| 322 |
$dbstage->rota->cyclical(0)->store; # Set Rota to non-cyclical. |
341 |
ok($sritem_1->advance, "Advancement overrules transfer limits."); |
|
|
342 |
## Refetch sritem_1 |
| 343 |
$sritem_1->discard_changes; |
| 344 |
## Test results |
| 345 |
is($sritem_1->stage->stage_id, $srstage_2->stage_id, "Stage updated ignoring transfer limits."); |
| 346 |
is( |
| 347 |
$sritem_1->itemnumber->homebranch, |
| 348 |
$srstage_2->branchcode_id, |
| 349 |
"Item homebranch updated ignoring transfer limits" |
| 350 |
); |
| 351 |
$transfer_request = $sritem_1->itemnumber->get_transfer; |
| 352 |
is($transfer_request->frombranch, $srstage_1->branchcode_id, "Origin correct ignoring transfer limits."); |
| 353 |
is($transfer_request->tobranch, $srstage_2->branchcode_id, "Target correct ignoring transfer limits."); |
| 354 |
|
| 355 |
# Arrive at new branch |
| 356 |
$transfer_request->datearrived(dt_from_string())->store; |
| 357 |
$sritem_1->itemnumber->holdingbranch($srstage_2->branchcode_id)->store; |
| 358 |
|
| 359 |
# Setup a conflicting manual transfer |
| 360 |
my $item = Koha::Items->find($item_id); |
| 361 |
$item->request_transfer({ to => $srstage_1->branchcode, reason => "Manual" }); |
| 362 |
$transfer_request = $item->get_transfer; |
| 363 |
is (ref($transfer_request), 'Koha::Item::Transfer', "Conflicting transfer added"); |
| 364 |
is ($transfer_request->reason, 'Manual', "Conflicting transfer reason is 'Manual'"); |
| 365 |
|
| 366 |
# Advance item whilst conflicting manual transfer exists |
| 367 |
ok($sritem_1->advance, "Advancement done."); |
| 368 |
## Refetch sritem_1 |
| 369 |
$sritem_1->discard_changes; |
| 370 |
|
| 371 |
## Refetch conflicted transfer |
| 372 |
$transfer_request->discard_changes; |
| 373 |
|
| 374 |
# Conflicted transfer should have been cancelled |
| 375 |
isnt($transfer_request->datecancelled, undef, "Conflicting manual transfer was cancelled"); |
| 376 |
|
| 377 |
# StockRotationAdvance transfer added |
| 378 |
$transfer_request = $sritem_1->itemnumber->get_transfer; |
| 379 |
is($transfer_request->reason, 'StockrotationAdvance', "StockrotationAdvance transfer added"); |
| 380 |
is($transfer_request->frombranch, $srstage_2->branchcode_id, "Origin correct."); |
| 381 |
is($transfer_request->tobranch, $srstage_1->branchcode_id, "Target correct."); |
| 382 |
|
| 383 |
# Arrive at new branch |
| 384 |
$transfer_request->datearrived(dt_from_string())->store; |
| 385 |
$sritem_1->itemnumber->holdingbranch($srstage_1->branchcode_id)->store; |
| 386 |
|
| 387 |
# Setup a conflicting reserve transfer |
| 388 |
$item->request_transfer({ to => $srstage_2->branchcode, reason => "Reserve" }); |
| 389 |
$transfer_request = $item->get_transfer; |
| 390 |
is (ref($transfer_request), 'Koha::Item::Transfer', "Conflicting transfer added"); |
| 391 |
is ($transfer_request->reason, 'Reserve', "Conflicting transfer reason is 'Reserve'"); |
| 392 |
|
| 393 |
# Advance item whilst conflicting reserve transfer exists |
| 394 |
ok($sritem_1->advance, "Advancement done."); |
| 395 |
## Refetch sritem_1 |
| 396 |
$sritem_1->discard_changes; |
| 397 |
|
| 398 |
## Refetch conflicted transfer |
| 399 |
$transfer_request->discard_changes; |
| 400 |
|
| 401 |
# Conflicted transfer should not been cancelled |
| 402 |
is($transfer_request->datecancelled, undef, "Conflicting reserve transfer was not cancelled"); |
| 403 |
|
| 404 |
# StockRotationAdvance transfer added |
| 405 |
my $transfer_requests = Koha::Item::Transfers->search( |
| 406 |
{ |
| 407 |
itemnumber => $sritem_1->itemnumber->itemnumber, |
| 408 |
datearrived => undef, |
| 409 |
datecancelled => undef |
| 410 |
} |
| 411 |
); |
| 412 |
is($transfer_requests->count, '2', "StockrotationAdvance transfer queued"); |
| 413 |
|
| 414 |
# Arrive at new branch |
| 415 |
$transfer_request->datearrived(dt_from_string())->store; |
| 416 |
$sritem_1->itemnumber->holdingbranch($srstage_2->branchcode_id)->store; |
| 417 |
|
| 418 |
# StockRotationAdvance transfer added |
| 419 |
$transfer_request = $sritem_1->itemnumber->get_transfer; |
| 420 |
is($transfer_request->reason, 'StockrotationAdvance', "StockrotationAdvance transfer remains after reserve is met"); |
| 421 |
is($transfer_request->frombranch, $srstage_1->branchcode_id, "Origin correct."); |
| 422 |
is($transfer_request->tobranch, $srstage_2->branchcode_id, "Target correct."); |
| 423 |
|
| 424 |
# Arrive at new branch |
| 425 |
$transfer_request->datearrived(dt_from_string())->store; |
| 426 |
$sritem_1->itemnumber->holdingbranch($srstage_2->branchcode_id)->store; |
| 427 |
|
| 428 |
$srstage_1->rota->cyclical(0)->store; # Set Rota to non-cyclical. |
| 429 |
|
| 430 |
my $srstage_3 = $builder->build_object({ |
| 431 |
class => 'Koha::StockRotationStages', |
| 432 |
value => { duration => 50 } |
| 433 |
}); |
| 434 |
$srstage_3->discard_changes; |
| 435 |
$srstage_3->move_to_group($sritem_1->stage->rota_id); |
| 436 |
$srstage_3->move_last; |
| 323 |
|
437 |
|
| 324 |
# Advance again, to end of rota. |
438 |
# Advance again, to end of rota. |
| 325 |
ok($dbitem->advance, "Non-cyclical advance to last stage."); |
439 |
ok($sritem_1->advance, "Non-cyclical advance to last stage."); |
| 326 |
|
440 |
|
| 327 |
# Arrive at new branch |
441 |
# Arrive at new branch |
| 328 |
$intransfer->datearrived(dt_from_string())->store; |
442 |
$transfer_request->datearrived(dt_from_string())->store; |
| 329 |
$dbitem->itemnumber->holdingbranch($srstage->{branchcode_id})->store; |
443 |
$sritem_1->itemnumber->holdingbranch($srstage_3->branchcode_id)->store; |
| 330 |
|
444 |
|
| 331 |
# Advance again, Remove from rota. |
445 |
# Advance again, Remove from rota. |
| 332 |
ok($dbitem->advance, "Non-cyclical advance."); |
446 |
ok($sritem_1->advance, "Non-cyclical advance."); |
| 333 |
## Refetch dbitem |
447 |
## Refetch sritem_1 |
| 334 |
$dbitem = Koha::StockRotationItems->find($sritem->{itemnumber_id}); |
448 |
$sritem_1 = Koha::StockRotationItems->find({ itemnumber_id => $item_id }); |
| 335 |
is($dbitem, undef, "StockRotationItem has been removed."); |
449 |
is($sritem_1, undef, "StockRotationItem has been removed."); |
| 336 |
my $item = Koha::Items->find($sritem->{itemnumber_id}); |
450 |
$item = Koha::Items->find($item_id); |
| 337 |
is($item->homebranch, $srstage->{branchcode_id}, "Item homebranch remains"); |
451 |
is($item->homebranch, $srstage_3->branchcode_id, "Item homebranch remains"); |
| 338 |
|
452 |
|
| 339 |
$schema->storage->txn_rollback; |
453 |
$schema->storage->txn_rollback; |
| 340 |
}; |
454 |
}; |
| 341 |
- |
|
|