Bug 12973: Additional unit tests for XSLT_Handler.t
[koha.git] / circ / reserveratios.pl
blob9585660e7100bc7f45e09ba0de77fb3580b818d9
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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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::Dates qw/format_date format_date_in_iso/;
31 use C4::Debug;
32 use C4::Biblio qw/GetMarcBiblio GetRecordValue GetFrameworkCode/;
33 use C4::Acquisition qw/GetOrdersByBiblionumber/;
35 my $input = new CGI;
36 my $startdate = $input->param('from');
37 my $enddate = $input->param('to');
38 my $ratio = $input->param('ratio');
40 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
42 template_name => "circ/reserveratios.tt",
43 query => $input,
44 type => "intranet",
45 authnotrequired => 0,
46 flagsrequired => { circulate => "circulate_remaining_permissions" },
47 debug => 1,
51 my $booksellerid = $input->param('booksellerid') // '';
52 my $basketno = $input->param('basketno') // '';
53 if ($booksellerid && $basketno) {
54 $template->param( booksellerid => $booksellerid, basketno => $basketno );
57 my ( $year, $month, $day ) = Today();
58 my $todaysdate = sprintf("%-04.4d-%-02.2d-%02.2d", $year, $month, $day);
59 # Find yesterday for the default shelf pull start and end dates
60 # A default of the prior years's holds is a reasonable way to pull holds
61 my $datelastyear = sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YM($year, $month, $day, -1, 0));
63 # Predefine the start and end dates if they are not already defined
64 # Check if null, should string match, if so set start and end date to yesterday
65 if (!defined($startdate) or $startdate !~ s/^\s*(\S+)\s*$/$1/) { # strip spaces, remove Taint
66 $startdate = format_date($datelastyear);
68 if (!defined($enddate) or $enddate !~ s/^\s*(\S+)\s*$/$1/) { # strip spaces, remove Taint
69 $enddate = format_date($todaysdate);
71 if (!defined($ratio)) {
72 $ratio = 3;
74 # Force to be a number
75 $ratio += 0;
76 if ($ratio <= 0) {
77 $ratio = 1; # prevent division by zero
80 my $dbh = C4::Context->dbh;
81 my $sqldatewhere = "";
82 $debug and warn format_date_in_iso($startdate) . "\n" . format_date_in_iso($enddate);
83 my @query_params = ();
84 if ($startdate) {
85 $sqldatewhere .= " AND reservedate >= ?";
86 push @query_params, format_date_in_iso($startdate);
88 if ($enddate) {
89 $sqldatewhere .= " AND reservedate <= ?";
90 push @query_params, format_date_in_iso($enddate);
93 my $strsth =
94 "SELECT reservedate,
95 reserves.borrowernumber as borrowernumber,
96 reserves.biblionumber,
97 reserves.branchcode as branch,
98 items.holdingbranch,
99 items.itemcallnumber,
100 items.itemnumber,
101 GROUP_CONCAT(DISTINCT items.itemcallnumber
102 ORDER BY items.itemnumber SEPARATOR '<br/>') as listcall,
103 GROUP_CONCAT(DISTINCT holdingbranch
104 ORDER BY items.itemnumber SEPARATOR '<br/>') as listbranch,
105 GROUP_CONCAT(DISTINCT items.location
106 ORDER BY items.itemnumber SEPARATOR '<br/>') as l_location,
107 GROUP_CONCAT(DISTINCT items.itype
108 ORDER BY items.itemnumber SEPARATOR '<br/>') as l_itype,
110 reserves.found,
111 biblio.title,
112 biblio.author,
113 count(DISTINCT reserves.borrowernumber) as reservecount,
114 count(DISTINCT items.itemnumber) as itemcount
115 FROM reserves
116 LEFT JOIN items ON items.biblionumber=reserves.biblionumber
117 LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber
118 WHERE
119 notforloan = 0 AND damaged = 0 AND itemlost = 0 AND withdrawn = 0
120 $sqldatewhere
123 if (C4::Context->preference('IndependentBranches')){
124 $strsth .= " AND items.holdingbranch=? ";
125 push @query_params, C4::Context->userenv->{'branch'};
128 $strsth .= " GROUP BY reserves.biblionumber ORDER BY reservecount DESC";
130 $template->param(sql => $strsth);
131 my $sth = $dbh->prepare($strsth);
132 $sth->execute(@query_params);
134 my $ratio_atleast1 = ($ratio >= 1) ? 1 : 0;
135 my @reservedata;
136 while ( my $data = $sth->fetchrow_hashref ) {
137 my $thisratio = $data->{reservecount} / $data->{itemcount};
138 my $ratiocalc = ($thisratio / $ratio);
139 ($thisratio / $ratio) >= 1 or next; # TODO: tighter targeting -- get ratio limit into SQL using HAVING clause
140 my $record = GetMarcBiblio($data->{biblionumber});
141 $data->{subtitle} = GetRecordValue('subtitle', $record, GetFrameworkCode($data->{biblionumber}));
142 push(
143 @reservedata,
145 reservedate => format_date( $data->{reservedate} ),
146 priority => $data->{priority},
147 name => $data->{borrower},
148 title => $data->{title},
149 subtitle => $data->{subtitle},
150 author => $data->{author},
151 itemnum => $data->{itemnumber},
152 biblionumber => $data->{biblionumber},
153 holdingbranch => $data->{holdingbranch},
154 listbranch => $data->{listbranch},
155 branch => $data->{branch},
156 itemcallnumber => $data->{itemcallnumber},
157 location => $data->{l_location},
158 itype => $data->{l_itype},
159 reservecount => $data->{reservecount},
160 itemcount => $data->{itemcount},
161 ratiocalc => sprintf("%.0d", $ratio_atleast1 ? ($thisratio / $ratio) : $thisratio),
162 thisratio => sprintf("%.2f", $thisratio),
163 thisratio_atleast1 => ($thisratio >= 1) ? 1 : 0,
164 listcall => $data->{listcall}
169 for my $rd ( @reservedata ) {
170 next unless $rd->{biblionumber};
171 $rd->{pendingorders} = CountPendingOrdersByBiblionumber( $rd->{biblionumber} );
174 $template->param(
175 ratio_atleast1 => $ratio_atleast1,
176 todaysdate => format_date($todaysdate),
177 from => $startdate,
178 to => $enddate,
179 ratio => $ratio,
180 reserveloop => \@reservedata,
183 output_html_with_http_headers $input, $cookie, $template->output;
185 sub CountPendingOrdersByBiblionumber {
186 my $biblionumber = shift;
187 my @orders = GetOrdersByBiblionumber( $biblionumber );
188 my $cnt = 0;
189 if (scalar(@orders)) {
190 for my $order ( @orders ) {
191 next if $order->{datecancellationprinted};
192 my $onum = $order->{quantity} // 0;
193 my $rnum = $order->{quantityreceived} // 0;
194 next if $rnum >= $onum;
195 $cnt += ($onum - $rnum);
198 return $cnt;