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 105-114 iterating each item of a biblio with Koha::Item->can_be_transferred(). Link Here
105
103
106
Takes HASHref that can have the following parameters:
104
Takes HASHref that can have the following parameters:
107
    MANDATORY PARAMETERS:
105
    MANDATORY PARAMETERS:
108
    $to   : Koha::Library or branchcode string
106
    $to   : Koha::Library
109
    OPTIONAL PARAMETERS:
107
    OPTIONAL PARAMETERS:
110
    $from : Koha::Library or branchcode string  # if given, only items from that
108
    $from : Koha::Library # if given, only items from that
111
                                                # holdingbranch are considered
109
                          # holdingbranch are considered
112
110
113
Returns 1 if at least one of the item of a biblio can be transferred
111
Returns 1 if at least one of the item of a biblio can be transferred
114
to $to_library, otherwise 0.
112
to $to_library, otherwise 0.
Lines 118-143 to $to_library, otherwise 0. Link Here
118
sub can_be_transferred {
116
sub can_be_transferred {
119
    my ($self, $params) = @_;
117
    my ($self, $params) = @_;
120
118
121
    my $to = $params->{'to'};
119
    my $to   = $params->{to};
122
    my $from = $params->{'from'};
120
    my $from = $params->{from};
123
    if (ref($to) ne 'Koha::Library') {
124
        my $tobranchcode = defined $to ? $to : '';
125
        $to = Koha::Libraries->find($tobranchcode);
126
        unless ($to) {
127
            Koha::Exceptions::Library::NotFound->throw(
128
                error => "Library '$tobranchcode' not found.",
129
            );
130
        }
131
    }
132
    if ($from && ref($from) ne 'Koha::Library') {
133
        my $frombranchcode = defined $from ? $from : '';
134
        $from = Koha::Libraries->find($frombranchcode);
135
        unless ($from) {
136
            Koha::Exceptions::Library::NotFound->throw(
137
                error => "Library '$frombranchcode' not found.",
138
            );
139
        }
140
    }
141
121
142
    return 1 unless C4::Context->preference('UseBranchTransferLimits');
122
    return 1 unless C4::Context->preference('UseBranchTransferLimits');
143
    my $limittype = C4::Context->preference('BranchTransferLimitsType');
123
    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 84-118 subtest 'subscriptions' => sub { Link Here
84
};
84
};
85
85
86
subtest 'can_be_transferred' => sub {
86
subtest 'can_be_transferred' => sub {
87
    plan tests => 11;
87
    plan tests => 8;
88
88
89
    t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
89
    t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
90
    t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
90
    t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
91
91
92
    my $library1 = $builder->build( { source => 'Branch' } )->{branchcode};
92
    my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
93
    my $library2 = $builder->build( { source => 'Branch' } )->{branchcode};
93
    my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
94
    my $library3 = $builder->build( { source => 'Branch' } )->{branchcode};
94
    my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
95
    my ($bibnum, $title, $bibitemnum) = create_helper_biblio('ONLY1');
95
    my ($bibnum, $title, $bibitemnum) = create_helper_biblio('ONLY1');
96
    my ($item_bibnum, $item_bibitemnum, $itemnumber)
96
    my ($item_bibnum, $item_bibitemnum, $itemnumber)
97
        = AddItem({ homebranch => $library1, holdingbranch => $library1 }, $bibnum);
97
        = AddItem({ homebranch => $library1->branchcode, holdingbranch => $library1->branchcode }, $bibnum);
98
    my $item  = Koha::Items->find($itemnumber);
98
    my $item  = Koha::Items->find($itemnumber);
99
    my $biblio = Koha::Biblios->find($bibnum);
99
    my $biblio = Koha::Biblios->find($bibnum);
100
100
101
    is(Koha::Item::Transfer::Limits->search({
101
    is(Koha::Item::Transfer::Limits->search({
102
        fromBranch => $library1,
102
        fromBranch => $library1->branchcode,
103
        toBranch => $library2,
103
        toBranch => $library2->branchcode,
104
    })->count, 0, 'There are no transfer limits between libraries.');
104
    })->count, 0, 'There are no transfer limits between libraries.');
105
    ok($biblio->can_be_transferred({ to => $library2 }),
105
    ok($biblio->can_be_transferred({ to => $library2 }),
106
        'Some items of this biblio can be transferred between libraries.');
106
        'Some items of this biblio can be transferred between libraries.');
107
107
108
    my $limit = Koha::Item::Transfer::Limit->new({
108
    my $limit = Koha::Item::Transfer::Limit->new({
109
        fromBranch => $library1,
109
        fromBranch => $library1->branchcode,
110
        toBranch => $library2,
110
        toBranch => $library2->branchcode,
111
        itemtype => $item->effective_itemtype,
111
        itemtype => $item->effective_itemtype,
112
    })->store;
112
    })->store;
113
    is(Koha::Item::Transfer::Limits->search({
113
    is(Koha::Item::Transfer::Limits->search({
114
        fromBranch => $library1,
114
        fromBranch => $library1->branchcode,
115
        toBranch => $library2,
115
        toBranch => $library2->branchcode,
116
    })->count, 1, 'Given we have added a transfer limit that applies for all '
116
    })->count, 1, 'Given we have added a transfer limit that applies for all '
117
        .'of this biblio\s items,');
117
        .'of this biblio\s items,');
118
    is($biblio->can_be_transferred({ to => $library2 }), 0,
118
    is($biblio->can_be_transferred({ to => $library2 }), 0,
Lines 120-148 subtest 'can_be_transferred' => sub { Link Here
120
        .'libraries.');
120
        .'libraries.');
121
    is($biblio->can_be_transferred({ to => $library2, from => $library1 }), 0,
121
    is($biblio->can_be_transferred({ to => $library2, from => $library1 }), 0,
122
         'We get the same result also if we pass the from-library parameter.');
122
         'We get the same result also if we pass the from-library parameter.');
123
    $item->holdingbranch($library2)->store;
123
    $item->holdingbranch($library2->branchcode)->store;
124
    is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given one of the '
124
    is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given one of the '
125
         .'items is already located at to-library, then the transfer is possible.');
125
         .'items is already located at to-library, then the transfer is possible.');
126
    $item->holdingbranch($library1)->store;
126
    $item->holdingbranch($library1->branchcode)->store;
127
    my ($item_bibnum2, $item_bibitemnum2, $itemnumber2)
127
    my ($item_bibnum2, $item_bibitemnum2, $itemnumber2)
128
        = AddItem({ homebranch => $library1, holdingbranch => $library3 }, $bibnum);
128
        = AddItem({ homebranch => $library1->branchcode, holdingbranch => $library3->branchcode }, $bibnum);
129
    my $item2  = Koha::Items->find($itemnumber2);
129
    my $item2  = Koha::Items->find($itemnumber2);
130
    is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given we added '
130
    is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given we added '
131
        .'another item that should have no transfer limits applying on, then '
131
        .'another item that should have no transfer limits applying on, then '
132
        .'the transfer is possible.');
132
        .'the transfer is possible.');
133
    $item2->holdingbranch($library1)->store;
133
    $item2->holdingbranch($library1->branchcode)->store;
134
    is($biblio->can_be_transferred({ to => $library2 }), 0, 'Given all of items'
134
    is($biblio->can_be_transferred({ to => $library2 }), 0, 'Given all of items'
135
        .' of the biblio are from same, transfer limited library, then transfer'
135
        .' of the biblio are from same, transfer limited library, then transfer'
136
        .' is not possible.');
136
        .' is not possible.');
137
    throws_ok { $biblio->can_be_transferred({ to => undef }); }
138
              'Koha::Exceptions::Library::NotFound',
139
              'Exception thrown when no library given.';
140
    throws_ok { $biblio->can_be_transferred({ to => 'heaven' }); }
141
              'Koha::Exceptions::Library::NotFound',
142
              'Exception thrown when invalid library is given.';
143
    throws_ok { $biblio->can_be_transferred({ to => $library2, from => 'hell' }); }
144
              'Koha::Exceptions::Library::NotFound',
145
              'Exception thrown when invalid library is given.';
146
};
137
};
147
138
148
$schema->storage->txn_rollback;
139
$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