Bug 7413: Add "No renewal before" to the circulation and fine rules
[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.tmpl",
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             notes,
128             notificationdate,
129             reminderdate,
130             max(priority) as priority,
131             reserves.found,
132             biblio.title,
133             biblio.author,
134             count(DISTINCT items.itemnumber) as icount,
135             count(DISTINCT reserves.borrowernumber) as rcount
136     FROM  reserves
137         LEFT JOIN items ON items.biblionumber=reserves.biblionumber 
138         LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber
139         LEFT JOIN branchtransfers ON items.itemnumber=branchtransfers.itemnumber
140         LEFT JOIN issues ON items.itemnumber=issues.itemnumber
141     WHERE
142     reserves.found IS NULL
143     $sqldatewhere
144     AND (reserves.itemnumber IS NULL OR reserves.itemnumber = items.itemnumber)
145     AND items.itemnumber NOT IN (SELECT itemnumber FROM branchtransfers where datearrived IS NULL)
146     AND items.itemnumber NOT IN (select itemnumber FROM reserves where found='W')
147     AND issues.itemnumber IS NULL
148     AND reserves.priority <> 0 
149     AND reserves.suspend = 0
150     AND notforloan = 0 AND damaged = 0 AND itemlost = 0 AND withdrawn = 0
151     ";
152     # GROUP BY reserves.biblionumber allows only items that are not checked out, else multiples occur when 
153     #    multiple patrons have a hold on an item
154
155
156     if (C4::Context->preference('IndependentBranches')){
157         $strsth .= " AND items.holdingbranch=? ";
158         push @query_params, C4::Context->userenv->{'branch'};
159     }
160     $strsth .= " GROUP BY reserves.biblionumber ORDER BY biblio.title ";
161
162     my $sth = $dbh->prepare($strsth);
163     $sth->execute(@query_params);
164
165     while ( my $data = $sth->fetchrow_hashref ) {
166         push(
167             @reservedata,
168             {
169                 reservedate     => format_date( $data->{l_reservedate} ),
170                 priority        => $data->{priority},
171                 name            => $data->{l_patron},
172                 title           => $data->{title},
173                 author          => $data->{author},
174                 borrowernumber  => $data->{borrowernumber},
175                 itemnum         => $data->{itemnumber},
176                 phone           => $data->{phone},
177                 email           => $data->{email},
178                 biblionumber    => $data->{biblionumber},
179                 statusw         => ( $data->{found} eq "W" ),
180                 statusf         => ( $data->{found} eq "F" ),
181                 holdingbranch   => $data->{l_holdingbranch},
182                 branch          => $data->{l_branch},
183                 itemcallnumber  => $data->{l_itemcallnumber},
184                 enumchron       => $data->{l_enumchron},
185                 copyno          => $data->{l_copynumber},
186                 notes           => $data->{notes},
187                 notificationdate=> $data->{notificationdate},
188                 reminderdate    => $data->{reminderdate},
189                 count           => $data->{icount},
190                 rcount          => $data->{rcount},
191                 pullcount       => $data->{icount} <= $data->{rcount} ? $data->{icount} : $data->{rcount},
192                 itype           => $data->{l_itype},
193                 location        => $data->{l_location},
194             }
195         );
196     }
197     $sth->finish;
198 }
199
200 $template->param(
201     todaysdate          => format_date($todaysdate),
202     from                => $startdate,
203     to                  => $enddate,
204     run_report          => $run_report,
205     reserveloop         => \@reservedata,
206     "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
207     HoldsToPullStartDate=> C4::Context->preference('HoldsToPullStartDate')||TWO_DAYS,
208     HoldsToPullEndDate  => C4::Context->preference('ConfirmFutureHolds')||0,
209 );
210
211 output_html_with_http_headers $input, $cookie, $template->output;