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 173-179 subtest "Tests for needs_advancing." => sub { Link Here
173
        'itemnumber'  => $dbitem->itemnumber_id,
174
        'itemnumber'  => $dbitem->itemnumber_id,
174
        'frombranch'  => $dbitem->stage->branchcode_id,
175
        'frombranch'  => $dbitem->stage->branchcode_id,
175
        'tobranch'    => $dbitem->stage->branchcode_id,
176
        'tobranch'    => $dbitem->stage->branchcode_id,
176
        'datesent'    => DateTime->now,
177
        'datesent'    => dt_from_string,
177
        'datearrived' => undef,
178
        'datearrived' => undef,
178
        'reason'      => "StockrotationAdvance",
179
        'reason'      => "StockrotationAdvance",
179
    })->store;
180
    })->store;
Lines 186-192 subtest "Tests for needs_advancing." => sub { Link Here
186
    $dbitem->fresh(0)->store;
187
    $dbitem->fresh(0)->store;
187
188
188
    # Test item will not be advanced if it has not spent enough time.
189
    # Test item will not be advanced if it has not spent enough time.
189
    $dbtransfer->datearrived(DateTime->now)->store;
190
    $dbtransfer->datearrived(dt_from_string)->store;
190
    is($dbitem->needs_advancing, 0, "Not ready to advance: Not spent enough time.");
191
    is($dbitem->needs_advancing, 0, "Not ready to advance: Not spent enough time.");
191
    # Test item will be advanced if it has not spent enough time, but is fresh.
192
    # Test item will be advanced if it has not spent enough time, but is fresh.
192
    $dbitem->fresh(1)->store;
193
    $dbitem->fresh(1)->store;
Lines 195-204 subtest "Tests for needs_advancing." => sub { Link Here
195
196
196
    # Test item will be advanced if it has spent enough time.
197
    # Test item will be advanced if it has spent enough time.
197
    $dbtransfer->datesent(      # Item was sent 100 days ago...
198
    $dbtransfer->datesent(      # Item was sent 100 days ago...
198
        DateTime->now - DateTime::Duration->new( days => 100 )
199
        dt_from_string - DateTime::Duration->new( days => 100 )
199
    )->store;
200
    )->store;
200
    $dbtransfer->datearrived(   # And arrived 75 days ago.
201
    $dbtransfer->datearrived(   # And arrived 75 days ago.
201
        DateTime->now - DateTime::Duration->new( days => 75 )
202
        dt_from_string - DateTime::Duration->new( days => 75 )
202
    )->store;
203
    )->store;
203
    is($dbitem->needs_advancing, 1, "Ready to be advanced.");
204
    is($dbitem->needs_advancing, 1, "Ready to be advanced.");
204
205
Lines 206-212 subtest "Tests for needs_advancing." => sub { Link Here
206
};
207
};
207
208
208
subtest "Tests for advance." => sub {
209
subtest "Tests for advance." => sub {
209
    plan tests => 15;
210
    plan tests => 23;
210
    $schema->storage->txn_begin;
211
    $schema->storage->txn_begin;
211
212
212
    my $sritem = $builder->build({
213
    my $sritem = $builder->build({
Lines 259-280 subtest "Tests for advance." => sub { Link Here
259
    $dbitem = Koha::StockRotationItems->find($sritem->{itemnumber_id});
260
    $dbitem = Koha::StockRotationItems->find($sritem->{itemnumber_id});
260
    ## Test results
261
    ## Test results
261
    is($dbitem->stage->stage_id, $dbstage2->stage_id, "Stage updated.");
262
    is($dbitem->stage->stage_id, $dbstage2->stage_id, "Stage updated.");
263
    is(
264
        $dbitem->itemnumber->homebranch,
265
        $dbstage2->branchcode_id,
266
        "Item homebranch updated"
267
    );
262
    my $intransfer = $dbitem->itemnumber->get_transfer;
268
    my $intransfer = $dbitem->itemnumber->get_transfer;
263
    is($intransfer->frombranch, $dbstage->branchcode_id, "Origin correct.");
269
    is($intransfer->frombranch, $dbstage->branchcode_id, "Origin correct.");
264
    is($intransfer->tobranch, $dbstage2->branchcode_id, "Target Correct.");
270
    is($intransfer->tobranch, $dbstage2->branchcode_id, "Target Correct.");
265
271
272
    # Arrive at new branch
273
    $intransfer->datearrived(dt_from_string)->store;
274
    $dbitem->itemnumber->holdingbranch($srstage->{branchcode_id})->store;
275
276
    # Test a cyclical advance
277
    ok($dbitem->advance, "Cyclical advancement done.");
278
    ## Refetch dbitem
279
    $dbitem = Koha::StockRotationItems->find($sritem->{itemnumber_id});
280
    ## Test results
281
    is($dbitem->stage->stage_id, $dbstage->stage_id, "Stage updated.");
282
    is(
283
        $dbitem->itemnumber->homebranch,
284
        $dbstage->branchcode_id,
285
        "Item homebranch updated"
286
    );
287
    $intransfer = $dbitem->itemnumber->get_transfer;
288
    is($intransfer->frombranch, $dbstage2->branchcode_id, "Origin correct.");
289
    is($intransfer->tobranch, $dbstage->branchcode_id, "Target correct.");
290
291
    # Arrive at new branch
292
    $intransfer->datearrived(dt_from_string)->store;
293
    $dbitem->itemnumber->holdingbranch($srstage->{branchcode_id})->store;
294
266
    $dbstage->rota->cyclical(0)->store;         # Set Rota to non-cyclical.
295
    $dbstage->rota->cyclical(0)->store;         # Set Rota to non-cyclical.
267
296
297
    # Advance again, to end of rota.
298
    ok($dbitem->advance, "Non-cyclical advance to last stage.");
299
268
    # Arrive at new branch
300
    # Arrive at new branch
269
    $intransfer->datearrived(DateTime->now)->store;
301
    $intransfer->datearrived(dt_from_string)->store;
270
    $dbitem->itemnumber->holdingbranch($srstage->{branchcode_id})->store;
302
    $dbitem->itemnumber->holdingbranch($srstage->{branchcode_id})->store;
271
    $dbitem->itemnumber->homebranch($srstage->{branchcode_id})->store;
272
303
273
    # Advance again, Remove from rota.
304
    # Advance again, Remove from rota.
274
    ok($dbitem->advance, "Non-cyclical advance.");
305
    ok($dbitem->advance, "Non-cyclical advance.");
275
    ## Refetch dbitem
306
    ## Refetch dbitem
276
    $dbitem = Koha::StockRotationItems->find($sritem->{itemnumber_id});
307
    $dbitem = Koha::StockRotationItems->find($sritem->{itemnumber_id});
277
    is($dbitem, undef, "StockRotationItem has been removed.");
308
    is($dbitem, undef, "StockRotationItem has been removed.");
309
    my $item = Koha::Items->find($sritem->{itemnumber_id});
310
    is($item->homebranch, $srstage->{branchcode_id}, "Item homebranch remains");
278
311
279
    $schema->storage->txn_rollback;
312
    $schema->storage->txn_rollback;
280
};
313
};
Lines 305-312 subtest "Tests for investigate (singular)." => sub { Link Here
305
        'itemnumber'  => $dbitem->itemnumber_id,
338
        'itemnumber'  => $dbitem->itemnumber_id,
306
        'frombranch'  => $dbitem->itemnumber->homebranch,
339
        'frombranch'  => $dbitem->itemnumber->homebranch,
307
        'tobranch'    => $dbitem->itemnumber->homebranch,
340
        'tobranch'    => $dbitem->itemnumber->homebranch,
308
        'datesent'    => DateTime->now,
341
        'datesent'    => dt_from_string,
309
        'datearrived' => DateTime->now,
342
        'datearrived' => dt_from_string,
310
        'reason'      => "StockrotationAdvance",
343
        'reason'      => "StockrotationAdvance",
311
    })->store;
344
    })->store;
312
    is($dbitem->investigate->{reason}, 'repatriation', "older item repatriates.");
345
    is($dbitem->investigate->{reason}, 'repatriation', "older item repatriates.");
Lines 321-328 subtest "Tests for investigate (singular)." => sub { Link Here
321
        'itemnumber'  => $dbitem->itemnumber_id,
354
        'itemnumber'  => $dbitem->itemnumber_id,
322
        'frombranch'  => $dbitem->itemnumber->homebranch,
355
        'frombranch'  => $dbitem->itemnumber->homebranch,
323
        'tobranch'    => $dbitem->stage->branchcode_id,
356
        'tobranch'    => $dbitem->stage->branchcode_id,
324
        'datesent'    => DateTime->now,
357
        'datesent'    => dt_from_string,
325
        'datearrived' => DateTime->now,
358
        'datearrived' => dt_from_string,
326
        'reason'      => "StockrotationAdvance",
359
        'reason'      => "StockrotationAdvance",
327
    })->store;
360
    })->store;
