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

(-)a/Koha/Exceptions/Item/Transfer.pm (+21 lines)
Line 0 Link Here
1
package Koha::Exceptions::Item::Transfer;
2
3
use Modern::Perl;
4
5
use Exception::Class (
6
7
    'Koha::Exceptions::Item::Transfer' => {
8
        description => 'Something went wrong'
9
    },
10
    'Koha::Exceptions::Item::Transfer::Found' => {
11
        isa => 'Koha::Exceptions::Item::Transfer',
12
        description => "Active item transfer already exists",
13
        fields => ['transfer']
14
    },
15
    'Koha::Exceptions::Item::Transfer::Limit' => {
16
        isa => 'Koha::Exceptions::Item::Transfer',
17
        description => "Transfer not allowed"
18
    }
19
);
20
21
1;
(-)a/Koha/Item.pm (-5 / +66 lines)
Lines 36-41 use C4::Log qw( logaction ); Link Here
36
use Koha::Checkouts;
36
use Koha::Checkouts;
37
use Koha::CirculationRules;
37
use Koha::CirculationRules;
38
use Koha::SearchEngine::Indexer;
38
use Koha::SearchEngine::Indexer;
39
use Koha::Exceptions::Item::Transfer;
39
use Koha::Item::Transfer::Limits;
40
use Koha::Item::Transfer::Limits;
40
use Koha::Item::Transfers;
41
use Koha::Item::Transfers;
41
use Koha::ItemTypes;
42
use Koha::ItemTypes;
Lines 398-416 sub holds { Link Here
398
    return Koha::Holds->_new_from_dbic( $holds_rs );
399
    return Koha::Holds->_new_from_dbic( $holds_rs );
399
}
400
}
400
401
402
=head3 request_transfer
403
404
  my $transfer = $item->request_transfer(
405
      { to => $to_library, reason => $reason, force => 0 } );
406
407
Add a transfer request for this item to the given branch for the given reason.
408
409
An exception will be thrown if the BranchTransferLimits would prevent the requested
410
transfer, unless 'force' is passed to override the limits.
411
412
Note: At this time, only one active transfer (i.e pending arrival date) may exist
413
at a time for any given item. An exception will be thrown should you attempt to
414
add a request when a transfer has already been queued, whether it is in transit
415
or just at the request stage.
416
417
=cut
418
419
sub request_transfer {
420
    my ( $self, $params ) = @_;
421
422
    # check for mandatory params
423
    my @mandatory = ( 'to', 'reason' );
424
    for my $param (@mandatory) {
425
        unless ( defined( $params->{$param} ) ) {
426
            Koha::Exceptions::MissingParameter->throw(
427
                error => "The $param parameter is mandatory" );
428
        }
429
    }
430
431
    my $request;
432
    Koha::Exceptions::Item::Transfer::Found->throw( transfer => $request )
433
      if ( $request = $self->get_transfer );
434
    # FIXME: Add override functionality to allow for queing transfers
435
436
    Koha::Exceptions::Item::Transfer::Limit->throw()
437
      unless ( $params->{force} || $self->can_be_transferred( { to => $params->{to} } ) );
438
439
    my $transfer = Koha::Item::Transfer->new(
440
        {
441
            itemnumber    => $self->itemnumber,
442
            daterequested => dt_from_string,
443
            frombranch    => $self->holdingbranch,
444
            tobranch      => $params->{to}->branchcode,
445
            reason        => $params->{reason},
446
            comments      => $params->{comment}
447
        }
448
    )->store();
449
    return $transfer;
450
}
451
401
=head3 get_transfer
452
=head3 get_transfer
402
453
403
my $transfer = $item->get_transfer;
454
  my $transfer = $item->get_transfer;
