Bug 20631: Remove unused RemoveLostItem from C4::Accounts
[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 $item_type = C4::Context->preference('item-level_itypes') ? "items.itype" : "biblioitems.itemtype";
93
94 my $strsth =
95     "SELECT min(reservedate) as l_reservedate,
96             reserves.borrowernumber as borrowernumber,
97             GROUP_CONCAT(DISTINCT items.holdingbranch 
98                     ORDER BY items.itemnumber SEPARATOR '|') l_holdingbranch,
99             reserves.biblionumber,
100             reserves.branchcode as l_branch,
101             GROUP_CONCAT(DISTINCT $item_type
102                     ORDER BY items.itemnumber SEPARATOR '|') l_item_type,
103             GROUP_CONCAT(DISTINCT items.location 
104                     ORDER BY items.itemnumber SEPARATOR '|') l_location,
105             GROUP_CONCAT(DISTINCT items.itemcallnumber 
106                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_itemcallnumber,
107             GROUP_CONCAT(DISTINCT items.enumchron
108                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_enumchron,
109             GROUP_CONCAT(DISTINCT items.copynumber
110                     ORDER BY items.itemnumber SEPARATOR '<br/>') l_copynumber,
111             biblio.title,
112             biblio.author,
113             count(DISTINCT items.itemnumber) as icount,
114             count(DISTINCT reserves.borrowernumber) as rcount,
115             borrowers.firstname,
116             borrowers.surname
117     FROM  reserves
118         LEFT JOIN items ON items.biblionumber=reserves.biblionumber 
119         LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber
120         LEFT JOIN biblioitems ON biblio.biblionumber=biblioitems.biblionumber
121         LEFT JOIN branchtransfers ON items.itemnumber=branchtransfers.itemnumber
122         LEFT JOIN issues ON items.itemnumber=issues.itemnumber
123         LEFT JOIN borrowers ON reserves.borrowernumber=borrowers.borrowernumber
124     WHERE
125     reserves.found IS NULL
126     $sqldatewhere
127     AND (reserves.itemnumber IS NULL OR reserves.itemnumber = items.itemnumber)
128     AND items.itemnumber NOT IN (SELECT itemnumber FROM branchtransfers where datearrived IS NULL)
129     AND items.itemnumber NOT IN (select itemnumber FROM reserves where found IS NOT NULL)
130     AND issues.itemnumber IS NULL
131     AND reserves.priority <> 0 
132     AND reserves.suspend = 0
133     AND notforloan = 0 AND damaged = 0 AND itemlost = 0 AND withdrawn = 0
134     ";
135     # GROUP BY reserves.biblionumber allows only items that are not checked out, else multiples occur when 
136     #    multiple patrons have a hold on an item
137
138
139 if (C4::Context->preference('IndependentBranches')){
140     $strsth .= " AND items.holdingbranch=? ";
141     push @query_params, C4::Context->userenv->{'branch'};
142 }
143 $strsth .= " GROUP BY reserves.biblionumber ORDER BY biblio.title ";
144
145 my $sth = $dbh->prepare($strsth);
146 $sth->execute(@query_params);
147
148 while ( my $data = $sth->fetchrow_hashref ) {
149     my $record = Koha::Biblios->find($data->{biblionumber});
150     if ($record){
151         $data->{subtitle} = [ $record->subtitles ];
152     }
153     push(
154         @reservedata, {
155             reservedate     => $data->{l_reservedate},
156             firstname       => $data->{firstname} || '',
157             surname         => $data->{surname},
158             title           => $data->{title},
159             subtitle        => $data->{subtitle},
160             author          => $data->{author},
161             borrowernumber  => $data->{borrowernumber},
162             biblionumber    => $data->{biblionumber},
163             holdingbranches => [split('\|', $data->{l_holdingbranch})],
164             branch          => $data->{l_branch},
165             itemcallnumber  => $data->{l_itemcallnumber},
166             enumchron       => $data->{l_enumchron},
167             copyno          => $data->{l_copynumber},
168             count           => $data->{icount},
169             rcount          => $data->{rcount},
170             pullcount       => $data->{icount} <= $data->{rcount} ? $data->{icount} : $data->{rcount},
171             itemTypes       => [split('\|', $data->{l_item_type})],
172             locations       => [split('\|', $data->{l_location})],
173         }
174     );
175 }
176 $sth->finish;
177
178 $template->param(
179     todaysdate          => $today,
180     from                => $startdate,
181     to                  => $enddate,
182     reserveloop         => \@reservedata,
183     "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
184     HoldsToPullStartDate => C4::Context->preference('HoldsToPullStartDate') || PULL_INTERVAL,
185     HoldsToPullEndDate  => C4::Context->preference('ConfirmFutureHolds') || 0,
186 );
187
188 output_html_with_http_headers $input, $cookie, $template->output;