Still a work in progress, pendingreserves.pl is quite broken
[wip/koha-chris_n.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 use strict;
22 use C4::Context;
23 use C4::Output;
24 use CGI;
25 use C4::Auth;
26 use C4::Dates qw/format_date format_date_in_iso/;
27
28 use vars qw($debug);
29
30 BEGIN {
31     $debug = $ENV{DEBUG} || 0;
32 }
33
34 my $input = new CGI;
35 my $order = $input->param('order');
36 my $startdate=$input->param('from');
37 my $enddate=$input->param('to');
38
39 my $theme = $input->param('theme');    # only used if allowthemeoverride is set
40
41 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
42     {
43         template_name   => "circ/pendingreserves.tmpl",
44         query           => $input,
45         type            => "intranet",
46         authnotrequired => 0,
47         flagsrequired   => { circulate => 1 },
48         debug           => 1,
49     }
50 );
51
52 my $duedate;
53 my $borrowernumber;
54 my $itemnum;
55 my $data1;
56 my $data2;
57 my $data3;
58 my $name;
59 my $phone;
60 my $email;
61 my $biblionumber;
62 my $title;
63 my $author;
64
65 my @datearr    = localtime( time() );
66 my $todaysdate =
67     ( 1900 + $datearr[5] ) . '-'
68   . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
69   . sprintf( "%0.2d", $datearr[3] );
70
71 my $dbh    = C4::Context->dbh;
72 my ($sqlorderby, $sqldatewhere) = ("","");
73 $debug and warn format_date_in_iso($startdate) . "\n" . format_date_in_iso($enddate);
74 $sqldatewhere .= " AND reservedate >= " . $dbh->quote(format_date_in_iso($startdate))  if ($startdate) ;
75 $sqldatewhere .= " AND reservedate <= " . $dbh->quote(format_date_in_iso($enddate))  if ($enddate) ;
76
77 if ($order eq "borrower") {
78         $sqlorderby = " order by  borrower, reservedate";
79 } elsif ($order eq "biblio") {
80         $sqlorderby = " order by biblio.title, priority,reservedate";
81 } elsif ($order eq "priority") {
82     $sqlorderby = "order by priority DESC";
83 } else {
84         $sqlorderby = " order by reservedate, borrower";
85 }
86 my $strsth =
87 "SELECT reservedate,
88         reserves.borrowernumber as borrowernumber,
89         concat(firstname,' ',surname) as borrower,
90         borrowers.phone,
91         borrowers.email,
92         reserves.biblionumber,
93         reserves.branchcode as branch,
94         items.holdingbranch,
95         items.itemcallnumber,
96         items.itemnumber,
97         notes,
98         notificationdate,
99         reminderdate,
100         priority,
101         reserves.found,
102         biblio.title,
103         biblio.author
104  FROM  reserves
105  LEFT JOIN items ON items.biblionumber=reserves.biblionumber 
106  LEFT JOIN borrowers ON reserves.borrowernumber=borrowers.borrowernumber
107  LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber
108  WHERE isnull(cancellationdate)
109  $sqldatewhere
110  AND reserves.found is NULL 
111  AND reserves.itemnumber is NULL";
112
113 if (C4::Context->preference('IndependantBranches')){
114         $strsth .= " AND items.holdingbranch=? ";
115 }
116 $strsth .= $sqlorderby;
117 my $sth = $dbh->prepare($strsth);
118
119 if (C4::Context->preference('IndependantBranches')){
120         $sth->execute(C4::Context->userenv->{'branch'});
121 }
122 else {
123         $sth->execute();
124 }       
125 my @reservedata;
126 my $previous;
127 my $this;
128 while ( my $data = $sth->fetchrow_hashref ) {
129     $this=$data->{biblionumber}.":".$data->{borrowernumber};
130     my @itemlist;
131     push(
132         @reservedata,
133         {
134             reservedate      => $previous eq $this?"":format_date( $data->{reservedate} ),
135             priority         => $previous eq $this?"":$data->{priority},
136             name             => $previous eq $this?"":$data->{borrower},
137             title            => $previous eq $this?"":$data->{title},
138             author           => $previous eq $this?"":$data->{author},
139             borrowernumber   => $previous eq $this?"":$data->{borrowernumber},
140             itemnum          => $previous eq $this?"":$data->{itemnumber},
141             phone            => $previous eq $this?"":$data->{phone},
142             email            => $previous eq $this?"":$data->{email},
143             biblionumber     => $previous eq $this?"":$data->{biblionumber},
144             statusw          => ( $data->{found} eq "w" ),
145             statusf          => ( $data->{found} eq "f" ),
146             holdingbranch    => $data->{holdingbranch},
147             branch           => $previous eq $this?"":$data->{branch},
148             itemcallnumber   => $data->{itemcallnumber},
149             notes            => $previous eq $this?"":$data->{notes},
150             notificationdate => $previous eq $this?"":$data->{notificationdate},
151             reminderdate     => $previous eq $this?"":$data->{reminderdate}
152         }
153     );
154     $previous=$this;
155 }
156
157 $sth->finish;
158 $strsth=~ s/AND reserves.itemnumber is NULL/AND reserves.itemnumber is NOT NULL/;
159 $strsth=~ s/LEFT JOIN items ON items.biblionumber=reserves.biblionumber/LEFT JOIN items ON items.biblionumber=reserves.itemnumber/;
160 $sth = $dbh->prepare($strsth);                                                                                                                          
161 if (C4::Context->preference('IndependantBranches')){
162         $sth->execute(C4::Context->userenv->{'branch'});
163 }     
164 else {
165         $sth->execute(); 
166 }              
167 while ( my $data = $sth->fetchrow_hashref ) {
168     $this=$data->{biblionumber}.":".$data->{borrowernumber};
169     my @itemlist;
170     push(
171         @reservedata,
172         {
173             reservedate      => $previous eq $this?"":format_date( $data->{reservedate} ),
174             priority         => $previous eq $this?"":$data->{priority},
175             name             => $previous eq $this?"":$data->{borrower},
176             title            => $previous eq $this?"":$data->{title},
177             author           => $previous eq $this?"":$data->{author},
178             borrowernumber   => $previous eq $this?"":$data->{borrowernumber},
179             itemnum          => $previous eq $this?"":$data->{itemnumber},
180             phone            => $previous eq $this?"":$data->{phone},
181             email            => $previous eq $this?"":$data->{email},
182             biblionumber     => $previous eq $this?"":$data->{biblionumber},
183             statusw          => ( $data->{found} eq "w" ),
184             statusf          => ( $data->{found} eq "f" ),
185             holdingbranch    => $data->{holdingbranch},
186             branch           => $previous eq $this?"":$data->{branch},
187             itemcallnumber   => $data->{itemcallnumber},
188             notes            => $previous eq $this?"":$data->{notes},
189             notificationdate => $previous eq $this?"":$data->{notificationdate},
190             reminderdate     => $previous eq $this?"":$data->{reminderdate},
191                         thisitemonly     => 1,
192         }
193     );
194     $previous=$this;
195 }
196
197 $sth->finish;
198
199 $template->param(
200     todaysdate      => format_date($todaysdate),
201     from             => $startdate,
202     to              => $enddate,
203     reserveloop     => \@reservedata,
204     "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
205     DHTMLcalendar_dateformat =>  C4::Dates->DHTMLcalendar(),
206 );
207
208 output_html_with_http_headers $input, $cookie, $template->output;