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::ItemTypes;
42
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 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