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

(-)a/Koha/Biblio.pm (-25 / +5 lines)
Lines 37-44 use Koha::Item::Transfer::Limits; Link Here
37
use Koha::Libraries;
37
use Koha::Libraries;
38
use Koha::Subscriptions;
38
use Koha::Subscriptions;
39
39
40
use Koha::Exceptions::Library;
41
42
=head1 NAME
40
=head1 NAME
43
41
44
Koha::Biblio - Koha Biblio Object class
42
Koha::Biblio - Koha Biblio Object class
Lines 119-128 iterating each item of a biblio with Koha::Item->can_be_transferred(). Link Here
119
117
120
Takes HASHref that can have the following parameters:
118
Takes HASHref that can have the following parameters:
121
    MANDATORY PARAMETERS:
119
    MANDATORY PARAMETERS:
122
    $to   : Koha::Library or branchcode string
120
    $to   : Koha::Library
123
    OPTIONAL PARAMETERS:
121
    OPTIONAL PARAMETERS:
124
    $from : Koha::Library or branchcode string  # if given, only items from that
122
    $from : Koha::Library # if given, only items from that
125
                                                # holdingbranch are considered
123
                          # holdingbranch are considered
126
124
127
Returns 1 if at least one of the item of a biblio can be transferred
125
Returns 1 if at least one of the item of a biblio can be transferred
128
to $to_library, otherwise 0.
126
to $to_library, otherwise 0.
Lines 132-157 to $to_library, otherwise 0. Link Here
132
sub can_be_transferred {
130
sub can_be_transferred {
133
    my ($self, $params) = @_;
131
    my ($self, $params) = @_;
134
132
135
    my $to = $params->{'to'};
133
    my $to   = $params->{to};
136
    my $from = $params->{'from'};
134
    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
135
156
    return 1 unless C4::Context->preference('UseBranchTransferLimits');
136
    return 1 unless C4::Context->preference('UseBranchTransferLimits');
157
    my $limittype = C4::Context->preference('BranchTransferLimitsType');
137
    my $limittype = C4::Context->preference('BranchTransferLimitsType');
(-)a/Koha/Item.pm (-26 / +6 lines)
Lines 32-39 use Koha::Item::Transfers; Link Here
32
use Koha::Patrons;
32
use Koha::Patrons;
33
use Koha::Libraries;
33
use Koha::Libraries;
34
34
35
use Koha::Exceptions::Library;
36
37
use base qw(Koha::Object);
35
use base qw(Koha::Object);
38
36
39
=head1 NAME
37
=head1 NAME
Lines 204-213 BranchTransferLimitsType to use either an itemnumber or ccode as an identifier Link Here
204
202
205
Takes HASHref that can have the following parameters:
203
Takes HASHref that can have the following parameters:
206
    MANDATORY PARAMETERS:
204
    MANDATORY PARAMETERS:
207
    $to   : Koha::Library or branchcode string
205
    $to   : Koha::Library
208
    OPTIONAL PARAMETERS:
206
    OPTIONAL PARAMETERS:
209
    $from : Koha::Library or branchcode string  # if not given, item holdingbranch
207
    $from : Koha::Library  # if not given, item holdingbranch
210
                                                # will be used instead
208
                           # will be used instead
211
209
212
Returns 1 if item can be transferred to $to_library, otherwise 0.
210
Returns 1 if item can be transferred to $to_library, otherwise 0.
213
211
Lines 220-247 multiple items of the same biblio. Link Here
220
sub can_be_transferred {
218
sub can_be_transferred {
221
    my ($self, $params) = @_;
219
    my ($self, $params) = @_;
222
220
223
    my $to = $params->{'to'};
221
    my $to   = $params->{to};
224
    my $from = $params->{'from'};
222
    my $from = $params->{from};
225
    if (ref($to) ne 'Koha::Library') {
226
        my $tobranchcode = defined $to ? $to : '';
227
        $to = Koha::Libraries->find($tobranchcode);
228
        unless ($to) {
229
            Koha::Exceptions::Library::NotFound->throw(
230
                error => "Library '$tobranchcode' not found.",
231
            );
232
        }
233
    }
234
    if (defined $from && ref($from) ne 'Koha::Library') {
235
        my $frombranchcode = defined $from ? $from : '';
236
        $from = Koha::Libraries->find($frombranchcode);
237
        unless ($from) {
238
            Koha::Exceptions::Library::NotFound->throw(
239
                error => "Library '$frombranchcode' not found.",
240
            );
241
        }
242
    }
243
223
244
    $to = $to->branchcode;
224
    $to   = $to->branchcode;
245
    $from = defined $from ? $from->branchcode : $self->holdingbranch;
225
    $from = defined $from ? $from->branchcode : $self->holdingbranch;
246
226
247
    return 1 if $from eq $to; # Transfer to current branch is allowed
227
    return 1 if $from eq $to; # Transfer to current branch is allowed
(-)a/t/db_dependent/Koha/Biblios.t (-24 / +15 lines)
Lines 135-169 subtest 'waiting_or_in_transit' => sub { Link Here
135
};
135
};
136
136
137
subtest 'can_be_transferred' => sub {
137
subtest 'can_be_transferred' => sub {
138
    plan tests => 11;
138
    plan tests => 8;
139
139
140
    t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
140
    t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
141
    t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
141
    t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
142
142
143
    my $library1 = $builder->build( { source => 'Branch' } )->{branchcode};
143
    my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
144
    my $library2 = $builder->build( { source => 'Branch' } )->{branchcode};
144
    my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
145
    my $library3 = $builder->build( { source => 'Branch' } )->{branchcode};
145
    my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
146
    my ($bibnum, $title, $bibitemnum) = create_helper_biblio('ONLY1');
146
    my ($bibnum, $title, $bibitemnum) = create_helper_biblio('ONLY1');
147
    my ($item_bibnum, $item_bibitemnum, $itemnumber)
147
    my ($item_bibnum, $item_bibitemnum, $itemnumber)
148
        = AddItem({ homebranch => $library1, holdingbranch => $library1 }, $bibnum);
148
        = AddItem({ homebranch => $library1->branchcode, holdingbranch => $library1->branchcode }, $bibnum);
149
    my $item  = Koha::Items->find($itemnumber);
149
    my $item  = Koha::Items->find($itemnumber);
150
    my $biblio = Koha::Biblios->find($bibnum);
150
    my $biblio = Koha::Biblios->find($bibnum);
151
151
152
    is(Koha::Item::Transfer::Limits->search({
152
    is(Koha::Item::Transfer::Limits->search({
153
        fromBranch => $library1,
153
        fromBranch => $library1->branchcode,
154
        toBranch => $library2,
154
        toBranch => $library2->branchcode,
155
    })->count, 0, 'There are no transfer limits between libraries.');
155
    })->count, 0, 'There are no transfer limits between libraries.');
156
    ok($biblio->can_be_transferred({ to => $library2 }),
156
    ok($biblio->can_be_transferred({ to => $library2 }),
157
        'Some items of this biblio can be transferred between libraries.');
157
        'Some items of this biblio can be transferred between libraries.');
158
158
159
    my $limit = Koha::Item::Transfer::Limit->new({
159
    my $limit = Koha::Item::Transfer::Limit->new({
160
        fromBranch => $library1,
160
        fromBranch => $library1->branchcode,
161
        toBranch => $library2,
161
        toBranch => $library2->branchcode,
162
        itemtype => $item->effective_itemtype,
162
        itemtype => $item->effective_itemtype,
163
    })->store;
163
    })->store;
164
    is(Koha::Item::Transfer::Limits->search({
164
    is(Koha::Item::Transfer::Limits->search({
165
        fromBranch => $library1,
165
        fromBranch => $library1->branchcode,
166
        toBranch => $library2,
166
        toBranch => $library2->branchcode,
167
    })->count, 1, 'Given we have added a transfer limit that applies for all '
167
    })->count, 1, 'Given we have added a transfer limit that applies for all '
168
        .'of this biblio\s items,');
168
        .'of this biblio\s items,');
169
    is($biblio->can_be_transferred({ to => $library2 }), 0,
169
    is($biblio->can_be_transferred({ to => $library2 }), 0,
Lines 171-199 subtest 'can_be_transferred' => sub { Link Here
171
        .'libraries.');
171
        .'libraries.');
172
    is($biblio->can_be_transferred({ to => $library2, from => $library1 }), 0,
172
    is($biblio->can_be_transferred({ to => $library2, from => $library1 }), 0,
173
         'We get the same result also if we pass the from-library parameter.');
173
         'We get the same result also if we pass the from-library parameter.');
174
    $item->holdingbranch($library2)->store;
174
    $item->holdingbranch($library2->branchcode)->store;
175
    is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given one of the '
175
    is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given one of the '
176
         .'items is already located at to-library, then the transfer is possible.');
176
         .'items is already located at to-library, then the transfer is possible.');
177
    $item->holdingbranch($library1)->store;
177
    $item->holdingbranch($library1->branchcode)->store;
178
    my ($item_bibnum2, $item_bibitemnum2, $itemnumber2)
178
    my ($item_bibnum2, $item_bibitemnum2, $itemnumber2)
179
        = AddItem({ homebranch => $library1, holdingbranch => $library3 }, $bibnum);
179
        = AddItem({ homebranch => $library1->branchcode, holdingbranch => $library3->branchcode }, $bibnum);
180
    my $item2  = Koha::Items->find($itemnumber2);
180
    my $item2  = Koha::Items->find($itemnumber2);
181
    is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given we added '
181
    is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given we added '
182
        .'another item that should have no transfer limits applying on, then '
182
        .'another item that should have no transfer limits applying on, then '
183
        .'the transfer is possible.');
183
        .'the transfer is possible.');
184
    $item2->holdingbranch($library1)->store;
184
    $item2->holdingbranch($library1->branchcode)->store;
185
    is($biblio->can_be_transferred({ to => $library2 }), 0, 'Given all of items'
185
    is($biblio->can_be_transferred({ to => $library2 }), 0, 'Given all of items'
186
        .' of the biblio are from same, transfer limited library, then transfer'
186
        .' of the biblio are from same, transfer limited library, then transfer'
187
        .' is not possible.');
187
        .' is not possible.');
188
    throws_ok { $biblio->can_be_transferred({ to => undef }); }
189
              'Koha::Exceptions::Library::NotFound',
190
              'Exception thrown when no library given.';
191
    throws_ok { $biblio->can_be_transferred({ to => 'heaven' }); }
192
              'Koha::Exceptions::Library::NotFound',
193
              'Exception thrown when invalid library is given.';
194
    throws_ok { $biblio->can_be_transferred({ to => $library2, from => 'hell' }); }
195
              'Koha::Exceptions::Library::NotFound',
196
              'Exception thrown when invalid library is given.';
197
};
188
};
198
189
199
$schema->storage->txn_rollback;
190
$schema->storage->txn_rollback;
(-)a/t/db_dependent/Koha/Items.t (-21 / +11 lines)
Lines 122-173 subtest 'checkout' => sub { Link Here
122
};
122
};
123
123
124
subtest 'can_be_transferred' => sub {
124
subtest 'can_be_transferred' => sub {
125
    plan tests => 8;
125
    plan tests => 5;
126
126
127
    t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
127
    t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
128
    t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
128
    t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
129
129
130
    my $library1 = $builder->build( { source => 'Branch' } )->{branchcode};
130
    my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
131
    my $library2 = $builder->build( { source => 'Branch' } )->{branchcode};
131
    my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
132
    my $item  = Koha::Item->new({
132
    my $item  = Koha::Item->new({
133
        biblionumber     => $biblioitem->{biblionumber},
133
        biblionumber     => $biblioitem->{biblionumber},
134
        biblioitemnumber => $biblioitem->{biblioitemnumber},
134
        biblioitemnumber => $biblioitem->{biblioitemnumber},
135
        homebranch       => $library1,
135
        homebranch       => $library1->branchcode,
136
        holdingbranch    => $library1,
136
        holdingbranch    => $library1->branchcode,
137
        itype            => 'test',
137
        itype            => 'test',
138
        barcode          => "newbarcode",
138
        barcode          => "newbarcode",
139
    })->store;
139
    })->store;
140
    $nb_of_items++;
140
    $nb_of_items++;
141
141
142
    is(Koha::Item::Transfer::Limits->search({
142
    is(Koha::Item::Transfer::Limits->search({
143
        fromBranch => $library1,
143
        fromBranch => $library1->branchcode,
144
        toBranch => $library2,
144
        toBranch => $library2->branchcode,
145
    })->count, 0, 'There are no transfer limits between libraries.');
145
    })->count, 0, 'There are no transfer limits between libraries.');
146
    ok($item->can_be_transferred({ to => $library2 }),
146
    ok($item->can_be_transferred({ to => $library2 }),
147
       'Item can be transferred between libraries.');
147
       'Item can be transferred between libraries.');
148
148
149
    my $limit = Koha::Item::Transfer::Limit->new({
149
    my $limit = Koha::Item::Transfer::Limit->new({
150
        fromBranch => $library1,
150
        fromBranch => $library1->branchcode,
151
        toBranch => $library2,
151
        toBranch => $library2->branchcode,
152
        itemtype => $item->effective_itemtype,
152
        itemtype => $item->effective_itemtype,
153
    })->store;
153
    })->store;
154
    is(Koha::Item::Transfer::Limits->search({
154
    is(Koha::Item::Transfer::Limits->search({
155
        fromBranch => $library1,
155
        fromBranch => $library1->branchcode,
156
        toBranch => $library2,
156
        toBranch => $library2->branchcode,
157
    })->count, 1, 'Given we have added a transfer limit,');
157
    })->count, 1, 'Given we have added a transfer limit,');
158
    is($item->can_be_transferred({ to => $library2 }), 0,
158
    is($item->can_be_transferred({ to => $library2 }), 0,
159
       'Item can no longer be transferred between libraries.');
159
       'Item can no longer be transferred between libraries.');
160
    is($item->can_be_transferred({ to => $library2, from => $library1 }), 0,
160
    is($item->can_be_transferred({ to => $library2, from => $library1 }), 0,
161
       'We get the same result also if we pass the from-library parameter.');
161
       'We get the same result also if we pass the from-library parameter.');
162
    throws_ok { $item->can_be_transferred({ to => undef }); }
163
              'Koha::Exceptions::Library::NotFound',
164
              'Exception thrown when no library given.';
165
    throws_ok { $item->can_be_transferred({ to => 'heaven' }); }
166
              'Koha::Exceptions::Library::NotFound',
167
              'Exception thrown when invalid library is given.';
168
    throws_ok { $item->can_be_transferred({ to => $library2, from => 'hell' }); }
169
              'Koha::Exceptions::Library::NotFound',
170
              'Exception thrown when invalid library is given.';
171
};
162
};
172
163
173
$retrieved_item_1->delete;
164
$retrieved_item_1->delete;
174
- 

Return to bug 18072