import of the authorized values fails on the step 3 of the web-installation (ru-RU)
[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 with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 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 C4::Context;
28 use C4::Output;
29 use CGI;
30 use C4::Auth;
31 use C4::Dates qw/format_date format_date_in_iso/;
32 use C4::Debug;
33 use Date::Calc qw/Today Add_Delta_YMD/;
34
35 my $input = new CGI;
36 my $order = $input->param('order');
37 my $startdate=$input->param('from');
38 my $enddate=$input->param('to');
39
40 my $theme = $input->param('theme');    # only used if allowthemeoverride is set
41
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
43     {
44         template_name   => "circ/pendingreserves.tmpl",
45         query           => $input,
46         type            => "intranet",
47         authnotrequired => 0,
48         flagsrequired   => { circulate => "circulate_remaining_permissions" },
49         debug           => 1,
50     }
51 );
52
53 my $duedate;
54 my $borrowernumber;
55 my $itemnum;
56 my $data1;
57 my $data2;
58 my $data3;
59 my $name;
60 my $phone;
61 my $email;
62 my $biblionumber;
63 my $title;
64 my $author;
65
66 my ( $year, $month, $day ) = Today();
67 my $todaysdate     = sprintf("%-04.4d-%-02.2d-%02.2d", $year, $month, $day);
68 my $yesterdaysdate = sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YMD($year, $month, $day,   0, 0, -1));
69 # Find 10 years ago for the default shelf pull start and end dates
70 #    A default of the prior day's holds is a reasonable way to pull holds 
71 my $pastdate       = sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YMD($year, $month, $day, -10, 0,  0));
72
73 #               Predefine the start and end dates if they are not already defined
74 $startdate =~ s/^\s+//;
75 $startdate =~ s/\s+$//;
76 $enddate =~ s/^\s+//;
77 $enddate =~ s/\s+$//;
78 #               Check if null, should string match, if so set start and end date to yesterday
79 if (!defined($startdate) or $startdate eq "") {
80         $startdate = format_date($pastdate);
81 }
82 if (!defined($enddate) or $enddate eq "") {
83         $enddate = format_date($yesterdaysdate);
84 }
85
86
87 my $dbh    = C4::Context->dbh;
88 my ($sqlorderby, $sqldatewhere) = ("","");
89 $debug and warn format_date_in_iso($startdate) . "\n" . format_date_in_iso($enddate);
90 my @query_params = ();
91 if ($startdate) {
92     $sqldatewhere .= " AND reservedate >= ?";
93     push @query_params, format_date_in_iso($startdate);
94 }
95 if ($enddate) {
96     $sqldatewhere .= " AND reservedate <= ?";
97     push @query_params, format_date_in_iso($enddate);
98 }
99
100 if ($order eq "biblio") {
101         $sqlorderby = " ORDER BY biblio.title ";
102 } elsif ($order eq "itype") {
103         $sqlorderby = " ORDER BY l_itype, location, l_itemcallnumber ";
104 } elsif ($order eq "location") {
105         $sqlorderby = " ORDER BY location, l_itemcallnumber, holdingbranch ";
106 } elsif ($order eq "date") {
107     $sqlorderby = " ORDER BY l_reservedate, location, l_itemcallnumber ";
108 } elsif ($order eq "library") {
109     $sqlorderby = " ORDER BY holdingbranch, l_itemcallnumber, location ";
110 } elsif ($order eq "call") {
111     $sqlorderby = " ORDER BY l_itemcallnumber, holdingbranch, location ";    
112 } else {
113         $sqlorderby = " ORDER BY biblio.title ";
114 }
115 my $strsth =
116 "SELECT min(reservedate) as l_reservedate,
117         reserves.borrowernumber as borrowernumber,
118         GROUP_CONCAT(DISTINCT items.holdingbranch 
119                         ORDER BY items.itemnumber SEPARATOR '<br/>') l_holdingbranch,
120         reserves.biblionumber,
121         reserves.branchcode,
122         GROUP_CONCAT(DISTINCT reserves.branchcode 
123                         ORDER BY items.itemnumber SEPARATOR ', ') l_branch,
124         items.holdingbranch as branch,
125         items.itemcallnumber,
126         GROUP_CONCAT(DISTINCT items.itype 
127                         ORDER BY items.itemnumber SEPARATOR '<br/>') l_itype,
128         GROUP_CONCAT(DISTINCT items.location 
129                         ORDER BY items.itemnumber SEPARATOR '<br/>') l_location,
130         GROUP_CONCAT(DISTINCT items.itemcallnumber 
131                         ORDER BY items.itemnumber SEPARATOR '<br/>') l_itemcallnumber,
132         items.itemnumber,
133         notes,
134         notificationdate,
135         reminderdate,
136         max(priority) as priority,
137         reserves.found,
138         biblio.title,
139         biblio.author,
140         count(DISTINCT items.itemnumber) as icount,
141         count(DISTINCT reserves.borrowernumber) as rcount
142  FROM  reserves
143         LEFT JOIN items ON items.biblionumber=reserves.biblionumber 
144         LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber
145         LEFT JOIN branchtransfers ON items.itemnumber=branchtransfers.itemnumber
146  WHERE
147 reserves.found IS NULL
148  $sqldatewhere
149 AND items.itemnumber NOT IN (SELECT itemnumber FROM branchtransfers where datearrived IS NULL)
150 AND items.itemnumber NOT IN (SELECT itemnumber FROM issues)
151 AND reserves.priority <> 0 
152 AND notforloan = 0 AND damaged = 0 AND itemlost = 0 AND wthdrawn = 0
153 ";
154 # GROUP BY reserves.biblionumber allows only items that are not checked out, else multiples occur when 
155 #    multiple patrons have a hold on an item
156
157
158 if (C4::Context->preference('IndependantBranches')){
159         $strsth .= " AND items.holdingbranch=? ";
160     push @query_params, C4::Context->userenv->{'branch'};
161 }
162 $strsth .= " GROUP BY reserves.biblionumber " . $sqlorderby;
163
164 my $sth = $dbh->prepare($strsth);
165 $sth->execute(@query_params);
166
167 my @reservedata;
168 my $previous;
169 my $this;
170 while ( my $data = $sth->fetchrow_hashref ) {
171     $this=$data->{biblionumber}.":".$data->{borrowernumber};
172     my @itemlist;
173     push(
174         @reservedata,
175         {
176             reservedate      => format_date( $data->{l_reservedate} ),
177             priority         => $data->{priority},
178             name             => $data->{l_patron},
179             title            => $data->{title},
180             author           => $data->{author},
181             borrowernumber   => $data->{borrowernumber},
182             itemnum          => $data->{itemnumber},
183             phone            => $data->{phone},
184             email            => $data->{email},
185             biblionumber     => $data->{biblionumber},
186             statusw          => ( $data->{found} eq "W" ),
187             statusf          => ( $data->{found} eq "F" ),
188             holdingbranch    => $data->{l_holdingbranch},
189             branch           => $data->{l_branch},
190             itemcallnumber   => $data->{l_itemcallnumber},
191             notes            => $data->{notes},
192             notificationdate => $data->{notificationdate},
193             reminderdate     => $data->{reminderdate},
194             count                                 => $data->{icount},
195             rcount                        => $data->{rcount},
196             pullcount             => $data->{icount} <= $data->{rcount} ? $data->{icount} : $data->{rcount},
197             itype                                 => $data->{l_itype},
198             location                      => $data->{l_location}
199         }
200     );
201     $previous=$this;
202 }
203
204 $sth->finish;
205
206 # *** I doubt any of this is needed now with the above fixes *** -d.u.
207
208 #$strsth=~ s/AND reserves.itemnumber is NULL/AND reserves.itemnumber is NOT NULL/;
209 #$strsth=~ s/LEFT JOIN items ON items.biblionumber=reserves.biblionumber/LEFT JOIN items ON items.biblionumber=reserves.itemnumber/;
210 #$sth = $dbh->prepare($strsth);
211 #if (C4::Context->preference('IndependantBranches')){
212 #       $sth->execute(C4::Context->userenv->{'branch'});
213 #}
214 #else {
215 #       $sth->execute();
216 #}
217 #while ( my $data = $sth->fetchrow_hashref ) {
218 #    $this=$data->{biblionumber}.":".$data->{borrowernumber};
219 #    my @itemlist;
220 #    push(
221 #        @reservedata,
222 #        {
223 #            reservedate      => format_date( $data->{l_reservedate} ),
224 #            priority         => $data->{priority},
225 #            name             => $data->{l_patron},
226 #            title            => $data->{title},
227 #            author           => $data->{author},
228 #            borrowernumber   => $data->{borrowernumber},
229 #            itemnum          => $data->{itemnumber},
230 #            phone            => $data->{phone},
231 #            email            => $data->{email},
232 #            biblionumber     => $data->{biblionumber},
233 #            statusw          => ( $data->{found} eq "W" ),
234 #            statusf          => ( $data->{found} eq "F" ),
235 #            holdingbranch    => $data->{l_holdingbranch},
236 #            branch           => $data->{l_branch},
237 #            itemcallnumber   => $data->{l_itemcallnumber},
238 #            notes            => $data->{notes},
239 #            notificationdate => $data->{notificationdate},
240 #            reminderdate     => $data->{reminderdate},
241 #            count                                => $data->{icount},
242 #            rcount                       => $data->{rcount},
243 #            pullcount            => $data->{icount} <= $data->{rcount} ? $data->{icount} : $data->{rcount},
244 #            itype                                => $data->{l_itype},
245 #            location                     => $data->{l_location},
246 #            thisitemonly     => 1,
247
248 #        }
249 #    );
250 #    $previous=$this;
251 #}
252 #$sth->finish;
253
254 $template->param(
255     todaysdate          => format_date($todaysdate),
256     from             => $startdate,
257     to                  => $enddate,
258     reserveloop         => \@reservedata,
259     "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
260     DHTMLcalendar_dateformat =>  C4::Dates->DHTMLcalendar(),
261         dateformat    => C4::Context->preference("dateformat"),
262 );
263
264 output_html_with_http_headers $input, $cookie, $template->output;