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

(-)a/Koha/Exceptions/Library.pm (+17 lines)
Line 0 Link Here
1
package Koha::Exceptions::Library;
2
3
use Modern::Perl;
4
5
use Exception::Class (
6
7
    'Koha::Exceptions::Library::Exception' => {
8
        description => 'Something went wrong!',
9
    },
10
11
    'Koha::Exceptions::Library::NotFound' => {
12
        isa => 'Koha::Exceptions::Library::Exception',
13
        description => 'Library not found',
14
    },
15
);
16
17
1;
(-)a/Koha/Item.pm (-1 / +58 lines)
Lines 27-36 use Koha::DateUtils qw( dt_from_string ); Link Here
27
use C4::Context;
27
use C4::Context;
28
use Koha::Checkouts;
28
use Koha::Checkouts;
29
use Koha::IssuingRules;
29
use Koha::IssuingRules;
30
use Koha::Item::Transfer;
30
use Koha::Item::Transfer::Limits;
31
use Koha::Item::Transfers;
31
use Koha::Patrons;
32
use Koha::Patrons;
32
use Koha::Libraries;
33
use Koha::Libraries;
33
34
35
use Koha::Exceptions::Library;
36
34
use base qw(Koha::Object);
37
use base qw(Koha::Object);
35
38
36
=head1 NAME
39
=head1 NAME
Lines 189-194 sub can_article_request { Link Here
189
    return q{};
192
    return q{};
190
}
193
}
191
194
195
=head3 can_be_transferred
196
197
Checks if an item can be transferred to given library.
198
199
This feature is controlled by two system preferences:
200
UseBranchTransferLimits to enable / disable the feature
201
BranchTransferLimitsType to use either an itemnumber or ccode as an identifier
202
                         for setting the limitations
203
204
    MANDATORY PARAMETERS:
205
    $to   : Koha::Library or branchcode string
206
    OPTIONAL PARAMETERS:
207
    $from : Koha::Library or branchcode string  # if not given, item holdingbranch
208
                                                # will be used instead
209
210
=cut
211
212
sub can_be_transferred {
213
    my ($self, $to, $from) = @_;
214
215
    if (ref($to) ne 'Koha::Library') {
216
        my $tobranchcode = defined $to ? $to : '';
217
        $to = Koha::Libraries->find($tobranchcode);
218
        unless ($to) {
219
            Koha::Exceptions::Library::NotFound->throw(
220
                error => "Library '$tobranchcode' not found.",
221
            );
222
        }
223
    }
224
    if (defined $from && ref($from) ne 'Koha::Library') {
225
        my $frombranchcode = defined $from ? $from : '';
226
        $from = Koha::Libraries->find($frombranchcode);
227
        unless ($from) {
228
            Koha::Exceptions::Library::NotFound->throw(
229
                error => "Library '$frombranchcode' not found.",
230
            );
231
        }
232
    }
233
234
    $to = $to->branchcode;
235
    $from = defined $from ? $from->branchcode : $self->holdingbranch;
236
237
    return 1 if $from eq $to; # Transfer to current branch is allowed
238
    return 1 unless C4::Context->preference('UseBranchTransferLimits');
239
240
    my $limittype = C4::Context->preference('BranchTransferLimitsType');
241
    return Koha::Item::Transfer::Limits->search({
242
        toBranch => $to,
243
        fromBranch => $from,
244
        $limittype => $limittype eq 'itemtype'
245
                        ? $self->effective_itemtype : $self->ccode
246
    })->count ? 0 : 1;
247
}
248
192
=head3 article_request_type
249
=head3 article_request_type
193
250
194
my $type = $item->article_request_type( $borrower )
251
my $type = $item->article_request_type( $borrower )
(-)a/t/db_dependent/Koha/Items.t (-2 / +49 lines)
Lines 19-32 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 8;
22
use Test::More tests => 9;
23
23
24
use C4::Circulation;
24
use C4::Circulation;
25
use Koha::Item;
25
use Koha::Item;
26
use Koha::Item::Transfer::Limits;
26
use Koha::Items;
27
use Koha::Items;
27
use Koha::Database;
28
use Koha::Database;
28
29
29
use t::lib::TestBuilder;
30
use t::lib::TestBuilder;
31
use t::lib::Mocks;
30
32
31
my $schema = Koha::Database->new->schema;
33
my $schema = Koha::Database->new->schema;
32
$schema->storage->txn_begin;
34
$schema->storage->txn_begin;
Lines 118-123 subtest 'checkout' => sub { Link Here
118
    is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no *current* checkout on this item' );
120
    is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no *current* checkout on this item' );
119
};
121
};
120
122
123
subtest 'can_be_transferred' => sub {
124
    plan tests => 8;
125
126
    t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
127
    t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
128
129
    my $library1 = $builder->build( { source => 'Branch' } )->{branchcode};
130
    my $library2 = $builder->build( { source => 'Branch' } )->{branchcode};
131
    my $item  = Koha::Item->new({
132
        biblionumber     => $biblioitem->{biblionumber},
133
        biblioitemnumber => $biblioitem->{biblioitemnumber},
134
        homebranch       => $library1,
135
        holdingbranch    => $library1,
136
        itype            => 'test',
137
        barcode          => "newbarcode",
138
    })->store;
139
    $nb_of_items++;
140
141
    is(Koha::Item::Transfer::Limits->search({
142
        fromBranch => $library1,
143
        toBranch => $library2,
144
    })->count, 0, 'There are no transfer limits between libraries.');
145
    ok($item->can_be_transferred($library2),
146
       'Item can be transferred between libraries.');
147
148
    my $limit = Koha::Item::Transfer::Limit->new({
149
        fromBranch => $library1,
150
        toBranch => $library2,
151
        itemtype => $item->effective_itemtype,
152
    })->store;
153
    is(Koha::Item::Transfer::Limits->search({
154
        fromBranch => $library1,
155
        toBranch => $library2,
156
    })->count, 1, 'Given we have added a transfer limit,');
157
    is($item->can_be_transferred($library2), 0,
158
       'Item can no longer be transferred between libraries.');
159
    is($item->can_be_transferred($library2, $library1), 0,
160
       'We get the same result also if we pass the from-library parameter.');
161
    eval { $item->can_be_transferred(); };
162
    is(ref($@), 'Koha::Exceptions::Library::NotFound', 'Exception thrown when no library given.');
163
    eval { $item->can_be_transferred('heaven'); };
164
    is(ref($@), 'Koha::Exceptions::Library::NotFound', 'Exception thrown when invalid library is given.');
165
    eval { $item->can_be_transferred($library2, 'hell'); };
166
    is(ref($@), 'Koha::Exceptions::Library::NotFound', 'Exception thrown when invalid library is given.');
167
};
168
121
$retrieved_item_1->delete;
169
$retrieved_item_1->delete;
122
is( Koha::Items->search->count, $nb_of_items + 1, 'Delete should have deleted the item' );
170
is( Koha::Items->search->count, $nb_of_items + 1, 'Delete should have deleted the item' );
123
171
124
- 

Return to bug 18072