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