various date-related cleanups in circ
[koha.git] / circ / pendingreserves.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 # Modification by D.Ulm, actually works (as long as indep. branches not turned on)
22 #               Someone let me know what indep. branches is supposed to do and I'll make that part work too
23 #
24 #               The reserve pull lists *works* as long as not for indepencdant branches, I can fix!
25
26 use strict;
27 use C4::Context;
28 use C4::Output;
29 use CGI;
30 use C4::Auth;
31 use C4::Dates qw/format_date format_date_in_iso/;
32 use Date::Calc qw/Today Add_Delta_YMD/;
33
34 use vars qw($debug);
35
36 BEGIN {
37     $debug = $ENV{DEBUG} || 0;
38 }
39
40 my $input = new CGI;
41 my $order = $input->param('order');
42 my $startdate=$input->param('from');
43 my $enddate=$input->param('to');
44
45 my $theme = $input->param('theme');    # only used if allowthemeoverride is set
46
47 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
48     {
49         template_name   => "circ/pendingreserves.tmpl",
50         query           => $input,
51         type            => "intranet",
52         authnotrequired => 0,
53         flagsrequired   => { circulate => 1 },
54         debug           => 1,
55     }
56 );
57
58 my $duedate;
59 my $borrowernumber;
60 my $itemnum;
61 my $data1;
62 my $data2;
63 my $data3;
64 my $name;
65 my $phone;
66 my $email;
67 my $biblionumber;
68 my $title;
69 my $author;
70
71 my ( $year, $month, $day ) = Today();
72 my $todaysdate     = sprintf("%-04.4d-%-02.2d-%02.2d", $year, $month, $day);
73 my $yesterdaysdate = sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YMD($year, $month, $day,   0, 0, -1));
74 # Find 10 years ago for the default shelf pull start and end dates
75 #    A default of the prior day's holds is a reasonable way to pull holds 
76 my $pastdate       = sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YMD($year, $month, $day, -10, 0,  0));
77
78 #               Predefine the start and end dates if they are not already defined
79 $startdate =~ s/^\s+//;
80 $startdate =~ s/\s+$//;
81 $enddate =~ s/^\s+//;
82 $enddate =~ s/\s+$//;
83 #               Check if null, should string match, if so set start and end date to yesterday
84 if (!defined($startdate) or $startdate eq "") {
85         $startdate = format_date($pastdate);
86 }
87 if (!defined($enddate) or $enddate eq "") {
88         $enddate = format_date($yesterdaysdate);
89 }
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
99 if ($order eq "biblio") {
100         $sqlorderby = " order by biblio.title ";
101 } elsif ($order eq "itype") {
102         $sqlorderby = " order by l_itype, location, l_itemcallnumber ";
103 } elsif ($order eq "location") {
104         $sqlorderby = " order by location, l_itemcallnumber, holdingbranch ";
105 } elsif ($order eq "date") {
106     $sqlorderby = " order by l_reservedate, location, l_itemcallnumber ";
107 } elsif ($order eq "library") {
108     $sqlorderby = " order by holdingbranch, l_itemcallnumber, location ";
109 } elsif ($order eq "call") {
110     $sqlorderby = " order by l_itemcallnumber, holdingbranch, location ";    
111 } else {
112         $sqlorderby = " order by biblio.title ";
113 }
114 my $strsth =
115 "SELECT min(reservedate) as l_reservedate,
116         reserves.borrowernumber as borrowernumber,
117         GROUP_CONCAT(DISTINCT items.holdingbranch 
118                         ORDER BY items.itemnumber SEPARATOR '<br>') l_holdingbranch,
119         reserves.biblionumber,
120         reserves.branchcode,
121         GROUP_CONCAT(DISTINCT reserves.branchcode 
122                         ORDER BY items.itemnumber SEPARATOR ', ') l_branch,
123         items.holdingbranch as branch,
124         items.itemcallnumber,
125         GROUP_CONCAT(DISTINCT items.itype 
126                         ORDER BY items.itemnumber SEPARATOR '<br>') l_itype,
127         GROUP_CONCAT(DISTINCT items.location 
128                         ORDER BY items.itemnumber SEPARATOR '<br>') l_location,
129         GROUP_CONCAT(DISTINCT items.itemcallnumber 
130                         ORDER BY items.itemnumber SEPARATOR '<br>') l_itemcallnumber,
131         items.itemnumber,
132         notes,
133         notificationdate,
134         reminderdate,
135         max(priority) as priority,
136         reserves.found,
137         biblio.title,
138         biblio.author,
139         count(DISTINCT items.itemnumber) as icount,
140         count(DISTINCT reserves.borrowernumber) as rcount
141  FROM  reserves
142         LEFT JOIN items ON items.biblionumber=reserves.biblionumber 
143         LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber
144         LEFT JOIN branchtransfers ON items.itemnumber=branchtransfers.itemnumber
145  WHERE
146 reserves.found IS NULL
147  $sqldatewhere
148 AND items.itemnumber NOT IN (SELECT itemnumber FROM branchtransfers where datearrived IS NULL)
149 AND items.itemnumber NOT IN (SELECT itemnumber FROM issues)
150 AND reserves.priority <> 0 
151 AND reserves.itemnumber is NULL
152 AND notforloan = 0 AND damaged = 0 AND itemlost = 0 AND wthdrawn = 0
153 ";
154 # GROUP BY reserves.biblionumber allows only items that are not checked out, else multiples occur when 
155 #    multiple patrons have a hold on an item
156
157
158 if (C4::Context->preference('IndependantBranches')){
159         $strsth .= " AND items.holdingbranch=? ";
160 }
161 $strsth .= " GROUP BY reserves.biblionumber " . $sqlorderby;
162 my $sth = $dbh->prepare($strsth);
163
164 if (C4::Context->preference('IndependantBranches')){
165         $sth->execute(C4::Context->userenv->{'branch'});
166 }
167 else {
168         $sth->execute();
169 }       
170 my @reservedata;
171 my $previous;
172 my $this;
173 while ( my $data = $sth->fetchrow_hashref ) {
174     $this=$data->{biblionumber}.":".$data->{borrowernumber};
175     my @itemlist;
176     push(
177         @reservedata,
178         {
179             reservedate      => format_date( $data->{l_reservedate} ),
180             priority         => $data->{priority},
181             name             => $data->{l_patron},
182             title            => $data->{title},
183             author           => $data->{author},
184             borrowernumber   => $data->{borrowernumber},
185             itemnum          => $data->{itemnumber},
186             phone            => $data->{phone},
187             email            => $data->{email},
188             biblionumber     => $data->{biblionumber},
189             statusw          => ( $data->{found} eq "W" ),
190             statusf          => ( $data->{found} eq "F" ),
191             holdingbranch    => $data->{l_holdingbranch},
192             branch           => $data->{l_branch},
193             itemcallnumber   => $data->{l_itemcallnumber},
194             notes            => $data->{notes},
195             notificationdate => $data->{notificationdate},
196             reminderdate     => $data->{reminderdate},
197             count                                 => $data->{icount},
198             rcount                        => $data->{rcount},
199             pullcount             => $data->{icount} <= $data->{rcount} ? $data->{icount} : $data->{rcount},
200             itype                                 => $data->{l_itype},
201             location                      => $data->{l_location}
202         }
203     );
204     $previous=$this;
205 }
206
207 $sth->finish;
208
209 # *** I doubt any of this is needed now with the above fixes *** -d.u.
210
211 #$strsth=~ s/AND reserves.itemnumber is NULL/AND reserves.itemnumber is NOT NULL/;
212 #$strsth=~ s/LEFT JOIN items ON items.biblionumber=reserves.biblionumber/LEFT JOIN items ON items.biblionumber=reserves.itemnumber/;
213 #$sth = $dbh->prepare($strsth);
214 #if (C4::Context->preference('IndependantBranches')){
215 #       $sth->execute(C4::Context->userenv->{'branch'});
216 #}
217 #else {
218 #       $sth->execute();
219 #}
220 #while ( my $data = $sth->fetchrow_hashref ) {
221 #    $this=$data->{biblionumber}.":".$data->{borrowernumber};
222 #    my @itemlist;
223 #    push(
224 #        @reservedata,
225 #        {
226 #            reservedate      => format_date( $data->{l_reservedate} ),
227 #            priority         => $data->{priority},
228 #            name             => $data->{l_patron},
229 #            title            => $data->{title},
230 #            author           => $data->{author},
231 #            borrowernumber   => $data->{borrowernumber},
232 #            itemnum          => $data->{itemnumber},
233 #            phone            => $data->{phone},
234 #            email            => $data->{email},
235 #            biblionumber     => $data->{biblionumber},
236 #            statusw          => ( $data->{found} eq "W" ),
237 #            statusf          => ( $data->{found} eq "F" ),
238 #            holdingbranch    => $data->{l_holdingbranch},
239 #            branch           => $data->{l_branch},
240 #            itemcallnumber   => $data->{l_itemcallnumber},
241 #            notes            => $data->{notes},
242 #            notificationdate => $data->{notificationdate},
243 #            reminderdate     => $data->{reminderdate},
244 #            count                                => $data->{icount},
245 #            rcount                       => $data->{rcount},
246 #            pullcount            => $data->{icount} <= $data->{rcount} ? $data->{icount} : $data->{rcount},
247 #            itype                                => $data->{l_itype},
248 #            location                     => $data->{l_location},
249 #            thisitemonly     => 1,
250
251 #        }
252 #    );
253 #    $previous=$this;
254 #}
255 #$sth->finish;
256
257 $template->param(
258     todaysdate          => format_date($todaysdate),
259     from             => $startdate,
260     to                  => $enddate,
261     reserveloop         => \@reservedata,
262     "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
263     DHTMLcalendar_dateformat =>  C4::Dates->DHTMLcalendar(),
264 );
265
266 output_html_with_http_headers $input, $cookie, $template->output;