328
    $dbitem->itemnumber->homebranch($dbitem->stage->branchcode_id)->store;
361
    $dbitem->itemnumber->homebranch($dbitem->stage->branchcode_id)->store;
Lines 340-347 subtest "Tests for investigate (singular)." => sub { Link Here
340
        'itemnumber'  => $dbitem->itemnumber_id,
373
        'itemnumber'  => $dbitem->itemnumber_id,
341
        'frombranch'  => $dbitem->itemnumber->homebranch,
374
        'frombranch'  => $dbitem->itemnumber->homebranch,
342
        'tobranch'    => $dbitem->stage->branchcode_id,
375
        'tobranch'    => $dbitem->stage->branchcode_id,
343
        'datesent'    => DateTime->now - $sent_duration,
376
        'datesent'    => dt_from_string - $sent_duration,
344
        'datearrived' => DateTime->now - $arrived_duration,
377
        'datearrived' => dt_from_string - $arrived_duration,
345
        'reason'      => "StockrotationAdvance",
378
        'reason'      => "StockrotationAdvance",
346
    })->store;
379
    })->store;
347
    $dbitem->itemnumber->homebranch($dbitem->stage->branchcode_id)->store;
380
    $dbitem->itemnumber->homebranch($dbitem->stage->branchcode_id)->store;
