View | Details | Raw Unified | Return to bug 24446
Collapse All | Expand All

(-)a/t/db_dependent/StockRotationItems.t (-18 / +50 lines)
Lines 22-27 use Modern::Perl; Link Here
22
use DateTime;
22
use DateTime;
23
use DateTime::Duration;
23
use DateTime::Duration;
24
use Koha::Database;
24
use Koha::Database;
25
use Koha::DateUtils;
25
use Koha::Item::Transfer;
26
use Koha::Item::Transfer;
26
use t::lib::TestBuilder;
27
use t::lib::TestBuilder;
27
28
Lines 195-201 subtest "Tests for needs_advancing." => sub { Link Here
195
        'itemnumber'  => $dbitem->itemnumber_id,
196
        'itemnumber'  => $dbitem->itemnumber_id,
196
        'frombranch'  => $dbitem->stage->branchcode_id,
197
        'frombranch'  => $dbitem->stage->branchcode_id,
197
        'tobranch'    => $dbitem->stage->branchcode_id,
198
        'tobranch'    => $dbitem->stage->branchcode_id,
198
        'datesent'    => DateTime->now,
199
        'datesent'    => dt_from_string,
199
        'datearrived' => undef,
200
        'datearrived' => undef,
200
        'reason'      => "StockrotationAdvance",
201
        'reason'      => "StockrotationAdvance",
201
    })->store;
202
    })->store;
