bug 2874 [3/3] flagsrequired => { circulate => "circulate_remaining_permissions" }
[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 => "circulate_remaining_permissions" },
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 $lib = $branchfilter ? "-library $branchfilter" :'';
60     my $csv = `../misc/cronjobs/overdue_notices.pl -csv -n $lib`;
61     print $input->header(-type => 'application/vnd.sun.xml.calc',
62                         -encoding    => 'utf-8',
63                         -attachment=>"overdues.csv",
64                         -filename=>"overdues.csv" );
65     print $csv;
66     exit;
67 }
68 my $req;
69 $req = $dbh->prepare( "select categorycode, description from categories order by description");
70 $req->execute;
71 my @borcatloop;
72 while (my ($catcode, $description) =$req->fetchrow) {
73   my $selected = 1 if $catcode eq $borcatfilter;
74   my %row =(value => $catcode,
75         selected => $selected,
76         catname => $description,
77       );
78   push @borcatloop, \%row;
79 }
80
81 $req = $dbh->prepare( "select itemtype, description from itemtypes order by description");
82 $req->execute;
83 my @itemtypeloop;
84 while (my ($itemtype, $description) =$req->fetchrow) {
85   my $selected = 1 if $itemtype eq $itemtypefilter;
86   my %row =(value => $itemtype,
87         selected => $selected,
88         itemtypename => $description,
89       );
90   push @itemtypeloop, \%row;
91 }
92 my $onlymine=C4::Context->preference('IndependantBranches') && 
93              C4::Context->userenv && 
94              C4::Context->userenv->{flags}!=1 && 
95              C4::Context->userenv->{branch};
96 my $branches = GetBranches($onlymine);
97 my @branchloop;
98
99 foreach my $thisbranch ( sort keys %$branches ) {
100      my %row = (
101         value      => $thisbranch,
102         branchname => $branches->{$thisbranch}->{'branchname'},
103         selected   => ($branches->{$thisbranch}->{'branchcode'} eq $branchfilter)
104     );
105     push @branchloop, \%row;
106 }
107 $branchfilter=C4::Context->userenv->{'branch'} if ($onlymine && !$branchfilter);
108
109 $template->param( branchloop => \@branchloop,
110                   branchfilter => $branchfilter);
111 $template->param(borcatloop=> \@borcatloop,
112           itemtypeloop => \@itemtypeloop,
113           branchloop=> \@branchloop,
114           borname => $bornamefilter,
115           order => $order,
116           showall => $showall);
117
118 my $duedate;
119 my $borrowernumber;
120 my $itemnum;
121 my $data1;
122 my $data2;
123 my $data3;
124 my $name;
125 my $phone;
126 my $email;
127 my $title;
128 my $author;
129
130 my $todaysdate = sprintf("%-04.4d-%-02.2d-%02.2d", Today());
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,biblio.biblionumber,borrowers.branchcode 
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 1=1 "; # placeholder, since it is possible that none of the additional
143              # conditions will be selected by user
144 $strsth.= " && date_due<'".$todaysdate."' " unless ($showall);
145 $strsth.=" && (borrowers.firstname like '".$bornamefilter."%' or borrowers.surname like '".$bornamefilter."%' or borrowers.cardnumber like '".$bornamefilter."%')" if($bornamefilter) ;
146 $strsth.=" && borrowers.categorycode = '".$borcatfilter."' " if($borcatfilter) ;
147 $strsth.=" && biblioitems.itemtype = '".$itemtypefilter."' " if($itemtypefilter) ;
148 $strsth.=" && borrowers.flags = '".$borflagsfilter."' " if ($borflagsfilter ne " ") ;
149 $strsth.=" && borrowers.branchcode = '".$branchfilter."' " if($branchfilter) ;
150 if ($order eq "borrower"){
151   $strsth.=" ORDER BY borrower,date_due " ;
152 } elsif ($order eq "title"){
153   $strsth.=" ORDER BY title,date_due,borrower ";
154 } elsif ($order eq "barcode"){
155   $strsth.=" ORDER BY items.barcode,date_due,borrower ";
156 }elsif ($order eq "borrower DESC"){
157   $strsth.=" ORDER BY borrower desc,date_due " ;
158 } elsif ($order eq "title DESC"){
159   $strsth.=" ORDER BY title desc,date_due,borrower ";
160 } elsif ($order eq "barcode DESC"){
161   $strsth.=" ORDER BY items.barcode desc,date_due,borrower ";
162 } elsif ($order eq "date_due DESC"){
163   $strsth.=" ORDER BY date_due DESC,borrower ";
164 } else {
165   $strsth.=" ORDER BY date_due,borrower ";
166 }
167 my $sth=$dbh->prepare($strsth);
168 #warn "overdue.pl : query string ".$strsth;
169 $sth->execute();
170
171 my @overduedata;
172 while (my $data=$sth->fetchrow_hashref) {
173   $duedate=$data->{'date_due'};
174   $duedate = format_date($duedate);
175   $itemnum=$data->{'itemnumber'};
176
177   $name=$data->{'borrower'};
178   $phone=$data->{'phone'};
179   $email=$data->{'email'};
180
181   $title=$data->{'title'};
182   $author=$data->{'author'};
183   push (@overduedata, {
184                         duedate        => $duedate,
185                         borrowernumber => $data->{borrowernumber},
186                         barcode        => $data->{barcode},
187                         itemnum        => $itemnum,
188                         name           => $name,
189                         phone          => $phone,
190                         email          => $email,
191                         biblionumber   => $data->{'biblionumber'},
192                         title          => $title,
193                         author         => $author,
194                         branchcode     => $data->{'branchcode'} });
195 }
196
197 $template->param(
198     todaysdate  => format_date($todaysdate),
199     overdueloop => \@overduedata
200 );
201
202 output_html_with_http_headers $input, $cookie, $template->output;