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

(-)a/Koha/Libraries.pm (+72 lines)
Lines 23-29 use Carp; Link Here
23
23
24
use C4::Context;
24
use C4::Context;
25
25
26
use Koha::Biblios;
26
use Koha::Database;
27
use Koha::Database;
28
use Koha::Item::Transfer::Limits;
29
use Koha::Items;
27
use Koha::Library;
30
use Koha::Library;
28
31
29
use base qw(Koha::Objects);
32
use base qw(Koha::Objects);
Lines 38-43 Koha::Libraries - Koha Library Object set class Link Here
38
41
39
=cut
42
=cut
40
43
44
=head3 pickup_locations
45
46
Returns available pickup locations for
47
    A. a specific item
48
    B. a biblio
49
    C. none of the above, simply all libraries with pickup_location => 1
50
51
This method determines the pickup location by two factors:
52
    1. is the library configured as pickup location
53
    2. can a specific item / at least one of the items of a biblio be transferred
54
       into the library
55
56
OPTIONAL PARAMETERS:
57
    item   # Koha::Item object / itemnumber, find pickup locations for item
58
    biblio # Koha::Biblio object / biblionumber, find pickup locations for biblio
59
60
If no parameters are given, all libraries with pickup_location => 1 are returned.
61
62
=cut
63
64
sub pickup_locations {
65
    my ($self, $params) = @_;
66
67
    my $item = $params->{'item'};
68
    my $biblio = $params->{'biblio'};
69
    if ($biblio && $item) {
70
        Koha::Exceptions::BadParameter->throw(
71
            error => "Koha::Libraries->pickup_locations takes either 'biblio' or "
72
            ." 'item' as parameter, but not both."
73
        );
74
    }
75
76
    # Select libraries that are configured as pickup locations
77
    my $libraries = $self->search({
78
        pickup_location => 1
79
    }, {
80
        order_by => ['branchname']
81
    });
82
83
    return $libraries->unblessed unless $item or $biblio;
84
    return $libraries->unblessed
85
        unless C4::Context->preference('UseBranchTransferLimits');
86
    my $limittype = C4::Context->preference('BranchTransferLimitsType');
87
88
    my $items;
89
    if ($item) {
90
        unless (ref($item) eq 'Koha::Item') {
91
            $item = Koha::Items->find($item);
92
            return $libraries->unblessed unless $item;
93
        }
94
    } else {
95
        unless (ref($biblio) eq 'Koha::Biblio') {
96
            $biblio = Koha::Biblios->find($biblio);
97
            return $libraries->unblessed unless $biblio;
98
        }
99
    }
100
101
    my @pickup_locations;
102
    foreach my $library ($libraries->as_list) {
103
        if ($item && $item->can_be_transferred({ to => $library })) {
104
            push @pickup_locations, $library->unblessed;
105
        } elsif ($biblio && $biblio->can_be_transferred({ to => $library })) {
106
            push @pickup_locations, $library->unblessed;
107
        }
108
    }
109
110
    return wantarray ? @pickup_locations : \@pickup_locations;
111
}
112
41
=head3 search_filtered
113
=head3 search_filtered
42
114
43
=cut
115
=cut
(-)a/t/db_dependent/Koha/Libraries.t (-2 / +346 lines)
Lines 19-26 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 4;
22
use Test::More tests => 5;
23
23
24
use C4::Biblio;
25
use C4::Context;
26
use C4::Items;
27
28
use Koha::Biblios;
29
use Koha::Item::Transfer::Limits;
30
use Koha::Items;
24
use Koha::Library;
31
use Koha::Library;
25
use Koha::Libraries;
32
use Koha::Libraries;
26
use Koha::Database;
33
use Koha::Database;
Lines 53-58 is( $retrieved_library_1->branchname, $new_library_1->branchname, 'Find a librar Link Here
53
$retrieved_library_1->delete;
60
$retrieved_library_1->delete;
54
is( Koha::Libraries->search->count, $nb_of_libraries + 1, 'Delete should have deleted the library' );
61
is( Koha::Libraries->search->count, $nb_of_libraries + 1, 'Delete should have deleted the library' );
55
62
63
subtest 'pickup_locations' => sub {
64
    plan tests => 2;
65
66
    my $from = Koha::Library->new({
67
        branchcode => 'zzzfrom',
68
        branchname => 'zzzfrom',
69
        branchnotes => 'zzzfrom',
70
    })->store;
71
    my $to = Koha::Library->new({
72
        branchcode => 'zzzto',
73
        branchname => 'zzzto',
74
        branchnotes => 'zzzto',
75
    })->store;
76
77
    my ($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY');
78
    # Create item instance for testing.
79
    my ($item_bibnum1, $item_bibitemnum1, $itemnumber1)
80
    = AddItem({ homebranch => $from->branchcode,
81
                holdingbranch => $from->branchcode } , $bibnum);
82
    my ($item_bibnum2, $item_bibitemnum2, $itemnumber2)
83
    = AddItem({ homebranch => $from->branchcode,
84
                holdingbranch => $from->branchcode } , $bibnum);
85
    my ($item_bibnum3, $item_bibitemnum3, $itemnumber3)
86
    = AddItem({ homebranch => $from->branchcode,
87
                holdingbranch => $from->branchcode } , $bibnum);
88
    my $item1 = Koha::Items->find($itemnumber1);
89
    my $item2 = Koha::Items->find($itemnumber2);
90
    my $item3 = Koha::Items->find($itemnumber3);
91
    my $biblio = Koha::Biblios->find($bibnum);
92
    my $itemtype = $biblio->itemtype;
93
94
    subtest 'UseBranchTransferLimits = OFF' => sub {
95
        plan tests => 5;
96
97
        t::lib::Mocks::mock_preference('UseBranchTransferLimits', 0);
98
        t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
99
        t::lib::Mocks::mock_preference('item-level_itypes', 1);
100
        Koha::Item::Transfer::Limits->delete;
101
        Koha::Item::Transfer::Limit->new({
102
            fromBranch => $from->branchcode,
103
            toBranch => $to->branchcode,
104
            itemtype => $biblio->itemtype,
105
        })->store;
106
        my $total_pickup = Koha::Libraries->search({
107
            pickup_location => 1
108
        })->count;
109
        my $pickup = Koha::Libraries->pickup_locations({ biblio => $bibnum });
110
        is(C4::Context->preference('UseBranchTransferLimits'), 0, 'Given system '
111
           .'preference UseBranchTransferLimits is switched OFF,');
112
        is(@{$pickup}, $total_pickup, 'Then the total number of pickup locations '
113
           .'equal number of libraries with pickup_location => 1');
114
115
        t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
116
        t::lib::Mocks::mock_preference('item-level_itypes', 1);
117
        $pickup = Koha::Libraries->pickup_locations({ biblio => $bibnum });
118
        is(@{$pickup}, $total_pickup, '...when '
119
           .'BranchTransferLimitsType = itemtype and item-level_itypes = 1');
120
        t::lib::Mocks::mock_preference('item-level_itypes', 0);
121
        $pickup = Koha::Libraries->pickup_locations({ biblio => $bibnum });
122
        is(@{$pickup}, $total_pickup, '...as well as when '
123
           .'BranchTransferLimitsType = itemtype and item-level_itypes = 0');
124
        t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'ccode');
125
        $pickup = Koha::Libraries->pickup_locations({ biblio => $bibnum });
126
        is(@{$pickup}, $total_pickup, '...as well as when '
127
           .'BranchTransferLimitsType = ccode');
128
        t::lib::Mocks::mock_preference('item-level_itypes', 1);
129
    };
130
131
    subtest 'UseBranchTransferLimits = ON' => sub {
132
        plan tests => 4;
133
        t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
134
135
        is(C4::Context->preference('UseBranchTransferLimits'), 1, 'Given system '
136
           .'preference UseBranchTransferLimits is switched ON,');
137
138
        subtest 'Given BranchTransferLimitsType = itemtype and '
139
               .'item-level_itypes = ON' => sub {
140
            plan tests => 11;
141
142
            t::lib::Mocks::mock_preference('BranchTransferLimitsType','itemtype');
143
            t::lib::Mocks::mock_preference('item-level_itypes', 1);
144
            Koha::Item::Transfer::Limits->delete;
145
            my $limit = Koha::Item::Transfer::Limit->new({
146
                fromBranch => $from->branchcode,
147
                toBranch => $to->branchcode,
148
                itemtype => $item1->effective_itemtype,
149
            })->store;
150
            ok($item1->effective_itemtype eq $item2->effective_itemtype
151
               && $item1->effective_itemtype eq $item3->effective_itemtype,
152
               'Given all items of a biblio have same the itemtype,');
153
            is($limit->itemtype, $item1->effective_itemtype, 'and given there '
154
               .'is an existing transfer limit for that itemtype,');
155
            my $pickup = Koha::Libraries->pickup_locations({ biblio => $bibnum });
156
            my $found = 0;
157
            foreach my $lib (@{$pickup}) {
158
                if ($lib->{'branchcode'} eq $limit->toBranch) {
159
                    $found = 1;
160
                }
161
            }
162
            is($found, 0, 'Then the to-library of which the limit applies for, '
163
               .'is not included in the list of pickup libraries.');
164
            $pickup = Koha::Libraries->pickup_locations({ item => $item1 });
165
            $found = 0;
166
            foreach my $lib (@{$pickup}) {
167
                if ($lib->{'branchcode'} eq $limit->toBranch) {
168
                    $found = 1;
169
                }
170
            }
171
            is($found, 0, 'The same applies when asking pickup locations of '
172
               .'a single item.');
173
            my $others = Koha::Libraries->search({
174
                pickup_location => 1,
175
                branchcode => { 'not in' => [$limit->toBranch] }})->count;
176
            is(@{$pickup}, $others, 'However, the number of other pickup '
177
               .'libraries is correct.');
178
            $item2->itype('BK')->store;
179
            ok($item1->effective_itemtype ne $item2->effective_itemtype,
180
               'Given one of the item in this biblio has a different itemtype,');
181
            is(Koha::Item::Transfer::Limits->search({
182
                itemtype => $item2->effective_itemtype })->count, 0, 'and it is'
183
               .' not restricted by transfer limits,');
184
            $pickup = Koha::Libraries->pickup_locations({ biblio => $bibnum });
185
            $found = 0;
186
            foreach my $lib (@{$pickup}) {
187
                if ($lib->{'branchcode'} eq $limit->toBranch) {
188
                    $found = 1;
189
                }
190
            }
191
            is($found, 1, 'Then the to-library of which the limit applies for, '
192
               .'is included in the list of pickup libraries.');
193
            $pickup = Koha::Libraries->pickup_locations({ item => $item2 });
194
            $found = 0;
195
            foreach my $lib (@{$pickup}) {
196
                if ($lib->{'branchcode'} eq $limit->toBranch) {
197
                    $found = 1;
198
                }
199
            }
200
            is($found, 1, 'The same applies when asking pickup locations of '
201
               .'a that particular item.');
202
            Koha::Item::Transfer::Limits->delete;
203
            $pickup = Koha::Libraries->pickup_locations({ biblio => $bibnum });
204
            $found = 0;
205
            foreach my $lib (@{$pickup}) {
206
                if ($lib->{'branchcode'} eq $limit->toBranch) {
207
                    $found = 1;
208
                }
209
            }
210
            is($found, 1, 'Given we deleted transfer limit, the previously '
211
               .'transfer-limited library is included in the list.');
212
            $pickup = Koha::Libraries->pickup_locations({ item => $item1 });
213
            $found = 0;
214
            foreach my $lib (@{$pickup}) {
215
                if ($lib->{'branchcode'} eq $limit->toBranch) {
216
                    $found = 1;
217
                }
218
            }
219
            is($found, 1, 'The same applies when asking pickup locations of '
220
               .'a single item.');
221
        };
222
223
        subtest 'Given BranchTransferLimitsType = itemtype and '
224
               .'item-level_itypes = OFF' => sub {
225
            plan tests => 9;
226
227
            t::lib::Mocks::mock_preference('BranchTransferLimitsType','itemtype');
228
            t::lib::Mocks::mock_preference('item-level_itypes', 0);
229
            $biblio->biblioitem->itemtype('BK')->store;
230
            Koha::Item::Transfer::Limits->delete;
231
            my $limit = Koha::Item::Transfer::Limit->new({
232
                fromBranch => $from->branchcode,
233
                toBranch => $to->branchcode,
234
                itemtype => $item1->effective_itemtype,
235
            })->store;
236
237
            ok($item1->effective_itemtype eq 'BK',
238
               'Given items use biblio-level itemtype,');
239
            is($limit->itemtype, $item1->effective_itemtype, 'and given there '
240
               .'is an existing transfer limit for that itemtype,');
241
            my $pickup = Koha::Libraries->pickup_locations({ biblio => $bibnum });
242
            my $found = 0;
243
            foreach my $lib (@{$pickup}) {
244
                if ($lib->{'branchcode'} eq $limit->toBranch) {
245
                    $found = 1;
246
                }
247
            }
248
            is($found, 0, 'Then the to-library of which the limit applies for, '
249
               .'is not included in the list of pickup libraries.');
250
            $pickup = Koha::Libraries->pickup_locations({ item => $item1 });
251
            $found = 0;
252
            foreach my $lib (@{$pickup}) {
253
                if ($lib->{'branchcode'} eq $limit->toBranch) {
254
                    $found = 1;
255
                }
256
            }
257
            is($found, 0, 'The same applies when asking pickup locations of '
258
               .'a single item.');
259
            my $others = Koha::Libraries->search({
260
                pickup_location => 1,
261
                branchcode => { 'not in' => [$limit->toBranch] }})->count;
262
            is(@{$pickup}, $others, 'However, the number of other pickup '
263
               .'libraries is correct.');
264
            Koha::Item::Transfer::Limits->delete;
265
            $pickup = Koha::Libraries->pickup_locations({ biblio => $bibnum });
266
            $found = 0;
267
            foreach my $lib (@{$pickup}) {
268
                if ($lib->{'branchcode'} eq $limit->toBranch) {
269
                    $found = 1;
270
                }
271
            }
272
            is($found, 1, 'Given we deleted transfer limit, the previously '
273
               .'transfer-limited library is included in the list.');
274
            $limit = Koha::Item::Transfer::Limit->new({
275
                fromBranch => $from->branchcode,
276
                toBranch => $to->branchcode,
277
                itemtype => $item1->itype,
278
            })->store;
279
            ok($item1->itype ne $item1->effective_itemtype
280
               && $limit->itemtype eq $item1->itype, 'Given we have added a limit'
281
               .' matching ITEM-level itemtype,');
282
            $pickup = Koha::Libraries->pickup_locations({ biblio => $bibnum });
283
            $found = 0;
284
            foreach my $lib (@{$pickup}) {
285
                if ($lib->{'branchcode'} eq $limit->toBranch) {
286
                    $found = 1;
287
                }
288
            }
289
            is($found, 1, 'Then the limited branch is still included as a pickup'
290
               .' library.');
291
            $pickup = Koha::Libraries->pickup_locations({ item => $item1 });
292
            $found = 0;
293
            foreach my $lib (@{$pickup}) {
294
                if ($lib->{'branchcode'} eq $limit->toBranch) {
295
                    $found = 1;
296
                }
297
            }
298
            is($found, 1, 'The same applies when asking pickup locations of '
299
               .'a single item.');
300
        };
301
302
        subtest 'Given BranchTransferLimitsType = ccode' => sub {
303
            plan tests => 10;
304
305
            t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'ccode');
306
            $item1->ccode('hi')->store;
307
            $item2->ccode('hi')->store;
308
            $item3->ccode('hi')->store;
309
            Koha::Item::Transfer::Limits->delete;
310
            my $limit = Koha::Item::Transfer::Limit->new({
311
                fromBranch => $from->branchcode,
312
                toBranch => $to->branchcode,
313
                ccode => $item1->ccode,
314
            })->store;
315
316
            is($limit->ccode, $item1->ccode, 'Given there '
317
               .'is an existing transfer limit for that ccode,');
318
            my $pickup = Koha::Libraries->pickup_locations({ biblio => $bibnum });
319
            my $found = 0;
320
            foreach my $lib (@{$pickup}) {
321
                if ($lib->{'branchcode'} eq $limit->toBranch) {
322
                    $found = 1;
323
                }
324
            }
325
            is($found, 0, 'Then the to-library of which the limit applies for, '
326
               .'is not included in the list of pickup libraries.');
327
            $pickup = Koha::Libraries->pickup_locations({ item => $item1 });
328
            $found = 0;
329
            foreach my $lib (@{$pickup}) {
330
                if ($lib->{'branchcode'} eq $limit->toBranch) {
331
                    $found = 1;
332
                }
333
            }
334
            is($found, 0, 'The same applies when asking pickup locations of '
335
               .'a single item.');
336
            my $others = Koha::Libraries->search({
337
                pickup_location => 1,
338
                branchcode => { 'not in' => [$limit->toBranch] }})->count;
339
            is(@{$pickup}, $others, 'However, the number of other pickup '
340
               .'libraries is correct.');
341
            $item3->ccode('yo')->store;
342
            ok($item1->ccode ne $item3->ccode,
343
               'Given one of the item in this biblio has a different ccode,');
344
            is(Koha::Item::Transfer::Limits->search({
345
                ccode => $item3->ccode })->count, 0, 'and it is'
346
               .' not restricted by transfer limits,');
347
            $pickup = Koha::Libraries->pickup_locations({ biblio => $bibnum });
348
            $found = 0;
349
            foreach my $lib (@{$pickup}) {
350
                if ($lib->{'branchcode'} eq $limit->toBranch) {
351
                    $found = 1;
352
                }
353
            }
354
            is($found, 1, 'Then the to-library of which the limit applies for, '
355
               .'is included in the list of pickup libraries.');
356
            $pickup = Koha::Libraries->pickup_locations({ item => $item3 });
357
            $found = 0;
358
            foreach my $lib (@{$pickup}) {
359
                if ($lib->{'branchcode'} eq $limit->toBranch) {
360
                    $found = 1;
361
                }
362
            }
363
            is($found, 1, 'The same applies when asking pickup locations of '
364
               .'a that particular item.');
365
            Koha::Item::Transfer::Limits->delete;
366
            $pickup = Koha::Libraries->pickup_locations({ biblio => $bibnum });
367
            $found = 0;
368
            foreach my $lib (@{$pickup}) {
369
                if ($lib->{'branchcode'} eq $limit->toBranch) {
370
                    $found = 1;
371
                }
372
            }
373
            is($found, 1, 'Given we deleted transfer limit, the previously '
374
               .'transfer-limited library is included in the list.');
375
            $pickup = Koha::Libraries->pickup_locations({ item => $item1 });
376
            $found = 0;
377
            foreach my $lib (@{$pickup}) {
378
                if ($lib->{'branchcode'} eq $limit->toBranch) {
379
                    $found = 1;
380
                }
381
            }
382
            is($found, 1, 'The same applies when asking pickup locations of '
383
               .'a single item.');
384
        };
385
    };
386
};
387
388
sub create_helper_biblio {
389
    my $itemtype = shift;
390
    my ($bibnum, $title, $bibitemnum);
391
    my $bib = MARC::Record->new();
392
    $title = 'Silence in the library';
393
    $bib->append_fields(
394
        MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
395
        MARC::Field->new('245', ' ', ' ', a => $title),
396
        MARC::Field->new('942', ' ', ' ', c => $itemtype),
397
    );
398
    return ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
399
}
400
56
$schema->storage->txn_rollback;
401
$schema->storage->txn_rollback;
57
402
58
subtest '->get_effective_marcorgcode' => sub {
403
subtest '->get_effective_marcorgcode' => sub {
59
- 

Return to bug 7614