Lines 360-367 subtest "Tests for investigate (singular)." => sub { Link Here
360
        'itemnumber'  => $dbitem->itemnumber_id,
393
        'itemnumber'  => $dbitem->itemnumber_id,
361
        'frombranch'  => $dbitem->itemnumber->homebranch,
394
        'frombranch'  => $dbitem->itemnumber->homebranch,
362
        'tobranch'    => $dbitem->stage->branchcode_id,
395
        'tobranch'    => $dbitem->stage->branchcode_id,
363
        'datesent'    => DateTime->now - $sent_duration,
396
        'datesent'    => dt_from_string - $sent_duration,
364
        'datearrived' => DateTime->now - $arrived_duration,
397
        'datearrived' => dt_from_string - $arrived_duration,
365
        'reason'      => "StockrotationAdvance",
398
        'reason'      => "StockrotationAdvance",
366
    })->store;
399
    })->store;
367
    $dbitem->itemnumber->homebranch($dbitem->stage->branchcode_id)->store;
400
    $dbitem->itemnumber->homebranch($dbitem->stage->branchcode_id)->store;
Lines 380-387 subtest "Tests for investigate (singular)." => sub { Link Here
380
        'itemnumber'  => $dbitem->itemnumber_id,
413
        'itemnumber'  => $dbitem->itemnumber_id,
381
        'frombranch'  => $dbitem->itemnumber->homebranch,
414
        'frombranch'  => $dbitem->itemnumber->homebranch,
382
        'tobranch'    => $dbitem->stage->branchcode_id,
415
        'tobranch'    => $dbitem->stage->branchcode_id,
383
        'datesent'    => DateTime->now - $sent_duration,
416
        'datesent'    => dt_from_string - $sent_duration,
384
        'datearrived' => DateTime->now - $arrived_duration,
417
        'datearrived' => dt_from_string - $arrived_duration,
385
        'reason'      => "StockrotationAdvance",
418
        'reason'      => "StockrotationAdvance",
386
    })->store;
419
    })->store;
387
    is($dbitem->investigate->{reason}, 'repatriation',
420
    is($dbitem->investigate->{reason}, 'repatriation',
388
- 

Return to bug 24446