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 / +348 lines)
Lines 19-32 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 9;
22
use Test::More tests => 10;
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::LibraryCategory;
33
use Koha::LibraryCategory;
27
use Koha::LibraryCategories;
34
use Koha::LibraryCategories;
28
use Koha::Database;
35
use Koha::Database;
29
36
37
use t::lib::Mocks;
30
use t::lib::TestBuilder;
38
use t::lib::TestBuilder;
31
39
32
my $schema = Koha::Database->new->schema;
40
my $schema = Koha::Database->new->schema;
Lines 86-90 is( Koha::Libraries->search->count, $nb_of_libraries + 1, 'Delete should have de Link Here
86
$retrieved_category_2->delete;
94
$retrieved_category_2->delete;
87
is( Koha::LibraryCategories->search->count, $nb_of_categories + 2, 'Delete should have deleted the library category' );
95
is( Koha::LibraryCategories->search->count, $nb_of_categories + 2, 'Delete should have deleted the library category' );
88
96
97
subtest 'pickup_locations' => sub {
98
    plan tests => 2;
99
100
    my $from = Koha::Library->new({
101
        branchcode => 'zzzfrom',
102
        branchname => 'zzzfrom',
103
        branchnotes => 'zzzfrom',
104
    })->store;
105
    my $to = Koha::Library->new({
106
        branchcode => 'zzzto',
107
        branchname => 'zzzto',
108
        branchnotes => 'zzzto',
109
    })->store;
110
111
    my ($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY');
112
    # Create item instance for testing.
113
    my ($item_bibnum1, $item_bibitemnum1, $itemnumber1)
114
    = AddItem({ homebranch => $from->branchcode,
115
                holdingbranch => $from->branchcode } , $bibnum);
116
    my ($item_bibnum2, $item_bibitemnum2, $itemnumber2)
117
    = AddItem({ homebranch => $from->branchcode,
118
                holdingbranch => $from->branchcode } , $bibnum);
119
    my ($item_bibnum3, $item_bibitemnum3, $itemnumber3)
120
    = AddItem({ homebranch => $from->branchcode,
121
                holdingbranch => $from->branchcode } , $bibnum);
122
    my $item1 = Koha::Items->find($itemnumber1);
123
    my $item2 = Koha::Items->find($itemnumber2);
124
    my $item3 = Koha::Items->find($itemnumber3);
125
    my $biblio = Koha::Biblios->find($bibnum);
126
    my $itemtype = $biblio->itemtype;
127
128
    subtest 'UseBranchTransferLimits = OFF' => sub {
129
        plan tests => 5;
130
131
        t::lib::Mocks::mock_preference('UseBranchTransferLimits', 0);
132
        t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
133
        t::lib::Mocks::mock_preference('item-level_itypes', 1);
134
        Koha::Item::Transfer::Limits->delete;
135
        Koha::Item::Transfer::Limit->new({
136
            fromBranch => $from->branchcode,
137
            toBranch => $to->branchcode,
138
            itemtype => $biblio->itemtype,
139
        })->store;
140
        my $total_pickup = Koha::Libraries->search({
141
            pickup_location => 1
142
        })->count;
143
        my $pickup = Koha::Libraries->pickup_locations({ biblio => $bibnum });
144
        is(C4::Context->preference('UseBranchTransferLimits'), 0, 'Given system '
145
           .'preference UseBranchTransferLimits is switched OFF,');
146
        is(@{$pickup}, $total_pickup, 'Then the total number of pickup locations '
147
           .'equal number of libraries with pickup_location => 1');
148
149
        t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
150
        t::lib::Mocks::mock_preference('item-level_itypes', 1);
151
        $pickup = Koha::Libraries->pickup_locations({ biblio => $bibnum });
152
        is(@{$pickup}, $total_pickup, '...when '
153
           .'BranchTransferLimitsType = itemtype and item-level_itypes = 1');
154
        t::lib::Mocks::mock_preference('item-level_itypes', 0);
155
        $pickup = Koha::Libraries->pickup_locations({ biblio => $bibnum });
156
        is(@{$pickup}, $total_pickup, '...as well as when '
157
           .'BranchTransferLimitsType = itemtype and item-level_itypes = 0');
158
        t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'ccode');
159
        $pickup = Koha::Libraries->pickup_locations({ biblio => $bibnum });
160
        is(@{$pickup}, $total_pickup, '...as well as when '
161
           .'BranchTransferLimitsType = ccode');
162
        t::lib::Mocks::mock_preference('item-level_itypes', 1);
163
    };
164
165
    subtest 'UseBranchTransferLimits = ON' => sub {
166
        plan tests => 4;
167
        t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
168
169
        is(C4::Context->preference('UseBranchTransferLimits'), 1, 'Given system '
170
           .'preference UseBranchTransferLimits is switched ON,');
171
172
        subtest 'Given BranchTransferLimitsType = itemtype and '
173
               .'item-level_itypes = ON' => sub {
174
            plan tests => 11;
175
176
            t::lib::Mocks::mock_preference('BranchTransferLimitsType','itemtype');
177
            t::lib::Mocks::mock_preference('item-level_itypes', 1);
178
            Koha::Item::Transfer::Limits->delete;
179
            my $limit = Koha::Item::Transfer::Limit->new({
180
                fromBranch => $from->branchcode,
181
                toBranch => $to->branchcode,
182
                itemtype => $item1->effective_itemtype,
183
            })->store;
184
            ok($item1->effective_itemtype eq $item2->effective_itemtype
185
               && $item1->effective_itemtype eq $item3->effective_itemtype,
186
               'Given all items of a biblio have same the itemtype,');
187
            is($limit->itemtype, $item1->effective_itemtype, 'and given there '
188
               .'is an existing transfer limit for that itemtype,');
189
            my $pickup = Koha::Libraries->pickup_locations({ biblio => $bibnum });
190
            my $found = 0;
191
            foreach my $lib (@{$pickup}) {
192
                if ($lib->{'branchcode'} eq $limit->toBranch) {
193
                    $found = 1;
194
                }
195
            }
196
            is($found, 0, 'Then the to-library of which the limit applies for, '
197
               .'is not included in the list of pickup libraries.');
198
            $pickup = Koha::Libraries->pickup_locations({ item => $item1 });
199
            $found = 0;
200
            foreach my $lib (@{$pickup}) {
201
                if ($lib->{'branchcode'} eq $limit->toBranch) {
202
                    $found = 1;
203
                }
204
            }
205
            is($found, 0, 'The same applies when asking pickup locations of '
206
               .'a single item.');
207
            my $others = Koha::Libraries->search({
208
                pickup_location => 1,
209
                branchcode => { 'not in' => [$limit->toBranch] }})->count;
210
            is(@{$pickup}, $others, 'However, the number of other pickup '
211
               .'libraries is correct.');
212
            $item2->itype('BK')->store;
213
            ok($item1->effective_itemtype ne $item2->effective_itemtype,
214
               'Given one of the item in this biblio has a different itemtype,');
215
            is(Koha::Item::Transfer::Limits->search({
216
                itemtype => $item2->effective_itemtype })->count, 0, 'and it is'
217
               .' not restricted by transfer limits,');
218
            $pickup = Koha::Libraries->pickup_locations({ biblio => $bibnum });
219
            $found = 0;
220
            foreach my $lib (@{$pickup}) {
221
                if ($lib->{'branchcode'} eq $limit->toBranch) {
222
                    $found = 1;
223
                }
224
            }
225
            is($found, 1, 'Then the to-library of which the limit applies for, '
226
               .'is included in the list of pickup libraries.');
227
            $pickup = Koha::Libraries->pickup_locations({ item => $item2 });
228
            $found = 0;
229
            foreach my $lib (@{$pickup}) {
230
                if ($lib->{'branchcode'} eq $limit->toBranch) {
231
                    $found = 1;
232
                }
233
            }
234
            is($found, 1, 'The same applies when asking pickup locations of '
235
               .'a that particular item.');
236
            Koha::Item::Transfer::Limits->delete;
237
            $pickup = Koha::Libraries->pickup_locations({ biblio => $bibnum });
238
            $found = 0;
239
            foreach my $lib (@{$pickup}) {
240
                if ($lib->{'branchcode'} eq $limit->toBranch) {
241
                    $found = 1;
242
                }
243
            }
244
            is($found, 1, 'Given we deleted transfer limit, the previously '
245
               .'transfer-limited library is included in the list.');
246
            $pickup = Koha::Libraries->pickup_locations({ item => $item1 });
247
            $found = 0;
248
            foreach my $lib (@{$pickup}) {
249
                if ($lib->{'branchcode'} eq $limit->toBranch) {
250
                    $found = 1;
251
                }
252
            }
253
            is($found, 1, 'The same applies when asking pickup locations of '
254
               .'a single item.');
255
        };
256
257
        subtest 'Given BranchTransferLimitsType = itemtype and '
258
               .'item-level_itypes = OFF' => sub {
259
            plan tests => 9;
260
261
            t::lib::Mocks::mock_preference('BranchTransferLimitsType','itemtype');
262
            t::lib::Mocks::mock_preference('item-level_itypes', 0);
263
            $biblio->biblioitem->itemtype('BK')->store;
264
            Koha::Item::Transfer::Limits->delete;
265
            my $limit = Koha::Item::Transfer::Limit->new({
266
                fromBranch => $from->branchcode,
267
                toBranch => $to->branchcode,
268
                itemtype => $item1->effective_itemtype,
269
            })->store;
270
271
            ok($item1->effective_itemtype eq 'BK',
272
               'Given items use biblio-level itemtype,');
273
            is($limit->itemtype, $item1->effective_itemtype, 'and given there '
274
               .'is an existing transfer limit for that itemtype,');
275
            my $pickup = Koha::Libraries->pickup_locations({ biblio => $bibnum });
276
            my $found = 0;
277
            foreach my $lib (@{$pickup}) {
278
                if ($lib->{'branchcode'} eq $limit->toBranch) {
279
                    $found = 1;
280
                }
281
            }
282
            is($found, 0, 'Then the to-library of which the limit applies for, '
283
               .'is not included in the list of pickup libraries.');
284
            $pickup = Koha::Libraries->pickup_locations({ item => $item1 });
285
            $found = 0;
286
            foreach my $lib (@{$pickup}) {
287
                if ($lib->{'branchcode'} eq $limit->toBranch) {
288
                    $found = 1;
289
                }
290
            }
291
            is($found, 0, 'The same applies when asking pickup locations of '
292
               .'a single item.');
293
            my $others = Koha::Libraries->search({
294
                pickup_location => 1,
295
                branchcode => { 'not in' => [$limit->toBranch] }})->count;
296
            is(@{$pickup}, $others, 'However, the number of other pickup '
297
               .'libraries is correct.');
298
            Koha::Item::Transfer::Limits->delete;
299
            $pickup = Koha::Libraries->pickup_locations({ biblio => $bibnum });
300
            $found = 0;
301
            foreach my $lib (@{$pickup}) {
302
                if ($lib->{'branchcode'} eq $limit->toBranch) {
303
                    $found = 1;
304
                }
305
            }
306
            is($found, 1, 'Given we deleted transfer limit, the previously '
307
               .'transfer-limited library is included in the list.');
308
            $limit = Koha::Item::Transfer::Limit->new({
309
                fromBranch => $from->branchcode,
310
                toBranch => $to->branchcode,
311
                itemtype => $item1->itype,
312
            })->store;
313
            ok($item1->itype ne $item1->effective_itemtype
314
               && $limit->itemtype eq $item1->itype, 'Given we have added a limit'
315
               .' matching ITEM-level itemtype,');
316
            $pickup = Koha::Libraries->pickup_locations({ biblio => $bibnum });
317
            $found = 0;
318
            foreach my $lib (@{$pickup}) {
319
                if ($lib->{'branchcode'} eq $limit->toBranch) {
320
                    $found = 1;
321
                }
322
            }
323
            is($found, 1, 'Then the limited branch is still included as a pickup'
324
               .' library.');
325
            $pickup = Koha::Libraries->pickup_locations({ item => $item1 });
326
            $found = 0;
327
            foreach my $lib (@{$pickup}) {
328
                if ($lib->{'branchcode'} eq $limit->toBranch) {
329
                    $found = 1;
330
                }
331
            }
332
            is($found, 1, 'The same applies when asking pickup locations of '
333
               .'a single item.');
334
        };
335
336
        subtest 'Given BranchTransferLimitsType = ccode' => sub {
337
            plan tests => 10;
338
339
            t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'ccode');
340
            $item1->ccode('hi')->store;
341
            $item2->ccode('hi')->store;
342
            $item3->ccode('hi')->store;
343
            Koha::Item::Transfer::Limits->delete;
344
            my $limit = Koha::Item::Transfer::Limit->new({
345
                fromBranch => $from->branchcode,
346
                toBranch => $to->branchcode,
347
                ccode => $item1->ccode,
348
            })->store;
349
350
            is($limit->ccode, $item1->ccode, 'Given there '
351
               .'is an existing transfer limit for that ccode,');
352
            my $pickup = Koha::Libraries->pickup_locations({ biblio => $bibnum });
353
            my $found = 0;
354
            foreach my $lib (@{$pickup}) {
355
                if ($lib->{'branchcode'} eq $limit->toBranch) {
356
                    $found = 1;
357
                }
358
            }
359
            is($found, 0, 'Then the to-library of which the limit applies for, '
360
               .'is not included in the list of pickup libraries.');
361
            $pickup = Koha::Libraries->pickup_locations({ item => $item1 });
362
            $found = 0;
363
            foreach my $lib (@{$pickup}) {
364
                if ($lib->{'branchcode'} eq $limit->toBranch) {
365
                    $found = 1;
366
                }
367
            }
368
            is($found, 0, 'The same applies when asking pickup locations of '
369
               .'a single item.');
370
            my $others = Koha::Libraries->search({
371
                pickup_location => 1,
372
                branchcode => { 'not in' => [$limit->toBranch] }})->count;
373
            is(@{$pickup}, $others, 'However, the number of other pickup '
374
               .'libraries is correct.');
375
            $item3->ccode('yo')->store;
376
            ok($item1->ccode ne $item3->ccode,
377
               'Given one of the item in this biblio has a different ccode,');
378
            is(Koha::Item::Transfer::Limits->search({
379
                ccode => $item3->ccode })->count, 0, 'and it is'
380
               .' not restricted by transfer limits,');
381
            $pickup = Koha::Libraries->pickup_locations({ biblio => $bibnum });
382
            $found = 0;
383
            foreach my $lib (@{$pickup}) {
384
                if ($lib->{'branchcode'} eq $limit->toBranch) {
385
                    $found = 1;
386
                }
387
            }
388
            is($found, 1, 'Then the to-library of which the limit applies for, '
389
               .'is included in the list of pickup libraries.');
390
            $pickup = Koha::Libraries->pickup_locations({ item => $item3 });
391
            $found = 0;
392
            foreach my $lib (@{$pickup}) {
393
                if ($lib->{'branchcode'} eq $limit->toBranch) {
394
                    $found = 1;
395
                }
396
            }
397
            is($found, 1, 'The same applies when asking pickup locations of '
398
               .'a that particular item.');
399
            Koha::Item::Transfer::Limits->delete;
400
            $pickup = Koha::Libraries->pickup_locations({ biblio => $bibnum });
401
            $found = 0;
402
            foreach my $lib (@{$pickup}) {
403
                if ($lib->{'branchcode'} eq $limit->toBranch) {
404
                    $found = 1;
405
                }
406
            }
407
            is($found, 1, 'Given we deleted transfer limit, the previously '
408
               .'transfer-limited library is included in the list.');
409
            $pickup = Koha::Libraries->pickup_locations({ item => $item1 });
410
            $found = 0;
411
            foreach my $lib (@{$pickup}) {
412
                if ($lib->{'branchcode'} eq $limit->toBranch) {
413
                    $found = 1;
414
                }
415
            }
416
            is($found, 1, 'The same applies when asking pickup locations of '
417
               .'a single item.');
418
        };
419
    };
420
};
421
422
sub create_helper_biblio {
423
    my $itemtype = shift;
424
    my ($bibnum, $title, $bibitemnum);
425
    my $bib = MARC::Record->new();
426
    $title = 'Silence in the library';
427
    $bib->append_fields(
428
        MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
429
        MARC::Field->new('245', ' ', ' ', a => $title),
430
        MARC::Field->new('942', ' ', ' ', c => $itemtype),
431
    );
432
    return ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
433
}
434
435
89
$schema->storage->txn_rollback;
436
$schema->storage->txn_rollback;
90
1;
437
1;
91
- 

Return to bug 7614