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

(-)a/Koha/Item.pm (-9 / +21 lines)
Lines 405-421 sub holds { Link Here
405
=head3 request_transfer
405
=head3 request_transfer
406
406
407
  my $transfer = $item->request_transfer(
407
  my $transfer = $item->request_transfer(
408
      { to => $to_library, reason => $reason, ignore_limits => 0 } );
408
    {
409
        to     => $to_library,
410
        reason => $reason,
411
        [ ignore_limits => 0, enqueue => 1, replace => 1 ]
412
    }
413
  );
409
414
410
Add a transfer request for this item to the given branch for the given reason.
415
Add a transfer request for this item to the given branch for the given reason.
411
416
412
An exception will be thrown if the BranchTransferLimits would prevent the requested
417
An exception will be thrown if the BranchTransferLimits would prevent the requested
413
transfer, unless 'ignore_limits' is passed to override the limits.
418
transfer, unless 'ignore_limits' is passed to override the limits.
414
419
415
Note: At this time, only one active transfer (i.e pending arrival date) may exist
420
An exception will be thrown if an active transfer (i.e pending arrival date) is found;
416
at a time for any given item. An exception will be thrown should you attempt to
421
The caller should catch such cases and retry the transfer request as appropriate passing
417
add a request when a transfer has already been queued, whether it is in transit
422
an appropriate override.
418
or just at the request stage.
423
424
Overrides
425
* enqueue - Used to queue up the transfer when the existing transfer is found to be in transit.
426
* replace - Used to replace the existing transfer request with your own.
419
427
420
=cut
428
=cut
421
429
Lines 431-444 sub request_transfer { Link Here
431
        }
439
        }
432
    }
440
    }
433
441
434
    my $request;
435
    Koha::Exceptions::Item::Transfer::Found->throw( transfer => $request )
436
      if ( $request = $self->get_transfer );
437
438
    Koha::Exceptions::Item::Transfer::Limit->throw()
442
    Koha::Exceptions::Item::Transfer::Limit->throw()
439
      unless ( $params->{ignore_limits}
443
      unless ( $params->{ignore_limits}
440
        || $self->can_be_transferred( { to => $params->{to} } ) );
444
        || $self->can_be_transferred( { to => $params->{to} } ) );
441
445
446
    my $request = $self->get_transfer;
447
    Koha::Exceptions::Item::Transfer::Found->throw( transfer => $request )
448
      if ( $request && !$params->{enqueue} && !$params->{replace} );
449
450
    $request->cancel( $params->{reason} )
451
      if ( defined($request) && $params->{replace} );
