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

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

Return to bug 24446