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

(-)a/t/db_dependent/Circulation/Returns.t (-1 / +88 lines)
Lines 18-24 Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::NoWarnings;
20
use Test::NoWarnings;
21
use Test::More tests => 8;
21
use Test::More tests => 9;
22
use Test::MockModule;
22
use Test::MockModule;
23
use Test::Warn;
23
use Test::Warn;
24
24
Lines 421-424 subtest 'Backdated returns should reduce fine if needed' => sub { Link Here
421
    is( $fine, undef, "Fine was removed correctly with a backdated return" );
421
    is( $fine, undef, "Fine was removed correctly with a backdated return" );
422
};
422
};
423
423
424
subtest 'Test library float limits' => sub {
425
    plan tests => 2;
426
427
    my $library1 = $builder->build( { source => 'Branch' } );
428
    my $library2 = $builder->build( { source => 'Branch' } );
429
    my $library3 = $builder->build( { source => 'Branch' } );
430
    my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } );
431
432
    my $biblio = $builder->build_sample_biblio();
433
434
    for ( 1 .. 5 ) {
435
        $builder->build_sample_item(
436
            {
437
                biblionumber  => $biblio->biblionumber,
438
                homebranch    => $library1->{branchcode},
439
                holdingbranch => $library1->{branchcode},
440
                itype         => $itemtype->itemtype,
441
            }
442
        );
443
    }
444
445
    for ( 1 .. 10 ) {
446
        $builder->build_sample_item(
447
            {
448
                biblionumber  => $biblio->biblionumber,
449
                homebranch    => $library2->{branchcode},
450
                holdingbranch => $library2->{branchcode},
451
                itype         => $itemtype->itemtype,
452
            }
453
        );
454
    }
455
456
    for ( 1 .. 15 ) {
457
        $builder->build_sample_item(
458
            {
459
                biblionumber  => $biblio->biblionumber,
460
                homebranch    => $library3->{branchcode},
461
                holdingbranch => $library3->{branchcode},
462
                itype         => $itemtype->itemtype,
463
            }
464
        );
465
    }
466
467
    my $item = $builder->build_sample_item(
468
        {
469
            biblionumber  => $biblio->biblionumber,
470
            homebranch    => $library1->{branchcode},
471
            holdingbranch => $library1->{branchcode},
472
            itype         => $itemtype->itemtype,
473
        }
474
    );
475
476
    my $limit1 = Koha::Library::FloatLimit->new(
477
        {
478
            branchcode  => $library1->{branchcode},
479
            itemtype    => $itemtype->itemtype,
480
            float_limit => 1,
481
        }
482
    )->store();
483
484
    my $limit2 = Koha::Library::FloatLimit->new(
485
        {
486
            branchcode  => $library2->{branchcode},
487
            itemtype    => $itemtype->itemtype,
488
            float_limit => 100,
489
        }
490
    )->store();
491
492
    my $limit3 = Koha::Library::FloatLimit->new(
493
        {
494
            branchcode  => $library3->{branchcode},
495
            itemtype    => $itemtype->itemtype,
496
            float_limit => 1000,
497
        }
498
    )->store();
499
500
    t::lib::Mocks::mock_preference( 'UseLibraryFloatLimits', '0' );
501
502
    my ( $doreturn, $messages, $iteminformation, $borrower ) = AddReturn( $item->barcode, $item->holdingbranch );
503
    is( $messages->{TransferTrigger}, undef, "Library float limit not triggered if syspref is not enabled" );
504
505
    t::lib::Mocks::mock_preference( 'UseLibraryFloatLimits', '1' );
506
507
    ( $doreturn, $messages, $iteminformation, $borrower ) = AddReturn( $item->barcode, $item->holdingbranch );
508
    is( $messages->{TransferTrigger}, 'LibraryFloatLimit', "Library float limit is triggered if syspref is enabled" );
