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