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
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 with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
26 use C4::Dates qw/format_date format_date_in_iso/;
28 use Date::Calc qw/Today Add_Delta_YM/;
31 my $order = $input->param('order');
32 my $startdate = $input->param('from');
33 my $enddate = $input->param('to');
34 my $ratio = $input->param('ratio');
36 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
38 template_name => "circ/reserveratios.tmpl",
42 flagsrequired => { circulate => "circulate_remaining_permissions" },
47 my ( $year, $month, $day ) = Today();
48 my $todaysdate = sprintf("%-04.4d-%-02.2d-%02.2d", $year, $month, $day);
49 # Find yesterday for the default shelf pull start and end dates
50 # A default of the prior years's holds is a reasonable way to pull holds
51 my $datelastyear = sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YM($year, $month, $day, -1, 0));
53 # Predefine the start and end dates if they are not already defined
54 # Check if null, should string match, if so set start and end date to yesterday
55 if (!defined($startdate) or $startdate !~ s/^\s*(\S+)\s*$/$1/) { # strip spaces, remove Taint
56 $startdate = format_date($datelastyear);
58 if (!defined($enddate) or $enddate !~ s/^\s*(\S+)\s*$/$1/) { # strip spaces, remove Taint
59 $enddate = format_date($todaysdate);
61 if (!defined($ratio) or $ratio !~ s/^\s*(0?\.?\d+)(\.0*)?\s*$/$1/) { # strip spaces, remove Taint
65 $ratio = 1; # prevent division by zero
68 my $dbh = C4::Context->dbh;
69 my ($sqlorderby, $sqldatewhere) = ("","");
70 $debug and warn format_date_in_iso($startdate) . "\n" . format_date_in_iso($enddate);
71 my @query_params = ();
73 $sqldatewhere .= " AND reservedate >= ?";
74 push @query_params, format_date_in_iso($startdate);
77 $sqldatewhere .= " AND reservedate <= ?";
78 push @query_params, format_date_in_iso($enddate);
81 if ($order eq "biblio") {
82 $sqlorderby = " ORDER BY biblio.title, holdingbranch, listcall, l_location ";
83 } elsif ($order eq "callnumber") {
84 $sqlorderby = " ORDER BY listcall, holdingbranch, l_location ";
85 } elsif ($order eq "itemcount") {
86 $sqlorderby = " ORDER BY itemcount, reservecount ";
87 } elsif ($order eq "itype") {
88 $sqlorderby = " ORDER BY l_itype, holdingbranch, listcall ";
89 } elsif ($order eq "location") {
90 $sqlorderby = " ORDER BY l_location, holdingbranch, listcall ";
91 } elsif ($order eq "reservecount") {
92 $sqlorderby = " ORDER BY reservecount DESC ";
93 } elsif ($order eq "branch") {
94 $sqlorderby = " ORDER BY holdingbranch, l_location, listcall ";
96 $sqlorderby = " ORDER BY reservecount DESC ";
100 reserves.borrowernumber as borrowernumber,
101 reserves.biblionumber,
102 reserves.branchcode as branch,
104 items.itemcallnumber,
106 GROUP_CONCAT(DISTINCT items.itemcallnumber
107 ORDER BY items.itemnumber SEPARATOR '<br/>') as listcall,
108 GROUP_CONCAT(DISTINCT holdingbranch
109 ORDER BY items.itemnumber SEPARATOR '<br/>') as listbranch,
110 GROUP_CONCAT(DISTINCT items.location
111 ORDER BY items.itemnumber SEPARATOR '<br/>') as l_location,
112 GROUP_CONCAT(DISTINCT items.itype
113 ORDER BY items.itemnumber SEPARATOR '<br/>') as l_itype,
118 count(DISTINCT reserves.borrowernumber) as reservecount,
119 count(DISTINCT items.itemnumber) as itemcount
121 LEFT JOIN items ON items.biblionumber=reserves.biblionumber
122 LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber
124 notforloan = 0 AND damaged = 0 AND itemlost = 0 AND wthdrawn = 0
128 if (C4::Context->preference('IndependantBranches')){
129 $strsth .= " AND items.holdingbranch=? ";
130 push @query_params, C4::Context->userenv->{'branch'};
133 $strsth .= " GROUP BY reserves.biblionumber " . $sqlorderby;
135 $template->param(sql => $strsth);
136 my $sth = $dbh->prepare($strsth);
137 $sth->execute(@query_params);
139 my $ratio_atleast1 = ($ratio >= 1) ? 1 : 0;
141 while ( my $data = $sth->fetchrow_hashref ) {
142 my $thisratio = $data->{reservecount} / $data->{itemcount};
143 my $ratiocalc = ($thisratio / $ratio);
144 ($thisratio / $ratio) >= 1 or next; # TODO: tighter targeting -- get ratio limit into SQL using HAVING clause
148 reservedate => format_date( $data->{reservedate} ),
149 priority => $data->{priority},
150 name => $data->{borrower},
151 title => $data->{title},
152 author => $data->{author},
153 notes => $data->{notes},
154 itemnum => $data->{itemnumber},
155 biblionumber => $data->{biblionumber},
156 holdingbranch => $data->{holdingbranch},
157 listbranch => $data->{listbranch},
158 branch => $data->{branch},
159 itemcallnumber => $data->{itemcallnumber},
160 location => $data->{l_location},
161 itype => $data->{l_itype},
162 reservecount => $data->{reservecount},
163 itemcount => $data->{itemcount},
164 ratiocalc => sprintf("%.0d", $ratio_atleast1 ? ($thisratio / $ratio) : $thisratio),
165 thisratio => sprintf("%.2f", $thisratio),
166 thisratio_atleast1 => ($thisratio >= 1) ? 1 : 0,
167 listcall => $data->{listcall}
173 ratio_atleast1 => $ratio_atleast1,
174 todaysdate => format_date($todaysdate),
178 reserveloop => \@reservedata,
179 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
182 output_html_with_http_headers $input, $cookie, $template->output;