452
442
    my $transfer = Koha::Item::Transfer->new(
453
    my $transfer = Koha::Item::Transfer->new(
443
        {
454
        {
444
            itemnumber    => $self->itemnumber,
455
            itemnumber    => $self->itemnumber,
Lines 449-454 sub request_transfer { Link Here
449
            comments      => $params->{comment}
460
            comments      => $params->{comment}
450
        }
461
        }
451
    )->store();
462
    )->store();
463
452
    return $transfer;
464
    return $transfer;
453
}
465
}
454
466
(-)a/Koha/StockRotationItem.pm (-15 / +56 lines)
Lines 26-31 use Koha::DateUtils qw/dt_from_string/; Link Here
26
use Koha::Item::Transfer;
26
use Koha::Item::Transfer;
27
use Koha::Item;
27
use Koha::Item;
28
use Koha::StockRotationStage;
28
use Koha::StockRotationStage;
29
use Try::Tiny;
29
30
30
use base qw(Koha::Object);
31
use base qw(Koha::Object);
31
32
Lines 143-169 sub needs_advancing { Link Here
143
144
144
  1|0 = $sritem->repatriate
145
  1|0 = $sritem->repatriate
145
146
146
Put this item into branch transfer with 'StockrotationCorrection' comment, so
147
Put this item into branch transfer with 'StockrotationRepatriation' comment, so
147
that it may return to it's stage.branch to continue its rota as normal.
148
that it may return to it's stage.branch to continue its rota as normal.
148
149
150
Note: Stockrotation falls outside of the normal branch transfer limits and so we
151
pass 'ignore_limits' in the call to request_transfer.
152
149
=cut
153
=cut
150
154
151
sub repatriate {
155
sub repatriate {
152
    my ( $self, $msg ) = @_;
156
    my ( $self, $msg ) = @_;
153
157
154
    # Create the transfer.
158
    # Create the transfer.
155
    my $transfer = $self->itemnumber->request_transfer(
159
    my $transfer = try {
156
        {
160
        $self->itemnumber->request_transfer(
157
            to      => $self->stage->branchcode,
161
            {
158
            reason  => "StockrotationRepatriation",
162
                to            => $self->stage->branchcode,
159
            comment => $msg
163
                reason        => "StockrotationRepatriation",
160
        }
164
                comment       => $msg,
161
    );
165
                ignore_limits => 1
166
            }
167
        );
168
    };
162
169
163
    # Ensure the homebranch is still in sync with the rota stage
170
    # Ensure the homebranch is still in sync with the rota stage
164
    $self->itemnumber->homebranch( $self->stage->branchcode_id )->store;
171
    $self->itemnumber->homebranch( $self->stage->branchcode_id )->store;
165
172
166
    return $transfer;
173
    return defined($transfer) ? 1 : 0;
167
}
174
}
168
175
169
=head3 advance
176
=head3 advance
Lines 180-185 StockRotationItem. Link Here
180
If this item is 'indemand', and advance is invoked, we disable 'indemand' and
187
If this item is 'indemand', and advance is invoked, we disable 'indemand' and
181
advance the item as per usual.
188
advance the item as per usual.
182
189
190
Note: Stockrotation falls outside of the normal branch transfer limits and so we
191
pass 'ignore_limits' in the call to request_transfer.
192
183
=cut
193
=cut
184
194
185
sub advance {
195
sub advance {
Lines 221-233 sub advance { Link Here
221
    # Update stage and record transfer
231
    # Update stage and record transfer
222
    $self->stage_id( $new_stage->stage_id )->store;          # Set new stage
232
    $self->stage_id( $new_stage->stage_id )->store;          # Set new stage
223
    $item->homebranch( $new_stage->branchcode_id )->store;   # Update homebranch
233
    $item->homebranch( $new_stage->branchcode_id )->store;   # Update homebranch
224
    $transfer = $item->request_transfer(
234
    $transfer = try {
225
        {
235
        $item->request_transfer(
226
            to            => $new_stage->branchcode,
236
            {
227
            reason        => "StockrotationAdvance",
237
                to            => $new_stage->branchcode,
228
            ignore_limits => 1
238
                reason        => "StockrotationAdvance",
239
                ignore_limits => 1                      # Ignore transfer limits
240
            }
241
        );                                              # Add transfer
242
    }
243
    catch {
244
        if ( $_->isa('Koha::Exceptions::Item::Transfer::Found') ) {
245
            my $exception = $_;
246
            my $found_transfer = $_->transfer;
247
            if (   $found_transfer->in_transit
248
                || $found_transfer->reason eq 'Reserve' )
249
            {
250
                return $item->request_transfer(
251
                    {
252
                        to            => $new_stage->branchcode,
253
                        reason        => "StockrotationAdvance",
254
                        ignore_limits => 1,
255
                        enqueue       => 1
256
                    }
257
                );                                      # Queue transfer
258
            } else {
259
                return $item->request_transfer(
260
                    {
261
                        to            => $new_stage->branchcode,
262
                        reason        => "StockrotationAdvance",
263
                        ignore_limits => 1,
264
                        replace       => 1
265
                    }
266
                );                                      # Replace transfer
267
            }
268
        } else {
269
            $_->rethrow();
229
        }
270
        }
230
    );                                                       # Add transfer
271
    };
231
    $transfer->receive
272
    $transfer->receive
232
      if $item->holdingbranch eq $new_stage->branchcode_id;  # Already at branch
273
      if $item->holdingbranch eq $new_stage->branchcode_id;  # Already at branch
233
274
(-)a/t/db_dependent/StockRotationItems.t (-76 / +224 lines)
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
- 

Return to bug 24446