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

(-)a/Koha/Biblio.pm (+90 lines)
Lines 33-40 use Koha::Biblioitems; Link Here
33
use Koha::ArticleRequests;
33
use Koha::ArticleRequests;
34
use Koha::ArticleRequest::Status;
34
use Koha::ArticleRequest::Status;
35
use Koha::IssuingRules;
35
use Koha::IssuingRules;
36
use Koha::Item::Transfer::Limits;
37
use Koha::Libraries;
36
use Koha::Subscriptions;
38
use Koha::Subscriptions;
37
39
40
use Koha::Exceptions::Library;
41
38
=head1 NAME
42
=head1 NAME
39
43
40
Koha::Biblio - Koha Biblio Object class
44
Koha::Biblio - Koha Biblio Object class
Lines 85-90 sub can_article_request { Link Here
85
    return q{};
89
    return q{};
86
}
90
}
87
91
92
=head3 can_be_transferred
93
94
$biblio->can_be_transferred({ to => $to_library, from => $from_library })
95
96
Checks if at least one item of a biblio can be transferred to given library.
97
98
This feature is controlled by two system preferences:
99
UseBranchTransferLimits to enable / disable the feature
100
BranchTransferLimitsType to use either an itemnumber or ccode as an identifier
101
                         for setting the limitations
102
103
Performance-wise, it is recommended to use this method for a biblio instead of
104
iterating each item of a biblio with Koha::Item->can_be_transferred().
105
106
Takes HASHref that can have the following parameters:
107
    MANDATORY PARAMETERS:
108
    $to   : Koha::Library or branchcode string
109
    OPTIONAL PARAMETERS:
110
    $from : Koha::Library or branchcode string  # if given, only items from that
111
                                                # holdingbranch are considered
