Merge remote-tracking branch 'origin/new/bug_7453'
[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             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             GROUP_CONCAT(DISTINCT items.enumchron
144                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_enumchron,
145             GROUP_CONCAT(DISTINCT items.copynumber
146                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_copynumber,
147             items.itemnumber,
148             notes,
149             notificationdate,
150             reminderdate,
151             max(priority) as priority,
152             reserves.found,
153             biblio.title,
154             biblio.author,
155             count(DISTINCT items.itemnumber) as icount,
156             count(DISTINCT reserves.borrowernumber) as rcount
157     FROM  reserves
158         LEFT JOIN items ON items.biblionumber=reserves.biblionumber 
159         LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber
160         LEFT JOIN branchtransfers ON items.itemnumber=branchtransfers.itemnumber
161         LEFT JOIN issues ON items.itemnumber=issues.itemnumber
162     WHERE
163     reserves.found IS NULL
164     $sqldatewhere
165     AND (reserves.itemnumber IS NULL OR reserves.itemnumber = items.itemnumber)
166     AND items.itemnumber NOT IN (SELECT itemnumber FROM branchtransfers where datearrived IS NULL)
167     AND issues.itemnumber IS NULL
168     AND reserves.priority <> 0 
169     AND notforloan = 0 AND damaged = 0 AND itemlost = 0 AND wthdrawn = 0
170     ";
171     # GROUP BY reserves.biblionumber allows only items that are not checked out, else multiples occur when 
172     #    multiple patrons have a hold on an item
173
174
175     if (C4::Context->preference('IndependantBranches')){
176         $strsth .= " AND items.holdingbranch=? ";
177         push @query_params, C4::Context->userenv->{'branch'};
178     }
179     $strsth .= " GROUP BY reserves.biblionumber " . $sqlorderby;
180
181     my $sth = $dbh->prepare($strsth);
182     $sth->execute(@query_params);
183
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                 enumchron        => $data->{l_enumchron},
208                 copyno           => $data->{l_copynumber},
209                 notes            => $data->{notes},
210                 notificationdate => $data->{notificationdate},
211                 reminderdate     => $data->{reminderdate},
212                 count                             => $data->{icount},
213                 rcount                    => $data->{rcount},
214                 pullcount                 => $data->{icount} <= $data->{rcount} ? $data->{icount} : $data->{rcount},
215                 itype                             => $data->{l_itype},
216                 location                          => $data->{l_location}
217             }
218         );
219         $previous=$this;
220     }
221
222     $sth->finish;
223
224     # Next Page?
225     if ($report_page > 0) {
226         $prev_results = $report_page  - 1;
227     }
228     if ( scalar(@reservedata) > 250 ) {
229         $next_results = $report_page + 1;
230         pop(@reservedata); # .. we retrieved 251 results
231     }
232     if ($prev_results || $next_results) {
233         $next_or_previous = 1;
234     }
235
236     # *** I doubt any of this is needed now with the above fixes *** -d.u.
237
238     #$strsth=~ s/AND reserves.itemnumber is NULL/AND reserves.itemnumber is NOT NULL/;
239     #$strsth=~ s/LEFT JOIN items ON items.biblionumber=reserves.biblionumber/LEFT JOIN items ON items.biblionumber=reserves.itemnumber/;
240     #$sth = $dbh->prepare($strsth);
241     #if (C4::Context->preference('IndependantBranches')){
242     #       $sth->execute(C4::Context->userenv->{'branch'});
243     #}
244     #else {
245     #       $sth->execute();
246     #}
247     #while ( my $data = $sth->fetchrow_hashref ) {
248     #    $this=$data->{biblionumber}.":".$data->{borrowernumber};
249     #    my @itemlist;
250     #    push(
251     #        @reservedata,
252     #        {
253     #            reservedate      => format_date( $data->{l_reservedate} ),
254     #            priority         => $data->{priority},
255     #            name             => $data->{l_patron},
256     #            title            => $data->{title},
257     #            author           => $data->{author},
258     #            borrowernumber   => $data->{borrowernumber},
259     #            itemnum          => $data->{itemnumber},
260     #            phone            => $data->{phone},
261     #            email            => $data->{email},
262     #            biblionumber     => $data->{biblionumber},
263     #            statusw          => ( $data->{found} eq "W" ),
264     #            statusf          => ( $data->{found} eq "F" ),
265     #            holdingbranch    => $data->{l_holdingbranch},
266     #            branch           => $data->{l_branch},
267     #            itemcallnumber   => $data->{l_itemcallnumber},
268     #            notes            => $data->{notes},
269     #            notificationdate => $data->{notificationdate},
270     #            reminderdate     => $data->{reminderdate},
271     #            count                            => $data->{icount},
272     #            rcount                   => $data->{rcount},
273     #            pullcount                => $data->{icount} <= $data->{rcount} ? $data->{icount} : $data->{rcount},
274     #            itype                            => $data->{l_itype},
275     #            location                         => $data->{l_location},
276     #            thisitemonly     => 1,
277     # 
278     #        }
279     #    );
280     #    $previous=$this;
281     #}
282     #$sth->finish;
283 }
284
285 $template->param(
286     todaysdate          => format_date($todaysdate),
287     from                => $startdate,
288     to                  => $enddate,
289     run_report          => $run_report,
290     report_page         => $report_page,
291     prev_results        => $prev_results,
292     next_results        => $next_results,
293     next_or_previous    => $next_or_previous,
294     reserveloop         => \@reservedata,
295     "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
296     DHTMLcalendar_dateformat =>  C4::Dates->DHTMLcalendar(),
297         dateformat    => C4::Context->preference("dateformat"),
298 );
299
300 output_html_with_http_headers $input, $cookie, $template->output;