Bugzilla – Attachment 67051 Details for
Bug 18072
Add Koha objects for Branch Transfer Limits
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 18072: Add Koha::Item->can_be_transferred
Bug-18072-Add-KohaItem-canbetransferred.patch (text/plain), 6.56 KB, created by
Tomás Cohen Arazi (tcohen)
on 2017-09-11 18:39:29 UTC
(
hide
)
Description:
Bug 18072: Add Koha::Item->can_be_transferred
Filename:
MIME Type:
Creator:
Tomás Cohen Arazi (tcohen)
Created:
2017-09-11 18:39:29 UTC
Size:
6.56 KB
patch
obsolete
>From 4d48d18e2cd2df96723b07d0a20283a47f0f92a7 Mon Sep 17 00:00:00 2001 >From: Lari Taskula <lari.taskula@jns.fi> >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 > >Signed-off-by: Josef Moravec <josef.moravec@gmail.com> >Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io> >--- > Koha/Exceptions/Library.pm | 17 +++++++++++++ > Koha/Item.pm | 59 ++++++++++++++++++++++++++++++++++++++++++++- > t/db_dependent/Koha/Items.t | 50 +++++++++++++++++++++++++++++++++++++- > 3 files changed, 124 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 bea05f8..6d81cc5 100644 >--- a/Koha/Item.pm >+++ b/Koha/Item.pm >@@ -27,10 +27,13 @@ use Koha::DateUtils qw( dt_from_string ); > use C4::Context; > use Koha::Checkouts; > 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 >@@ -189,6 +192,60 @@ sub can_article_request { > return q{}; > } > >+=head3 can_be_transferred >+ >+Checks if an item can be transferred to given library. >+ >+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: >+ $to : Koha::Library or branchcode string >+ OPTIONAL PARAMETERS: >+ $from : Koha::Library or branchcode string # if not given, item holdingbranch >+ # will be used instead >+ >+=cut >+ >+sub can_be_transferred { >+ my ($self, $to, $from) = @_; >+ >+ if (ref($to) ne 'Koha::Library') { >+ my $tobranchcode = defined $to ? $to : ''; >+ $to = Koha::Libraries->find($tobranchcode); >+ unless ($to) { >+ Koha::Exceptions::Library::NotFound->throw( >+ error => "Library '$tobranchcode' not found.", >+ ); >+ } >+ } >+ if (defined $from && ref($from) ne 'Koha::Library') { >+ my $frombranchcode = defined $from ? $from : ''; >+ $from = Koha::Libraries->find($frombranchcode); >+ unless ($from) { >+ Koha::Exceptions::Library::NotFound->throw( >+ error => "Library '$frombranchcode' not found.", >+ ); >+ } >+ } >+ >+ $to = $to->branchcode; >+ $from = defined $from ? $from->branchcode : $self->holdingbranch; >+ >+ return 1 if $from eq $to; # 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, >+ fromBranch => $from, >+ $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 0864825..7f5eb38 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 => 8; >+use Test::More tests => 9; > > 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; >@@ -118,6 +120,52 @@ subtest 'checkout' => sub { > is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no *current* checkout on this item' ); > }; > >+subtest 'can_be_transferred' => sub { >+ plan tests => 8; >+ >+ 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($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($library2), 0, >+ 'Item can no longer be transferred between libraries.'); >+ is($item->can_be_transferred($library2, $library1), 0, >+ 'We get the same result also if we pass the from-library parameter.'); >+ eval { $item->can_be_transferred(); }; >+ is(ref($@), 'Koha::Exceptions::Library::NotFound', 'Exception thrown when no library given.'); >+ eval { $item->can_be_transferred('heaven'); }; >+ is(ref($@), 'Koha::Exceptions::Library::NotFound', 'Exception thrown when invalid library is given.'); >+ eval { $item->can_be_transferred($library2, 'hell'); }; >+ is(ref($@), 'Koha::Exceptions::Library::NotFound', 'Exception thrown when invalid library is given.'); >+}; >+ > $retrieved_item_1->delete; > is( Koha::Items->search->count, $nb_of_items + 1, 'Delete should have deleted the item' ); > >-- >2.7.4
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 18072
:
59979
|
59980
|
59981
|
59982
|
60009
|
60010
|
60011
|
60116
|
60117
|
60782
|
60783
|
60784
|
63683
|
63684
|
63685
|
63686
|
63687
|
66724
|
67049
|
67050
|
67051
|
67052
|
67053
|
67054
|
67209
|
76282
|
76283
|
76284
|
76285
|
76286
|
76287
|
76288
|
76292
|
76293
|
76294
|
76295
|
76296
|
76297
|
76298