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 25-34 use Koha::Database; Link Here
25
25
26
use C4::Context;
26
use C4::Context;
27
use Koha::IssuingRules;
27
use Koha::IssuingRules;
28
use Koha::Item::Transfer;
28
use Koha::Item::Transfer::Limits;
29
use Koha::Item::Transfers;
29
use Koha::Patrons;
30
use Koha::Patrons;
30
use Koha::Libraries;
31
use Koha::Libraries;
31
32
33
use Koha::Exceptions::Library;
34
32
use base qw(Koha::Object);
35
use base qw(Koha::Object);
33
36
34
=head1 NAME
37
=head1 NAME
Lines 158-163 sub can_article_request { Link Here
158
    return q{};
161
    return q{};
159
}
162
}
160
163
164
=head3 can_be_transferred
165
166
Checks if an item can be transferred to given library.
167
168
This feature is controlled by two system preferences:
169
UseBranchTransferLimits to enable / disable the feature
170
BranchTransferLimitsType to use either an itemnumber or ccode as an identifier
171
                         for setting the limitations
172
173
    MANDATORY PARAMETERS:
174
    $to   : Koha::Library or branchcode string
175
    OPTIONAL PARAMETERS:
176
    $from : Koha::Library or branchcode string  # if not given, item holdingbranch
177
                                                # will be used instead
178
179
=cut
180
181
sub can_be_transferred {
182
    my ($self, $to, $from) = @_;
183
184
    if (ref($to) ne 'Koha::Library') {
185
        my $tobranchcode = defined $to ? $to : '';
186
        $to = Koha::Libraries->find($tobranchcode);
187
        unless ($to) {
188
            Koha::Exceptions::Library::NotFound->throw(
189
                error => "Library '$tobranchcode' not found.",
190
            );
191
        }
192
    }
193
    if (defined $from && ref($from) ne 'Koha::Library') {
194
        my $frombranchcode = defined $from ? $from : '';
195
        $from = Koha::Libraries->find($frombranchcode);
196
        unless ($from) {
197
            Koha::Exceptions::Library::NotFound->throw(
198
                error => "Library '$frombranchcode' not found.",
199
            );
200
        }
201
    }
202
203
    $to = $to->branchcode;
204
    $from = defined $from ? $from->branchcode : $self->holdingbranch;
205
206
    return 1 if $from eq $to; # Transfer to current branch is allowed
207
    return 1 unless C4::Context->preference('UseBranchTransferLimits');
208
209
    my $limittype = C4::Context->preference('BranchTransferLimitsType');
210
    return Koha::Item::Transfer::Limits->search({
211
        toBranch => $to,
212
        fromBranch => $from,
213
        $limittype => $limittype eq 'itemtype'
214
                        ? $self->effective_itemtype : $self->ccode
215
    })->count ? 0 : 1;
216
}
217
161
=head3 article_request_type
218
=head3 article_request_type
162
219
163
my $type = $item->article_request_type( $borrower )
220
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 => 6;
22
use Test::More tests => 7;
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 82-87 subtest 'biblio' => sub { Link Here
82
    is( $biblio->biblionumber, $retrieved_item_1->biblionumber, 'Koha::Item->biblio should return the correct biblio' );
84
    is( $biblio->biblionumber, $retrieved_item_1->biblionumber, 'Koha::Item->biblio should return the correct biblio' );
83
};
85
};
84
86
87
subtest 'can_be_transferred' => sub {
88
    plan tests => 8;
89
90
    t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
91
    t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
92
93
    my $library1 = $builder->build( { source => 'Branch' } )->{branchcode};
94
    my $library2 = $builder->build( { source => 'Branch' } )->{branchcode};
95
    my $item  = Koha::Item->new({
96
        biblionumber     => $biblioitem->{biblionumber},
97
        biblioitemnumber => $biblioitem->{biblioitemnumber},
98
        homebranch       => $library1,
99
        holdingbranch    => $library1,
100
        itype            => 'test',
101
        barcode          => "newbarcode",
102
    })->store;
103
    $nb_of_items++;
104
105
    is(Koha::Item::Transfer::Limits->search({
106
        fromBranch => $library1,
107
        toBranch => $library2,
108
    })->count, 0, 'There are no transfer limits between libraries.');
109
    ok($item->can_be_transferred($library2),
110
       'Item can be transferred between libraries.');
111
112
    my $limit = Koha::Item::Transfer::Limit->new({
113
        fromBranch => $library1,
114
        toBranch => $library2,
115
        itemtype => $item->effective_itemtype,
116
    })->store;
117
    is(Koha::Item::Transfer::Limits->search({
118
        fromBranch => $library1,
119
        toBranch => $library2,
120
    })->count, 1, 'Given we have added a transfer limit,');
121
    is($item->can_be_transferred($library2), 0,
122
       'Item can no longer be transferred between libraries.');
123
    is($item->can_be_transferred($library2, $library1), 0,
124
       'We get the same result also if we pass the from-library parameter.');
125
    eval { $item->can_be_transferred(); };
126
    is(ref($@), 'Koha::Exceptions::Library::NotFound', 'Exception thrown when no library given.');
127
    eval { $item->can_be_transferred('heaven'); };
128
    is(ref($@), 'Koha::Exceptions::Library::NotFound', 'Exception thrown when invalid library is given.');
129
    eval { $item->can_be_transferred($library2, 'hell'); };
130
    is(ref($@), 'Koha::Exceptions::Library::NotFound', 'Exception thrown when invalid library is given.');
131
};
132
85
$retrieved_item_1->delete;
133
$retrieved_item_1->delete;
86
is( Koha::Items->search->count, $nb_of_items + 1, 'Delete should have deleted the item' );
134
is( Koha::Items->search->count, $nb_of_items + 1, 'Delete should have deleted the item' );
87
135
88
- 

Return to bug 18072