disallow division by zero in hold ratio report
[koha.git] / circ / reserveratios.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
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.
12 #
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.
16 #
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
20
21 use strict;
22 use C4::Context;
23 use C4::Output;
24 use CGI;
25 use C4::Auth;
26 use C4::Dates qw/format_date format_date_in_iso/;
27
28 use vars qw($debug);
29
30 BEGIN {
31     $debug = $ENV{DEBUG} || 0;
32 }
33
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');
39
40 my $theme = $input->param('theme');    # only used if allowthemeoverride is set
41
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
43     {
44         template_name   => "circ/reserveratios.tmpl",
45         query           => $input,
46         type            => "intranet",
47         authnotrequired => 0,
48         flagsrequired   => { circulate => 1 },
49         debug           => 1,
50     }
51 );
52
53 my $duedate;
54 my $borrowernumber;
55 my $itemnum;
56 my $data1;
57 my $data2;
58 my $data3;
59 my $name;
60 my $phone;
61 my $email;
62 my $biblionumber;
63 my $title;
64 my $author;
65
66 my @datearr    = localtime( time() );
67 my 
68 $todaysdate =
69     ( 1900 + $datearr[5] ) . '-'
70   . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
71   . sprintf( "%0.2d", $datearr[3] );
72
73 # Find yesterday for the default shelf pull start and end dates
74 #    A defualt of the prior years's holds is a reasonable way to pull holds 
75 my @datearr_yesterday    = localtime( time() - 86400*365 );
76 my $yesterdaysdate =
77     ( 1900 + $datearr_yesterday[5] ) . '-'
78   . sprintf( "%0.2d", ( $datearr_yesterday[4] + 1 ) ) . '-'
79   . sprintf( "%0.2d", $datearr_yesterday[3] );
80
81 #               Predefine the start and end dates if they are not already defined
82 $startdate =~ s/^\s+//;
83 $startdate =~ s/\s+$//;
84 $enddate =~ s/^\s+//;
85 $enddate =~ s/\s+$//;
86 #               Check if null, should string match, if so set start and end date to yesterday
87 if (!defined($startdate) or $startdate eq "") {
88         $startdate = format_date($yesterdaysdate);
89 }
90 if (!defined($enddate) or $enddate eq "") {
91         $enddate = format_date($todaysdate);
92 }
93 if (!defined($ratio)  or $ratio eq "" or $ratio !~ /^\s*\d+\s*$/ ) {
94         $ratio = 3;
95 }
96 if ($ratio == 0) {
97     $ratio = 1; # prevent division by zero
98 }
99
100 my $dbh    = C4::Context->dbh;
101 my ($sqlorderby, $sqldatewhere) = ("","");
102 $debug and warn format_date_in_iso($startdate) . "\n" . format_date_in_iso($enddate);
103 $sqldatewhere .= " AND reservedate >= " . $dbh->quote(format_date_in_iso($startdate))  if ($startdate) ;
104 $sqldatewhere .= " AND reservedate <= " . $dbh->quote(format_date_in_iso($enddate))  if ($enddate) ;
105
106 if ($order eq "biblio") {
107         $sqlorderby = " order by biblio.title, holdingbranch, listcall, l_location ";
108 } elsif ($order eq "callnumber") {
109     $sqlorderby = " order by listcall, holdingbranch, l_location ";
110 } elsif ($order eq "itemcount") {
111     $sqlorderby = " order by itemcount, reservecount ";
112 } elsif ($order eq "itype") {
113     $sqlorderby = " order by l_itype, holdingbranch, listcall ";
114 } elsif ($order eq "location") {
115     $sqlorderby = " order by l_location, holdingbranch, listcall ";
116 } elsif ($order eq "reservecount") {
117     $sqlorderby = " order by reservecount DESC ";
118 } elsif ($order eq "branch") {
119     $sqlorderby = " order by holdingbranch, l_location, listcall ";
120 } else {
121         $sqlorderby = " order by reservecount DESC ";
122 }
123 my $strsth =
124 "SELECT reservedate,
125         reserves.borrowernumber as borrowernumber,
126         reserves.biblionumber,
127         reserves.branchcode as branch,
128         items.holdingbranch,
129         items.itemcallnumber,
130         items.itemnumber,
131         GROUP_CONCAT(DISTINCT items.itemcallnumber 
132                         ORDER BY items.itemnumber SEPARATOR '<br>') as listcall,
133         GROUP_CONCAT(DISTINCT holdingbranch 
134                         ORDER BY items.itemnumber SEPARATOR '<br>') as listbranch,
135         GROUP_CONCAT(DISTINCT items.location 
136                         ORDER BY items.itemnumber SEPARATOR '<br>') as l_location,
137         GROUP_CONCAT(DISTINCT items.itype 
138                         ORDER BY items.itemnumber SEPARATOR '<br>') as l_itype,
139         notes,
140         reserves.found,
141         biblio.title,
142         biblio.author,
143         count(DISTINCT reserves.borrowernumber) as reservecount, 
144         count(DISTINCT items.itemnumber) as itemcount 
145  FROM  reserves
146  LEFT JOIN items ON items.biblionumber=reserves.biblionumber 
147  LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber
148  WHERE 
149 notforloan = 0 AND damaged = 0 AND itemlost = 0 AND wthdrawn = 0
150  $sqldatewhere
151 ";
152
153
154 if (C4::Context->preference('IndependantBranches')){
155         $strsth .= " AND items.holdingbranch=? ";
156 }
157 $strsth .= " GROUP BY reserves.biblionumber " . $sqlorderby;
158 my $sth = $dbh->prepare($strsth);
159
160 if (C4::Context->preference('IndependantBranches')){
161         $sth->execute(C4::Context->userenv->{'branch'});
162 }
163 else {
164         $sth->execute();
165 }       
166 my @reservedata;
167 while ( my $data = $sth->fetchrow_hashref ) {
168     my @itemlist;
169     my $ratiocalc =  int(10 * $data->{reservecount} / $data->{itemcount} / $ratio )/10;
170     push(
171         @reservedata,
172         {
173             reservedate      => format_date( $data->{reservedate} ),
174             priority         => $data->{priority},
175             name             => $data->{borrower},
176             title            => $data->{title},
177             author           => $data->{author},
178             notes                                 => $data->{notes},
179             itemnum          => $data->{itemnumber},
180             biblionumber     => $data->{biblionumber},
181             holdingbranch    => $data->{holdingbranch},
182             listbranch            => $data->{listbranch},
183             branch           => $data->{branch},
184             itemcallnumber   => $data->{itemcallnumber},
185             location                      => $data->{l_location},
186             itype                            => $data->{l_itype},
187             reservecount     => $data->{reservecount},
188             itemcount             => $data->{itemcount},
189             ratiocalc             => $ratiocalc,
190             ratio_ge_one          => $ratiocalc ge 1.0 ? 1 : "",
191             listcall              => $data->{listcall}    
192         }
193     );
194 }
195
196
197 $sth->finish;
198
199 $template->param(
200     todaysdate      => format_date($todaysdate),
201     from            => $startdate,
202     to              => $enddate,
203     ratio           => $ratio,
204     reserveloop     => \@reservedata,
205     "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
206     DHTMLcalendar_dateformat =>  C4::Dates->DHTMLcalendar(),
207 );
208
209 output_html_with_http_headers $input, $cookie, $template->output;