Lines 208-214 subtest "Tests for needs_advancing." => sub { Link Here
208
    $dbitem->fresh(0)->store;
209
    $dbitem->fresh(0)->store;
209
210
210
    # Test item will not be advanced if it has not spent enough time.
211
    # Test item will not be advanced if it has not spent enough time.
211
    $dbtransfer->datearrived(DateTime->now)->store;
212
    $dbtransfer->datearrived(dt_from_string)->store;
212
    is($dbitem->needs_advancing, 0, "Not ready to advance: Not spent enough time.");
213
    is($dbitem->needs_advancing, 0, "Not ready to advance: Not spent enough time.");
213
    # Test item will be advanced if it has not spent enough time, but is fresh.
214
    # Test item will be advanced if it has not spent enough time, but is fresh.
214
    $dbitem->fresh(1)->store;
215
    $dbitem->fresh(1)->store;
Lines 217-226 subtest "Tests for needs_advancing." => sub { Link Here
217
218
218
    # Test item will be advanced if it has spent enough time.
219
    # Test item will be advanced if it has spent enough time.
219
    $dbtransfer->datesent(      # Item was sent 100 days ago...
220
    $dbtransfer->datesent(      # Item was sent 100 days ago...
220
        DateTime->now - DateTime::Duration->new( days => 100 )
221
        dt_from_string - DateTime::Duration->new( days => 100 )
221
    )->store;
222
    )->store;
222
    $dbtransfer->datearrived(   # And arrived 75 days ago.
223
    $dbtransfer->datearrived(   # And arrived 75 days ago.
223
        DateTime->now - DateTime::Duration->new( days => 75 )
224
        dt_from_string - DateTime::Duration->new( days => 75 )
224
    )->store;
225
    )->store;
225
    is($dbitem->needs_advancing, 1, "Ready to be advanced.");
226
    is($dbitem->needs_advancing, 1, "Ready to be advanced.");
226
227
Lines 228-234 subtest "Tests for needs_advancing." => sub { Link Here
228
};
229
};
229
230
230
subtest "Tests for advance." => sub {
231
subtest "Tests for advance." => sub {
231
    plan tests => 15;
232
    plan tests => 23;
232
    $schema->storage->txn_begin;
233
    $schema->storage->txn_begin;
233
234
234
    my $sritem = $builder->build(
235
    my $sritem = $builder->build(
Lines 286-307 subtest "Tests for advance." => sub { Link Here
286
    $dbitem = Koha::StockRotationItems->find($sritem->{itemnumber_id});
287
    $dbitem = Koha::StockRotationItems->find($sritem->{itemnumber_id});
287
    ## Test results
288
    ## Test results
288
    is($dbitem->stage->stage_id, $dbstage2->stage_id, "Stage updated.");
289
    is($dbitem->stage->stage_id, $dbstage2->stage_id, "Stage updated.");
290
    is(
291
        $dbitem->itemnumber->homebranch,
292
        $dbstage2->branchcode_id,
293
        "Item homebranch updated"
294
    );
289
    my $intransfer = $dbitem->itemnumber->get_transfer;
295
    my $intransfer = $dbitem->itemnumber->get_transfer;
290
    is($intransfer->frombranch, $dbstage->branchcode_id, "Origin correct.");
296
    is($intransfer->frombranch, $dbstage->branchcode_id, "Origin correct.");
291
    is($intransfer->tobranch, $dbstage2->branchcode_id, "Target Correct.");
297
    is($intransfer->tobranch, $dbstage2->branchcode_id, "Target Correct.");
292
298
299
    # Arrive at new branch
300
    $intransfer->datearrived(dt_from_string)->store;
301
    $dbitem->itemnumber->holdingbranch($srstage->{branchcode_id})->store;
302
303
    # Test a cyclical advance
304
    ok($dbitem->advance, "Cyclical advancement done.");
305
    ## Refetch dbitem
306
    $dbitem = Koha::StockRotationItems->find($sritem->{itemnumber_id});
307
    ## Test results
308
    is($dbitem->stage->stage_id, $dbstage->stage_id, "Stage updated.");
309
    is(
310
        $dbitem->itemnumber->homebranch,
311
        $dbstage->branchcode_id,
312
        "Item homebranch updated"
313
    );
314
    $intransfer = $dbitem->itemnumber->get_transfer;
315
    is($intransfer->frombranch, $dbstage2->branchcode_id, "Origin correct.");
316
    is($intransfer->tobranch, $dbstage->branchcode_id, "Target correct.");
317
318
    # Arrive at new branch
319
    $intransfer->datearrived(dt_from_string)->store;
320
    $dbitem->itemnumber->holdingbranch($srstage->{branchcode_id})->store;
321
293
    $dbstage->rota->cyclical(0)->store;         # Set Rota to non-cyclical.
322
    $dbstage->rota->cyclical(0)->store;         # Set Rota to non-cyclical.
294
323
324
    # Advance again, to end of rota.
325
    ok($dbitem->advance, "Non-cyclical advance to last stage.");
326
295
    # Arrive at new branch
327
    # Arrive at new branch
296
    $intransfer->datearrived(DateTime->now)->store;
328
    $intransfer->datearrived(dt_from_string)->store;
297
    $dbitem->itemnumber->holdingbranch($srstage->{branchcode_id})->store;
329
    $dbitem->itemnumber->holdingbranch($srstage->{branchcode_id})->store;
298
    $dbitem->itemnumber->homebranch($srstage->{branchcode_id})->store;
299
330
300
    # Advance again, Remove from rota.
331
    # Advance again, Remove from rota.
301
    ok($dbitem->advance, "Non-cyclical advance.");
332
    ok($dbitem->advance, "Non-cyclical advance.");
302
    ## Refetch dbitem
333
    ## Refetch dbitem
303
    $dbitem = Koha::StockRotationItems->find($sritem->{itemnumber_id});
334
    $dbitem = Koha::StockRotationItems->find($sritem->{itemnumber_id});
304
    is($dbitem, undef, "StockRotationItem has been removed.");
335
    is($dbitem, undef, "StockRotationItem has been removed.");
336
    my $item = Koha::Items->find($sritem->{itemnumber_id});
337
    is($item->homebranch, $srstage->{branchcode_id}, "Item homebranch remains");
305
338
306
    $schema->storage->txn_rollback;
339
    $schema->storage->txn_rollback;
307
};
340
};
Lines 353-360 subtest "Tests for investigate (singular)." => sub { Link Here
353
        'itemnumber'  => $dbitem->itemnumber_id,
386
        'itemnumber'  => $dbitem->itemnumber_id,
354
        'frombranch'  => $dbitem->itemnumber->homebranch,
387
        'frombranch'  => $dbitem->itemnumber->homebranch,
355
        'tobranch'    => $dbitem->itemnumber->homebranch,
388
        'tobranch'    => $dbitem->itemnumber->homebranch,
356
        'datesent'    => DateTime->now,
389
        'datesent'    => dt_from_string,
357
        'datearrived' => DateTime->now,
390
        'datearrived' => dt_from_string,
358
        'reason'      => "StockrotationAdvance",
391
        'reason'      => "StockrotationAdvance",
359
    })->store;
392
    })->store;
360
    is($dbitem->investigate->{reason}, 'repatriation', "older item repatriates.");
393
    is($dbitem->investigate->{reason}, 'repatriation', "older item repatriates.");
Lines 374-381 subtest "Tests for investigate (singular)." => sub { Link Here
374
        'itemnumber'  => $dbitem->itemnumber_id,
407
        'itemnumber'  => $dbitem->itemnumber_id,
375
        'frombranch'  => $dbitem->itemnumber->homebranch,
408
        'frombranch'  => $dbitem->itemnumber->homebranch,
376
        'tobranch'    => $dbitem->stage->branchcode_id,
409
        'tobranch'    => $dbitem->stage->branchcode_id,
377
        'datesent'    => DateTime->now,
410
        'datesent'    => dt_from_string,
378
        'datearrived' => DateTime->now,
411
        'datearrived' => dt_from_string,
379
        'reason'      => "StockrotationAdvance",
412
        'reason'      => "StockrotationAdvance",
380
    })->store;