112
113
Returns 1 if at least one of the item of a biblio can be transferred
114
to $to_library, otherwise 0.
115
116
=cut
117
118
sub can_be_transferred {
119
    my ($self, $params) = @_;
120
121
    my $to = $params->{'to'};
122
    my $from = $params->{'from'};
123
    if (ref($to) ne 'Koha::Library') {
124
        my $tobranchcode = defined $to ? $to : '';
125
        $to = Koha::Libraries->find($tobranchcode);
126
        unless ($to) {
127
            Koha::Exceptions::Library::NotFound->throw(
128
                error => "Library '$tobranchcode' not found.",
129
            );
130
        }
131
    }
132
    if ($from && ref($from) ne 'Koha::Library') {
133
        my $frombranchcode = defined $from ? $from : '';
134
        $from = Koha::Libraries->find($frombranchcode);
135
        unless ($from) {
136
            Koha::Exceptions::Library::NotFound->throw(
137
                error => "Library '$frombranchcode' not found.",
138
            );
139
        }
140
    }
141
142
    return 1 unless C4::Context->preference('UseBranchTransferLimits');
143
    my $limittype = C4::Context->preference('BranchTransferLimitsType');
144
145
    my $items;
146
    foreach my $item_of_bib ($self->items) {
147
        next unless $item_of_bib->holdingbranch;
148
        next if $from && $from->branchcode ne $item_of_bib->holdingbranch;
149
        return 1 if $item_of_bib->holdingbranch eq $to->branchcode;
150
        my $code = $limittype eq 'itemtype'
151
            ? $item_of_bib->effective_itemtype
152
            : $item_of_bib->ccode;
153
        return 1 unless $code;
154
        $items->{$code}->{$item_of_bib->holdingbranch} = 1;
155
    }
156
157
    # At this point we will have a HASHref containing each itemtype/ccode that
158
    # this biblio has, inside which are all of the holdingbranches where those
159
    # items are located at. Then, we will query Koha::Item::Transfer::Limits to
160
    # find out whether a transfer limits for such $limittype from any of the
161
    # listed holdingbranches to the given $to library exist. If at least one
162
    # holdingbranch for that $limittype does not have a transfer limit to given
163
    # $to library, then we know that the transfer is possible.
164
    foreach my $code (keys %{$items}) {
165
        my @holdingbranches = keys %{$items->{$code}};
166
        return 1 if Koha::Item::Transfer::Limits->search({
167
            toBranch => $to->branchcode,
168
            fromBranch => { 'in' => \@holdingbranches },
169
            $limittype => $code
170
        }, {
171
            group_by => [qw/fromBranch/]
172
        })->count == scalar(@holdingbranches) ? 0 : 1;
173
    }
174
175
    return 0;
176
}
177
88
=head3 article_request_type
178
=head3 article_request_type
89
179
90
my $type = $biblio->article_request_type( $borrower );
180
my $type = $biblio->article_request_type( $borrower );
(-)a/Koha/Item.pm (+4 lines)
Lines 211-216 Takes HASHref that can have the following parameters: Link Here
211
211
212
Returns 1 if item can be transferred to $to_library, otherwise 0.
212
Returns 1 if item can be transferred to $to_library, otherwise 0.
213
213
214
To find out whether at least one item of a Koha::Biblio can be transferred, please
215
see Koha::Biblio->can_be_transferred() instead of using this method for
216
multiple items of the same biblio.
217
214
=cut
218
=cut
215
219
216
sub can_be_transferred {
220
sub can_be_transferred {
(-)a/t/db_dependent/Koha/Biblios.t (-2 / +78 lines)
Lines 19-26 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 2;
22
use Test::More tests => 3;
23
23
24
use C4::Biblio;
25
use C4::Items;
24
use C4::Reserves;
26
use C4::Reserves;
25
27
26
use Koha::DateUtils qw( dt_from_string );
28
use Koha::DateUtils qw( dt_from_string );
Lines 80-84 subtest 'subscriptions' => sub { Link Here
80
    is( $subscriptions->count, 2, 'Koha::Biblio->subscriptions should return the correct number of subscriptions');
82
    is( $subscriptions->count, 2, 'Koha::Biblio->subscriptions should return the correct number of subscriptions');
81
};
83
};
82
84
85
subtest 'can_be_transferred' => sub {
86
    plan tests => 11;
87
88
    t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
89
    t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
90
91
    my $library1 = $builder->build( { source => 'Branch' } )->{branchcode};
92
    my $library2 = $builder->build( { source => 'Branch' } )->{branchcode};
93
    my $library3 = $builder->build( { source => 'Branch' } )->{branchcode};
94
    my ($bibnum, $title, $bibitemnum) = create_helper_biblio('ONLY1');
95
    my ($item_bibnum, $item_bibitemnum, $itemnumber)
96
        = AddItem({ homebranch => $library1, holdingbranch => $library1 }, $bibnum);
97
    my $item  = Koha::Items->find($itemnumber);
98
    my $biblio = Koha::Biblios->find($bibnum);
99
100
    is(Koha::Item::Transfer::Limits->search({
101
        fromBranch => $library1,
102
        toBranch => $library2,
103
    })->count, 0, 'There are no transfer limits between libraries.');
104
    ok($biblio->can_be_transferred({ to => $library2 }),
105
        'Some items of this biblio can be transferred between libraries.');
106
107
    my $limit = Koha::Item::Transfer::Limit->new({
108
        fromBranch => $library1,
109
        toBranch => $library2,
110
        itemtype => $item->effective_itemtype,
111
    })->store;
112
    is(Koha::Item::Transfer::Limits->search({
113
        fromBranch => $library1,
114
        toBranch => $library2,
115
    })->count, 1, 'Given we have added a transfer limit that applies for all '
116
        .'of this biblio\s items,');
117
    is($biblio->can_be_transferred({ to => $library2 }), 0,
118
        'None of the items of biblio can no longer be transferred between '
119
        .'libraries.');
120
    is($biblio->can_be_transferred({ to => $library2, from => $library1 }), 0,
121
         'We get the same result also if we pass the from-library parameter.');
122
    $item->holdingbranch($library2)->store;
123
    is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given one of the '
124
         .'items is already located at to-library, then the transfer is possible.');
125
    $item->holdingbranch($library1)->store;
126
    my ($item_bibnum2, $item_bibitemnum2, $itemnumber2)
127
        = AddItem({ homebranch => $library1, holdingbranch => $library3 }, $bibnum);
128
    my $item2  = Koha::Items->find($itemnumber2);
129
    is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given we added '
130
        .'another item that should have no transfer limits applying on, then '
131
        .'the transfer is possible.');
132
    $item2->holdingbranch($library1)->store;
133
    is($biblio->can_be_transferred({ to => $library2 }), 0, 'Given all of items'
134
        .' of the biblio are from same, transfer limited library, then transfer'
135
        .' is not possible.');
136
    eval { $biblio->can_be_transferred({ to => undef }); };
137
    is(ref($@), 'Koha::Exceptions::Library::NotFound', 'Exception thrown when no'
138
        .' library given.');
139
    eval { $biblio->can_be_transferred({ to => 'heaven' }); };
140
    is(ref($@), 'Koha::Exceptions::Library::NotFound', 'Exception thrown when'
141
        .' invalid library is given.');
142
    eval { $biblio->can_be_transferred({ to => $library2, from => 'hell' }); };
143
    is(ref($@), 'Koha::Exceptions::Library::NotFound', 'Exception thrown when'
144
        .' invalid library is given.');
145
};
146
83
$schema->storage->txn_rollback;
147
$schema->storage->txn_rollback;
84
148
85
- 
149
# Helper method to set up a Biblio.
150
sub create_helper_biblio {
151
    my $itemtype = shift;
152
    my ($bibnum, $title, $bibitemnum);
153
    my $bib = MARC::Record->new();
154
    $title = 'Silence in the library';
155
    $bib->append_fields(
156
        MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
157
        MARC::Field->new('245', ' ', ' ', a => $title),
158
        MARC::Field->new('942', ' ', ' ', c => $itemtype),
159
    );
160
    return ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
161
}

Return to bug 18072