Bug 15049: (Signed off) Hide currency but preserv alert
[koha.git] / circ / reserveratios.pl
blobbd51b7c2d7557521d972b680f486a6d8ad47459c
1 #!/usr/bin/perl
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>.
21 use strict;
22 use warnings;
24 use CGI qw ( -utf8 );
25 use Date::Calc qw/Today Add_Delta_YM/;
27 use C4::Context;
28 use C4::Output;
29 use C4::Auth;
30 use C4::Debug;
31 use C4::Biblio qw/GetMarcBiblio GetRecordValue GetFrameworkCode/;
32 use C4::Acquisition qw/GetOrdersByBiblionumber/;
33 use Koha::DateUtils;
35 my $input = new CGI;
36 my $startdate = $input->param('from');
37 my $enddate = $input->param('to');
38 my $ratio = $input->param('ratio');
39 my $include_ordered = $input->param('include_ordered');
41 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
43 template_name => "circ/reserveratios.tt",
44 query => $input,
45 type => "intranet",
46 authnotrequired => 0,
47 flagsrequired => { circulate => "circulate_remaining_permissions" },
48 debug => 1,
52 my $booksellerid = $input->param('booksellerid') // '';
53 my $basketno = $input->param('basketno') // '';
54 if ($booksellerid && $basketno) {
55 $template->param( booksellerid => $booksellerid, basketno => $basketno );
58 $startdate = eval { dt_from_string( $startdate ) } if $startdate;
59 $enddate = eval { dt_from_string( $enddate ) } if $enddate;
61 my $todaysdate = dt_from_string;
63 # A default of the prior years's holds is a reasonable way to pull holds
64 $enddate = $todaysdate unless $enddate;
65 $startdate = $todaysdate->clone->subtract( years => 1 ) unless $startdate;
67 if (!defined($ratio)) {
68 $ratio = 3;
70 # Force to be a number
71 $ratio += 0;
72 if ($ratio <= 0) {
73 $ratio = 1; # prevent division by zero
76 my $dbh = C4::Context->dbh;
77 my $sqldatewhere = "";
78 $debug and warn output_pref({ dt => $startdate, dateformat => 'iso', dateonly => 1 }) . "\n" . output_pref({ dt => $enddate, dateformat => 'iso', dateonly => 1 });
79 my @query_params = ();
81 $sqldatewhere .= " AND reservedate >= ?";
82 push @query_params, output_pref({ dt => $startdate, dateformat => 'iso' }) ;
83 $sqldatewhere .= " AND reservedate <= ?";
84 push @query_params, output_pref({ dt => $enddate, dateformat => 'iso' });
86 my $nfl_comparison = $include_ordered ? '<=' : '=';
87 my $strsth =
88 "SELECT reservedate,
89 reserves.borrowernumber as borrowernumber,
90 reserves.biblionumber,
91 reserves.branchcode as branch,
92 items.holdingbranch,
93 items.itemcallnumber,
94 items.itemnumber,
95 GROUP_CONCAT(DISTINCT items.itemcallnumber
96 ORDER BY items.itemnumber SEPARATOR '<br/>') as listcall,
97 GROUP_CONCAT(DISTINCT homebranch
98 ORDER BY items.itemnumber SEPARATOR '<br/>') as homebranch_list,
99 GROUP_CONCAT(DISTINCT holdingbranch
100 ORDER BY items.itemnumber SEPARATOR '<br/>') as holdingbranch_list,
101 GROUP_CONCAT(DISTINCT items.location
102 ORDER BY items.itemnumber SEPARATOR '<br/>') as l_location,
103 GROUP_CONCAT(DISTINCT items.itype
104 ORDER BY items.itemnumber SEPARATOR '<br/>') as l_itype,
106 reserves.found,
107 biblio.title,
108 biblio.author,
109 count(DISTINCT reserves.borrowernumber) as reservecount,
110 count(DISTINCT items.itemnumber) as itemcount
111 FROM reserves
112 LEFT JOIN items ON items.biblionumber=reserves.biblionumber
113 LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber
114 WHERE
115 notforloan $nfl_comparison 0 AND damaged = 0 AND itemlost = 0 AND withdrawn = 0
116 $sqldatewhere
119 if (C4::Context->preference('IndependentBranches')){
120 $strsth .= " AND items.holdingbranch=? ";
121 push @query_params, C4::Context->userenv->{'branch'};
124 $strsth .= " GROUP BY reserves.biblionumber ORDER BY reservecount DESC";
126 $template->param(sql => $strsth);
127 my $sth = $dbh->prepare($strsth);
128 $sth->execute(@query_params);
130 my $ratio_atleast1 = ($ratio >= 1) ? 1 : 0;
131 my @reservedata;
132 while ( my $data = $sth->fetchrow_hashref ) {
133 my $thisratio = $data->{reservecount} / $data->{itemcount};
134 my $ratiocalc = ($thisratio / $ratio);
135 ($thisratio / $ratio) >= 1 or next; # TODO: tighter targeting -- get ratio limit into SQL using HAVING clause
136 my $record = GetMarcBiblio($data->{biblionumber});
137 $data->{subtitle} = GetRecordValue('subtitle', $record, GetFrameworkCode($data->{biblionumber}));
138 push(
139 @reservedata,
141 reservedate => $data->{reservedate},
142 priority => $data->{priority},
143 name => $data->{borrower},
144 title => $data->{title},
145 subtitle => $data->{subtitle},
146 author => $data->{author},
147 itemnum => $data->{itemnumber},
148 biblionumber => $data->{biblionumber},
149 holdingbranch => $data->{holdingbranch},
150 homebranch_list => $data->{homebranch_list},
151 holdingbranch_list => $data->{holdingbranch_list},
152 branch => $data->{branch},
153 itemcallnumber => $data->{itemcallnumber},
154 location => $data->{l_location},
155 itype => $data->{l_itype},
156 reservecount => $data->{reservecount},
157 itemcount => $data->{itemcount},
158 ratiocalc => sprintf( "%.0d", $ratio_atleast1 ? ( $thisratio / $ratio ) : $thisratio ),
159 thisratio => sprintf( "%.2f", $thisratio ),
160 thisratio_atleast1 => ( $thisratio >= 1 ) ? 1 : 0,
161 listcall => $data->{listcall}
166 for my $rd ( @reservedata ) {
167 next unless $rd->{biblionumber};
168 $rd->{pendingorders} = CountPendingOrdersByBiblionumber( $rd->{biblionumber} );
171 $template->param(
172 ratio_atleast1 => $ratio_atleast1,
173 todaysdate => $todaysdate,
174 from => $startdate,
175 to => $enddate,
176 ratio => $ratio,
177 include_ordered => $include_ordered,
178 reserveloop => \@reservedata,
181 output_html_with_http_headers $input, $cookie, $template->output;
183 sub CountPendingOrdersByBiblionumber {
184 my $biblionumber = shift;
185 my @orders = GetOrdersByBiblionumber( $biblionumber );
186 my $cnt = 0;
187 if (scalar(@orders)) {
188 for my $order ( @orders ) {
189 next if $order->{datecancellationprinted};
190 my $onum = $order->{quantity} // 0;
191 my $rnum = $order->{quantityreceived} // 0;
192 next if $rnum >= $onum;
193 $cnt += ($onum - $rnum);
196 return $cnt;