509
};
510
424
$schema->storage->txn_rollback;
511
$schema->storage->txn_rollback;
(-)a/t/db_dependent/Koha/Library/FloatLimits.t (-1 / +303 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# Copyright 2023 ByWater Solutions
4
#
5
# This file is part of Koha
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Test::More tests => 6;
23
24
use Koha::Database;
25
use C4::Circulation qw(CreateBranchTransferLimit);
26
use Koha::DateUtils qw( dt_from_string );
27
28
use t::lib::Mocks;
29
use t::lib::TestBuilder;
30
31
BEGIN {
32
    use_ok('Koha::Library::FloatLimit');
33
    use_ok('Koha::Library::FloatLimits');
34
}
35
36
my $schema = Koha::Database->new->schema;
37
$schema->storage->txn_begin;
38
39
my $builder  = t::lib::TestBuilder->new;
40
my $library1 = $builder->build( { source => 'Branch' } );
41
my $library2 = $builder->build( { source => 'Branch' } );
42
my $library3 = $builder->build( { source => 'Branch' } );
43
my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } );
44
45
my $biblio = $builder->build_sample_biblio();
46
47
for ( 1 .. 5 ) {
48
    $builder->build_sample_item(
49
        {
50
            biblionumber  => $biblio->biblionumber,
51
            homebranch    => $library1->{branchcode},
52
            holdingbranch => $library1->{branchcode},
53
            itype         => $itemtype->itemtype,
54
        }
55
    );
56
}
57
58
for ( 1 .. 10 ) {
59
    $builder->build_sample_item(
60
        {
61
            biblionumber  => $biblio->biblionumber,
62
            homebranch    => $library2->{branchcode},
63
            holdingbranch => $library2->{branchcode},
64
            itype         => $itemtype->itemtype,
65
        }
66
    );
67
}
68
69
for ( 1 .. 15 ) {
70
    $builder->build_sample_item(
71
        {
72
            biblionumber  => $biblio->biblionumber,
73
            homebranch    => $library3->{branchcode},
74
            holdingbranch => $library3->{branchcode},
75
            itype         => $itemtype->itemtype,
76
        }
77
    );
78
}
79
80
my $item = $builder->build_sample_item(
81
    {
82
        biblionumber  => $biblio->biblionumber,
83
        homebranch    => $library1->{branchcode},
84
        holdingbranch => $library1->{branchcode},
85
        itype         => $itemtype->itemtype,
86
    }
87
);
88
89
my $limit1 = Koha::Library::FloatLimit->new(
90
    {
91
        branchcode  => $library1->{branchcode},
92
        itemtype    => $itemtype->itemtype,
93
        float_limit => 1,
94
    }
95
)->store();
96
97
my $float_limit2 = Koha::Library::FloatLimit->new(
98
    {
99
        branchcode  => $library2->{branchcode},
100
        itemtype    => $itemtype->itemtype,
101
        float_limit => 100,
102
    }
103
)->store();
104
105
my $float_limit3 = Koha::Library::FloatLimit->new(
106
    {
107
        branchcode  => $library3->{branchcode},
108
        itemtype    => $itemtype->itemtype,
109
        float_limit => 1000,
110
    }
111
)->store();
112
113
is(
114
    Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode,
115
    $library3->{branchcode},
116
    "Correct library selected for float limit transfer"
117
);
118
119
t::lib::Mocks::mock_preference( 'UseBranchTransferLimits',  '1' );
120
t::lib::Mocks::mock_preference( 'BranchTransferLimitsType', 'itemtype' );
121
122
my $to   = $library3->{branchcode};
123
my $from = $item->holdingbranch;
124
my $code = $itemtype->itemtype;
125
CreateBranchTransferLimit( $to, $from, $code );
126
127
is( C4::Circulation::IsBranchTransferAllowed( $to, $from, $code ), 0, "Transfer to best library is no longer allowed" );
128
say "C4::Circulation::IsBranchTransferAllowed( $to, $from, $code )";
129
130
subtest 'onloan items excluded from ratio calculation' => sub {
131
    plan tests => 5;
132
133
    # Reset transfer limits for clean test
134
    t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '0' );
135
136
    # Verify initial item counts
137
    my $library2_initial = Koha::Items->search(
138
        {
139
            holdingbranch => $library2->{branchcode},
140
            itype         => $itemtype->itemtype,
141
            onloan        => undef
142
        }
143
    )->count;
144
145
    my $library3_initial = Koha::Items->search(
146
        {
147
            holdingbranch => $library3->{branchcode},
148
            itype         => $itemtype->itemtype,
149
            onloan        => undef
150
        }
151
    )->count;
152
153
    is( $library2_initial, 10, "Library2 initially has 10 available items" );
154
    is( $library3_initial, 15, "Library3 initially has 15 available items" );
155
156
    # Initial library selection based on ratios
157
    is(
158
        Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode,
159
        $library3->{branchcode},
160
        "Library3 initially selected (lowest ratio: 15/1000 = 0.015)"
161
    );
162
163
    # Check out enough items from library3 to change the selection
