@@ -, +, @@ library for each item INSERT INTO branches (branchcode,branchname,pickup_location) SELECT CONCAT(branchcode,"D"),CONCAT(branchname,"A"),pickup_location FROM branches; INSERT INTO branches (branchcode,branchname,pickup_location) SELECT CONCAT(branchcode,"D"),CONCAT(branchname,"B"),pickup_location FROM branches; INSERT INTO branches (branchcode,branchname,pickup_location) SELECT CONCAT(branchcode,"D"),CONCAT(branchname,"C"),pickup_location FROM branches; INSERT INTO branches (branchcode,branchname,pickup_location) SELECT CONCAT(branchcode,"D"),CONCAT(branchname,"D"),pickup_location FROM branches; INSERT INTO items (biblionumber,biblioitemnumber,barcode,itype,homebranch,holdingbranch) SELECT 1,1,CONCAT("test-",branchcode),'BKS',branchcode,branchcode FROM branches; UseBranchTransferLimits = 'enforce' BranchTransferLimitsType = 'item type' --- Koha/Item.pm | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 7 deletions(-) --- a/Koha/Item.pm +++ a/Koha/Item.pm @@ -550,6 +550,60 @@ sub can_be_transferred { $limittype => $limittype eq 'itemtype' ? $self->effective_itemtype : $self->ccode })->count ? 0 : 1; + +} + +=head3 libs_can_transfer_to + +$item->libs_can_transfer_to({ to => $to_libraries, from => $from_library }) +Checks if an item can be transferred to given libraries. + +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 + +Takes HASHref that can have the following parameters: + MANDATORY PARAMETERS: + $to : Array of Koha::Libraries + OPTIONAL PARAMETERS: + $from : Koha::Library # if not given, item holdingbranch + # will be used instead + +Returns arry of Koha::Libraries that item can be transferred to $to_library. + +If checking only one library please use $item->can_be_transferred. + +=cut + +sub libs_can_transfer_to { + my ($self, $params ) = @_; + + my $to = $params->{to}; + my $from = $params->{from}; + $from = defined $from ? $from->branchcode : $self->holdingbranch; + + return $to unless C4::Context->preference('UseBranchTransferLimits'); + + my $limittype = C4::Context->preference('BranchTransferLimitsType'); + my $limiter = $limittype eq 'itemtype' ? $self->effective_itemtype : $self->ccode; + + my @destinations = map { $_->branchcode } @$to; + my @cant_transfer; + my $limits = Koha::Item::Transfer::Limits->search({ + fromBranch => $from, + $limittype => $limiter + }); + my @limits = $limits->get_column('toBranch'); + return $to unless @limits; + + my @can_transfer = Koha::Libraries->search({ + branchcode => { + -in => \@destinations, + -not_in => \@limits, + } + }); + return \@can_transfer; } =head3 pickup_locations @@ -596,14 +650,11 @@ sub pickup_locations { })->as_list; } - my @pickup_locations; - foreach my $library (@libs) { - if ($library->pickup_location && $self->can_be_transferred({ to => $library })) { - push @pickup_locations, $library; - } - } + my $pickup_locations = $self->libs_can_transfer_to({ + to => \@libs + }); - return \@pickup_locations; + return $pickup_locations; } =head3 article_request_type --