Merge remote branch 'kc/new/enh/bug_5917' into kcmaster
[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
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 warnings; FIXME - Bug 2505
28 use C4::Context;
29 use C4::Output;
30 use CGI;
31 use C4::Auth;
32 use C4::Dates qw/format_date format_date_in_iso/;
33 use C4::Debug;
34 use Date::Calc qw/Today Add_Delta_YMD/;
35
36 my $input = new CGI;
37 my $order = $input->param('order');
38 my $startdate=$input->param('from');
39 my $enddate=$input->param('to');
40 my $run_report=$input->param('run_report');
41 my $report_page=$input->param('report_page');
42
43 my $theme = $input->param('theme');    # only used if allowthemeoverride is set
44
45 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
46     {
47         template_name   => "circ/pendingreserves.tmpl",
48         query           => $input,
49         type            => "intranet",
50         authnotrequired => 0,
51         flagsrequired   => { circulate => "circulate_remaining_permissions" },
52         debug           => 1,
53     }
54 );
55
56 my $duedate;
57 my $borrowernumber;
58 my $itemnum;
59 my $data1;
60 my $data2;
61 my $data3;
62 my $name;
63 my $phone;
64 my $email;
65 my $biblionumber;
66 my $title;
67 my $author;
68
69 my ( $year, $month, $day ) = Today();
70 my $todaysdate     = sprintf("%-04.4d-%-02.2d-%02.2d", $year, $month, $day);
71 my $yesterdaysdate = sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YMD($year, $month, $day,   0, 0, -1));
72 #changed from delivered range of 10 years-yesterday to 2 days ago-today
73 # Find two days ago for the default shelf pull start and end dates
74 my $pastdate       = sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YMD($year, $month, $day, 0, 0, -2));
75
76 #               Predefine the start and end dates if they are not already defined
77 $startdate =~ s/^\s+//;
78 $startdate =~ s/\s+$//;
79 $enddate =~ s/^\s+//;
80 $enddate =~ s/\s+$//;
81 #               Check if null, should string match, if so set start and end date to yesterday
82 if (!defined($startdate) or $startdate eq "") {
83         $startdate = format_date($pastdate);
84 }
85 if (!defined($enddate) or $enddate eq "") {
86         $enddate = format_date($todaysdate);
87 }
88
89
90 my @reservedata;
91 my ($prev_results, $next_results, $next_or_previous) = (0,0,0);
92 if ( $run_report ) {
93     my $dbh    = C4::Context->dbh;
94     my ($sqlorderby, $sqldatewhere, $sqllimitoffset) = ("","","");
95     $debug and warn format_date_in_iso($startdate) . "\n" . format_date_in_iso($enddate);
96     my @query_params = ();
97     if ($startdate) {
98         $sqldatewhere .= " AND reservedate >= ?";
99         push @query_params, format_date_in_iso($startdate);
100     }
101     if ($enddate) {
102         $sqldatewhere .= " AND reservedate <= ?";
103         push @query_params, format_date_in_iso($enddate);
104     }
105
106     $sqllimitoffset = " LIMIT 251";
107     if ($report_page) {
108         $sqllimitoffset  .= " OFFSET=?";
109         push @query_params, ($report_page * 250);
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         LEFT JOIN issues ON items.itemnumber=issues.itemnumber
159     WHERE
160     reserves.found IS NULL
161     $sqldatewhere
162     AND items.itemnumber NOT IN (SELECT itemnumber FROM branchtransfers where datearrived IS NULL)
163     AND issues.itemnumber IS NULL
164     AND reserves.priority <> 0 
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         push @query_params, C4::Context->userenv->{'branch'};
174     }
175     $strsth .= " GROUP BY reserves.biblionumber " . $sqlorderby;
176
177     my $sth = $dbh->prepare($strsth);
178     $sth->execute(@query_params);
179
180     my $previous;
181     my $this;
182     while ( my $data = $sth->fetchrow_hashref ) {
183         $this=$data->{biblionumber}.":".$data->{borrowernumber};
184         my @itemlist;
185         push(
186             @reservedata,
187             {
188                 reservedate      => format_date( $data->{l_reservedate} ),
189                 priority         => $data->{priority},
190                 name             => $data->{l_patron},
191                 title            => $data->{title},
192                 author           => $data->{author},
193                 borrowernumber   => $data->{borrowernumber},
194                 itemnum          => $data->{itemnumber},
195                 phone            => $data->{phone},
196                 email            => $data->{email},
197                 biblionumber     => $data->{biblionumber},
198                 statusw          => ( $data->{found} eq "W" ),
199                 statusf          => ( $data->{found} eq "F" ),
200                 holdingbranch    => $data->{l_holdingbranch},
201                 branch           => $data->{l_branch},
202                 itemcallnumber   => $data->{l_itemcallnumber},
203                 notes            => $data->{notes},
204                 notificationdate => $data->{notificationdate},
205                 reminderdate     => $data->{reminderdate},
206                 count                             => $data->{icount},
207                 rcount                    => $data->{rcount},
208                 pullcount                 => $data->{icount} <= $data->{rcount} ? $data->{icount} : $data->{rcount},
209                 itype                             => $data->{l_itype},
210                 location                          => $data->{l_location}
211             }
212         );
213         $previous=$this;
214     }
215
216     $sth->finish;
217
218     # Next Page?
219     if ($report_page > 0) {
220         $prev_results = $report_page  - 1;
221     }
222     if ( scalar(@reservedata) > 250 ) {
223         $next_results = $report_page + 1;
224         pop(@reservedata); # .. we retrieved 251 results
225     }
226     if ($prev_results || $next_results) {
227         $next_or_previous = 1;
228     }
229
230     # *** I doubt any of this is needed now with the above fixes *** -d.u.
231
232     #$strsth=~ s/AND reserves.itemnumber is NULL/AND reserves.itemnumber is NOT NULL/;
233     #$strsth=~ s/LEFT JOIN items ON items.biblionumber=reserves.biblionumber/LEFT JOIN items ON items.biblionumber=reserves.itemnumber/;
234     #$sth = $dbh->prepare($strsth);
235     #if (C4::Context->preference('IndependantBranches')){
236     #       $sth->execute(C4::Context->userenv->{'branch'});
237     #}
238     #else {
239     #       $sth->execute();
240     #}
241     #while ( my $data = $sth->fetchrow_hashref ) {
242     #    $this=$data->{biblionumber}.":".$data->{borrowernumber};
243     #    my @itemlist;
244     #    push(
245     #        @reservedata,
246     #        {
247     #            reservedate      => format_date( $data->{l_reservedate} ),
248     #            priority         => $data->{priority},
249     #            name             => $data->{l_patron},
250     #            title            => $data->{title},
251     #            author           => $data->{author},
252     #            borrowernumber   => $data->{borrowernumber},
253     #            itemnum          => $data->{itemnumber},
254     #            phone            => $data->{phone},
255     #            email            => $data->{email},
256     #            biblionumber     => $data->{biblionumber},
257     #            statusw          => ( $data->{found} eq "W" ),
258     #            statusf          => ( $data->{found} eq "F" ),
259     #            holdingbranch    => $data->{l_holdingbranch},
260     #            branch           => $data->{l_branch},
261     #            itemcallnumber   => $data->{l_itemcallnumber},
262     #            notes            => $data->{notes},
263     #            notificationdate => $data->{notificationdate},
264     #            reminderdate     => $data->{reminderdate},
265     #            count                            => $data->{icount},
266     #            rcount                   => $data->{rcount},
267     #            pullcount                => $data->{icount} <= $data->{rcount} ? $data->{icount} : $data->{rcount},
268     #            itype                            => $data->{l_itype},
269     #            location                         => $data->{l_location},
270     #            thisitemonly     => 1,
271     # 
272     #        }
273     #    );
274     #    $previous=$this;
275     #}
276     #$sth->finish;
277 }
278
279 $template->param(
280     todaysdate          => format_date($todaysdate),
281     from                => $startdate,
282     to                  => $enddate,
283     run_report          => $run_report,
284     report_page         => $report_page,
285     prev_results        => $prev_results,
286     next_results        => $next_results,
287     next_or_previous    => $next_or_previous,
288     reserveloop         => \@reservedata,
289     "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
290     DHTMLcalendar_dateformat =>  C4::Dates->DHTMLcalendar(),
291         dateformat    => C4::Context->preference("dateformat"),
292 );
293
294 output_html_with_http_headers $input, $cookie, $template->output;