All patches to Koha 3 beta to date. All work with branches.
[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 "") {
94         $ratio = 3;
95 }
96
97 my $dbh    = C4::Context->dbh;
98 my ($sqlorderby, $sqldatewhere) = ("","");
99 $debug and warn format_date_in_iso($startdate) . "\n" . format_date_in_iso($enddate);
100 $sqldatewhere .= " AND reservedate >= " . $dbh->quote(format_date_in_iso($startdate))  if ($startdate) ;
101 $sqldatewhere .= " AND reservedate <= " . $dbh->quote(format_date_in_iso($enddate))  if ($enddate) ;
102
103 if ($order eq "biblio") {
104         $sqlorderby = " order by biblio.title, holdingbranch, listcall, l_location ";
105 } elsif ($order eq "callnumber") {
106     $sqlorderby = " order by listcall, holdingbranch, l_location ";
107 } elsif ($order eq "itemcount") {
108     $sqlorderby = " order by itemcount, reservecount ";
109 } elsif ($order eq "itype") {
110     $sqlorderby = " order by l_itype, holdingbranch, listcall ";
111 } elsif ($order eq "location") {
112     $sqlorderby = " order by l_location, holdingbranch, listcall ";
113 } elsif ($order eq "reservecount") {
114     $sqlorderby = " order by reservecount DESC ";
115 } elsif ($order eq "branch") {
116     $sqlorderby = " order by holdingbranch, l_location, listcall ";
117 } else {
118         $sqlorderby = " order by reservecount DESC ";
119 }
120 my $strsth =
121 "SELECT reservedate,
122         reserves.borrowernumber as borrowernumber,
123         reserves.biblionumber,
124         reserves.branchcode as branch,
125         items.holdingbranch,
126         items.itemcallnumber,
127         items.itemnumber,
128         GROUP_CONCAT(DISTINCT items.itemcallnumber 
129                         ORDER BY items.itemnumber SEPARATOR '<br>') as listcall,
130         GROUP_CONCAT(DISTINCT holdingbranch 
131                         ORDER BY items.itemnumber SEPARATOR '<br>') as listbranch,
132         GROUP_CONCAT(DISTINCT items.location 
133                         ORDER BY items.itemnumber SEPARATOR '<br>') as l_location,
134         GROUP_CONCAT(DISTINCT items.itype 
135                         ORDER BY items.itemnumber SEPARATOR '<br>') as l_itype,
136         notes,
137         reserves.found,
138         biblio.title,
139         biblio.author,
140         count(DISTINCT reserves.borrowernumber) as reservecount, 
141         count(DISTINCT items.itemnumber) as itemcount 
142  FROM  reserves
143  LEFT JOIN items ON items.biblionumber=reserves.biblionumber 
144  LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber
145  WHERE 
146 notforloan = 0 AND damaged = 0 AND itemlost = 0 AND wthdrawn = 0
147  $sqldatewhere
148 ";
149
150
151 if (C4::Context->preference('IndependantBranches')){
152         $strsth .= " AND items.holdingbranch=? ";
153 }
154 $strsth .= " GROUP BY reserves.biblionumber " . $sqlorderby;
155 my $sth = $dbh->prepare($strsth);
156
157 if (C4::Context->preference('IndependantBranches')){
158         $sth->execute(C4::Context->userenv->{'branch'});
159 }
160 else {
161         $sth->execute();
162 }       
163 my @reservedata;
164 while ( my $data = $sth->fetchrow_hashref ) {
165     my @itemlist;
166     my $ratiocalc =  int(10 * $data->{reservecount} / $data->{itemcount} / $ratio )/10;
167     push(
168         @reservedata,
169         {
170             reservedate      => format_date( $data->{reservedate} ),
171             priority         => $data->{priority},
172             name             => $data->{borrower},
173             title            => $data->{title},
174             author           => $data->{author},
175             notes                                 => $data->{notes},
176             itemnum          => $data->{itemnumber},
177             biblionumber     => $data->{biblionumber},
178             holdingbranch    => $data->{holdingbranch},
179             listbranch            => $data->{listbranch},
180             branch           => $data->{branch},
181             itemcallnumber   => $data->{itemcallnumber},
182             location                      => $data->{l_location},
183             itype                            => $data->{l_itype},
184             reservecount     => $data->{reservecount},
185             itemcount             => $data->{itemcount},
186             ratiocalc             => $ratiocalc,
187             ratio_ge_one          => $ratiocalc ge 1.0 ? 1 : "",
188             listcall              => $data->{listcall}    
189         }
190     );
191 }
192
193
194 $sth->finish;
195
196 $template->param(
197     todaysdate      => format_date($todaysdate),
198     from            => $startdate,
199     to              => $enddate,
200     ratio           => $ratio,
201     reserveloop     => \@reservedata,
202     "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
203     DHTMLcalendar_dateformat =>  C4::Dates->DHTMLcalendar(),
204 );
205
206 output_html_with_http_headers $input, $cookie, $template->output;