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