From 548454974d9b747fb286a2e0407c2713452a1b54 Mon Sep 17 00:00:00 2001 From: Lari Taskula Date: Tue, 7 Feb 2017 13:52:29 +0200 Subject: [PATCH] Bug 18072: Add Koha::Item->can_be_transferred This patch adds a new method Koha::Item->can_be_transferred. Includes unit test. To test: 1. prove t/db_dependent/Koha/Items.t --- Koha/Exceptions/Library.pm | 17 ++++++++++++++ Koha/Item.pm | 54 ++++++++++++++++++++++++++++++++++++++++++++- t/db_dependent/Koha/Items.t | 42 ++++++++++++++++++++++++++++++++++- 3 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 Koha/Exceptions/Library.pm diff --git a/Koha/Exceptions/Library.pm b/Koha/Exceptions/Library.pm new file mode 100644 index 0000000..d4b0cfe --- /dev/null +++ b/Koha/Exceptions/Library.pm @@ -0,0 +1,17 @@ +package Koha::Exceptions::Library; + +use Modern::Perl; + +use Exception::Class ( + + 'Koha::Exceptions::Library::Exception' => { + description => 'Something went wrong!', + }, + + 'Koha::Exceptions::Library::NotFound' => { + isa => 'Koha::Exceptions::Library::Exception', + description => 'Library not found', + }, +); + +1; diff --git a/Koha/Item.pm b/Koha/Item.pm index 52a7700..4cec398 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -25,10 +25,13 @@ use Koha::Database; use C4::Context; use Koha::IssuingRules; -use Koha::Item::Transfer; +use Koha::Item::Transfer::Limits; +use Koha::Item::Transfers; use Koha::Patrons; use Koha::Libraries; +use Koha::Exceptions::Library; + use base qw(Koha::Object); =head1 NAME @@ -144,6 +147,55 @@ sub can_article_request { return q{}; } +=head3 can_be_transferred + +Checks if a transfer of an item from library to another is allowed. + +This feature is controlled by two system preferences: +UseBranchTransferLimits to enable / disable the feature +BranchTransferLimitsType to use either an itemnumber or ccode as an identifier + for setting the limitations + + MANDATORY PARAMETERS: + $from : Koha::Library or branchcode string + $to : Koha::Library or branchcode string + +=cut + +sub can_be_transferred { + my ($self, $from, $to) = @_; + + if (ref($from) ne 'Koha::Library') { + my $frombranchcode = $from; + $from = Koha::Libraries->find($frombranchcode); + unless ($from) { + Koha::Exceptions::Library::NotFound->throw( + error => "Library '$frombranchcode' not found.", + ); + } + } + if (ref($to) ne 'Koha::Library') { + my $tobranchcode = $to; + $to = Koha::Libraries->find($tobranchcode); + unless ($to) { + Koha::Exceptions::Library::NotFound->throw( + error => "Library '$tobranchcode' not found.", + ); + } + } + + return 1 if $from->branchcode eq $to->branchcode; # Transfer to current branch is allowed + return 1 unless C4::Context->preference('UseBranchTransferLimits'); + + my $limittype = C4::Context->preference('BranchTransferLimitsType'); + return Koha::Item::Transfer::Limits->search({ + toBranch => $to->branchcode, + fromBranch => $from->branchcode, + $limittype => $limittype eq 'itemtype' + ? $self->effective_itemtype : $self->ccode + })->count ? 0 : 1; +} + =head3 article_request_type my $type = $item->article_request_type( $borrower ) diff --git a/t/db_dependent/Koha/Items.t b/t/db_dependent/Koha/Items.t index 843bf44..e5a82af 100644 --- a/t/db_dependent/Koha/Items.t +++ b/t/db_dependent/Koha/Items.t @@ -19,14 +19,16 @@ use Modern::Perl; -use Test::More tests => 5; +use Test::More tests => 6; use C4::Circulation; use Koha::Item; +use Koha::Item::Transfer::Limits; use Koha::Items; use Koha::Database; use t::lib::TestBuilder; +use t::lib::Mocks; my $schema = Koha::Database->new->schema; $schema->storage->txn_begin; @@ -74,6 +76,44 @@ subtest 'get_transfer' => sub { is( $transfer->itemnumber, $new_item_1->itemnumber, 'Koha::Item->get_transfer should return a valid Koha::Item::Transfers object' ); }; +subtest 'can_be_transferred' => sub { + plan tests => 4; + + t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1); + t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype'); + + my $library1 = $builder->build( { source => 'Branch' } )->{branchcode}; + my $library2 = $builder->build( { source => 'Branch' } )->{branchcode}; + my $item = Koha::Item->new({ + biblionumber => $biblioitem->{biblionumber}, + biblioitemnumber => $biblioitem->{biblioitemnumber}, + homebranch => $library1, + holdingbranch => $library1, + itype => 'test', + barcode => "newbarcode", + })->store; + $nb_of_items++; + + is(Koha::Item::Transfer::Limits->search({ + fromBranch => $library1, + toBranch => $library2, + })->count, 0, 'There are no transfer limits between libraries.'); + ok($item->can_be_transferred($library1, $library2), + 'Item can be transferred between libraries.'); + + my $limit = Koha::Item::Transfer::Limit->new({ + fromBranch => $library1, + toBranch => $library2, + itemtype => $item->effective_itemtype, + })->store; + is(Koha::Item::Transfer::Limits->search({ + fromBranch => $library1, + toBranch => $library2, + })->count, 1, 'Given we have added a transfer limit,'); + is($item->can_be_transferred($library1, $library2), 0, + 'Item can no longer be transferred between libraries.'); +}; + $retrieved_item_1->delete; is( Koha::Items->search->count, $nb_of_items + 1, 'Delete should have deleted the item' ); -- 1.9.1