Merge remote-tracking branch 'origin/new/bug_5668'
[koha.git] / circ / reserveratios.pl
blob1517adfb97223db13b44b1cccf56aaa8483c53f4
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 $order = $input->param('order') || '';
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.tmpl",
43 query => $input,
44 type => "intranet",
45 authnotrequired => 0,
46 flagsrequired => { circulate => "circulate_remaining_permissions" },
47 debug => 1,
51 my ( $year, $month, $day ) = Today();
52 my $todaysdate = sprintf("%-04.4d-%-02.2d-%02.2d", $year, $month, $day);
53 # Find yesterday for the default shelf pull start and end dates
54 # A default of the prior years's holds is a reasonable way to pull holds
55 my $datelastyear = sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YM($year, $month, $day, -1, 0));
57 # Predefine the start and end dates if they are not already defined
58 # Check if null, should string match, if so set start and end date to yesterday
59 if (!defined($startdate) or $startdate !~ s/^\s*(\S+)\s*$/$1/) { # strip spaces, remove Taint
60 $startdate = format_date($datelastyear);
62 if (!defined($enddate) or $enddate !~ s/^\s*(\S+)\s*$/$1/) { # strip spaces, remove Taint
63 $enddate = format_date($todaysdate);
65 if (!defined($ratio)) {
66 $ratio = 3;
68 # Force to be a number
69 $ratio += 0;
70 if ($ratio <= 0) {
71 $ratio = 1; # prevent division by zero
74 my $dbh = C4::Context->dbh;
75 my ($sqlorderby, $sqldatewhere) = ("","");
76 $debug and warn format_date_in_iso($startdate) . "\n" . format_date_in_iso($enddate);
77 my @query_params = ();
78 if ($startdate) {
79 $sqldatewhere .= " AND reservedate >= ?";
80 push @query_params, format_date_in_iso($startdate);
82 if ($enddate) {
83 $sqldatewhere .= " AND reservedate <= ?";
84 push @query_params, format_date_in_iso($enddate);
87 if ($order eq "biblio") {
88 $sqlorderby = " ORDER BY biblio.title, holdingbranch, listcall, l_location ";
89 } elsif ($order eq "callnumber") {
90 $sqlorderby = " ORDER BY listcall, holdingbranch, l_location ";
91 } elsif ($order eq "itemcount") {
92 $sqlorderby = " ORDER BY itemcount, reservecount ";
93 } elsif ($order eq "itype") {
94 $sqlorderby = " ORDER BY l_itype, holdingbranch, listcall ";
95 } elsif ($order eq "location") {
96 $sqlorderby = " ORDER BY l_location, holdingbranch, listcall ";
97 } elsif ($order eq "reservecount") {
98 $sqlorderby = " ORDER BY reservecount DESC ";
99 } elsif ($order eq "branch") {
100 $sqlorderby = " ORDER BY holdingbranch, l_location, listcall ";
101 } else {
102 $sqlorderby = " ORDER BY reservecount DESC ";
104 my $strsth =
105 "SELECT reservedate,
106 reserves.borrowernumber as borrowernumber,
107 reserves.biblionumber,
108 reserves.branchcode as branch,
109 items.holdingbranch,
110 items.itemcallnumber,
111 items.itemnumber,
112 GROUP_CONCAT(DISTINCT items.itemcallnumber
113 ORDER BY items.itemnumber SEPARATOR '<br/>') as listcall,
114 GROUP_CONCAT(DISTINCT holdingbranch
115 ORDER BY items.itemnumber SEPARATOR '<br/>') as listbranch,
116 GROUP_CONCAT(DISTINCT items.location
117 ORDER BY items.itemnumber SEPARATOR '<br/>') as l_location,
118 GROUP_CONCAT(DISTINCT items.itype
119 ORDER BY items.itemnumber SEPARATOR '<br/>') as l_itype,
120 notes,
121 reserves.found,
122 biblio.title,
123 biblio.author,
124 count(DISTINCT reserves.borrowernumber) as reservecount,
125 count(DISTINCT items.itemnumber) as itemcount
126 FROM reserves
127 LEFT JOIN items ON items.biblionumber=reserves.biblionumber
128 LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber
129 WHERE
130 notforloan = 0 AND damaged = 0 AND itemlost = 0 AND wthdrawn = 0
131 $sqldatewhere
134 if (C4::Context->preference('IndependantBranches')){
135 $strsth .= " AND items.holdingbranch=? ";
136 push @query_params, C4::Context->userenv->{'branch'};
139 $strsth .= " GROUP BY reserves.biblionumber " . $sqlorderby;
141 $template->param(sql => $strsth);
142 my $sth = $dbh->prepare($strsth);
143 $sth->execute(@query_params);
145 my $ratio_atleast1 = ($ratio >= 1) ? 1 : 0;
146 my @reservedata;
147 while ( my $data = $sth->fetchrow_hashref ) {
148 my $thisratio = $data->{reservecount} / $data->{itemcount};
149 my $ratiocalc = ($thisratio / $ratio);
150 ($thisratio / $ratio) >= 1 or next; # TODO: tighter targeting -- get ratio limit into SQL using HAVING clause
151 my $record = GetMarcBiblio($data->{biblionumber});
152 $data->{subtitle} = GetRecordValue('subtitle', $record, GetFrameworkCode($data->{biblionumber}));
153 push(
154 @reservedata,
156 reservedate => format_date( $data->{reservedate} ),
157 priority => $data->{priority},
158 name => $data->{borrower},
159 title => $data->{title},
160 subtitle => $data->{subtitle},
161 author => $data->{author},
162 notes => $data->{notes},
163 itemnum => $data->{itemnumber},
164 biblionumber => $data->{biblionumber},
165 holdingbranch => $data->{holdingbranch},
166 listbranch => $data->{listbranch},
167 branch => $data->{branch},
168 itemcallnumber => $data->{itemcallnumber},
169 location => $data->{l_location},
170 itype => $data->{l_itype},
171 reservecount => $data->{reservecount},
172 itemcount => $data->{itemcount},
173 ratiocalc => sprintf("%.0d", $ratio_atleast1 ? ($thisratio / $ratio) : $thisratio),
174 thisratio => sprintf("%.2f", $thisratio),
175 thisratio_atleast1 => ($thisratio >= 1) ? 1 : 0,
176 listcall => $data->{listcall}
181 $template->param(
182 ratio_atleast1 => $ratio_atleast1,
183 todaysdate => format_date($todaysdate),
184 from => $startdate,
185 to => $enddate,
186 ratio => $ratio,
187 reserveloop => \@reservedata,
188 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
191 output_html_with_http_headers $input, $cookie, $template->output;