fix for 2382: Self Checkout Won't Work for Staff with Circ
[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 qw/format_date/;
28 use Date::Calc qw/Today/;
29
30 my $input = new CGI;
31 my $type    = $input->param('type');
32 my $theme   = $input->param('theme');    # only used if allowthemeoverride is set
33 my $order   = $input->param('order');
34 my $showall = $input->param('showall');
35
36 my  $bornamefilter = $input->param('borname');
37 my   $borcatfilter = $input->param('borcat');
38 my $itemtypefilter = $input->param('itemtype');
39 my $borflagsfilter = $input->param('borflags') || " ";
40 my   $branchfilter = $input->param('branch');
41 my $op             = $input->param('op');
42
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
44     {
45         template_name   => "circ/overdue.tmpl",
46         query           => $input,
47         type            => "intranet",
48         authnotrequired => 0,
49         flagsrequired   => { reports => 1, circulate => 1 },
50         debug           => 1,
51     }
52 );
53 my $dbh = C4::Context->dbh;
54
55
56 # download the complete CSV
57 if ($op eq 'csv') {
58 warn "BRANCH : $branchfilter";
59     my $csv = `../misc/cronjobs/overdue_notices.pl -csv -n`; # -library $branchfilter`;
60     print $input->header(-type => 'application/vnd.sun.xml.calc',
61                         -encoding    => 'utf-8',
62                         -attachment=>"overdues.csv",
63                         -filename=>"overdues.csv" );
64     print $csv;
65     exit;
66 }
67 my $req;
68 $req = $dbh->prepare( "select categorycode, description from categories order by description");
69 $req->execute;
70 my @borcatloop;
71 while (my ($catcode, $description) =$req->fetchrow) {
72   my $selected = 1 if $catcode eq $borcatfilter;
73   my %row =(value => $catcode,
74         selected => $selected,
75         catname => $description,
76       );
77   push @borcatloop, \%row;
78 }
79
80 $req = $dbh->prepare( "select itemtype, description from itemtypes order by description");
81 $req->execute;
82 my @itemtypeloop;
83 while (my ($itemtype, $description) =$req->fetchrow) {
84   my $selected = 1 if $itemtype eq $itemtypefilter;
85   my %row =(value => $itemtype,
86         selected => $selected,
87         itemtypename => $description,
88       );
89   push @itemtypeloop, \%row;
90 }
91 my $onlymine=C4::Context->preference('IndependantBranches') && 
92              C4::Context->userenv && 
93              C4::Context->userenv->{flags}!=1 && 
94              C4::Context->userenv->{branch};
95 my $branches = GetBranches($onlymine);
96 my @branchloop;
97
98 foreach my $thisbranch ( sort keys %$branches ) {
99      my %row = (
100         value      => $thisbranch,
101         branchname => $branches->{$thisbranch}->{'branchname'},
102         selected   => ($branches->{$thisbranch}->{'branchcode'} eq $branchfilter)
103     );
104     push @branchloop, \%row;
105 }
106 $branchfilter=C4::Context->userenv->{'branch'} if ($onlymine && !$branchfilter);
107
108 $template->param( branchloop => \@branchloop,
109                   branchfilter => $branchfilter);
110 $template->param(borcatloop=> \@borcatloop,
111           itemtypeloop => \@itemtypeloop,
112           branchloop=> \@branchloop,
113           borname => $bornamefilter,
114           order => $order,
115           showall => $showall);
116
117 my $duedate;
118 my $borrowernumber;
119 my $itemnum;
120 my $data1;
121 my $data2;
122 my $data3;
123 my $name;
124 my $phone;
125 my $email;
126 my $title;
127 my $author;
128
129 my $todaysdate = sprintf("%-04.4d-%-02.2d-%02.2d", Today());
130
131 $bornamefilter =~s/\*/\%/g;
132 $bornamefilter =~s/\?/\_/g;
133
134 my $strsth="SELECT date_due,concat(surname,' ', firstname) as borrower, 
135   borrowers.phone, borrowers.email,issues.itemnumber, items.barcode, biblio.title, biblio.author,borrowers.borrowernumber,biblio.biblionumber,borrowers.branchcode 
136   FROM issues
137 LEFT JOIN borrowers ON (issues.borrowernumber=borrowers.borrowernumber )
138 LEFT JOIN items ON (issues.itemnumber=items.itemnumber)
139 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
140 LEFT JOIN biblio ON (biblio.biblionumber=items.biblionumber )
141 WHERE 1=1 "; # placeholder, since it is possible that none of the additional
142              # conditions will be selected by user
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.=" && borrowers.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, {
183                         duedate        => $duedate,
184                         borrowernumber => $data->{borrowernumber},
185                         barcode        => $data->{barcode},
186                         itemnum        => $itemnum,
187                         name           => $name,
188                         phone          => $phone,
189                         email          => $email,
190                         biblionumber   => $data->{'biblionumber'},
191                         title          => $title,
192                         author         => $author,
193                         branchcode     => $data->{'branchcode'} });
194 }
195
196 $template->param(
197     todaysdate  => format_date($todaysdate),
198     overdueloop => \@overduedata
199 );
200
201 output_html_with_http_headers $input, $cookie, $template->output;