bug fix : items.homebranch must be VARCHAR(10)
[koha.git] / circ / pendingreserves.pl
1 #!/usr/bin/perl
2
3 # $Id$
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 use strict;
23 use C4::Context;
24 use C4::Output;
25 use CGI;
26 use C4::Auth;
27 use C4::Date;
28
29 my $input = new CGI;
30 my $order = $input->param('order');
31 my $startdate=$input->param('from');
32 my $enddate=$input->param('to');
33
34 my $theme = $input->param('theme');    # only used if allowthemeoverride is set
35
36 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
37     {
38         template_name   => "circ/pendingreserves.tmpl",
39         query           => $input,
40         type            => "intranet",
41         authnotrequired => 0,
42         flagsrequired   => { circulate => 1 },
43         debug           => 1,
44     }
45 );
46
47 my $duedate;
48 my $borrowernumber;
49 my $itemnum;
50 my $data1;
51 my $data2;
52 my $data3;
53 my $name;
54 my $phone;
55 my $email;
56 my $biblionumber;
57 my $title;
58 my $author;
59
60 my @datearr    = localtime( time() );
61 my $todaysdate =
62     ( 1900 + $datearr[5] ) . '-'
63   . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
64   . sprintf( "%0.2d", $datearr[3] );
65
66 my $dbh    = C4::Context->dbh;
67 my ($sqlorderby, $sqldatewhere) = ("","");
68
69 $sqldatewhere .= " && reservedate >= " . $dbh->quote($startdate)  if ($startdate) ;
70 $sqldatewhere .= " && reservedate <= " . $dbh->quote($enddate)  if ($enddate) ;
71
72 if ($order eq "borrower") {
73         $sqlorderby = " order by  borrower, reservedate";
74 } elsif ($order eq "biblio") {
75         $sqlorderby = " order by biblio.title, priority,reservedate";
76 } elsif ($order eq "priority") {
77     $sqlorderby = "order by priority DESC";
78 } else {
79         $sqlorderby = " order by reservedate, borrower";
80 }
81 my $strsth =
82 "SELECT reservedate,
83         reserves.borrowernumber as borrowernumber,
84         concat(firstname,' ',surname) as borrower,
85         borrowers.phone,
86         borrowers.email,
87         reserves.biblionumber,
88         reserves.branchcode as branch,
89         items.holdingbranch,
90         items.itemcallnumber,
91         items.itemnumber,
92         notes,
93         notificationdate,
94         reminderdate,
95         priority,
96         reserves.found,
97         biblio.title,
98         biblio.author
99  FROM  reserves
100  LEFT JOIN items ON items.biblionumber=reserves.biblionumber,
101   borrowers,biblio
102  WHERE isnull(cancellationdate)
103   && reserves.borrowernumber=borrowers.borrowernumber 
104   && reserves.biblionumber=biblio.biblionumber
105   && reserves.found is NULL
106   && items.holdingbranch=?
107  ";
108
109 $strsth .= $sqlorderby;
110
111 my $sth = $dbh->prepare($strsth);
112
113 $sth->execute(C4::Context->userenv->{'branch'});
114
115 my @reservedata;
116 my $previous;
117 my $this;
118 while ( my $data = $sth->fetchrow_hashref ) {
119     $this=$data->{biblionumber}.":".$data->{borrowernumber};
120     my @itemlist;
121     push(
122         @reservedata,
123         {
124             reservedate      => $previous eq $this?"":format_date( $data->{reservedate} ),
125             priority         => $previous eq $this?"":$data->{priority},
126             name             => $previous eq $this?"":$data->{borrower},
127             title            => $previous eq $this?"":$data->{title},
128             author           => $previous eq $this?"":$data->{author},
129             borrowernumber   => $previous eq $this?"":$data->{borrowernumber},
130             itemnum          => $previous eq $this?"":$data->{itemnumber},
131             phone            => $previous eq $this?"":$data->{phone},
132             email            => $previous eq $this?"":$data->{email},
133             biblionumber     => $previous eq $this?"":$data->{biblionumber},
134             statusw          => ( $data->{found} eq "w" ),
135             statusf          => ( $data->{found} eq "f" ),
136             holdingbranch    => $data->{holdingbranch},
137             branch           => $previous eq $this?"":$data->{branch},
138             itemcallnumber   => $data->{itemcallnumber},
139             notes            => $previous eq $this?"":$data->{notes},
140             notificationdate => $previous eq $this?"":$data->{notificationdate},
141             reminderdate     => $previous eq $this?"":$data->{reminderdate}
142         }
143     );
144     $previous=$this;
145 }
146
147 $sth->finish;
148
149 $template->param(
150     todaysdate              => format_date($todaysdate),
151         from                        => $startdate,
152         to                                  => $enddate,
153     reserveloop             => \@reservedata,
154     intranetcolorstylesheet =>
155       C4::Context->preference("intranetcolorstylesheet"),
156     intranetstylesheet => C4::Context->preference("intranetstylesheet"),
157     IntranetNav        => C4::Context->preference("IntranetNav"),
158     "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
159 );
160
161 output_html_with_http_headers $input, $cookie, $template->output;