Revert "Bug 10606: Remove MySQLism in GetUpcomingDueIssues"
[koha.git] / circ / pendingreserves.pl
blobf964074c3bdaf0c4bf99dac734d92322cd76ab6a
1 #!/usr/bin/perl
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
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.
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.
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.
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
24 # The reserve pull lists *works* as long as not for indepencdant branches, I can fix!
26 use strict;
27 #use warnings; FIXME - Bug 2505
29 use constant TWO_DAYS => 2;
30 use constant TWO_DAYS_AGO => -2;
32 use C4::Context;
33 use C4::Output;
34 use CGI qw ( -utf8 );
35 use C4::Auth;
36 use C4::Dates qw/format_date format_date_in_iso/;
37 use C4::Debug;
38 use Date::Calc qw/Today Add_Delta_YMD/;
40 my $input = new CGI;
41 my $startdate=$input->param('from');
42 my $enddate=$input->param('to');
43 my $run_report = ( not defined $input->param('run_report') ) ? 1 : $input->param('run_report');
45 my $theme = $input->param('theme'); # only used if allowthemeoverride is set
47 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
49 template_name => "circ/pendingreserves.tt",
50 query => $input,
51 type => "intranet",
52 authnotrequired => 0,
53 flagsrequired => { circulate => "circulate_remaining_permissions" },
54 debug => 1,
58 my $duedate;
59 my $borrowernumber;
60 my $itemnum;
61 my $data1;
62 my $data2;
63 my $data3;
64 my $name;
65 my $phone;
66 my $email;
67 my $biblionumber;
68 my $title;
69 my $author;
71 my ( $year, $month, $day ) = Today();
72 my $todaysdate = sprintf("%-04.4d-%-02.2d-%02.2d", $year, $month, $day);
73 $startdate =~ s/^\s+//;
74 $startdate =~ s/\s+$//;
75 $enddate =~ s/^\s+//;
76 $enddate =~ s/\s+$//;
78 if (!defined($startdate) or $startdate eq "") {
79 # changed from delivered range of 10 years-yesterday to 2 days ago-today
80 # Find two days ago for the default shelf pull start date, unless HoldsToPullStartDate sys pref is set.
81 my $pastdate= sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YMD($year, $month, $day, 0, 0, -C4::Context->preference('HoldsToPullStartDate')||TWO_DAYS_AGO ));
82 $startdate = format_date($pastdate);
85 if (!defined($enddate) or $enddate eq "") {
86 #similarly: calculate end date with ConfirmFutureHolds (days)
87 my $d=sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YMD($year, $month, $day, 0, 0, C4::Context->preference('ConfirmFutureHolds')||0 ));
88 $enddate = format_date($d);
91 my @reservedata;
92 if ( $run_report ) {
93 my $dbh = C4::Context->dbh;
94 my $sqldatewhere = "";
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);
101 if ($enddate) {
102 $sqldatewhere .= " AND reservedate <= ?";
103 push @query_params, format_date_in_iso($enddate);
106 my $strsth =
107 "SELECT min(reservedate) as l_reservedate,
108 reserves.borrowernumber as borrowernumber,
109 GROUP_CONCAT(DISTINCT items.holdingbranch
110 ORDER BY items.itemnumber SEPARATOR '<br/>') l_holdingbranch,
111 reserves.biblionumber,
112 reserves.branchcode,
113 GROUP_CONCAT(DISTINCT reserves.branchcode
114 ORDER BY items.itemnumber SEPARATOR ', ') l_branch,
115 items.holdingbranch as branch,
116 GROUP_CONCAT(DISTINCT items.itype
117 ORDER BY items.itemnumber SEPARATOR '<br/>') l_itype,
118 GROUP_CONCAT(DISTINCT items.location
119 ORDER BY items.itemnumber SEPARATOR '<br/>') l_location,
120 GROUP_CONCAT(DISTINCT items.itemcallnumber
121 ORDER BY items.itemnumber SEPARATOR '<br/>') l_itemcallnumber,
122 GROUP_CONCAT(DISTINCT items.enumchron
123 ORDER BY items.itemnumber SEPARATOR '<br/>') l_enumchron,
124 GROUP_CONCAT(DISTINCT items.copynumber
125 ORDER BY items.itemnumber SEPARATOR '<br/>') l_copynumber,
126 items.itemnumber,
127 notificationdate,
128 reminderdate,
129 max(priority) as priority,
130 reserves.found,
131 biblio.title,
132 biblio.author,
133 count(DISTINCT items.itemnumber) as icount,
134 count(DISTINCT reserves.borrowernumber) as rcount
135 FROM reserves
136 LEFT JOIN items ON items.biblionumber=reserves.biblionumber
137 LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber
138 LEFT JOIN branchtransfers ON items.itemnumber=branchtransfers.itemnumber
139 LEFT JOIN issues ON items.itemnumber=issues.itemnumber
140 WHERE
141 reserves.found IS NULL
142 $sqldatewhere
143 AND (reserves.itemnumber IS NULL OR reserves.itemnumber = items.itemnumber)
144 AND items.itemnumber NOT IN (SELECT itemnumber FROM branchtransfers where datearrived IS NULL)
145 AND items.itemnumber NOT IN (select itemnumber FROM reserves where found='W')
146 AND issues.itemnumber IS NULL
147 AND reserves.priority <> 0
148 AND reserves.suspend = 0
149 AND notforloan = 0 AND damaged = 0 AND itemlost = 0 AND withdrawn = 0
151 # GROUP BY reserves.biblionumber allows only items that are not checked out, else multiples occur when
152 # multiple patrons have a hold on an item
155 if (C4::Context->preference('IndependentBranches')){
156 $strsth .= " AND items.holdingbranch=? ";
157 push @query_params, C4::Context->userenv->{'branch'};
159 $strsth .= " GROUP BY reserves.biblionumber ORDER BY biblio.title ";
161 my $sth = $dbh->prepare($strsth);
162 $sth->execute(@query_params);
164 while ( my $data = $sth->fetchrow_hashref ) {
165 push(
166 @reservedata,
168 reservedate => $data->{l_reservedate},
169 priority => $data->{priority},
170 name => $data->{l_patron},
171 title => $data->{title},
172 author => $data->{author},
173 borrowernumber => $data->{borrowernumber},
174 itemnum => $data->{itemnumber},
175 phone => $data->{phone},
176 email => $data->{email},
177 biblionumber => $data->{biblionumber},
178 statusw => ( $data->{found} eq "W" ),
179 statusf => ( $data->{found} eq "F" ),
180 holdingbranch => $data->{l_holdingbranch},
181 branch => $data->{l_branch},
182 itemcallnumber => $data->{l_itemcallnumber},
183 enumchron => $data->{l_enumchron},
184 copyno => $data->{l_copynumber},
185 notificationdate=> $data->{notificationdate},
186 reminderdate => $data->{reminderdate},
187 count => $data->{icount},
188 rcount => $data->{rcount},
189 pullcount => $data->{icount} <= $data->{rcount} ? $data->{icount} : $data->{rcount},
190 itype => $data->{l_itype},
191 location => $data->{l_location},
195 $sth->finish;
198 $template->param(
199 todaysdate => $todaysdate,
200 from => $startdate,
201 to => $enddate,
202 run_report => $run_report,
203 reserveloop => \@reservedata,
204 "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
205 HoldsToPullStartDate=> C4::Context->preference('HoldsToPullStartDate')||TWO_DAYS,
206 HoldsToPullEndDate => C4::Context->preference('ConfirmFutureHolds')||0,
209 output_html_with_http_headers $input, $cookie, $template->output;