FIX for Date calculation
[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=format_date_in_iso($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  LEFT JOIN borrowers ON reserves.borrowernumber=borrowers.borrowernumber
102  LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber
103  WHERE isnull(cancellationdate)
104   && reserves.found is NULL
105   && items.holdingbranch=?
106  ";
107
108 $strsth .= $sqlorderby;
109
110 my $sth = $dbh->prepare($strsth);
111
112 $sth->execute(C4::Context->userenv->{'branch'});
113
114 my @reservedata;
115 my $previous;
116 my $this;
117 while ( my $data = $sth->fetchrow_hashref ) {
118     $this=$data->{biblionumber}.":".$data->{borrowernumber};
119     my @itemlist;
120     push(
121         @reservedata,
122         {
123             reservedate      => $previous eq $this?"":format_date( $data->{reservedate} ),
124             priority         => $previous eq $this?"":$data->{priority},
125             name             => $previous eq $this?"":$data->{borrower},
126             title            => $previous eq $this?"":$data->{title},
127             author           => $previous eq $this?"":$data->{author},
128             borrowernumber   => $previous eq $this?"":$data->{borrowernumber},
129             itemnum          => $previous eq $this?"":$data->{itemnumber},
130             phone            => $previous eq $this?"":$data->{phone},
131             email            => $previous eq $this?"":$data->{email},
132             biblionumber     => $previous eq $this?"":$data->{biblionumber},
133             statusw          => ( $data->{found} eq "w" ),
134             statusf          => ( $data->{found} eq "f" ),
135             holdingbranch    => $data->{holdingbranch},
136             branch           => $previous eq $this?"":$data->{branch},
137             itemcallnumber   => $data->{itemcallnumber},
138             notes            => $previous eq $this?"":$data->{notes},
139             notificationdate => $previous eq $this?"":$data->{notificationdate},
140             reminderdate     => $previous eq $this?"":$data->{reminderdate}
141         }
142     );
143     $previous=$this;
144 }
145
146 $sth->finish;
147
148 $template->param(
149     todaysdate      => format_date($todaysdate),
150     fro             => $startdate,
151     to              => $enddate,
152     reserveloop     => \@reservedata,
153     "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
154     DHTMLcalendar_dateformat => get_date_format_string_for_DHTMLcalendar(),
155 );
156
157 output_html_with_http_headers $input, $cookie, $template->output;