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

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

Return to bug 25755