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

(-)a/Koha/Library/FloatLimits.pm (-4 / +38 lines)
Lines 1-5 Link Here
1
## Please see file perltidy.ERR
2
## Please see file perltidy.ERR
3
package Koha::Library::FloatLimits;
1
package Koha::Library::FloatLimits;
4
2
5
# Copyright ByWater Solutions 2016
3
# Copyright ByWater Solutions 2016
Lines 65-85 sub lowest_ratio_library { Link Here
65
        my $item_count;
63
        my $item_count;
66
64
67
        if ($use_item_level) {
65
        if ($use_item_level) {
68
            $item_count = Koha::Items->search(
66
            my $at_branch_count = Koha::Items->search(
69
                {
67
                {
70
                    itype         => $item->effective_itemtype,
68
                    itype         => $item->effective_itemtype,
71
                    holdingbranch => $branch,
69
                    holdingbranch => $branch,
72
                    onloan        => undef
70
                    onloan        => undef
73
                },
71
                },
74
            )->count;
72
            )->count;
73
74
            # Count items in transit TO this branch
75
            my $in_transit_count = Koha::Items->search(
76
                {
77
                    itype => $item->effective_itemtype,
78
79
                    # Join with active transfers where this branch is the destination
80
                },
81
                {
82
                    join  => 'branchtransfers',
83
                    where => {
84
                        'branchtransfers.tobranch'      => $branch,
85
                        'branchtransfers.datearrived'   => undef,     # Still in transit
86
                        'branchtransfers.datecancelled' => undef,     #Not cancelled
87
                    },
88
                    distinct => 1
89
                }
90
            )->count;
91
            $item_count = $at_branch_count + $in_transit_count;
75
        } else {
92
        } else {
76
            $item_count = Koha::Items->search(
93
            my $at_branch_count = Koha::Items->search(
77
                {
94
                {
78
                    holdingbranch         => $branch,
95
                    holdingbranch         => $branch,
79
                    'biblioitem.itemtype' => $item->effective_itemtype,
96
                    'biblioitem.itemtype' => $item->effective_itemtype,
80
                    onloan                => undef
97
                    onloan                => undef
81
                },
98
                },
82
            )->count;
99
            )->count;
100
101
            # Count items in transit TO this branch
102
            my $in_transit_count = Koha::Items->search(
103
                {
104
                    itype => $item->effective_itemtype,
105
                },
106
                {
107
                    join  => 'branchtransfers',
108
                    where => {
109
                        'branchtransfers.tobranch'      => $branch,
110
                        'branchtransfers.datearrived'   => undef,     # Still in transit
111
                        'branchtransfers.datecancelled' => undef,     #Not cancelled
112
                    },
113
                    distinct => 1
114
                }
115
            )->count;
116
            $item_count = $at_branch_count + $in_transit_count;
83
        }
117
        }
84
118
85
        my $ratio = $item_count / $float_limit_val;
119
        my $ratio = $item_count / $float_limit_val;
(-)a/t/db_dependent/Koha/Library/FloatLimits.t (-2 / +108 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 6;
22
use Test::More tests => 7;
23
23
24
use Koha::Database;
24
use Koha::Database;
25
use C4::Circulation qw(CreateBranchTransferLimit);
25
use C4::Circulation qw(CreateBranchTransferLimit);
Lines 199-202 subtest 'onloan items excluded from ratio calculation' => sub { Link Here
199
    );
199
    );
200
};
200
};
201
201
202
subtest 'items in transit counted toward destination branch' => sub {
203
    plan tests => 6;
204
205
    # Reset transfer limits for clean test
206
    t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '0' );
207
208
    Koha::Items->search(
209
        {
210
            holdingbranch => $library3->{branchcode},
211
            itype         => $itemtype->itemtype,
212
            onloan        => { '!=' => undef }
213
        }
214
    )->update( { onloan => undef } );
215
216
    # Verify initial counts
217
    my $library2_initial = Koha::Items->search(
218
        {
219
            holdingbranch => $library2->{branchcode},
220
            itype         => $itemtype->itemtype,
221
            onloan        => undef
222
        }
223
    )->count;
224
225
    my $library3_initial = Koha::Items->search(
226
        {
227
            holdingbranch => $library3->{branchcode},
228
            itype         => $itemtype->itemtype,
229
            onloan        => undef
230
        }
231
    )->count;
232
233
    is( $library2_initial, 10, "Library2 initially has 10 available items" );
234
    is( $library3_initial, 15, "Library3 initially has 15 available items" );
235
236
    # Initial selection should be library3
237
    is(
238
        Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode,
239
        $library3->{branchcode},
240
        "Library3 initially selected (ratio: 15/1000 = 0.015)"
241
    );
242
243
    # Create items in transit TO library2
244
    my @transit_items;
245
    for ( 1 .. 2 ) {
246
        my $transit_item = $builder->build_sample_item(
247
            {
248
                biblionumber  => $biblio->biblionumber,
249
                homebranch    => $library1->{branchcode},
250
                holdingbranch => $library1->{branchcode},
251
                itype         => $itemtype->itemtype,
252
            }
253
        );
254
        push @transit_items, $transit_item;
255
    }
256
257
    # Create active transfers to library2
258
    my $transfer_date = dt_from_string();
259
    for my $transit_item (@transit_items) {
260
        Koha::Item::Transfer->new(
261
            {
262
                itemnumber    => $transit_item->itemnumber,
263
                frombranch    => $library1->{branchcode},
264
                tobranch      => $library2->{branchcode},
265
                daterequested => $transfer_date,
266
                datesent      => $transfer_date,
267
                reason        => 'LibraryFloatLimit'
268
            }
269
        )->store;
270
    }
271
272
    # Verify that items in transit are counted toward destination
273
    my $in_transit_count = Koha::Items->search(
274
        {
275
            itype => $itemtype->itemtype,
276
        },
277
        {
278
            join  => 'branchtransfers',
279
            where => {
280
                'branchtransfers.tobranch'      => $library2->{branchcode},
281
                'branchtransfers.datearrived'   => undef,
282
                'branchtransfers.datecancelled' => undef,
283
            },
284
            distinct => 1
285
        }
286
    )->count;
287
288
    is( $in_transit_count, 2, "2 items are in transit to library2" );
289
290
    # Test 5: Library2's physical count should still be 10
291
    my $library2_physical = Koha::Items->search(
292
        {
293
            holdingbranch => $library2->{branchcode},
294
            itype         => $itemtype->itemtype,
295
            onloan        => undef
296
        }
297
    )->count;
298
299
    is( $library2_physical, 10, "Library2 still has 10 physical items" );
300
301
    # Library2's count should now be 10 + 2 = 12
302
    is(
303
        Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode,
304
        $library3->{branchcode},
305
        "Library3 still selected after items in transit to library2"
306
    );
307
};
308
202
$schema->storage->txn_rollback;
309
$schema->storage->txn_rollback;
203
- 

Return to bug 28530