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

(-)a/Koha/Library/FloatLimits.pm (-4 / +25 lines)
Lines 41-47 Koha::Library::FloatLimits - Koha Library::FloatLimit object set class Link Here
41
41
42
sub lowest_ratio_library {
42
sub lowest_ratio_library {
43
    my ( $self, $item, $branchcode ) = @_;
43
    my ( $self, $item, $branchcode ) = @_;
44
45
    my $schema = Koha::Database->new->schema;
44
    my $schema = Koha::Database->new->schema;
46
45
47
    my @float_limits = $schema->resultset('LibraryFloatLimit')->search(
46
    my @float_limits = $schema->resultset('LibraryFloatLimit')->search(
Lines 51-56 sub lowest_ratio_library { Link Here
51
        }
50
        }
52
    )->all;
51
    )->all;
53
52
53
    # check the items return policy
54
    my $hbr = Koha::CirculationRules->get_return_branch_policy($item);
55
56
    # if item only floats in the library group we must eliminate all other candidates
57
    if ( $hbr && $hbr eq 'returnbylibrarygroup' ) {
58
        my $current_library = Koha::Libraries->find($branchcode);
59
        my $float_libraries = $current_library->get_float_libraries;
60
61
        if ( $float_libraries->count > 0 ) {
62
            my @allowed_branchcodes = $float_libraries->get_column('branchcode');
63
            my %allowed_branches    = map { $_ => 1 } @allowed_branchcodes;
64
            @float_limits = grep { $allowed_branches{ $_->get_column('branchcode') } } @float_limits;
65
        }
66
    }
67
54
    return unless @float_limits;
68
    return unless @float_limits;
55
69
56
    my @candidates;
70
    my @candidates;
Lines 153-160 sub lowest_ratio_library { Link Here
153
        };
167
        };
154
    }
168
    }
155
169
156
    # sort the branches by lowest ratio in the event of a tie choose a random branch
170
    # sort the branches by lowest ratio
157
    @candidates = sort { $a->{ratio} <=> $b->{ratio} || rand() <=> rand() } @candidates;
171
    # in the event of a tie the item should stay where it is, if the current branch is involved in the tie
172
    # when the current branch is not involved in the tie a random branch is choosen from those who tied
173
    @candidates = sort {
174
               $a->{ratio} <=> $b->{ratio}
175
            || ( $a->{branchcode} eq $branchcode ? -1 : 0 ) - ( $b->{branchcode} eq $branchcode ? -1 : 0 )
176
            || rand() <=> rand()
177
    } @candidates;
158
178
159
    my $UseBranchTransferLimits = C4::Context->preference("UseBranchTransferLimits");
179
    my $UseBranchTransferLimits = C4::Context->preference("UseBranchTransferLimits");
160
    my $BranchTransferLimitsType =
180
    my $BranchTransferLimitsType =
Lines 162-173 sub lowest_ratio_library { Link Here
162
182
163
    my $transfer_branch;
183
    my $transfer_branch;
164
    for my $candidate (@candidates) {
184
    for my $candidate (@candidates) {
185
165
        if ($UseBranchTransferLimits) {
186
        if ($UseBranchTransferLimits) {
166
            my $allowed = C4::Circulation::IsBranchTransferAllowed(
187
            my $allowed = C4::Circulation::IsBranchTransferAllowed(
167
                $candidate->{branchcode},
188
                $candidate->{branchcode},
168
                $branchcode,
189
                $branchcode,
169
                $item->$BranchTransferLimitsType
190
                $item->$BranchTransferLimitsType
170
            );
191
            );
192
171
            $transfer_branch = Koha::Libraries->find( $candidate->{branchcode} ) if $allowed;
193
            $transfer_branch = Koha::Libraries->find( $candidate->{branchcode} ) if $allowed;
172
            last;
194
            last;
173
        } else {
195
        } else {
174
- 

Return to bug 28530