Bug 14918: [QA Follow-up] Correct enddate and use constant
[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 # Modification by D.Ulm, actually works (as long as indep. branches not turned on)
21 #               Someone let me know what indep. branches is supposed to do and I'll make that part work too
22 #
23 #               The reserve pull lists *works* as long as not for indepencdant branches, I can fix!
24
25 use strict;
26 #use warnings; FIXME - Bug 2505
27
28 use constant PULL_INTERVAL => 2;
29
30 use C4::Context;
31 use C4::Output;
32 use CGI qw ( -utf8 );
33 use C4::Auth;
34 use C4::Debug;
35 use Koha::DateUtils;
36 use DateTime::Duration;
37
38 my $input = new CGI;
39 my $startdate=$input->param('from');
40 my $enddate=$input->param('to');
41 my $run_report = ( not defined $input->param('run_report') ) ? 1 : $input->param('run_report');
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.tt",
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 $today = dt_from_string;
70 $startdate =~ s/^\s+//;
71 $startdate =~ s/\s+$//;
72 $enddate =~ s/^\s+//;
73 $enddate =~ s/\s+$//;
74
75 if ( $startdate ) {
76     $startdate = eval{dt_from_string( $startdate )};
77 }
78 unless ( $startdate ){
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     $startdate = $today - DateTime::Duration->new( days => C4::Context->preference('HoldsToPullStartDate') || PULL_INTERVAL );
82 }
83
84 if ( $enddate ) {
85     $enddate = eval{dt_from_string( $enddate )};
86 }
87 unless ( $enddate ) {
88     #similarly: calculate end date with ConfirmFutureHolds (days)
89     $enddate = $today + DateTime::Duration->new( days => C4::Context->preference('ConfirmFutureHolds') || 0 );
90 }
91
92 my @reservedata;
93 if ( $run_report ) {
94     my $dbh    = C4::Context->dbh;
95     my $sqldatewhere = "";
96     my $startdate_iso = output_pref({ dt => $startdate, dateformat => 'iso', dateonly => 1 });
97     my $enddate_iso   = output_pref({ dt => $enddate, dateformat => 'iso', dateonly => 1 });
98     $debug and warn $startdate_iso. "\n" . $enddate_iso;
99     my @query_params = ();
100     if ($startdate_iso) {
101         $sqldatewhere .= " AND reservedate >= ?";
102         push @query_params, $startdate_iso;
103     }
104     if ($enddate_iso) {
105         $sqldatewhere .= " AND reservedate <= ?";
106         push @query_params, $enddate_iso;
107     }
108
109     my $strsth =
110     "SELECT min(reservedate) as l_reservedate,
111             reserves.borrowernumber as borrowernumber,
112             GROUP_CONCAT(DISTINCT items.holdingbranch 
113                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_holdingbranch,
114             reserves.biblionumber,
115             reserves.branchcode,
116             GROUP_CONCAT(DISTINCT reserves.branchcode 
117                     ORDER BY items.itemnumber SEPARATOR ', ') l_branch,
118             items.holdingbranch as branch,
119             GROUP_CONCAT(DISTINCT items.itype 
120                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_itype,
121             GROUP_CONCAT(DISTINCT items.location 
122                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_location,
123             GROUP_CONCAT(DISTINCT items.itemcallnumber 
124                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_itemcallnumber,
125             GROUP_CONCAT(DISTINCT items.enumchron
126                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_enumchron,
127             GROUP_CONCAT(DISTINCT items.copynumber
128                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_copynumber,
129             items.itemnumber,
130             notificationdate,
131             reminderdate,
132             max(priority) as priority,
133             reserves.found,
134             biblio.title,
135             biblio.author,
136             count(DISTINCT items.itemnumber) as icount,
137             count(DISTINCT reserves.borrowernumber) as rcount
138     FROM  reserves
139         LEFT JOIN items ON items.biblionumber=reserves.biblionumber 
140         LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber
141         LEFT JOIN branchtransfers ON items.itemnumber=branchtransfers.itemnumber
142         LEFT JOIN issues ON items.itemnumber=issues.itemnumber
143     WHERE
144     reserves.found IS NULL
145     $sqldatewhere
146     AND (reserves.itemnumber IS NULL OR reserves.itemnumber = items.itemnumber)
147     AND items.itemnumber NOT IN (SELECT itemnumber FROM branchtransfers where datearrived IS NULL)
148     AND items.itemnumber NOT IN (select itemnumber FROM reserves where found='W')
149     AND issues.itemnumber IS NULL
150     AND reserves.priority <> 0 
151     AND reserves.suspend = 0
152     AND notforloan = 0 AND damaged = 0 AND itemlost = 0 AND withdrawn = 0
153     ";
154     # GROUP BY reserves.biblionumber allows only items that are not checked out, else multiples occur when 
155     #    multiple patrons have a hold on an item
156
157
158     if (C4::Context->preference('IndependentBranches')){
159         $strsth .= " AND items.holdingbranch=? ";
160         push @query_params, C4::Context->userenv->{'branch'};
161     }
162     $strsth .= " GROUP BY reserves.biblionumber ORDER BY biblio.title ";
163
164     my $sth = $dbh->prepare($strsth);
165     $sth->execute(@query_params);
166
167     while ( my $data = $sth->fetchrow_hashref ) {
168         push(
169             @reservedata,
170             {
171                 reservedate     => $data->{l_reservedate},
172                 priority        => $data->{priority},
173                 name            => $data->{l_patron},
174                 title           => $data->{title},
175                 author          => $data->{author},
176                 borrowernumber  => $data->{borrowernumber},
177                 itemnum         => $data->{itemnumber},
178                 phone           => $data->{phone},
179                 email           => $data->{email},
180                 biblionumber    => $data->{biblionumber},
181                 statusw         => ( $data->{found} eq "W" ),
182                 statusf         => ( $data->{found} eq "F" ),
183                 holdingbranch   => $data->{l_holdingbranch},
184                 branch          => $data->{l_branch},
185                 itemcallnumber  => $data->{l_itemcallnumber},
186                 enumchron       => $data->{l_enumchron},
187                 copyno          => $data->{l_copynumber},
188                 notificationdate=> $data->{notificationdate},
189                 reminderdate    => $data->{reminderdate},
190                 count           => $data->{icount},
191                 rcount          => $data->{rcount},
192                 pullcount       => $data->{icount} <= $data->{rcount} ? $data->{icount} : $data->{rcount},
193                 itype           => $data->{l_itype},
194                 location        => $data->{l_location},
195             }
196         );
197     }
198     $sth->finish;
199 }
200
201 $template->param(
202     todaysdate          => $today,
203     from                => $startdate,
204     to                  => $enddate,
205     run_report          => $run_report,
206     reserveloop         => \@reservedata,
207     "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
208     HoldsToPullStartDate => C4::Context->preference('HoldsToPullStartDate') || PULL_INTERVAL,
209     HoldsToPullEndDate  => C4::Context->preference('ConfirmFutureHolds') || 0,
210 );
211
212 output_html_with_http_headers $input, $cookie, $template->output;