413
    })->store;
381
    $dbitem->itemnumber->homebranch($dbitem->stage->branchcode_id)->store;
414
    $dbitem->itemnumber->homebranch($dbitem->stage->branchcode_id)->store;
Lines 401-408 subtest "Tests for investigate (singular)." => sub { Link Here
401
        'itemnumber'  => $dbitem->itemnumber_id,
434
        'itemnumber'  => $dbitem->itemnumber_id,
402
        'frombranch'  => $dbitem->itemnumber->homebranch,
435
        'frombranch'  => $dbitem->itemnumber->homebranch,
403
        'tobranch'    => $dbitem->stage->branchcode_id,
436
        'tobranch'    => $dbitem->stage->branchcode_id,
404
        'datesent'    => DateTime->now - $sent_duration,
437
        'datesent'    => dt_from_string - $sent_duration,
405
        'datearrived' => DateTime->now - $arrived_duration,
438
        'datearrived' => dt_from_string - $arrived_duration,
406
        'reason'      => "StockrotationAdvance",
439
        'reason'      => "StockrotationAdvance",
407
    })->store;
440
    })->store;
408
    $dbitem->itemnumber->homebranch($dbitem->stage->branchcode_id)->store;
441
    $dbitem->itemnumber->homebranch($dbitem->stage->branchcode_id)->store;
Lines 429-436 subtest "Tests for investigate (singular)." => sub { Link Here
429
        'itemnumber'  => $dbitem->itemnumber_id,
462
        'itemnumber'  => $dbitem->itemnumber_id,
430
        'frombranch'  => $dbitem->itemnumber->homebranch,
463
        'frombranch'  => $dbitem->itemnumber->homebranch,
431
        'tobranch'    => $dbitem->stage->branchcode_id,
464
        'tobranch'    => $dbitem->stage->branchcode_id,
432
        'datesent'    => DateTime->now - $sent_duration,
465
        'datesent'    => dt_from_string - $sent_duration,
433
        'datearrived' => DateTime->now - $arrived_duration,
466
        'datearrived' => dt_from_string - $arrived_duration,
434
        'reason'      => "StockrotationAdvance",
467
        'reason'      => "StockrotationAdvance",
435
    })->store;
468
    })->store;
436
    $dbitem->itemnumber->homebranch($dbitem->stage->branchcode_id)->store;
469
    $dbitem->itemnumber->homebranch($dbitem->stage->branchcode_id)->store;
Lines 457-464 subtest "Tests for investigate (singular)." => sub { Link Here
457
        'itemnumber'  => $dbitem->itemnumber_id,
490
        'itemnumber'  => $dbitem->itemnumber_id,
458
        'frombranch'  => $dbitem->itemnumber->homebranch,
491
        'frombranch'  => $dbitem->itemnumber->homebranch,
459
        'tobranch'    => $dbitem->stage->branchcode_id,
492
        'tobranch'    => $dbitem->stage->branchcode_id,
460
        'datesent'    => DateTime->now - $sent_duration,
493
        'datesent'    => dt_from_string - $sent_duration,
461
        'datearrived' => DateTime->now - $arrived_duration,
494
        'datearrived' => dt_from_string - $arrived_duration,
462
        'reason'      => "StockrotationAdvance",
495
        'reason'      => "StockrotationAdvance",
463
    })->store;
496
    })->store;
464
    is($dbitem->investigate->{reason}, 'repatriation',
497
    is($dbitem->investigate->{reason}, 'repatriation',
465
- 

Return to bug 24446