404
455
405
Return the transfer if the item is in transit or undef
456
Return the active transfer request or undef
457
458
Note: Transfers are retrieved in a LIFO (Last In First Out) order using this method.
459
460
FIXME: Add Tests for LIFO functionality
406
461
407
=cut
462
=cut
408
463
409
sub get_transfer {
464
sub get_transfer {
410
    my ( $self ) = @_;
465
    my ($self) = @_;
411
    my $transfer_rs = $self->_result->branchtransfers->search({ datearrived => undef })->first;
466
    my $transfer_rs = $self->_result->branchtransfers->search(
467
        { datearrived => undef },
468
        {
469
            order_by => [ { -asc => 'datesent' }, { -asc => 'daterequested' } ],
470
            rows     => 1
471
        }
472
    )->first;
412
    return unless $transfer_rs;
473
    return unless $transfer_rs;
413
    return Koha::Item::Transfer->_new_from_dbic( $transfer_rs );
474
    return Koha::Item::Transfer->_new_from_dbic($transfer_rs);
414
}
475
}
415
476
416
=head3 last_returned_by
477
=head3 last_returned_by
(-)a/t/db_dependent/Koha/Item.t (-2 / +64 lines)
Lines 19-31 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 7;
22
use Test::More tests => 8;
23
use Test::Exception;
23
24
24
use C4::Biblio;
25
use C4::Biblio;
25
use C4::Circulation;
26
use C4::Circulation;
26
27
27
use Koha::Items;
28
use Koha::Items;
28
use Koha::Database;
29
use Koha::Database;
30
use Koha::DateUtils;
29
use Koha::Old::Items;
31
use Koha::Old::Items;
30
32
31
use List::MoreUtils qw(all);
33
use List::MoreUtils qw(all);
Lines 339-344 subtest 'pickup_locations' => sub { Link Here
339
    $schema->storage->txn_rollback;
341
    $schema->storage->txn_rollback;
340
};
342
};
341
343
344
subtest 'request_transfer' => sub {
345
    plan tests => 7;
346
    $schema->storage->txn_begin;
347
348
    my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
349
    my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
350
    my $item     = $builder->build_sample_item(
351
        {
352
            homebranch    => $library1->branchcode,
353
            holdingbranch => $library2->branchcode,
354
        }
355
    );
356
357
    # Mandatory fields tests
358
    throws_ok { $item->request_transfer( { to => $library1 } ) }
359
    'Koha::Exceptions::MissingParameter',
360
      'Exception thrown if `reason` parameter is missing';
361
362
    throws_ok { $item->request_transfer( { reason => 'Manual' } ) }
363
    'Koha::Exceptions::MissingParameter',
364
      'Exception thrown if `to` parameter is missing';
365
366
    # Successful request
367
    my $transfer = $item->request_transfer({ to => $library1, reason => 'Manual' });
368
    is( ref($transfer), 'Koha::Item::Transfer',
369
        'Koha::Item->request_transfer should return a Koha::Item::Transfer object'
370
    );
371
372
    # Transfer already in progress
373
    throws_ok { $item->request_transfer( { to => $library2, reason => 'Manual' } ) }
374
    'Koha::Exceptions::Item::Transfer::Found',
375
      'Exception thrown if transfer is already in progress';
376
377
    my $exception = $@;
378
    is( ref( $exception->transfer ),
379
        'Koha::Item::Transfer',
380
        'The exception contains the found Koha::Item::Transfer' );
381
382
    $transfer->datearrived(dt_from_string)->store();
383
384
    # BranchTransferLimits
385
    t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
386
    t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
387
    my $limit = Koha::Item::Transfer::Limit->new({
388
        fromBranch => $library2->branchcode,
389
        toBranch => $library1->branchcode,
390
        itemtype => $item->effective_itemtype,
391
    })->store;
392
393
    throws_ok { $item->request_transfer( { to => $library1, reason => 'Manual' } ) }
394
    'Koha::Exceptions::Item::Transfer::Limit',
395
      'Exception thrown if transfer is prevented by limits';
396
397
    my $forced_transfer = $item->request_transfer( { to => $library1, reason => 'Manual', force => 1 } );
398
    is( ref($forced_transfer), 'Koha::Item::Transfer',
399
        'Koha::Item->request_transfer allowed when forced'
400
    );
401
402
    $schema->storage->txn_rollback;
403
};
404
342
subtest 'deletion' => sub {
405
subtest 'deletion' => sub {
343
    plan tests => 12;
406
    plan tests => 12;
344
407
345
- 

Return to bug 25755