Reintroducing overdue filters.
[koha.git] / circ / overdue.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::Branch;
28 use C4::Date;
29
30 my $input = new CGI;
31 my $type  = $input->param('type');
32
33 my $theme = $input->param('theme');    # only used if allowthemeoverride is set
34 my $order=$input->param('order');
35 my $bornamefilter=$input->param('borname');
36 my $borcatfilter=$input->param('borcat');
37 my $itemtypefilter=$input->param('itemtype');
38 my $borflagsfilter=$input->param('borflags') || " ";
39 my $branchfilter=$input->param('branch');
40 my $showall=$input->param('showall');
41 my $theme = $input->param('theme'); # only used if allowthemeoverride is set
42
43
44 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
45     {
46         template_name   => "circ/overdue.tmpl",
47         query           => $input,
48         type            => "intranet",
49         authnotrequired => 0,
50         flagsrequired   => { reports => 1, circulate => 1 },
51         debug           => 1,
52     }
53 );
54 my $dbh = C4::Context->dbh;
55
56 my $req;
57 $req = $dbh->prepare( "select categorycode, description from categories order by description");
58 $req->execute;
59 my @borcatloop;
60 while (my ($catcode, $description) =$req->fetchrow) {
61   my $selected = 1 if $catcode eq $borcatfilter;
62   my %row =(value => $catcode,
63         selected => $selected,
64         catname => $description,
65       );
66   push @borcatloop, \%row;
67 }
68
69 $req = $dbh->prepare( "select itemtype, description from itemtypes order by description");
70 $req->execute;
71 my @itemtypeloop;
72 while (my ($itemtype, $description) =$req->fetchrow) {
73   my $selected = 1 if $itemtype eq $itemtypefilter;
74   my %row =(value => $itemtype,
75         selected => $selected,
76         itemtypename => $description,
77       );
78   push @itemtypeloop, \%row;
79 }
80 my $onlymine=C4::Context->preference('IndependantBranches') && 
81              C4::Context->userenv && 
82              C4::Context->userenv->{flags}!=1 && 
83              C4::Context->userenv->{branch};
84 my $branches = GetBranches($onlymine);
85 my @branchloop;
86 my @selectflags;
87 push @selectflags, " ";#
88 push @selectflags,"gonenoaddress";#
89 push @selectflags,"debarred";#
90 push @selectflags,"lost";#
91 my $CGIflags=CGI::scrolling_list( -name     => 'borflags',
92             -id =>'borflags',
93             -values   => \@selectflags,
94             -size     => 1,
95             -multiple => 0 );
96
97 foreach my $thisbranch ( sort keys %$branches ) {
98      my %row = (
99         value      => $thisbranch,
100         branchname => $branches->{$thisbranch}->{'branchname'},
101         selected   => (C4::Context->userenv && $branches->{$thisbranch}->{'branchcode'} eq C4::Context->userenv->{'branch'})
102     );
103     push @branchloop, \%row;
104 }
105 $branchfilter=C4::Context->userenv->{'branch'} if ($onlymine && !$branchfilter);
106
107 $template->param( branchloop => \@branchloop );
108 $template->param(borcatloop=> \@borcatloop,
109           itemtypeloop => \@itemtypeloop,
110           branchloop=> \@branchloop,
111           CGIflags     => $CGIflags,
112           borname => $bornamefilter,
113           order => $order,
114           showall => $showall);
115
116 my $duedate;
117 my $borrowernumber;
118 my $itemnum;
119 my $data1;
120 my $data2;
121 my $data3;
122 my $name;
123 my $phone;
124 my $email;
125 my $biblionumber;
126 my $title;
127 my $author;
128 my @datearr    = localtime( time() );
129 my $todaysdate =
130     ( 1900 + $datearr[5] ) . '-'
131   . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
132   . sprintf( "%0.2d", $datearr[3] );
133
134
135 $bornamefilter =~s/\*/\%/g;
136 $bornamefilter =~s/\?/\_/g;
137
138 my $strsth="select date_due,concat(surname,' ', firstname) as borrower, 
139   borrowers.phone, borrowers.email,issues.itemnumber, items.barcode, biblio.title, biblio.author,borrowers.borrowernumber 
140   from issues
141 LEFT JOIN borrowers ON (issues.borrowernumber=borrowers.borrowernumber )
142 LEFT JOIN items ON (issues.itemnumber=items.itemnumber)
143 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
144 LEFT JOIN biblio ON (biblio.biblionumber=items.biblionumber )
145 where isnull(returndate) ";
146 $strsth.= " && date_due<'".$todaysdate."' " unless ($showall);
147 $strsth.=" && (borrowers.firstname like '".$bornamefilter."%' or borrowers.surname like '".$bornamefilter."%' or borrowers.cardnumber like '".$bornamefilter."%')" if($bornamefilter) ;
148 $strsth.=" && borrowers.categorycode = '".$borcatfilter."' " if($borcatfilter) ;
149 $strsth.=" && biblioitems.itemtype = '".$itemtypefilter."' " if($itemtypefilter) ;
150 $strsth.=" && borrowers.flags = '".$borflagsfilter."' " if ($borflagsfilter ne " ") ;
151 $strsth.=" && issues.branchcode = '".$branchfilter."' " if($branchfilter) ;
152 if ($order eq "borrower"){
153   $strsth.=" order by borrower,date_due " ;
154 } elsif ($order eq "title"){
155   $strsth.=" order by title,date_due,borrower ";
156 } elsif ($order eq "barcode"){
157   $strsth.=" order by items.barcode,date_due,borrower ";
158 }elsif ($order eq "borrower desc"){
159   $strsth.=" order by borrower desc,date_due " ;
160 } elsif ($order eq "title desc"){
161   $strsth.=" order by title desc,date_due,borrower ";
162 } elsif ($order eq "barcode desc"){
163   $strsth.=" order by items.barcode desc,date_due,borrower ";
164 } elsif ($order eq "date_due desc"){
165   $strsth.=" order by date_due desc,borrower ";
166 } else {
167   $strsth.=" order by date_due,borrower ";
168 }
169 my $sth=$dbh->prepare($strsth);
170 #warn "overdue.pl : query string ".$strsth;
171 $sth->execute();
172
173 my @overduedata;
174 while (my $data=$sth->fetchrow_hashref) {
175   $duedate=$data->{'date_due'};
176   $duedate = format_date($duedate);
177   $itemnum=$data->{'itemnumber'};
178
179   $name=$data->{'borrower'};
180   $phone=$data->{'phone'};
181   $email=$data->{'email'};
182
183   $title=$data->{'title'};
184   $author=$data->{'author'};
185   push (@overduedata, { duedate      => $duedate,
186       bornum       => $data->{borrowernumber},
187       barcode      => $data->{barcode},
188       itemnum      => $itemnum,
189       name         => $name,
190       phone        => $phone,
191       email        => $email,
192       biblionumber => $biblionumber,
193       title        => $title,
194       author       => $author });
195
196 }
197
198 $template->param(
199     todaysdate  => $todaysdate,
200     overdueloop => \@overduedata
201 );
202
203 output_html_with_http_headers $input, $cookie, $template->output;