Bug 11349: Change .tmpl -> .tt in scripts using templates
[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
29 use constant TWO_DAYS => 2;
30 use constant TWO_DAYS_AGO => -2;
31
32 use C4::Context;
33 use C4::Output;
34 use CGI;
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/;
39
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');
44
45 my $theme = $input->param('theme');    # only used if allowthemeoverride is set
46
47 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
48     {
49         template_name   => "circ/pendingreserves.tt",
50         query           => $input,
51         type            => "intranet",
52         authnotrequired => 0,
53         flagsrequired   => { circulate => "circulate_remaining_permissions" },
54         debug           => 1,
55     }
56 );
57
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;
70
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+$//;
77
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);
83 }
84
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);
89 }
90
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);
100     }
101     if ($enddate) {
102         $sqldatewhere .= " AND reservedate <= ?";
103         push @query_params, format_date_in_iso($enddate);
104     }
105
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
150     ";
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
153
154
155     if (C4::Context->preference('IndependentBranches')){
156         $strsth .= " AND items.holdingbranch=? ";
157         push @query_params, C4::Context->userenv->{'branch'};
158     }
159     $strsth .= " GROUP BY reserves.biblionumber ORDER BY biblio.title ";
160
161     my $sth = $dbh->prepare($strsth);
162     $sth->execute(@query_params);
163
164     while ( my $data = $sth->fetchrow_hashref ) {
165         push(
166             @reservedata,
167             {
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},
192             }
193         );
194     }
195     $sth->finish;
196 }
197
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,
207 );
208
209 output_html_with_http_headers $input, $cookie, $template->output;