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