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