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

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

Return to bug 24368