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

(-)a/C4/Circulation.pm (-3 / +3 lines)
Lines 2272-2280 sub AddReturn { Link Here
2272
        if ($limit) {
2272
        if ($limit) {
2273
            my $count = Koha::Items->count( { itype => $limit->itemtype, holdingbranch => $branch } );
2273
            my $count = Koha::Items->count( { itype => $limit->itemtype, holdingbranch => $branch } );
2274
            if ( $count >= $limit->float_limit ) {
2274
            if ( $count >= $limit->float_limit ) {
2275
                my $transfer_branchcode = Koha::Library::FloatLimits->lowest_ratio_library( $item, $branch );
2275
                my $transfer_library = Koha::Library::FloatLimits->lowest_ratio_library( $item, $branch );
2276
                if ( $transfer_branchcode ne $branch ) {
2276
                if ( $transfer_library->branchcode ne $branch ) {
2277
                    $returnbranch     = $transfer_branchcode;
2277
                    $returnbranch     = $transfer_library->branchcode;
2278
                    $transfer_trigger = 'LibraryFloatLimit';
2278
                    $transfer_trigger = 'LibraryFloatLimit';
2279
                }
2279
                }
2280
            }
2280
            }
(-)a/Koha/Library/FloatLimits.pm (-23 / +54 lines)
Lines 24-29 use Modern::Perl; Link Here
24
use Koha::Database;
24
use Koha::Database;
25
25
26
use Koha::Library::FloatLimit;
26
use Koha::Library::FloatLimit;
27
use Koha::Libraries;
27
use C4::Circulation qw(IsBranchTransferAllowed);
28
use C4::Circulation qw(IsBranchTransferAllowed);
28
29
29
use base qw(Koha::Objects);
30
use base qw(Koha::Objects);
Lines 43-82 Koha::Library::FloatLimits - Koha Library::FloatLimit object set class Link Here
43
sub lowest_ratio_library {
44
sub lowest_ratio_library {
44
    my ( $self, $item, $branchcode ) = @_;
45
    my ( $self, $item, $branchcode ) = @_;
45
46
46
    my $field = C4::Context->preference('item-level_itypes') ? 'items.itype' : 'biblioitems.itemtype';
47
    my $schema = Koha::Database->new->schema;
47
    my $query = qq{
48
48
        SELECT branchcode
49
    my @float_limits = $schema->resultset('LibraryFloatLimit')->search(
49
        FROM library_float_limits
50
        {
50
        LEFT JOIN items ON ( items.itype = library_float_limits.itemtype AND items.holdingbranch = library_float_limits.branchcode )
51
            itemtype    => $item->effective_itemtype,
51
        LEFT JOIN biblioitems ON ( items.biblioitemnumber = biblioitems.biblioitemnumber )
52
            float_limit => { '!=' => 0 }
52
        WHERE library_float_limits.itemtype = ?
53
        }
53
              AND ( $field = ? OR $field IS NULL )
54
    )->all;
54
              AND library_float_limits.float_limit != 0
55
55
        GROUP BY branchcode
56
    return unless @float_limits;
56
        ORDER BY COUNT(items.holdingbranch)/library_float_limits.float_limit ASC, library_float_limits.float_limit DESC;
57
57
    };
58
    my @candidates;
58
59
    my $use_item_level = C4::Context->preference('item-level_itypes');
59
    my $results = C4::Context->dbh->selectall_arrayref(
60
60
        $query, { Slice => {} }, $item->effective_itemtype,
61
    foreach my $limit (@float_limits) {
61
        $item->effective_itemtype
62
        my $branch          = $limit->get_column('branchcode');
62
    );
63
        my $float_limit_val = $limit->get_column('float_limit');
64
65
        my $item_count;
66
67
        if ($use_item_level) {
68
            $item_count = Koha::Items->search(
69
                {
70
                    itype         => $item->effective_itemtype,
71
                    holdingbranch => $branch
72
                }
73
            )->count;
74
        } else {
75
            $item_count = Koha::Items->search(
76
                {
77
                    holdingbranch         => $branch,
78
                    'biblioitem.itemtype' => $item->effective_itemtype
79
                },
80
                { join => 'biblioitem' }
81
            )->count;
82
        }
83
84
        my $ratio = $item_count / $float_limit_val;
85
        push @candidates, {
86
            branchcode  => $branch,
87
            ratio       => $ratio,
88
            float_limit => $float_limit_val
89
        };
90
    }
91
92
    @candidates = sort { $a->{ratio} <=> $b->{ratio} || $b->{float_limit} <=> $a->{float_limit} } @candidates;
63
93
64
    my $UseBranchTransferLimits = C4::Context->preference("UseBranchTransferLimits");
94
    my $UseBranchTransferLimits = C4::Context->preference("UseBranchTransferLimits");
65
    my $BranchTransferLimitsType =
95
    my $BranchTransferLimitsType =
66
        C4::Context->preference("BranchTransferLimitsType") eq 'itemtype' ? 'effective_itemtype' : 'ccode';
96
        C4::Context->preference("BranchTransferLimitsType") eq 'itemtype' ? 'effective_itemtype' : 'ccode';
67
97
68
    foreach my $r (@$results) {
98
    for my $candidate (@candidates) {
69
        if ($UseBranchTransferLimits) {
99
        if ($UseBranchTransferLimits) {
70
            my $allowed = C4::Circulation::IsBranchTransferAllowed(
100
            my $allowed = C4::Circulation::IsBranchTransferAllowed(
71
                $r->{branchcode},    # to
101
                $candidate->{branchcode},
72
                $branchcode,         # from
102
                $branchcode,
73
                $item->$BranchTransferLimitsType
103
                $item->$BranchTransferLimitsType
74
            );
104
            );
75
            return $r->{branchcode} if $allowed;
105
            return Koha::Libraries->find( $candidate->{branchcode} ) if $allowed;
76
        } else {
106
        } else {
77
            return $r->{branchcode};
107
            return Koha::Libraries->find( $candidate->{branchcode} );
78
        }
108
        }
79
    }
109
    }
110
111
    return;
80
}
112
}
81
113
82
=head3 _type
114
=head3 _type
83
- 

Return to bug 28530