Merge branch 'bug_6554' into 3.14-master
[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 use C4::Context;
29 use C4::Output;
30 use CGI;
31 use C4::Auth;
32 use C4::Dates qw/format_date format_date_in_iso/;
33 use C4::Debug;
34 use Date::Calc qw/Today Add_Delta_YMD/;
35
36 my $input = new CGI;
37 my $startdate=$input->param('from');
38 my $enddate=$input->param('to');
39 my $run_report = ( not defined $input->param('run_report') ) ? 1 : $input->param('run_report');
40
41 my $theme = $input->param('theme');    # only used if allowthemeoverride is set
42
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
44     {
45         template_name   => "circ/pendingreserves.tmpl",
46         query           => $input,
47         type            => "intranet",
48         authnotrequired => 0,
49         flagsrequired   => { circulate => "circulate_remaining_permissions" },
50         debug           => 1,
51     }
52 );
53
54 my $duedate;
55 my $borrowernumber;
56 my $itemnum;
57 my $data1;
58 my $data2;
59 my $data3;
60 my $name;
61 my $phone;
62 my $email;
63 my $biblionumber;
64 my $title;
65 my $author;
66
67 my ( $year, $month, $day ) = Today();
68 my $todaysdate     = sprintf("%-04.4d-%-02.2d-%02.2d", $year, $month, $day);
69 my $yesterdaysdate = sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YMD($year, $month, $day,   0, 0, -1));
70 # changed from delivered range of 10 years-yesterday to 2 days ago-today
71 # Find two days ago for the default shelf pull start and end dates, unless HoldsToPullStartDate sys pref is set.
72 my $defaultstartdate = ( C4::Context->preference('HoldsToPullStartDate') ) ? "-".C4::Context->preference('HoldsToPullStartDate') : -2;
73 my $pastdate       = sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YMD($year, $month, $day, 0, 0, $defaultstartdate));
74
75 # Predefine the start and end dates if they are not already defined
76 $startdate =~ s/^\s+//;
77 $startdate =~ s/\s+$//;
78 $enddate =~ s/^\s+//;
79 $enddate =~ s/\s+$//;
80 # Check if null, should string match, if so set start and end date to yesterday
81 if (!defined($startdate) or $startdate eq "") {
82     $startdate = format_date($pastdate);
83 }
84 if (!defined($enddate) or $enddate eq "") {
85     $enddate = format_date($todaysdate);
86 }
87
88
89 my @reservedata;
90 if ( $run_report ) {
91     my $dbh    = C4::Context->dbh;
92     my $sqldatewhere = "";
93     $debug and warn format_date_in_iso($startdate) . "\n" . format_date_in_iso($enddate);
94     my @query_params = ();
95     if ($startdate) {
96         $sqldatewhere .= " AND reservedate >= ?";
97         push @query_params, format_date_in_iso($startdate);
98     }
99     if ($enddate) {
100         $sqldatewhere .= " AND reservedate <= ?";
101         push @query_params, format_date_in_iso($enddate);
102     }
103
104     my $strsth =
105     "SELECT min(reservedate) as l_reservedate,
106             reserves.borrowernumber as borrowernumber,
107             GROUP_CONCAT(DISTINCT items.holdingbranch 
108                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_holdingbranch,
109             reserves.biblionumber,
110             reserves.branchcode,
111             GROUP_CONCAT(DISTINCT reserves.branchcode 
112                     ORDER BY items.itemnumber SEPARATOR ', ') l_branch,
113             items.holdingbranch as branch,
114             GROUP_CONCAT(DISTINCT items.itype 
115                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_itype,
116             GROUP_CONCAT(DISTINCT items.location 
117                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_location,
118             GROUP_CONCAT(DISTINCT items.itemcallnumber 
119                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_itemcallnumber,
120             GROUP_CONCAT(DISTINCT items.enumchron
121                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_enumchron,
122             GROUP_CONCAT(DISTINCT items.copynumber
123                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_copynumber,
124             items.itemnumber,
125             notes,
126             notificationdate,
127             reminderdate,
128             max(priority) as priority,
129             reserves.found,
130             biblio.title,
131             biblio.author,
132             count(DISTINCT items.itemnumber) as icount,
133             count(DISTINCT reserves.borrowernumber) as rcount
134     FROM  reserves
135         LEFT JOIN items ON items.biblionumber=reserves.biblionumber 
136         LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber
137         LEFT JOIN branchtransfers ON items.itemnumber=branchtransfers.itemnumber
138         LEFT JOIN issues ON items.itemnumber=issues.itemnumber
139     WHERE
140     reserves.found IS NULL
141     $sqldatewhere
142     AND (reserves.itemnumber IS NULL OR reserves.itemnumber = items.itemnumber)
143     AND items.itemnumber NOT IN (SELECT itemnumber FROM branchtransfers where datearrived IS NULL)
144     AND items.itemnumber NOT IN (select itemnumber FROM reserves where found='W')
145     AND issues.itemnumber IS NULL
146     AND reserves.priority <> 0 
147     AND reserves.suspend = 0
148     AND notforloan = 0 AND damaged = 0 AND itemlost = 0 AND wthdrawn = 0
149     ";
150     # GROUP BY reserves.biblionumber allows only items that are not checked out, else multiples occur when 
151     #    multiple patrons have a hold on an item
152
153
154     if (C4::Context->preference('IndependantBranches')){
155         $strsth .= " AND items.holdingbranch=? ";
156         push @query_params, C4::Context->userenv->{'branch'};
157     }
158     $strsth .= " GROUP BY reserves.biblionumber ORDER BY biblio.title ";
159
160     my $sth = $dbh->prepare($strsth);
161     $sth->execute(@query_params);
162
163     while ( my $data = $sth->fetchrow_hashref ) {
164         push(
165             @reservedata,
166             {
167                 reservedate     => format_date( $data->{l_reservedate} ),
168                 priority        => $data->{priority},
169                 name            => $data->{l_patron},
170                 title           => $data->{title},
171                 author          => $data->{author},
172                 borrowernumber  => $data->{borrowernumber},
173                 itemnum         => $data->{itemnumber},
174                 phone           => $data->{phone},
175                 email           => $data->{email},
176                 biblionumber    => $data->{biblionumber},
177                 statusw         => ( $data->{found} eq "W" ),
178                 statusf         => ( $data->{found} eq "F" ),
179                 holdingbranch   => $data->{l_holdingbranch},
180                 branch          => $data->{l_branch},
181                 itemcallnumber  => $data->{l_itemcallnumber},
182                 enumchron       => $data->{l_enumchron},
183                 copyno          => $data->{l_copynumber},
184                 notes           => $data->{notes},
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          => format_date($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')?C4::Context->preference('HoldsToPullStartDate'):2),
206 );
207
208 output_html_with_http_headers $input, $cookie, $template->output;