|
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 24-29
use Modern::Perl;
Link Here
|
| 24 |
use Koha::Database; |
22 |
use Koha::Database; |
| 25 |
|
23 |
|
| 26 |
use Koha::Library::FloatLimit; |
24 |
use Koha::Library::FloatLimit; |
|
|
25 |
use Koha::Libraries; |
| 27 |
use C4::Circulation qw(IsBranchTransferAllowed); |
26 |
use C4::Circulation qw(IsBranchTransferAllowed); |
| 28 |
|
27 |
|
| 29 |
use base qw(Koha::Objects); |
28 |
use base qw(Koha::Objects); |
|
Lines 43-82
Koha::Library::FloatLimits - Koha Library::FloatLimit object set class
Link Here
|
| 43 |
sub lowest_ratio_library { |
42 |
sub lowest_ratio_library { |
| 44 |
my ( $self, $item, $branchcode ) = @_; |
43 |
my ( $self, $item, $branchcode ) = @_; |
| 45 |
|
44 |
|
| 46 |
my $field = C4::Context->preference('item-level_itypes') ? 'items.itype' : 'biblioitems.itemtype'; |
45 |
my $schema = Koha::Database->new->schema; |
| 47 |
my $query = qq{ |
46 |
|
| 48 |
SELECT branchcode |
47 |
my @float_limits = $schema->resultset('LibraryFloatLimit')->search( |
| 49 |
FROM library_float_limits |
48 |
{ |
| 50 |
LEFT JOIN items ON ( items.itype = library_float_limits.itemtype AND items.holdingbranch = library_float_limits.branchcode ) |
49 |
itemtype => $item->effective_itemtype, |
| 51 |
LEFT JOIN biblioitems ON ( items.biblioitemnumber = biblioitems.biblioitemnumber ) |
50 |
float_limit => { '!=' => 0 } |
| 52 |
WHERE library_float_limits.itemtype = ? |
51 |
} |
| 53 |
AND ( $field = ? OR $field IS NULL ) |
52 |
)->all; |
| 54 |
AND library_float_limits.float_limit != 0 |
53 |
|
| 55 |
GROUP BY branchcode |
54 |
return unless @float_limits; |
| 56 |
ORDER BY COUNT(items.holdingbranch)/library_float_limits.float_limit ASC, library_float_limits.float_limit DESC; |
55 |
|
| 57 |
}; |
56 |
my @candidates; |
| 58 |
|
57 |
my $use_item_level = C4::Context->preference('item-level_itypes'); |
| 59 |
my $results = C4::Context->dbh->selectall_arrayref( |
58 |
|
| 60 |
$query, { Slice => {} }, $item->effective_itemtype, |
59 |
foreach my $limit (@float_limits) { |
| 61 |
$item->effective_itemtype |
60 |
my $branch = $limit->get_column('branchcode'); |
| 62 |
); |
61 |
my $float_limit_val = $limit->get_column('float_limit'); |
|
|
62 |
|
| 63 |
my $item_count; |
| 64 |
|
| 65 |
if ($use_item_level) { |
| 66 |
my $at_branch_count = Koha::Items->search( |
| 67 |
{ |
| 68 |
itype => $item->effective_itemtype, |
| 69 |
holdingbranch => $branch, |
| 70 |
onloan => undef |
| 71 |
}, |
| 72 |
)->count; |
| 73 |
|
| 74 |
# Count items in transit TO this branch |
| 75 |
my $in_transit_to_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 |
|
| 92 |
my $in_transit_from_count = Koha::Items->search( |
| 93 |
{ itype => $item->effective_itemtype }, |
| 94 |
{ |
| 95 |
join => 'branchtransfers', |
| 96 |
where => { |
| 97 |
'branchtransfers.frombranch' => $branch, |
| 98 |
'branchtransfers.datearrived' => undef, # Still in transit |
| 99 |
'branchtransfers.datecancelled' => undef, #Not cancelled |
| 100 |
}, |
| 101 |
distinct => 1 |
| 102 |
} |
| 103 |
)->count; |
| 104 |
$item_count = $at_branch_count + $in_transit_to_count - $in_transit_from_count; |
| 105 |
} else { |
| 106 |
my $at_branch_count = Koha::Items->search( |
| 107 |
{ |
| 108 |
holdingbranch => $branch, |
| 109 |
'biblioitem.itemtype' => $item->effective_itemtype, |
| 110 |
onloan => undef |
| 111 |
}, |
| 112 |
)->count; |
| 113 |
|
| 114 |
# Count items in transit TO this branch |
| 115 |
my $in_transit_to_count = Koha::Items->search( |
| 116 |
{ |
| 117 |
itype => $item->effective_itemtype, |
| 118 |
}, |
| 119 |
{ |
| 120 |
join => 'branchtransfers', |
| 121 |
where => { |
| 122 |
'branchtransfers.tobranch' => $branch, |
| 123 |
'branchtransfers.datearrived' => undef, # Still in transit |
| 124 |
'branchtransfers.datecancelled' => undef, #Not cancelled |
| 125 |
}, |
| 126 |
distinct => 1 |
| 127 |
} |
| 128 |
)->count; |
| 129 |
|
| 130 |
my $in_transit_from_count = Koha::Items->search( |
| 131 |
{ |
| 132 |
itype => $item->effective_itemtype, |
| 133 |
}, |
| 134 |
{ |
| 135 |
join => 'branchtransfers', |
| 136 |
where => { |
| 137 |
'branchtransfers.frombranch' => $branch, |
| 138 |
'branchtransfers.datearrived' => undef, # Still in transit |
| 139 |
'branchtransfers.datecancelled' => undef, #Not cancelled |
| 140 |
}, |
| 141 |
distinct => 1 |
| 142 |
} |
| 143 |
)->count; |
| 144 |
$item_count = $at_branch_count + $in_transit_to_count - $in_transit_from_count; |
| 145 |
} |
| 146 |
|
| 147 |
my $ratio = $item_count / $float_limit_val; |
| 148 |
|
| 149 |
push @candidates, { |
| 150 |
branchcode => $branch, |
| 151 |
ratio => $ratio, |
| 152 |
float_limit => $float_limit_val |
| 153 |
}; |
| 154 |
} |
| 155 |
|
| 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; |
| 63 |
|
158 |
|
| 64 |
my $UseBranchTransferLimits = C4::Context->preference("UseBranchTransferLimits"); |
159 |
my $UseBranchTransferLimits = C4::Context->preference("UseBranchTransferLimits"); |
| 65 |
my $BranchTransferLimitsType = |
160 |
my $BranchTransferLimitsType = |
| 66 |
C4::Context->preference("BranchTransferLimitsType") eq 'itemtype' ? 'effective_itemtype' : 'ccode'; |
161 |
C4::Context->preference("BranchTransferLimitsType") eq 'itemtype' ? 'effective_itemtype' : 'ccode'; |
| 67 |
|
162 |
|
| 68 |
foreach my $r (@$results) { |
163 |
my $transfer_branch; |
|
|
164 |
for my $candidate (@candidates) { |
| 69 |
if ($UseBranchTransferLimits) { |
165 |
if ($UseBranchTransferLimits) { |
| 70 |
my $allowed = C4::Circulation::IsBranchTransferAllowed( |
166 |
my $allowed = C4::Circulation::IsBranchTransferAllowed( |
| 71 |
$r->{branchcode}, # to |
167 |
$candidate->{branchcode}, |
| 72 |
$branchcode, # from |
168 |
$branchcode, |
| 73 |
$item->$BranchTransferLimitsType |
169 |
$item->$BranchTransferLimitsType |
| 74 |
); |
170 |
); |
| 75 |
return $r->{branchcode} if $allowed; |
171 |
$transfer_branch = Koha::Libraries->find( $candidate->{branchcode} ) if $allowed; |
|
|
172 |
last; |
| 76 |
} else { |
173 |
} else { |
| 77 |
return $r->{branchcode}; |
174 |
$transfer_branch = Koha::Libraries->find( $candidate->{branchcode} ); |
|
|
175 |
last; |
| 78 |
} |
176 |
} |
| 79 |
} |
177 |
} |
|
|
178 |
|
| 179 |
return $transfer_branch; |
| 80 |
} |
180 |
} |
| 81 |
|
181 |
|
| 82 |
=head3 _type |
182 |
=head3 _type |