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 99-104 sub can_article_request { Link Here
99
    return q{};
103
    return q{};
100
}
104
}
101
105
106
=head3 can_be_transferred
107
108
$biblio->can_be_transferred({ to => $to_library, from => $from_library })
109
110
Checks if at least one item of a biblio can be transferred to given library.
111
112
This feature is controlled by two system preferences:
113
UseBranchTransferLimits to enable / disable the feature
114
BranchTransferLimitsType to use either an itemnumber or ccode as an identifier
115
                         for setting the limitations
116
117
Performance-wise, it is recommended to use this method for a biblio instead of
118
iterating each item of a biblio with Koha::Item->can_be_transferred().
119
120
Takes HASHref that can have the following parameters:
121
    MANDATORY PARAMETERS:
122
    $to   : Koha::Library or branchcode string
123
    OPTIONAL PARAMETERS:
124
    $from : Koha::Library or branchcode string  # if given, only items from that
125
                                                # holdingbranch are considered
126
127
Returns 1 if at least one of the item of a biblio can be transferred
128
to $to_library, otherwise 0.
129
130
=cut
131
132
sub can_be_transferred {
133
    my ($self, $params) = @_;
134
135
    my $to = $params->{'to'};
136
    my $from = $params->{'from'};
137
    if (ref($to) ne 'Koha::Library') {
138
        my $tobranchcode = defined $to ? $to : '';
139
        $to = Koha::Libraries->find($tobranchcode);
140
        unless ($to) {
141
            Koha::Exceptions::Library::NotFound->throw(
142
                error => "Library '$tobranchcode' not found.",
143
            );
144
        }
145
    }
146
    if ($from && ref($from) ne 'Koha::Library') {
147
        my $frombranchcode = defined $from ? $from : '';
148
        $from = Koha::Libraries->find($frombranchcode);
149
        unless ($from) {
150
            Koha::Exceptions::Library::NotFound->throw(
151
                error => "Library '$frombranchcode' not found.",
152
            );
153
        }
154
    }
155
156
    return 1 unless C4::Context->preference('UseBranchTransferLimits');
157
    my $limittype = C4::Context->preference('BranchTransferLimitsType');
158
159
    my $items;
160
    foreach my $item_of_bib ($self->items) {
161
        next unless $item_of_bib->holdingbranch;
162
        next if $from && $from->branchcode ne $item_of_bib->holdingbranch;
163
        return 1 if $item_of_bib->holdingbranch eq $to->branchcode;
164
        my $code = $limittype eq 'itemtype'
165
            ? $item_of_bib->effective_itemtype
166
            : $item_of_bib->ccode;
167
        return 1 unless $code;
168
        $items->{$code}->{$item_of_bib->holdingbranch} = 1;
169
    }
170
171
    # At this point we will have a HASHref containing each itemtype/ccode that
172
    # this biblio has, inside which are all of the holdingbranches where those
173
    # items are located at. Then, we will query Koha::Item::Transfer::Limits to
174
    # find out whether a transfer limits for such $limittype from any of the
175
    # listed holdingbranches to the given $to library exist. If at least one
176
    # holdingbranch for that $limittype does not have a transfer limit to given
177
    # $to library, then we know that the transfer is possible.
178
    foreach my $code (keys %{$items}) {
179
        my @holdingbranches = keys %{$items->{$code}};
180
        return 1 if Koha::Item::Transfer::Limits->search({
181
            toBranch => $to->branchcode,
182
            fromBranch => { 'in' => \@holdingbranches },
183
            $limittype => $code
184
        }, {
185
            group_by => [qw/fromBranch/]
186
        })->count == scalar(@holdingbranches) ? 0 : 1;
187
    }
188
189
    return 0;
190
}
191
102
=head3 article_request_type
192
=head3 article_request_type
103
193
104
my $type = $biblio->article_request_type( $borrower );
194
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 (-1 / +77 lines)
Lines 21-26 use Modern::Perl; Link Here
21
21
22
use Test::More tests => 4;
22
use Test::More tests => 4;
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 output_pref );
28
use Koha::DateUtils qw( dt_from_string output_pref );
Lines 131-135 subtest 'waiting_or_in_transit' => sub { Link Here
131
    is($biblio->has_items_waiting_or_intransit, 1, 'Item has transfer');
133
    is($biblio->has_items_waiting_or_intransit, 1, 'Item has transfer');
132
};
134
};
133
135
136
subtest 'can_be_transferred' => sub {
137
    plan tests => 11;
138
139
    t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
140
    t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
141
142
    my $library1 = $builder->build( { source => 'Branch' } )->{branchcode};
143
    my $library2 = $builder->build( { source => 'Branch' } )->{branchcode};
144
    my $library3 = $builder->build( { source => 'Branch' } )->{branchcode};
145
    my ($bibnum, $title, $bibitemnum) = create_helper_biblio('ONLY1');
146
    my ($item_bibnum, $item_bibitemnum, $itemnumber)
147
        = AddItem({ homebranch => $library1, holdingbranch => $library1 }, $bibnum);
148
    my $item  = Koha::Items->find($itemnumber);
149
    my $biblio = Koha::Biblios->find($bibnum);
150
151
    is(Koha::Item::Transfer::Limits->search({
152
        fromBranch => $library1,
153
        toBranch => $library2,
154
    })->count, 0, 'There are no transfer limits between libraries.');
155
    ok($biblio->can_be_transferred({ to => $library2 }),
156
        'Some items of this biblio can be transferred between libraries.');
157
158
    my $limit = Koha::Item::Transfer::Limit->new({
159
        fromBranch => $library1,
160
        toBranch => $library2,
161
        itemtype => $item->effective_itemtype,
162
    })->store;
163
    is(Koha::Item::Transfer::Limits->search({
164
        fromBranch => $library1,
165
        toBranch => $library2,
166
    })->count, 1, 'Given we have added a transfer limit that applies for all '
167
        .'of this biblio\s items,');
168
    is($biblio->can_be_transferred({ to => $library2 }), 0,
169
        'None of the items of biblio can no longer be transferred between '
170
        .'libraries.');
171
    is($biblio->can_be_transferred({ to => $library2, from => $library1 }), 0,
172
         'We get the same result also if we pass the from-library parameter.');
173
    $item->holdingbranch($library2)->store;
174
    is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given one of the '
175
         .'items is already located at to-library, then the transfer is possible.');
