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