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

(-)a/C4/Circulation.pm (-7 / +4 lines)
Lines 2278-2290 sub AddReturn { Link Here
2278
        my $effective_itemtype = $item->effective_itemtype;
2278
        my $effective_itemtype = $item->effective_itemtype;
2279
        my $limit = Koha::Library::FloatLimits->find( { itemtype => $effective_itemtype, branchcode => $branch } );
2279
        my $limit = Koha::Library::FloatLimits->find( { itemtype => $effective_itemtype, branchcode => $branch } );
2280
        if ($limit) {
2280
        if ($limit) {
2281
            my $count = Koha::Items->count( { itype => $limit->itemtype, holdingbranch => $branch } );
2281
            my $transfer_library = Koha::Library::FloatLimits->lowest_ratio_library( $item, $branch );
2282
            if ( $count >= $limit->float_limit ) {
2282
            if ( $transfer_library && $transfer_library->branchcode ne $branch ) {
2283
                my $transfer_library = Koha::Library::FloatLimits->lowest_ratio_library( $item, $branch );
2283
                $returnbranch     = $transfer_library->branchcode;
2284
                if ( $transfer_library && $transfer_library->branchcode ne $branch ) {
2284
                $transfer_trigger = 'LibraryFloatLimit';
2285
                    $returnbranch     = $transfer_library->branchcode;
2286
                    $transfer_trigger = 'LibraryFloatLimit';
2287
                }
2288
            }
2285
            }
2289
        }
2286
        }
2290
    }
2287
    }
(-)a/Koha/Library/FloatLimits.pm (-12 / +12 lines)
Lines 60-68 sub lowest_ratio_library { Link Here
60
        my $branch          = $limit->get_column('branchcode');
60
        my $branch          = $limit->get_column('branchcode');
61
        my $float_limit_val = $limit->get_column('float_limit');
61
        my $float_limit_val = $limit->get_column('float_limit');
62
62
63
        #don't try to transfer something to where it already is
64
        next if $branch eq $branchcode;
65
66
        my $item_count;
63
        my $item_count;
67
64
68
        if ($use_item_level) {
65
        if ($use_item_level) {
Lines 98-105 sub lowest_ratio_library { Link Here
98
                    join  => 'branchtransfers',
95
                    join  => 'branchtransfers',
99
                    where => {
96
                    where => {
100
                        'branchtransfers.frombranch'    => $branch,
97
                        'branchtransfers.frombranch'    => $branch,
101
                        'branchtransfers.datearrived'   => undef,
98
                        'branchtransfers.datearrived'   => undef,     # Still in transit
102
                        'branchtransfers.datecancelled' => undef,
99
                        'branchtransfers.datecancelled' => undef,     #Not cancelled
103
                    },
100
                    },
104
                    distinct => 1
101
                    distinct => 1
105
                }
102
                }
Lines 147-156 sub lowest_ratio_library { Link Here
147
            $item_count = $at_branch_count + $in_transit_to_count - $in_transit_from_count;
144
            $item_count = $at_branch_count + $in_transit_to_count - $in_transit_from_count;
148
        }
145
        }
149
146
150
        #don't transfer to branches that are already at their float limit
151
        next if $item_count >= $float_limit_val;
152
153
        my $ratio = $item_count / $float_limit_val;
147
        my $ratio = $item_count / $float_limit_val;
148
154
        push @candidates, {
149
        push @candidates, {
155
            branchcode  => $branch,
150
            branchcode  => $branch,
156
            ratio       => $ratio,
151
            ratio       => $ratio,
Lines 158-169 sub lowest_ratio_library { Link Here
158
        };
153
        };
159
    }
154
    }
160
155
161
    @candidates = sort { $a->{ratio} <=> $b->{ratio} || $b->{float_limit} <=> $a->{float_limit} } @candidates;
156
    # sort the branches by lowest ratio in the event of a tie choose a random branch
157
    @candidates = sort { $a->{ratio} <=> $b->{ratio} || rand() <=> rand() } @candidates;
162
158
163
    my $UseBranchTransferLimits = C4::Context->preference("UseBranchTransferLimits");
159
    my $UseBranchTransferLimits = C4::Context->preference("UseBranchTransferLimits");
164
    my $BranchTransferLimitsType =
160
    my $BranchTransferLimitsType =
165
        C4::Context->preference("BranchTransferLimitsType") eq 'itemtype' ? 'effective_itemtype' : 'ccode';
161
        C4::Context->preference("BranchTransferLimitsType") eq 'itemtype' ? 'effective_itemtype' : 'ccode';
166
162
163
    my $transfer_branch;
167
    for my $candidate (@candidates) {
164
    for my $candidate (@candidates) {
168
        if ($UseBranchTransferLimits) {
165
        if ($UseBranchTransferLimits) {
169
            my $allowed = C4::Circulation::IsBranchTransferAllowed(
166
            my $allowed = C4::Circulation::IsBranchTransferAllowed(
Lines 171-182 sub lowest_ratio_library { Link Here
171
                $branchcode,
168
                $branchcode,
172
                $item->$BranchTransferLimitsType
169
                $item->$BranchTransferLimitsType
173
            );
170
            );
174
            return Koha::Libraries->find( $candidate->{branchcode} ) if $allowed;
171
            $transfer_branch = Koha::Libraries->find( $candidate->{branchcode} ) if $allowed;
172
            last;
175
        } else {
173
        } else {
176
            return Koha::Libraries->find( $candidate->{branchcode} );
174
            $transfer_branch = Koha::Libraries->find( $candidate->{branchcode} );
175
            last;
177
        }
176
        }
178
    }
177
    }
179
    return;
178
179
    return $transfer_branch;
180
}
180
}
181
181
182
=head3 _type
182
=head3 _type
(-)a/t/db_dependent/Koha/Library/FloatLimits.t (-8 / +1 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 7;
22
use Test::More tests => 6;
23
23
24
use Koha::Database;
24
use Koha::Database;
25
use C4::Circulation qw(CreateBranchTransferLimit);
25
use C4::Circulation qw(CreateBranchTransferLimit);
Lines 127-138 CreateBranchTransferLimit( $to, $from, $code ); Link Here
127
is( C4::Circulation::IsBranchTransferAllowed( $to, $from, $code ), 0, "Transfer to best library is no longer allowed" );
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 )";
128
say "C4::Circulation::IsBranchTransferAllowed( $to, $from, $code )";
129
129
130
is(
131
    Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode,
132
    $library2->{branchcode},
133
    "Correct library selected for float limit transfer when best cannot be used"
134
);
135
136
subtest 'onloan items excluded from ratio calculation' => sub {
130
subtest 'onloan items excluded from ratio calculation' => sub {
137
    plan tests => 5;
131
    plan tests => 5;
138
132
139
- 

Return to bug 28530