4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
27 use C4::Output qw( output_html_with_http_headers );
28 use C4::Auth qw( get_template_and_user );
29 use C4::Acquisition qw/GetOrdersByBiblionumber/;
30 use Koha::DateUtils qw( dt_from_string output_pref );
31 use Koha::Acquisition::Baskets;
34 my $startdate = $input->param('from');
35 my $enddate = $input->param('to');
36 my $ratio = $input->param('ratio');
37 my $include_ordered = $input->param('include_ordered');
38 my $include_suspended = $input->param('include_suspended');
40 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
42 template_name => "circ/reserveratios.tt",
45 flagsrequired => { circulate => "circulate_remaining_permissions" },
49 my $booksellerid = $input->param('booksellerid') // '';
50 my $basketno = $input->param('basketno') // '';
51 if ($booksellerid && $basketno) {
52 $template->param( booksellerid => $booksellerid, basketno => $basketno );
55 my $effective_create_items = q{};
57 my $basket = Koha::Acquisition::Baskets->find( $basketno );
59 $effective_create_items = $basket->effective_create_items;
61 $effective_create_items = C4::Context->preference('AcqCreateItem');
65 $startdate = eval { dt_from_string( $startdate ) } if $startdate;
66 $enddate = eval { dt_from_string( $enddate ) } if $enddate;
68 my $todaysdate = dt_from_string;
70 # A default of the prior years's holds is a reasonable way to pull holds
71 $enddate = $todaysdate unless $enddate;
72 $startdate = $todaysdate->clone->subtract( years => 1 ) unless $startdate;
74 if (!defined($ratio)) {
77 # Force to be a number
80 $ratio = 1; # prevent division by zero
83 my $dbh = C4::Context->dbh;
84 my $sqldatewhere = "";
85 my @query_params = ();
87 $sqldatewhere .= " AND reservedate >= ?";
88 push @query_params, output_pref({ dt => $startdate, dateformat => 'iso' }) ;
89 $sqldatewhere .= " AND reservedate <= ?";
90 push @query_params, output_pref({ dt => $enddate, dateformat => 'iso' });
92 my $include_aqorders_qty =
93 $effective_create_items eq 'receiving'
94 ? '+ COALESCE(aqorders.quantity, 0) - COALESCE(aqorders.quantityreceived, 0)'
97 my $include_aqorders_qty_join =
98 $effective_create_items eq 'receiving'
99 ? 'LEFT JOIN aqorders ON reserves.biblionumber=aqorders.biblionumber'
102 my $nfl_comparison = $include_ordered ? '<=' : '=';
103 my $sus_comparison = $include_suspended ? '<=' : '<';
106 reserves.borrowernumber as borrowernumber,
107 reserves.biblionumber,
108 reserves.branchcode as branch,
110 items.itemcallnumber,
112 GROUP_CONCAT(DISTINCT items.itemcallnumber
113 ORDER BY items.itemnumber SEPARATOR '|') as listcall,
114 GROUP_CONCAT(DISTINCT homebranch
115 ORDER BY items.itemnumber SEPARATOR '|') as homebranch_list,
116 GROUP_CONCAT(DISTINCT holdingbranch
117 ORDER BY items.itemnumber SEPARATOR '|') as holdingbranch_list,
118 GROUP_CONCAT(DISTINCT items.location
119 ORDER BY items.itemnumber SEPARATOR '|') as l_location,
120 GROUP_CONCAT(DISTINCT items.itype
121 ORDER BY items.itemnumber SEPARATOR '|') as l_itype,
130 count(DISTINCT reserves.borrowernumber) as reservecount,
131 count(DISTINCT items.itemnumber) $include_aqorders_qty as itemcount
133 LEFT JOIN items ON items.biblionumber=reserves.biblionumber
134 LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber
135 $include_aqorders_qty_join
137 notforloan $nfl_comparison 0 AND damaged = 0 AND itemlost = 0 AND withdrawn = 0
138 AND suspend $sus_comparison 1
142 if (C4::Context->preference('IndependentBranches')){
143 $strsth .= " AND items.holdingbranch=? ";
144 push @query_params, C4::Context->userenv->{'branch'};
147 $strsth .= " GROUP BY reserves.biblionumber ORDER BY reservecount DESC";
149 $template->param(sql => $strsth);
150 my $sth = $dbh->prepare($strsth);
151 $sth->execute(@query_params);
154 while ( my $data = $sth->fetchrow_hashref ) {
155 my $thisratio = $data->{reservecount} / $data->{itemcount};
156 my $copies_to_buy = ceil($data->{reservecount}/$ratio - $data->{itemcount});
157 $thisratio >= $ratio or next; # TODO: tighter targeting -- get ratio limit into SQL using HAVING clause
161 reservedate => $data->{reservedate},
162 priority => $data->{priority},
163 name => $data->{borrower},
164 title => $data->{title},
165 subtitle => $data->{subtitle},
166 medium => $data->{medium},
167 part_number => $data->{part_number},
168 part_name => $data->{part_name},
169 author => $data->{author},
170 itemnum => $data->{itemnumber},
171 biblionumber => $data->{biblionumber},
172 holdingbranch => $data->{holdingbranch},
173 homebranch_list => [split('\|', $data->{homebranch_list})],
174 holdingbranch_list => [split('\|', $data->{holdingbranch_list})],
175 branch => $data->{branch},
176 itemcallnumber => $data->{itemcallnumber},
177 location => [split('\|', $data->{l_location})],
178 itype => [split('\|', $data->{l_itype})],
179 reservecount => $data->{reservecount},
180 itemcount => $data->{itemcount},
181 copies_to_buy => sprintf( "%d", $copies_to_buy ),
182 thisratio => sprintf( "%.2f", $thisratio ),
183 thisratio_atleast1 => ( $thisratio >= 1 ) ? 1 : 0,
184 listcall => [split('\|', $data->{listcall})]
189 for my $rd ( @reservedata ) {
190 next unless $rd->{biblionumber};
191 $rd->{pendingorders} = CountPendingOrdersByBiblionumber( $rd->{biblionumber} );
195 todaysdate => $todaysdate,
199 include_ordered => $include_ordered,
200 include_suspended => $include_suspended,
201 reserveloop => \@reservedata,
204 output_html_with_http_headers $input, $cookie, $template->output;
206 sub CountPendingOrdersByBiblionumber {
207 my $biblionumber = shift;
208 my @orders = GetOrdersByBiblionumber( $biblionumber );
210 if (scalar(@orders)) {
211 for my $order ( @orders ) {
212 next if $order->{datecancellationprinted};
213 my $onum = $order->{quantity} // 0;
214 my $rnum = $order->{quantityreceived} // 0;
215 next if $rnum >= $onum;
216 $cnt += ($onum - $rnum);