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 / +53 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 a transfer of an item from library to another is allowed.
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
    $from : Koha::Library or branchcode string
161
    $to   : Koha::Library or branchcode string
162
163
=cut
164
165
sub can_be_transferred {
166
    my ($self, $from, $to) = @_;
167
168
    if (ref($from) ne 'Koha::Library') {
169
        my $frombranchcode = $from;
170
        $from = Koha::Libraries->find($frombranchcode);
171
        unless ($from) {
172
            Koha::Exceptions::Library::NotFound->throw(
173
                error => "Library '$frombranchcode' not found.",
174
            );
175
        }
176
    }
177
    if (ref($to) ne 'Koha::Library') {
178
        my $tobranchcode = $to;
179
        $to = Koha::Libraries->find($tobranchcode);
180
        unless ($to) {
181
            Koha::Exceptions::Library::NotFound->throw(
182
                error => "Library '$tobranchcode' not found.",
183
            );
184
        }
185
    }
186
187
    return 1 if $from->branchcode eq $to->branchcode; # Transfer to current branch is allowed
188
    return 1 unless C4::Context->preference('UseBranchTransferLimits');
189
190
    my $limittype = C4::Context->preference('BranchTransferLimitsType');
191
    return Koha::Item::Transfer::Limits->search({
192
        toBranch => $to->branchcode,
193
        fromBranch => $from->branchcode,
194
        $limittype => $limittype eq 'itemtype'
195
                        ? $self->effective_itemtype : $self->ccode
196
    })->count ? 0 : 1;
197
}
198
147
=head3 article_request_type
199
=head3 article_request_type
148
200
149
my $type = $item->article_request_type( $borrower )
201
my $type = $item->article_request_type( $borrower )
(-)a/t/db_dependent/Koha/Items.t (-2 / +41 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 => 4;
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($library1, $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($library1, $library2), 0,
114
       'Item can no longer be transferred between libraries.');
115
};
116
77
$retrieved_item_1->delete;
117
$retrieved_item_1->delete;
78
is( Koha::Items->search->count, $nb_of_items + 1, 'Delete should have deleted the item' );
118
is( Koha::Items->search->count, $nb_of_items + 1, 'Delete should have deleted the item' );
79
119
80
- 

Return to bug 18072