Lines 22-27
use Modern::Perl;
Link Here
|
22 |
use Koha::Database; |
22 |
use Koha::Database; |
23 |
|
23 |
|
24 |
use Koha::Library::FloatLimit; |
24 |
use Koha::Library::FloatLimit; |
|
|
25 |
use Koha::Libraries; |
25 |
use C4::Circulation qw(IsBranchTransferAllowed); |
26 |
use C4::Circulation qw(IsBranchTransferAllowed); |
26 |
|
27 |
|
27 |
use base qw(Koha::Objects); |
28 |
use base qw(Koha::Objects); |
Lines 41-80
Koha::Library::FloatLimits - Koha Library::FloatLimit object set class
Link Here
|
41 |
sub lowest_ratio_library { |
42 |
sub lowest_ratio_library { |
42 |
my ( $self, $item, $branchcode ) = @_; |
43 |
my ( $self, $item, $branchcode ) = @_; |
43 |
|
44 |
|
44 |
my $field = C4::Context->preference('item-level_itypes') ? 'items.itype' : 'biblioitems.itemtype'; |
45 |
my $schema = Koha::Database->new->schema; |
45 |
my $query = qq{ |
46 |
|
46 |
SELECT branchcode |
47 |
my @float_limits = $schema->resultset('LibraryFloatLimit')->search( |
47 |
FROM library_float_limits |
48 |
{ |
48 |
LEFT JOIN items ON ( items.itype = library_float_limits.itemtype AND items.holdingbranch = library_float_limits.branchcode ) |
49 |
itemtype => $item->effective_itemtype, |
49 |
LEFT JOIN biblioitems ON ( items.biblioitemnumber = biblioitems.biblioitemnumber ) |
50 |
float_limit => { '!=' => 0 } |
50 |
WHERE library_float_limits.itemtype = ? |
51 |
} |
51 |
AND ( $field = ? OR $field IS NULL ) |
52 |
)->all; |
52 |
AND library_float_limits.float_limit != 0 |
53 |
|
53 |
GROUP BY branchcode |
54 |
return unless @float_limits; |
54 |
ORDER BY COUNT(items.holdingbranch)/library_float_limits.float_limit ASC, library_float_limits.float_limit DESC; |
55 |
|
55 |
}; |
56 |
my @candidates; |
56 |
|
57 |
my $use_item_level = C4::Context->preference('item-level_itypes'); |
57 |
my $results = C4::Context->dbh->selectall_arrayref( |
58 |
|
58 |
$query, { Slice => {} }, $item->effective_itemtype, |
59 |
foreach my $limit (@float_limits) { |
59 |
$item->effective_itemtype |
60 |
my $branch = $limit->get_column('branchcode'); |
60 |
); |
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; |
61 |
|
158 |
|
62 |
my $UseBranchTransferLimits = C4::Context->preference("UseBranchTransferLimits"); |
159 |
my $UseBranchTransferLimits = C4::Context->preference("UseBranchTransferLimits"); |
63 |
my $BranchTransferLimitsType = |
160 |
my $BranchTransferLimitsType = |
64 |
C4::Context->preference("BranchTransferLimitsType") eq 'itemtype' ? 'effective_itemtype' : 'ccode'; |
161 |
C4::Context->preference("BranchTransferLimitsType") eq 'itemtype' ? 'effective_itemtype' : 'ccode'; |
65 |
|
162 |
|
66 |
foreach my $r (@$results) { |
163 |
my $transfer_branch; |
|
|
164 |
for my $candidate (@candidates) { |
67 |
if ($UseBranchTransferLimits) { |
165 |
if ($UseBranchTransferLimits) { |
68 |
my $allowed = C4::Circulation::IsBranchTransferAllowed( |
166 |
my $allowed = C4::Circulation::IsBranchTransferAllowed( |
69 |
$r->{branchcode}, # to |
167 |
$candidate->{branchcode}, |
70 |
$branchcode, # from |
168 |
$branchcode, |
71 |
$item->$BranchTransferLimitsType |
169 |
$item->$BranchTransferLimitsType |
72 |
); |
170 |
); |
73 |
return $r->{branchcode} if $allowed; |
171 |
$transfer_branch = Koha::Libraries->find( $candidate->{branchcode} ) if $allowed; |
|
|
172 |
last; |
74 |
} else { |
173 |
} else { |
75 |
return $r->{branchcode}; |
174 |
$transfer_branch = Koha::Libraries->find( $candidate->{branchcode} ); |
|
|
175 |
last; |
76 |
} |
176 |
} |
77 |
} |
177 |
} |
|
|
178 |
|
179 |
return $transfer_branch; |
78 |
} |
180 |
} |
79 |
|
181 |
|
80 |
=head3 _type |
182 |
=head3 _type |