4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
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
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.
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
22 # use warnings; # FIXME
28 use C4::Dates qw/format_date/;
29 use Date::Calc qw/Today/;
33 my $order = $input->param( 'order' ) || '';
34 my $showall = $input->param('showall');
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' ) || '';
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
45 template_name => "circ/overdue.tmpl",
49 flagsrequired => { reports => 1, circulate => "circulate_remaining_permissions" },
54 my $dbh = C4::Context->dbh;
57 $req = $dbh->prepare( "select categorycode, description from categories order by description");
60 while (my ($catcode, $description) =$req->fetchrow) {
63 selected => $catcode eq $borcatfilter ? 1 : 0,
64 catname => $description,
68 $req = $dbh->prepare( "select itemtype, description from itemtypes order by description");
71 while (my ($itemtype, $description) =$req->fetchrow) {
74 selected => $itemtype eq $itemtypefilter ? 1 : 0,
75 itemtypename => $description,
78 my $onlymine=C4::Context->preference('IndependantBranches') &&
79 C4::Context->userenv &&
80 C4::Context->userenv->{flags}!=1 &&
81 C4::Context->userenv->{branch};
83 $branchfilter = C4::Context->userenv->{'branch'} if ($onlymine && !$branchfilter);
86 branchloop => GetBranchesLoop($branchfilter, $onlymine),
87 branchfilter => $branchfilter,
88 borcatloop => \@borcatloop,
89 itemtypeloop => \@itemtypeloop,
90 borname => $bornamefilter,
93 csv_param_string => $input->query_string(),
96 my @sort_roots = qw(borrower title barcode date_due);
97 push @sort_roots, map {$_ . " desc"} @sort_roots;
98 my @order_loop = ({selected => $order ? 0 : 1}); # initial blank row
99 foreach (@sort_roots) {
101 $tmpl_name =~ s/\s/_/g;
103 selected => $order eq $_ ? 1 : 0,
106 'order_' . $tmpl_name => 1,
109 $template->param(ORDER_LOOP => \@order_loop);
111 my $todaysdate = sprintf("%-04.4d-%-02.2d-%02.2d", Today());
113 $bornamefilter =~s/\*/\%/g;
114 $bornamefilter =~s/\?/\_/g;
116 my $strsth="SELECT date_due,
117 concat(surname,' ', firstname) as borrower,
126 items.itemcallnumber,
129 borrowers.borrowernumber,
133 LEFT JOIN borrowers ON (issues.borrowernumber=borrowers.borrowernumber )
134 LEFT JOIN items ON (issues.itemnumber=items.itemnumber)
135 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
136 LEFT JOIN biblio ON (biblio.biblionumber=items.biblionumber )
137 WHERE 1=1 "; # placeholder, since it is possible that none of the additional
138 # conditions will be selected by user
139 $strsth.=" AND date_due < NOW() " unless ($showall);
140 $strsth.=" AND (borrowers.firstname like '".$bornamefilter."%' or borrowers.surname like '".$bornamefilter."%' or borrowers.cardnumber like '".$bornamefilter."%')" if($bornamefilter) ;
141 $strsth.=" AND borrowers.categorycode = '" . $borcatfilter . "' " if $borcatfilter;
142 $strsth.=" AND biblioitems.itemtype = '" . $itemtypefilter . "' " if $itemtypefilter;
143 $strsth.=" AND borrowers.flags = '" . $borflagsfilter . "' " if $borflagsfilter;
144 $strsth.=" AND borrowers.branchcode = '" . $branchfilter . "' " if $branchfilter;
145 $strsth.=" ORDER BY " . (
146 ($order eq "borrower" or $order eq "borrower desc") ? "$order, date_due" :
147 ($order eq "title" or $order eq "title desc") ? "$order, date_due, borrower" :
148 ($order eq "barcode" or $order eq "barcode desc") ? "items.$order, date_due, borrower" :
149 ($order eq "date_due desc") ? "date_due DESC, borrower" :
150 "date_due, borrower" # default sort order
152 $template->param(sql=>$strsth);
153 my $sth=$dbh->prepare($strsth);
154 #warn "overdue.pl : query string ".$strsth;
158 while (my $data=$sth->fetchrow_hashref) {
160 issuedate => format_date($data->{issuedate}),
161 duedate => format_date($data->{date_due}),
162 borrowernumber => $data->{borrowernumber},
163 barcode => $data->{barcode},
164 itemnum => $data->{itemnumber},
165 itemcallnumber => $data->{itemcallnumber},
166 name => $data->{borrower},
167 address => $data->{address},
168 city => $data->{city},
169 zipcode => $data->{zipcode},
170 phone => $data->{phone},
171 email => $data->{email},
172 biblionumber => $data->{biblionumber},
173 title => $data->{title},
174 author => $data->{author},
175 branchcode => $data->{branchcode},
180 todaysdate => format_date($todaysdate),
181 overdueloop => \@overduedata
184 # download the complete CSV
186 binmode(STDOUT, ":utf8");
187 my $csv = build_csv(\@overduedata);
188 print $input->header(-type => 'application/vnd.sun.xml.calc',
189 -encoding => 'utf-8',
190 -attachment=>"overdues.csv",
191 -filename=>"overdues.csv" );
196 output_html_with_http_headers $input, $cookie, $template->output;
200 my $overdues = shift;
202 return "" if scalar(@$overdues) == 0;
207 my @keys = sort keys %{ $overdues->[0] };
208 my $csv = Text::CSV_XS->new({
209 sep_char => C4::Context->preference("delimiter") ?
210 C4::Context->preference("delimiter") : ';' ,
212 $csv->combine(@keys);
213 push @lines, $csv->string();
215 # ... and rest of report
216 foreach my $overdue ( @{ $overdues } ) {
217 push @lines, $csv->string() if $csv->combine(map { $overdue->{$_} } @keys);
220 return join("\n", @lines) . "\n";