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