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 144-149 sub can_article_request { Link Here
144
    return q{};
147
    return q{};
145
}
148
}
146
149
150
=head3 can_be_transferred
151
152
Checks if an item can be transferred to given library.
153
154
This feature is controlled by two system preferences:
155
UseBranchTransferLimits to enable / disable the feature
156
BranchTransferLimitsType to use either an itemnumber or ccode as an identifier
157
                         for setting the limitations
158
159
    MANDATORY PARAMETERS:
160
    $to   : Koha::Library or branchcode string
161
    OPTIONAL PARAMETERS:
162
    $from : Koha::Library or branchcode string  # if not given, item holdingbranch
163
                                                # will be used instead
164
165
=cut
166
167
sub can_be_transferred {
168
    my ($self, $to, $from) = @_;
169
170
    if (ref($to) ne 'Koha::Library') {
171
        my $tobranchcode = defined $to ? $to : '';
172
        $to = Koha::Libraries->find($tobranchcode);
173
        unless ($to) {
174
            Koha::Exceptions::Library::NotFound->throw(
175
                error => "Library '$tobranchcode' not found.",
176
            );
177
        }
178
    }
179
    if (defined $from && ref($from) ne 'Koha::Library') {
180
        my $frombranchcode = defined $from ? $from : '';
181
        $from = Koha::Libraries->find($frombranchcode);
182
        unless ($from) {
183
            Koha::Exceptions::Library::NotFound->throw(
184
                error => "Library '$frombranchcode' not found.",
185
            );
186
        }
187
    }
188
189
    $to = $to->branchcode;
190
    $from = defined $from ? $from->branchcode : $self->holdingbranch;
191
192
    return 1 if $from eq $to; # Transfer to current branch is allowed
193
    return 1 unless C4::Context->preference('UseBranchTransferLimits');
194
195
    my $limittype = C4::Context->preference('BranchTransferLimitsType');
196
    return Koha::Item::Transfer::Limits->search({
197
        toBranch => $to,
198
        fromBranch => $from,
199
        $limittype => $limittype eq 'itemtype'
200
                        ? $self->effective_itemtype : $self->ccode
201
    })->count ? 0 : 1;
202
}
203
147
=head3 article_request_type
204
=head3 article_request_type
148
205
149
my $type = $item->article_request_type( $borrower )
206
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 => 5;
22
use Test::More tests => 6;
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 74-79 subtest 'get_transfer' => sub { Link Here
74
    is( $transfer->itemnumber, $new_item_1->itemnumber, 'Koha::Item->get_transfer should return a valid Koha::Item::Transfers object' );
76
    is( $transfer->itemnumber, $new_item_1->itemnumber, 'Koha::Item->get_transfer should return a valid Koha::Item::Transfers object' );
75
};
77
};
76
78
79
subtest 'can_be_transferred' => sub {
80
    plan tests => 8;
81
82
    t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
83
    t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
84
85
    my $library1 = $builder->build( { source => 'Branch' } )->{branchcode};
86
    my $library2 = $builder->build( { source => 'Branch' } )->{branchcode};
87
    my $item  = Koha::Item->new({
88
        biblionumber     => $biblioitem->{biblionumber},
89
        biblioitemnumber => $biblioitem->{biblioitemnumber},
90
        homebranch       => $library1,
91
        holdingbranch    => $library1,
92
        itype            => 'test',
93
        barcode          => "newbarcode",
94
    })->store;
95
    $nb_of_items++;
96
97
    is(Koha::Item::Transfer::Limits->search({
98
        fromBranch => $library1,
99
        toBranch => $library2,
100
    })->count, 0, 'There are no transfer limits between libraries.');
101
    ok($item->can_be_transferred($library2),
102
       'Item can be transferred between libraries.');
103
104
    my $limit = Koha::Item::Transfer::Limit->new({
105
        fromBranch => $library1,
106
        toBranch => $library2,
107
        itemtype => $item->effective_itemtype,
108
    })->store;
109
    is(Koha::Item::Transfer::Limits->search({
110
        fromBranch => $library1,
111
        toBranch => $library2,
112
    })->count, 1, 'Given we have added a transfer limit,');
113
    is($item->can_be_transferred($library2), 0,
114
       'Item can no longer be transferred between libraries.');
115
    is($item->can_be_transferred($library2, $library1), 0,
116
       'We get the same result also if we pass the from-library parameter.');
117
    eval { $item->can_be_transferred(); };
118
    is(ref($@), 'Koha::Exceptions::Library::NotFound', 'Exception thrown when no library given.');
119
    eval { $item->can_be_transferred('heaven'); };
120
    is(ref($@), 'Koha::Exceptions::Library::NotFound', 'Exception thrown when invalid library is given.');
121
    eval { $item->can_be_transferred($library2, 'hell'); };
122
    is(ref($@), 'Koha::Exceptions::Library::NotFound', 'Exception thrown when invalid library is given.');
123
};
124
77
$retrieved_item_1->delete;
125
$retrieved_item_1->delete;
78
is( Koha::Items->search->count, $nb_of_items + 1, 'Delete should have deleted the item' );
126
is( Koha::Items->search->count, $nb_of_items + 1, 'Delete should have deleted the item' );
79
127
80
- 

Return to bug 18072