Bug #2162: Step 1, prevent running of report upon entry to page. Add name run_report...
[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 C4::Debug;
33 use Date::Calc qw/Today Add_Delta_YMD/;
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 $run_report=$input->param('run_report');
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/pendingreserves.tmpl",
46         query           => $input,
47         type            => "intranet",
48         authnotrequired => 0,
49         flagsrequired   => { circulate => "circulate_remaining_permissions" },
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 my $yesterdaysdate = sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YMD($year, $month, $day,   0, 0, -1));
70 #changed from delivered range of 10 years-yesterday to 2 days ago-today
71 # Find two days ago for the default shelf pull start and end dates
72 my $pastdate       = sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YMD($year, $month, $day, 0, 0, -2));
73
74 #               Predefine the start and end dates if they are not already defined
75 $startdate =~ s/^\s+//;
76 $startdate =~ s/\s+$//;
77 $enddate =~ s/^\s+//;
78 $enddate =~ s/\s+$//;
79 #               Check if null, should string match, if so set start and end date to yesterday
80 if (!defined($startdate) or $startdate eq "") {
81         $startdate = format_date($pastdate);
82 }
83 if (!defined($enddate) or $enddate eq "") {
84         $enddate = format_date($todaysdate);
85 }
86
87
88 my @reservedata;
89 if ( $run_report ) {
90     my $dbh    = C4::Context->dbh;
91     my ($sqlorderby, $sqldatewhere) = ("","");
92     $debug and warn format_date_in_iso($startdate) . "\n" . format_date_in_iso($enddate);
93     my @query_params = ();
94     if ($startdate) {
95         $sqldatewhere .= " AND reservedate >= ?";
96         push @query_params, format_date_in_iso($startdate);
97     }
98     if ($enddate) {
99         $sqldatewhere .= " AND reservedate <= ?";
100         push @query_params, format_date_in_iso($enddate);
101     }
102
103     if ($order eq "biblio") {
104         $sqlorderby = " ORDER BY biblio.title ";
105     } elsif ($order eq "itype") {
106         $sqlorderby = " ORDER BY l_itype, location, l_itemcallnumber ";
107     } elsif ($order eq "location") {
108         $sqlorderby = " ORDER BY location, l_itemcallnumber, holdingbranch ";
109     } elsif ($order eq "date") {
110         $sqlorderby = " ORDER BY l_reservedate, location, l_itemcallnumber ";
111     } elsif ($order eq "library") {
112         $sqlorderby = " ORDER BY holdingbranch, l_itemcallnumber, location ";
113     } elsif ($order eq "call") {
114         $sqlorderby = " ORDER BY l_itemcallnumber, holdingbranch, location ";    
115     } else {
116         $sqlorderby = " ORDER BY biblio.title ";
117     }
118     my $strsth =
119     "SELECT min(reservedate) as l_reservedate,
120             reserves.borrowernumber as borrowernumber,
121             GROUP_CONCAT(DISTINCT items.holdingbranch 
122                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_holdingbranch,
123             reserves.biblionumber,
124             reserves.branchcode,
125             GROUP_CONCAT(DISTINCT reserves.branchcode 
126                     ORDER BY items.itemnumber SEPARATOR ', ') l_branch,
127             items.holdingbranch as branch,
128             items.itemcallnumber,
129             GROUP_CONCAT(DISTINCT items.itype 
130                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_itype,
131             GROUP_CONCAT(DISTINCT items.location 
132                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_location,
133             GROUP_CONCAT(DISTINCT items.itemcallnumber 
134                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_itemcallnumber,
135             items.itemnumber,
136             notes,
137             notificationdate,
138             reminderdate,
139             max(priority) as priority,
140             reserves.found,
141             biblio.title,
142             biblio.author,
143             count(DISTINCT items.itemnumber) as icount,
144             count(DISTINCT reserves.borrowernumber) as rcount
145     FROM  reserves
146         LEFT JOIN items ON items.biblionumber=reserves.biblionumber 
147         LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber
148         LEFT JOIN branchtransfers ON items.itemnumber=branchtransfers.itemnumber
149         LEFT JOIN issues ON items.itemnumber=issues.itemnumber
150     WHERE
151     reserves.found IS NULL
152     $sqldatewhere
153     AND items.itemnumber NOT IN (SELECT itemnumber FROM branchtransfers where datearrived IS NULL)
154     AND issues.itemnumber IS NULL
155     AND reserves.priority <> 0 
156     AND notforloan = 0 AND damaged = 0 AND itemlost = 0 AND wthdrawn = 0
157     ";
158     # GROUP BY reserves.biblionumber allows only items that are not checked out, else multiples occur when 
159     #    multiple patrons have a hold on an item
160
161
162     if (C4::Context->preference('IndependantBranches')){
163         $strsth .= " AND items.holdingbranch=? ";
164         push @query_params, C4::Context->userenv->{'branch'};
165     }
166     $strsth .= " GROUP BY reserves.biblionumber " . $sqlorderby;
167
168     my $sth = $dbh->prepare($strsth);
169     $sth->execute(@query_params);
170
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
258 $template->param(
259     todaysdate          => format_date($todaysdate),
260     from                => $startdate,
261     to                  => $enddate,
262     run_report          => $run_report,
263     reserveloop         => \@reservedata,
264     "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
265     DHTMLcalendar_dateformat =>  C4::Dates->DHTMLcalendar(),
266         dateformat    => C4::Context->preference("dateformat"),
267 );
268
269 output_html_with_http_headers $input, $cookie, $template->output;