Bug 5204 Followup
[koha.git] / circ / reserveratios.pl
blob8807339f0d8eeb3c0c2328e4c6f73b1c51d57bb8
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 C4::Context;
25 use C4::Output;
26 use CGI;
27 use C4::Auth;
28 use C4::Dates qw/format_date format_date_in_iso/;
29 use C4::Debug;
30 use Date::Calc qw/Today Add_Delta_YM/;
32 my $input = new CGI;
33 my $order = $input->param('order') || '';
34 my $startdate = $input->param('from');
35 my $enddate = $input->param('to');
36 my $ratio = $input->param('ratio');
38 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
40 template_name => "circ/reserveratios.tmpl",
41 query => $input,
42 type => "intranet",
43 authnotrequired => 0,
44 flagsrequired => { circulate => "circulate_remaining_permissions" },
45 debug => 1,
49 my ( $year, $month, $day ) = Today();
50 my $todaysdate = sprintf("%-04.4d-%-02.2d-%02.2d", $year, $month, $day);
51 # Find yesterday for the default shelf pull start and end dates
52 # A default of the prior years's holds is a reasonable way to pull holds
53 my $datelastyear = sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YM($year, $month, $day, -1, 0));
55 # Predefine the start and end dates if they are not already defined
56 # Check if null, should string match, if so set start and end date to yesterday
57 if (!defined($startdate) or $startdate !~ s/^\s*(\S+)\s*$/$1/) { # strip spaces, remove Taint
58 $startdate = format_date($datelastyear);
60 if (!defined($enddate) or $enddate !~ s/^\s*(\S+)\s*$/$1/) { # strip spaces, remove Taint
61 $enddate = format_date($todaysdate);
63 if (!defined($ratio) or $ratio !~ s/^\s*(0?\.?\d+)(\.0*)?\s*$/$1/) { # strip spaces, remove Taint
64 $ratio = 3;
66 if ($ratio == 0) {
67 $ratio = 1; # prevent division by zero
70 my $dbh = C4::Context->dbh;
71 my ($sqlorderby, $sqldatewhere) = ("","");
72 $debug and warn format_date_in_iso($startdate) . "\n" . format_date_in_iso($enddate);
73 my @query_params = ();
74 if ($startdate) {
75 $sqldatewhere .= " AND reservedate >= ?";
76 push @query_params, format_date_in_iso($startdate);
78 if ($enddate) {
79 $sqldatewhere .= " AND reservedate <= ?";
80 push @query_params, format_date_in_iso($enddate);
83 if ($order eq "biblio") {
84 $sqlorderby = " ORDER BY biblio.title, holdingbranch, listcall, l_location ";
85 } elsif ($order eq "callnumber") {
86 $sqlorderby = " ORDER BY listcall, holdingbranch, l_location ";
87 } elsif ($order eq "itemcount") {
88 $sqlorderby = " ORDER BY itemcount, reservecount ";
89 } elsif ($order eq "itype") {
90 $sqlorderby = " ORDER BY l_itype, holdingbranch, listcall ";
91 } elsif ($order eq "location") {
92 $sqlorderby = " ORDER BY l_location, holdingbranch, listcall ";
93 } elsif ($order eq "reservecount") {
94 $sqlorderby = " ORDER BY reservecount DESC ";
95 } elsif ($order eq "branch") {
96 $sqlorderby = " ORDER BY holdingbranch, l_location, listcall ";
97 } else {
98 $sqlorderby = " ORDER BY reservecount DESC ";
100 my $strsth =
101 "SELECT reservedate,
102 reserves.borrowernumber as borrowernumber,
103 reserves.biblionumber,
104 reserves.branchcode as branch,
105 items.holdingbranch,
106 items.itemcallnumber,
107 items.itemnumber,
108 GROUP_CONCAT(DISTINCT items.itemcallnumber
109 ORDER BY items.itemnumber SEPARATOR '<br/>') as listcall,
110 GROUP_CONCAT(DISTINCT holdingbranch
111 ORDER BY items.itemnumber SEPARATOR '<br/>') as listbranch,
112 GROUP_CONCAT(DISTINCT items.location
113 ORDER BY items.itemnumber SEPARATOR '<br/>') as l_location,
114 GROUP_CONCAT(DISTINCT items.itype
115 ORDER BY items.itemnumber SEPARATOR '<br/>') as l_itype,
116 notes,
117 reserves.found,
118 biblio.title,
119 biblio.author,
120 count(DISTINCT reserves.borrowernumber) as reservecount,
121 count(DISTINCT items.itemnumber) as itemcount
122 FROM reserves
123 LEFT JOIN items ON items.biblionumber=reserves.biblionumber
124 LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber
125 WHERE
126 notforloan = 0 AND damaged = 0 AND itemlost = 0 AND wthdrawn = 0
127 $sqldatewhere
130 if (C4::Context->preference('IndependantBranches')){
131 $strsth .= " AND items.holdingbranch=? ";
132 push @query_params, C4::Context->userenv->{'branch'};
135 $strsth .= " GROUP BY reserves.biblionumber " . $sqlorderby;
137 $template->param(sql => $strsth);
138 my $sth = $dbh->prepare($strsth);
139 $sth->execute(@query_params);
141 my $ratio_atleast1 = ($ratio >= 1) ? 1 : 0;
142 my @reservedata;
143 while ( my $data = $sth->fetchrow_hashref ) {
144 my $thisratio = $data->{reservecount} / $data->{itemcount};
145 my $ratiocalc = ($thisratio / $ratio);
146 ($thisratio / $ratio) >= 1 or next; # TODO: tighter targeting -- get ratio limit into SQL using HAVING clause
147 push(
148 @reservedata,
150 reservedate => format_date( $data->{reservedate} ),
151 priority => $data->{priority},
152 name => $data->{borrower},
153 title => $data->{title},
154 author => $data->{author},
155 notes => $data->{notes},
156 itemnum => $data->{itemnumber},
157 biblionumber => $data->{biblionumber},
158 holdingbranch => $data->{holdingbranch},
159 listbranch => $data->{listbranch},
160 branch => $data->{branch},
161 itemcallnumber => $data->{itemcallnumber},
162 location => $data->{l_location},
163 itype => $data->{l_itype},
164 reservecount => $data->{reservecount},
165 itemcount => $data->{itemcount},
166 ratiocalc => sprintf("%.0d", $ratio_atleast1 ? ($thisratio / $ratio) : $thisratio),
167 thisratio => sprintf("%.2f", $thisratio),
168 thisratio_atleast1 => ($thisratio >= 1) ? 1 : 0,
169 listcall => $data->{listcall}
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,
181 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
184 output_html_with_http_headers $input, $cookie, $template->output;