Bug 10550: Fix database typo wthdrawn
[koha.git] / circ / reserveratios.pl
blob55eb01825a389d707051ab0ea70d4becdedaf2ae
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;
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/;
34 my $input = new CGI;
35 my $startdate = $input->param('from');
36 my $enddate = $input->param('to');
37 my $ratio = $input->param('ratio');
39 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
41 template_name => "circ/reserveratios.tmpl",
42 query => $input,
43 type => "intranet",
44 authnotrequired => 0,
45 flagsrequired => { circulate => "circulate_remaining_permissions" },
46 debug => 1,
50 my ( $year, $month, $day ) = Today();
51 my $todaysdate = sprintf("%-04.4d-%-02.2d-%02.2d", $year, $month, $day);
52 # Find yesterday for the default shelf pull start and end dates
53 # A default of the prior years's holds is a reasonable way to pull holds
54 my $datelastyear = sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YM($year, $month, $day, -1, 0));
56 # Predefine the start and end dates if they are not already defined
57 # Check if null, should string match, if so set start and end date to yesterday
58 if (!defined($startdate) or $startdate !~ s/^\s*(\S+)\s*$/$1/) { # strip spaces, remove Taint
59 $startdate = format_date($datelastyear);
61 if (!defined($enddate) or $enddate !~ s/^\s*(\S+)\s*$/$1/) { # strip spaces, remove Taint
62 $enddate = format_date($todaysdate);
64 if (!defined($ratio)) {
65 $ratio = 3;
67 # Force to be a number
68 $ratio += 0;
69 if ($ratio <= 0) {
70 $ratio = 1; # prevent division by zero
73 my $dbh = C4::Context->dbh;
74 my $sqldatewhere = "";
75 $debug and warn format_date_in_iso($startdate) . "\n" . format_date_in_iso($enddate);
76 my @query_params = ();
77 if ($startdate) {
78 $sqldatewhere .= " AND reservedate >= ?";
79 push @query_params, format_date_in_iso($startdate);
81 if ($enddate) {
82 $sqldatewhere .= " AND reservedate <= ?";
83 push @query_params, format_date_in_iso($enddate);
86 my $strsth =
87 "SELECT reservedate,
88 reserves.borrowernumber as borrowernumber,
89 reserves.biblionumber,
90 reserves.branchcode as branch,
91 items.holdingbranch,
92 items.itemcallnumber,
93 items.itemnumber,
94 GROUP_CONCAT(DISTINCT items.itemcallnumber
95 ORDER BY items.itemnumber SEPARATOR '<br/>') as listcall,
96 GROUP_CONCAT(DISTINCT holdingbranch
97 ORDER BY items.itemnumber SEPARATOR '<br/>') as listbranch,
98 GROUP_CONCAT(DISTINCT items.location
99 ORDER BY items.itemnumber SEPARATOR '<br/>') as l_location,
100 GROUP_CONCAT(DISTINCT items.itype
101 ORDER BY items.itemnumber SEPARATOR '<br/>') as l_itype,
102 notes,
103 reserves.found,
104 biblio.title,
105 biblio.author,
106 count(DISTINCT reserves.borrowernumber) as reservecount,
107 count(DISTINCT items.itemnumber) as itemcount
108 FROM reserves
109 LEFT JOIN items ON items.biblionumber=reserves.biblionumber
110 LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber
111 WHERE
112 notforloan = 0 AND damaged = 0 AND itemlost = 0 AND withdrawn = 0
113 $sqldatewhere
116 if (C4::Context->preference('IndependentBranches')){
117 $strsth .= " AND items.holdingbranch=? ";
118 push @query_params, C4::Context->userenv->{'branch'};
121 $strsth .= " GROUP BY reserves.biblionumber ORDER BY reservecount DESC";
123 $template->param(sql => $strsth);
124 my $sth = $dbh->prepare($strsth);
125 $sth->execute(@query_params);
127 my $ratio_atleast1 = ($ratio >= 1) ? 1 : 0;
128 my @reservedata;
129 while ( my $data = $sth->fetchrow_hashref ) {
130 my $thisratio = $data->{reservecount} / $data->{itemcount};
131 my $ratiocalc = ($thisratio / $ratio);
132 ($thisratio / $ratio) >= 1 or next; # TODO: tighter targeting -- get ratio limit into SQL using HAVING clause
133 my $record = GetMarcBiblio($data->{biblionumber});
134 $data->{subtitle} = GetRecordValue('subtitle', $record, GetFrameworkCode($data->{biblionumber}));
135 push(
136 @reservedata,
138 reservedate => format_date( $data->{reservedate} ),
139 priority => $data->{priority},
140 name => $data->{borrower},
141 title => $data->{title},
142 subtitle => $data->{subtitle},
143 author => $data->{author},
144 notes => $data->{notes},
145 itemnum => $data->{itemnumber},
146 biblionumber => $data->{biblionumber},
147 holdingbranch => $data->{holdingbranch},
148 listbranch => $data->{listbranch},
149 branch => $data->{branch},
150 itemcallnumber => $data->{itemcallnumber},
151 location => $data->{l_location},
152 itype => $data->{l_itype},
153 reservecount => $data->{reservecount},
154 itemcount => $data->{itemcount},
155 ratiocalc => sprintf("%.0d", $ratio_atleast1 ? ($thisratio / $ratio) : $thisratio),
156 thisratio => sprintf("%.2f", $thisratio),
157 thisratio_atleast1 => ($thisratio >= 1) ? 1 : 0,
158 listcall => $data->{listcall}
163 $template->param(
164 ratio_atleast1 => $ratio_atleast1,
165 todaysdate => format_date($todaysdate),
166 from => $startdate,
167 to => $enddate,
168 ratio => $ratio,
169 reserveloop => \@reservedata,
172 output_html_with_http_headers $input, $cookie, $template->output;