Bug 20249: (bug 18789 follow-up) "Patron has no outstanding fines" now appears alongs...
[koha.git] / circ / pendingreserves.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use constant PULL_INTERVAL => 2;
23
24 use C4::Context;
25 use C4::Output;
26 use CGI qw ( -utf8 );
27 use C4::Auth;
28 use Koha::Biblios;
29 use C4::Debug;
30 use Koha::DateUtils;
31 use DateTime::Duration;
32
33 my $input = new CGI;
34 my $startdate = $input->param('from');
35 my $enddate = $input->param('to');
36
37 my $theme = $input->param('theme');    # only used if allowthemeoverride is set
38
39 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
40     {
41         template_name   => "circ/pendingreserves.tt",
42         query           => $input,
43         type            => "intranet",
44         authnotrequired => 0,
45         flagsrequired   => { circulate => "circulate_remaining_permissions" },
46         debug           => 1,
47     }
48 );
49
50 my $today = dt_from_string;
51
52 if ( $startdate ) {
53     $startdate =~ s/^\s+//;
54     $startdate =~ s/\s+$//;
55     $startdate = eval{dt_from_string( $startdate )};
56 }
57 unless ( $startdate ){
58     # changed from delivered range of 10 years-yesterday to 2 days ago-today
59     # Find two days ago for the default shelf pull start date, unless HoldsToPullStartDate sys pref is set.
60     $startdate = $today - DateTime::Duration->new( days => C4::Context->preference('HoldsToPullStartDate') || PULL_INTERVAL );
61 }
62
63 if ( $enddate ) {
64     $enddate =~ s/^\s+//;
65     $enddate =~ s/\s+$//;
66     $enddate = eval{dt_from_string( $enddate )};
67 }
68 unless ( $enddate ) {
69     #similarly: calculate end date with ConfirmFutureHolds (days)
70     $enddate = $today + DateTime::Duration->new( days => C4::Context->preference('ConfirmFutureHolds') || 0 );
71 }
72
73 my @reservedata;
74 my $dbh = C4::Context->dbh;
75 my $sqldatewhere = "";
76 my $startdate_iso = output_pref({ dt => $startdate, dateformat => 'iso', dateonly => 1 });
77 my $enddate_iso   = output_pref({ dt => $enddate, dateformat => 'iso', dateonly => 1 });
78
79 $debug and warn $startdate_iso. "\n" . $enddate_iso;
80
81 my @query_params = ();
82
83 if ($startdate_iso) {
84     $sqldatewhere .= " AND reservedate >= ?";
85     push @query_params, $startdate_iso;
86 }
87 if ($enddate_iso) {
88     $sqldatewhere .= " AND reservedate <= ?";
89     push @query_params, $enddate_iso;
90 }
91
92 my $strsth =
93     "SELECT min(reservedate) as l_reservedate,
94             reserves.borrowernumber as borrowernumber,
95             GROUP_CONCAT(DISTINCT items.holdingbranch 
96                     ORDER BY items.itemnumber SEPARATOR '|') l_holdingbranch,
97             reserves.biblionumber,
98             reserves.branchcode as l_branch,
99             GROUP_CONCAT(DISTINCT items.itype 
100                     ORDER BY items.itemnumber SEPARATOR '|') l_itype,
101             GROUP_CONCAT(DISTINCT items.location 
102                     ORDER BY items.itemnumber SEPARATOR '|') l_location,
103             GROUP_CONCAT(DISTINCT items.itemcallnumber 
104                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_itemcallnumber,
105             GROUP_CONCAT(DISTINCT items.enumchron
106                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_enumchron,
107             GROUP_CONCAT(DISTINCT items.copynumber
108                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_copynumber,
109             biblio.title,
110             biblio.author,
111             count(DISTINCT items.itemnumber) as icount,
112             count(DISTINCT reserves.borrowernumber) as rcount,
113             borrowers.firstname,
114             borrowers.surname
115     FROM  reserves
116         LEFT JOIN items ON items.biblionumber=reserves.biblionumber 
117         LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber
118         LEFT JOIN branchtransfers ON items.itemnumber=branchtransfers.itemnumber
119         LEFT JOIN issues ON items.itemnumber=issues.itemnumber
120         LEFT JOIN borrowers ON reserves.borrowernumber=borrowers.borrowernumber
121     WHERE
122     reserves.found IS NULL
123     $sqldatewhere
124     AND (reserves.itemnumber IS NULL OR reserves.itemnumber = items.itemnumber)
125     AND items.itemnumber NOT IN (SELECT itemnumber FROM branchtransfers where datearrived IS NULL)
126     AND items.itemnumber NOT IN (select itemnumber FROM reserves where found IS NOT NULL)
127     AND issues.itemnumber IS NULL
128     AND reserves.priority <> 0 
129     AND reserves.suspend = 0
130     AND notforloan = 0 AND damaged = 0 AND itemlost = 0 AND withdrawn = 0
131     ";
132     # GROUP BY reserves.biblionumber allows only items that are not checked out, else multiples occur when 
133     #    multiple patrons have a hold on an item
134
135
136 if (C4::Context->preference('IndependentBranches')){
137     $strsth .= " AND items.holdingbranch=? ";
138     push @query_params, C4::Context->userenv->{'branch'};
139 }
140 $strsth .= " GROUP BY reserves.biblionumber ORDER BY biblio.title ";
141
142 my $sth = $dbh->prepare($strsth);
143 $sth->execute(@query_params);
144
145 while ( my $data = $sth->fetchrow_hashref ) {
146     my $record = Koha::Biblios->find($data->{biblionumber});
147     if ($record){
148         $data->{subtitle} = [ $record->subtitles ];
149     }
150     push(
151         @reservedata, {
152             reservedate     => $data->{l_reservedate},
153             firstname       => $data->{firstname} || '',
154             surname         => $data->{surname},
155             title           => $data->{title},
156             subtitle        => $data->{subtitle},
157             author          => $data->{author},
158             borrowernumber  => $data->{borrowernumber},
159             biblionumber    => $data->{biblionumber},
160             holdingbranches => [split('\|', $data->{l_holdingbranch})],
161             branch          => $data->{l_branch},
162             itemcallnumber  => $data->{l_itemcallnumber},
163             enumchron       => $data->{l_enumchron},
164             copyno          => $data->{l_copynumber},
165             count           => $data->{icount},
166             rcount          => $data->{rcount},
167             pullcount       => $data->{icount} <= $data->{rcount} ? $data->{icount} : $data->{rcount},
168             itypes          => [split('\|', $data->{l_itype})],
169             locations       => [split('\|', $data->{l_location})],
170         }
171     );
172 }
173 $sth->finish;
174
175 $template->param(
176     todaysdate          => $today,
177     from                => $startdate,
178     to                  => $enddate,
179     reserveloop         => \@reservedata,
180     "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
181     HoldsToPullStartDate => C4::Context->preference('HoldsToPullStartDate') || PULL_INTERVAL,
182     HoldsToPullEndDate  => C4::Context->preference('ConfirmFutureHolds') || 0,
183 );
184
185 output_html_with_http_headers $input, $cookie, $template->output;