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