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 37-42 use Koha::Checkouts; Link Here
37
use Koha::CirculationRules;
37
use Koha::CirculationRules;
38
use Koha::CoverImages;
38
use Koha::CoverImages;
39
use Koha::SearchEngine::Indexer;
39
use Koha::SearchEngine::Indexer;
40
use Koha::Exceptions::Item::Transfer;
40
use Koha::Item::Transfer::Limits;
41
use Koha::Item::Transfer::Limits;
41
use Koha::Item::Transfers;
42
use Koha::Item::Transfers;
42
use Koha::ItemTypes;
43
use Koha::ItemTypes;
Lines 401-419 sub holds { Link Here
401
    return Koha::Holds->_new_from_dbic( $holds_rs );
402
    return Koha::Holds->_new_from_dbic( $holds_rs );
402
}
403
}
403
404
405
=head3 request_transfer
406
407
  my $transfer = $item->request_transfer(
408
      { to => $to_library, reason => $reason, force => 0 } );
409
410
Add a transfer request for this item to the given branch for the given reason.
411
412
An exception will be thrown if the BranchTransferLimits would prevent the requested
413
transfer, unless 'force' is passed to override the limits.
414
415
Note: At this time, only one active transfer (i.e pending arrival date) may exist
416
at a time for any given item. An exception will be thrown should you attempt to
417
add a request when a transfer has already been queued, whether it is in transit
418
or just at the request stage.
419
420
=cut
421
422
sub request_transfer {
423
    my ( $self, $params ) = @_;
424
425
    # check for mandatory params
426
    my @mandatory = ( 'to', 'reason' );
427
    for my $param (@mandatory) {
428
        unless ( defined( $params->{$param} ) ) {
429
            Koha::Exceptions::MissingParameter->throw(
430
                error => "The $param parameter is mandatory" );
431
        }
432
    }
433
434
    my $request;
435
    Koha::Exceptions::Item::Transfer::Found->throw( transfer => $request )
436
      if ( $request = $self->get_transfer );
437
    # FIXME: Add override functionality to allow for queing transfers
438
439
    Koha::Exceptions::Item::Transfer::Limit->throw()
440
      unless ( $params->{force} || $self->can_be_transferred( { to => $params->{to} } ) );
441
442
    my $transfer = Koha::Item::Transfer->new(
443
        {
444
            itemnumber    => $self->itemnumber,
445
            daterequested => dt_from_string,
446
            frombranch    => $self->holdingbranch,
447
            tobranch      => $params->{to}->branchcode,
448
            reason        => $params->{reason},
449
            comments      => $params->{comment}
450
        }
451
    )->store();
452
    return $transfer;
453
}
454
404
=head3 get_transfer
455
=head3 get_transfer
405
456
406
my $transfer = $item->get_transfer;
457
  my $transfer = $item->get_transfer;
407
458
408
Return the transfer if the item is in transit or undef
459
Return the active transfer request or undef
460
461
Note: Transfers are retrieved in a LIFO (Last In First Out) order using this method.
462
463
FIXME: Add Tests for LIFO functionality
409
464
410
=cut
465
=cut
411
466
412
sub get_transfer {
467
sub get_transfer {
413
    my ( $self ) = @_;
468
    my ($self) = @_;
414
    my $transfer_rs = $self->_result->branchtransfers->search({ datearrived => undef })->first;
469
    my $transfer_rs = $self->_result->branchtransfers->search(
470
        { datearrived => undef },
471
        {
472
            order_by => [ { -asc => 'datesent' }, { -asc => 'daterequested' } ],
473
            rows     => 1
474
        }
475
    )->first;
415
    return unless $transfer_rs;
476
    return unless $transfer_rs;
416
    return Koha::Item::Transfer->_new_from_dbic( $transfer_rs );
477
    return Koha::Item::Transfer->_new_from_dbic($transfer_rs);
417
}
478
}
418
479
419
=head3 last_returned_by
480
=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 451-456 subtest 'pickup_locations' => sub { Link Here
451
    $schema->storage->txn_rollback;
453
    $schema->storage->txn_rollback;
452
};
454
};
453
455
456
subtest 'request_transfer' => sub {
457
    plan tests => 7;
458
    $schema->storage->txn_begin;
459
460
    my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
461
    my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
462
    my $item     = $builder->build_sample_item(
463
        {
464
            homebranch    => $library1->branchcode,
465
            holdingbranch => $library2->branchcode,
466
        }
467
    );
468
469
    # Mandatory fields tests
470
    throws_ok { $item->request_transfer( { to => $library1 } ) }
471
    'Koha::Exceptions::MissingParameter',
472
      'Exception thrown if `reason` parameter is missing';
473
474
    throws_ok { $item->request_transfer( { reason => 'Manual' } ) }
475
    'Koha::Exceptions::MissingParameter',
476
      'Exception thrown if `to` parameter is missing';
477
478
    # Successful request
479
    my $transfer = $item->request_transfer({ to => $library1, reason => 'Manual' });
480
    is( ref($transfer), 'Koha::Item::Transfer',
481
        'Koha::Item->request_transfer should return a Koha::Item::Transfer object'
482
    );
483
484
    # Transfer already in progress
485
    throws_ok { $item->request_transfer( { to => $library2, reason => 'Manual' } ) }
486
    'Koha::Exceptions::Item::Transfer::Found',
487
      'Exception thrown if transfer is already in progress';
488
489
    my $exception = $@;
490
    is( ref( $exception->transfer ),
491
        'Koha::Item::Transfer',
492
        'The exception contains the found Koha::Item::Transfer' );
493
494
    $transfer->datearrived(dt_from_string)->store();
495
496
    # BranchTransferLimits
497
    t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
498
    t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
499
    my $limit = Koha::Item::Transfer::Limit->new({
500
        fromBranch => $library2->branchcode,
501
        toBranch => $library1->branchcode,
502
        itemtype => $item->effective_itemtype,
503
    })->store;
504
505
    throws_ok { $item->request_transfer( { to => $library1, reason => 'Manual' } ) }
506
    'Koha::Exceptions::Item::Transfer::Limit',
507
      'Exception thrown if transfer is prevented by limits';
508
509
    my $forced_transfer = $item->request_transfer( { to => $library1, reason => 'Manual', force => 1 } );
510
    is( ref($forced_transfer), 'Koha::Item::Transfer',
511
        'Koha::Item->request_transfer allowed when forced'
512
    );
513
514
    $schema->storage->txn_rollback;
515
};
516
454
subtest 'deletion' => sub {
517
subtest 'deletion' => sub {
455
    plan tests => 12;
518
    plan tests => 12;
456
519
457
- 

Return to bug 25755