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
36
37
use Koha::Checkouts;
37
use Koha::Checkouts;
38
use Koha::CirculationRules;
38
use Koha::CirculationRules;
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::Patrons;
42
use Koha::Patrons;
Lines 379-397 sub holds { Link Here
379
    return Koha::Holds->_new_from_dbic( $holds_rs );
380
    return Koha::Holds->_new_from_dbic( $holds_rs );
380
}
381
}
381
382
383
=head3 request_transfer
384
385
  my $transfer = $item->request_transfer(
386
      { to => $to_library, reason => $reason, force => 0 } );
387
388
Add a transfer request for this item to the given branch for the given reason.
389
390
An exception will be thrown if the BranchTransferLimits would prevent the requested
391
transfer, unless 'force' is passed to override the limits.
392
393
Note: At this time, only one active transfer (i.e pending arrival date) may exist
394
at a time for any given item. An exception will be thrown should you attempt to
395
add a request when a transfer has already been queued, whether it is in transit
396
or just at the request stage.
397
398
=cut
399
400
sub request_transfer {
401
    my ( $self, $params ) = @_;
402
403
    # check for mandatory params
404
    my @mandatory = ( 'to', 'reason' );
405
    for my $param (@mandatory) {
406
        unless ( defined( $params->{$param} ) ) {
407
            Koha::Exceptions::MissingParameter->throw(
408
                error => "The $param parameter is mandatory" );
409
        }
410
    }
411
412
    my $request;
413
    Koha::Exceptions::Item::Transfer::Found->throw( transfer => $request )
414
      if ( $request = $self->get_transfer );
415
    # FIXME: Add override functionality to allow for queing transfers
416
    
417
    Koha::Exceptions::Item::Transfer::Limit->throw()
418
      unless ( $params->{force} || $self->can_be_transferred( { to => $params->{to} } ) );
419
420
    my $transfer = Koha::Item::Transfer->new(
421
        {
422
            itemnumber    => $self->itemnumber,
423
            daterequested => dt_from_string,
424
            frombranch    => $self->holdingbranch,
425
            tobranch      => $params->{to}->branchcode,
426
            reason        => $params->{reason},
427
            comments      => $params->{comment}
428
        }
429
    )->store();
430
    return $transfer;
431
}
432
382
=head3 get_transfer
433
=head3 get_transfer
383
434
384
my $transfer = $item->get_transfer;
435
  my $transfer = $item->get_transfer;
436
437
Return the active transfer request or undef
385
438
386
Return the transfer if the item is in transit or undef
439
Note: Transfers are retrieved in a LIFO (Last In First Out) order using this method.
440
441
FIXME: Add Tests for LIFO functionality
387
442
388
=cut
443
=cut
389
444
390
sub get_transfer {
445
sub get_transfer {
391
    my ( $self ) = @_;
446
    my ($self) = @_;
392
    my $transfer_rs = $self->_result->branchtransfers->search({ datearrived => undef })->first;
447
    my $transfer_rs = $self->_result->branchtransfers->search(
448
        { datearrived => undef },
449
        {
450
            order_by => [ { -asc => 'datesent' }, { -asc => 'daterequested' } ],
451
            rows     => 1
452
        }
453
    )->first;
393
    return unless $transfer_rs;
454
    return unless $transfer_rs;
394
    return Koha::Item::Transfer->_new_from_dbic( $transfer_rs );
455
    return Koha::Item::Transfer->_new_from_dbic($transfer_rs);
395
}
456
}
396
457
397
=head3 last_returned_by
458
=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 => 6;
22
use Test::More tests => 7;
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 341-346 subtest 'pickup_locations' => sub { Link Here
341
    $schema->storage->txn_rollback;
343
    $schema->storage->txn_rollback;
342
};
344
};
343
345
346
subtest 'request_transfer' => sub {
347
    plan tests => 7;
348
    $schema->storage->txn_begin;
349
350
    my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
351
    my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
352
    my $item     = $builder->build_sample_item(
353
        {
354
            homebranch    => $library1->branchcode,
355
            holdingbranch => $library2->branchcode,
356
        }
357
    );
358
359
    # Mandatory fields tests
360
    throws_ok { $item->request_transfer( { to => $library1 } ) }
361
    'Koha::Exceptions::MissingParameter',
362
      'Exception thrown if `reason` parameter is missing';
363
364
    throws_ok { $item->request_transfer( { reason => 'Manual' } ) }
365
    'Koha::Exceptions::MissingParameter',
366
      'Exception thrown if `to` parameter is missing';
367
368
    # Successful request
369
    my $transfer = $item->request_transfer({ to => $library1, reason => 'Manual' });
370
    is( ref($transfer), 'Koha::Item::Transfer',
371
        'Koha::Item->request_transfer should return a Koha::Item::Transfer object'
372
    );
373
374
    # Transfer already in progress
375
    throws_ok { $item->request_transfer( { to => $library2, reason => 'Manual' } ) }
376
    'Koha::Exceptions::Item::Transfer::Found',
377
      'Exception thrown if transfer is already in progress';
378
379
    my $exception = $@;
380
    is( ref( $exception->transfer ),
381
        'Koha::Item::Transfer',
382
        'The exception contains the found Koha::Item::Transfer' );
383
384
    $transfer->datearrived(dt_from_string)->store();
385
386
    # BranchTransferLimits
387
    t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
388
    t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
389
    my $limit = Koha::Item::Transfer::Limit->new({
390
        fromBranch => $library2->branchcode,
391
        toBranch => $library1->branchcode,
392
        itemtype => $item->effective_itemtype,
393
    })->store;
394
395
    throws_ok { $item->request_transfer( { to => $library1, reason => 'Manual' } ) }
396
    'Koha::Exceptions::Item::Transfer::Limit',
397
      'Exception thrown if transfer is prevented by limits';
398
399
    my $forced_transfer = $item->request_transfer( { to => $library1, reason => 'Manual', force => 1 } );
400
    is( ref($forced_transfer), 'Koha::Item::Transfer',
401
        'Koha::Item->request_transfer allowed when forced'
402
    );
403
404
    $schema->storage->txn_rollback;
405
};
406
344
subtest 'deletion' => sub {
407
subtest 'deletion' => sub {
345
    plan tests => 12;
408
    plan tests => 12;
346
409
347
- 

Return to bug 25755