Bug 14918: Remove C4::Dates from circ/pendingreserves.pl
[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
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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
29 use constant TWO_DAYS => 2;
30
31 use C4::Context;
32 use C4::Output;
33 use CGI qw ( -utf8 );
34 use C4::Auth;
35 use C4::Debug;
36 use Koha::DateUtils;
37 use DateTime::Duration;
38
39 my $input = new CGI;
40 my $startdate=$input->param('from');
41 my $enddate=$input->param('to');
42 my $run_report = ( not defined $input->param('run_report') ) ? 1 : $input->param('run_report');
43
44 my $theme = $input->param('theme');    # only used if allowthemeoverride is set
45
46 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
47     {
48         template_name   => "circ/pendingreserves.tt",
49         query           => $input,
50         type            => "intranet",
51         authnotrequired => 0,
52         flagsrequired   => { circulate => "circulate_remaining_permissions" },
53         debug           => 1,
54     }
55 );
56
57 my $duedate;
58 my $borrowernumber;
59 my $itemnum;
60 my $data1;
61 my $data2;
62 my $data3;
63 my $name;
64 my $phone;
65 my $email;
66 my $biblionumber;
67 my $title;
68 my $author;
69
70 my $today = dt_from_string;
71 $startdate =~ s/^\s+//;
72 $startdate =~ s/\s+$//;
73 $enddate =~ s/^\s+//;
74 $enddate =~ s/\s+$//;
75
76 if ( $startdate ) {
77     $startdate = eval{dt_from_string( $startdate )};
78 }
79 unless ( $startdate ){
80     # changed from delivered range of 10 years-yesterday to 2 days ago-today
81     # Find two days ago for the default shelf pull start date, unless HoldsToPullStartDate sys pref is set.
82     $startdate = $today - DateTime::Duration->new( days => C4::Context->preference('HoldsToPullStartDate') || 2 );
83 }
84
85 if ( $enddate ) {
86     $enddate = eval{dt_from_string( $enddate )};
87 }
88 unless ( $enddate ) {
89     #similarly: calculate end date with ConfirmFutureHolds (days)
90     $enddate = $today - DateTime::Duration->new( days => C4::Context->preference('ConfirmFutureHolds') || 0 );
91 }
92
93 my @reservedata;
94 if ( $run_report ) {
95     my $dbh    = C4::Context->dbh;
96     my $sqldatewhere = "";
97     my $startdate_iso = output_pref({ dt => $startdate, dateformat => 'iso', dateonly => 1 });
98     my $enddate_iso   = output_pref({ dt => $enddate, dateformat => 'iso', dateonly => 1 });
99     $debug and warn $startdate_iso. "\n" . $enddate_iso;
100     my @query_params = ();
101     if ($startdate_iso) {
102         $sqldatewhere .= " AND reservedate >= ?";
103         push @query_params, $startdate_iso;
104     }
105     if ($enddate_iso) {
106         $sqldatewhere .= " AND reservedate <= ?";
107         push @query_params, $enddate_iso;
108     }
109
110     my $strsth =
111     "SELECT min(reservedate) as l_reservedate,
112             reserves.borrowernumber as borrowernumber,
113             GROUP_CONCAT(DISTINCT items.holdingbranch 
114                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_holdingbranch,
115             reserves.biblionumber,
116             reserves.branchcode,
117             GROUP_CONCAT(DISTINCT reserves.branchcode 
118                     ORDER BY items.itemnumber SEPARATOR ', ') l_branch,
119             items.holdingbranch as branch,
120             GROUP_CONCAT(DISTINCT items.itype 
121                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_itype,
122             GROUP_CONCAT(DISTINCT items.location 
123                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_location,
124             GROUP_CONCAT(DISTINCT items.itemcallnumber 
125                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_itemcallnumber,
126             GROUP_CONCAT(DISTINCT items.enumchron
127                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_enumchron,
128             GROUP_CONCAT(DISTINCT items.copynumber
129                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_copynumber,
130             items.itemnumber,
131             notificationdate,
132             reminderdate,
133             max(priority) as priority,
134             reserves.found,
135             biblio.title,
136             biblio.author,
137             count(DISTINCT items.itemnumber) as icount,
138             count(DISTINCT reserves.borrowernumber) as rcount
139     FROM  reserves
140         LEFT JOIN items ON items.biblionumber=reserves.biblionumber 
141         LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber
142         LEFT JOIN branchtransfers ON items.itemnumber=branchtransfers.itemnumber
143         LEFT JOIN issues ON items.itemnumber=issues.itemnumber
144     WHERE
145     reserves.found IS NULL
146     $sqldatewhere
147     AND (reserves.itemnumber IS NULL OR reserves.itemnumber = items.itemnumber)
148     AND items.itemnumber NOT IN (SELECT itemnumber FROM branchtransfers where datearrived IS NULL)
149     AND items.itemnumber NOT IN (select itemnumber FROM reserves where found='W')
150     AND issues.itemnumber IS NULL
151     AND reserves.priority <> 0 
152     AND reserves.suspend = 0
153     AND notforloan = 0 AND damaged = 0 AND itemlost = 0 AND withdrawn = 0
154     ";
155     # GROUP BY reserves.biblionumber allows only items that are not checked out, else multiples occur when 
156     #    multiple patrons have a hold on an item
157
158
159     if (C4::Context->preference('IndependentBranches')){
160         $strsth .= " AND items.holdingbranch=? ";
161         push @query_params, C4::Context->userenv->{'branch'};
162     }
163     $strsth .= " GROUP BY reserves.biblionumber ORDER BY biblio.title ";
164
165     my $sth = $dbh->prepare($strsth);
166     $sth->execute(@query_params);
167
168     while ( my $data = $sth->fetchrow_hashref ) {
169         push(
170             @reservedata,
171             {
172                 reservedate     => $data->{l_reservedate},
173                 priority        => $data->{priority},
174                 name            => $data->{l_patron},
175                 title           => $data->{title},
176                 author          => $data->{author},
177                 borrowernumber  => $data->{borrowernumber},
178                 itemnum         => $data->{itemnumber},
179                 phone           => $data->{phone},
180                 email           => $data->{email},
181                 biblionumber    => $data->{biblionumber},
182                 statusw         => ( $data->{found} eq "W" ),
183                 statusf         => ( $data->{found} eq "F" ),
184                 holdingbranch   => $data->{l_holdingbranch},
185                 branch          => $data->{l_branch},
186                 itemcallnumber  => $data->{l_itemcallnumber},
187                 enumchron       => $data->{l_enumchron},
188                 copyno          => $data->{l_copynumber},
189                 notificationdate=> $data->{notificationdate},
190                 reminderdate    => $data->{reminderdate},
191                 count           => $data->{icount},
192                 rcount          => $data->{rcount},
193                 pullcount       => $data->{icount} <= $data->{rcount} ? $data->{icount} : $data->{rcount},
194                 itype           => $data->{l_itype},
195                 location        => $data->{l_location},
196             }
197         );
198     }
199     $sth->finish;
200 }
201
202 $template->param(
203     todaysdate          => $today,
204     from                => $startdate,
205     to                  => $enddate,
206     run_report          => $run_report,
207     reserveloop         => \@reservedata,
208     "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
209     HoldsToPullStartDate=> C4::Context->preference('HoldsToPullStartDate')||TWO_DAYS,
210     HoldsToPullEndDate  => C4::Context->preference('ConfirmFutureHolds')||0,
211 );
212
213 output_html_with_http_headers $input, $cookie, $template->output;