176
    $item->holdingbranch($library1)->store;
177
    my ($item_bibnum2, $item_bibitemnum2, $itemnumber2)
178
        = AddItem({ homebranch => $library1, holdingbranch => $library3 }, $bibnum);
179
    my $item2  = Koha::Items->find($itemnumber2);
180
    is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given we added '
181
        .'another item that should have no transfer limits applying on, then '
182
        .'the transfer is possible.');
183
    $item2->holdingbranch($library1)->store;
184
    is($biblio->can_be_transferred({ to => $library2 }), 0, 'Given all of items'
185
        .' of the biblio are from same, transfer limited library, then transfer'
186
        .' is not possible.');
187
    eval { $biblio->can_be_transferred({ to => undef }); };
188
    is(ref($@), 'Koha::Exceptions::Library::NotFound', 'Exception thrown when no'
189
        .' library given.');
190
    eval { $biblio->can_be_transferred({ to => 'heaven' }); };
191
    is(ref($@), 'Koha::Exceptions::Library::NotFound', 'Exception thrown when'
192
        .' invalid library is given.');
193
    eval { $biblio->can_be_transferred({ to => $library2, from => 'hell' }); };
194
    is(ref($@), 'Koha::Exceptions::Library::NotFound', 'Exception thrown when'
195
        .' invalid library is given.');
196
};
197
134
$schema->storage->txn_rollback;
198
$schema->storage->txn_rollback;
135
199
136
- 
200
# Helper method to set up a Biblio.
201
sub create_helper_biblio {
202
    my $itemtype = shift;
203
    my ($bibnum, $title, $bibitemnum);
204
    my $bib = MARC::Record->new();
205
    $title = 'Silence in the library';
206
    $bib->append_fields(
207
        MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
208
        MARC::Field->new('245', ' ', ' ', a => $title),
209
        MARC::Field->new('942', ' ', ' ', c => $itemtype),
210
    );
211
    return ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
212
}

Return to bug 18072