164
    my @library3_items = Koha::Items->search(
165
        {
166
            holdingbranch => $library3->{branchcode},
167
            itype         => $itemtype->itemtype,
168
            onloan        => undef
169
        }
170
    )->as_list;
171
172
    my $checkout_date = dt_from_string();
173
    for my $i ( 0 .. 9 ) {
174
        $library3_items[$i]->onloan($checkout_date)->store;
175
    }
176
177
    # Verify the count decreased
178
    my $library3_after_checkout = Koha::Items->search(
179
        {
180
            holdingbranch => $library3->{branchcode},
181
            itype         => $itemtype->itemtype,
182
            onloan        => undef
183
        }
184
    )->count;
185
186
    is( $library3_after_checkout, 5, "Library3 now has 5 available items after 10 checkouts" );
187
188
    # Library3 should still be selected
189
    is(
190
        Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode,
191
        $library3->{branchcode},
192
        "Library3 still selected after checkouts (ratio now 5/1000 = 0.005, still better than library2's 10/100 = 0.1)"
193
    );
194
};
195
196
subtest 'items in transit counted toward destination branch' => sub {
197
    plan tests => 6;
198
199
    # Reset transfer limits for clean test
200
    t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '0' );
201
202
    Koha::Items->search(
203
        {
204
            holdingbranch => $library3->{branchcode},
205
            itype         => $itemtype->itemtype,
206
            onloan        => { '!=' => undef }
207
        }
208
    )->update( { onloan => undef } );
209
210
    # Verify initial counts
211
    my $library2_initial = Koha::Items->search(
212
        {
213
            holdingbranch => $library2->{branchcode},
214
            itype         => $itemtype->itemtype,
215
            onloan        => undef
216
        }
217
    )->count;
218
219
    my $library3_initial = Koha::Items->search(
220
        {
221
            holdingbranch => $library3->{branchcode},
222
            itype         => $itemtype->itemtype,
223
            onloan        => undef
224
        }
225
    )->count;
226
227
    is( $library2_initial, 10, "Library2 initially has 10 available items" );
228
    is( $library3_initial, 15, "Library3 initially has 15 available items" );
229
230
    # Initial selection should be library3
231
    is(
232
        Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode,
233
        $library3->{branchcode},
234
        "Library3 initially selected (ratio: 15/1000 = 0.015)"
235
    );
236
237
    # Create items in transit TO library2
238
    my @transit_items;
239
    for ( 1 .. 2 ) {
240
        my $transit_item = $builder->build_sample_item(
241
            {
242
                biblionumber  => $biblio->biblionumber,
243
                homebranch    => $library1->{branchcode},
244
                holdingbranch => $library1->{branchcode},
245
                itype         => $itemtype->itemtype,
246
            }
247
        );
248
        push @transit_items, $transit_item;
249
    }
250
251
    # Create active transfers to library2
252
    my $transfer_date = dt_from_string();
253
    for my $transit_item (@transit_items) {
254
        Koha::Item::Transfer->new(
255
            {
256
                itemnumber    => $transit_item->itemnumber,
257
                frombranch    => $library1->{branchcode},
258
                tobranch      => $library2->{branchcode},
259
                daterequested => $transfer_date,
260
                datesent      => $transfer_date,
261
                reason        => 'LibraryFloatLimit'
262
            }
263
        )->store;
264
    }
265
266
    # Verify that items in transit are counted toward destination
267
    my $in_transit_count = Koha::Items->search(
268
        {
269
            itype => $itemtype->itemtype,
270
        },
271
        {
272
            join  => 'branchtransfers',
273
            where => {
274
                'branchtransfers.tobranch'      => $library2->{branchcode},
275
                'branchtransfers.datearrived'   => undef,
276
                'branchtransfers.datecancelled' => undef,
277
            },
278
            distinct => 1
279
        }
280
    )->count;
281
282
    is( $in_transit_count, 2, "2 items are in transit to library2" );
283
284
    # Test 5: Library2's physical count should still be 10
285
    my $library2_physical = Koha::Items->search(
286
        {
287
            holdingbranch => $library2->{branchcode},
288
            itype         => $itemtype->itemtype,
289
            onloan        => undef
290
        }
291
    )->count;
292
293
    is( $library2_physical, 10, "Library2 still has 10 physical items" );
294
295
    # Library2's count should now be 10 + 2 = 12
296
    is(
297
        Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode,
298
        $library3->{branchcode},
299
        "Library3 still selected after items in transit to library2"
300
    );
301
};
302
303
$schema->storage->txn_rollback;

Return to bug 28530