Bug 18579: Fix call to append_fields, fix tests
[koha.git] / reports / borrowers_out.pl
blob50c73b0dcbed7ba5ca9e2373d5076999f629de66
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use strict;
21 use warnings;
23 use CGI qw ( -utf8 );
24 use C4::Auth;
25 use C4::Context;
26 use C4::Koha;
27 use C4::Output;
28 use C4::Circulation;
29 use C4::Reports;
30 use C4::Members;
32 use Koha::DateUtils;
33 use Koha::Patron::Categories;
35 =head1 NAME
37 reports/borrowers_out.pl
39 =head1 DESCRIPTION
41 Plugin that shows a stats on borrowers
43 =cut
45 my $input = new CGI;
46 my $do_it=$input->param('do_it');
47 my $fullreportname = "reports/borrowers_out.tt";
48 my $limit = $input->param("Limit");
49 my $column = $input->param("Criteria");
50 my @filters = $input->multi_param("Filter");
51 $filters[1] = eval { output_pref( { dt => dt_from_string( $filters[1]), dateonly => 1, dateformat => 'iso' } ); }
52 if ( $filters[1] );
54 my $output = $input->param("output");
55 my $basename = $input->param("basename");
56 our $sep = $input->param("sep") || '';
57 $sep = "\t" if ($sep eq 'tabulation');
58 my ($template, $borrowernumber, $cookie)
59 = get_template_and_user({template_name => $fullreportname,
60 query => $input,
61 type => "intranet",
62 authnotrequired => 0,
63 flagsrequired => {reports => '*'},
64 debug => 1,
65 });
66 $template->param(do_it => $do_it,
68 if ($do_it) {
69 # Displaying results
70 my $results = calculate($limit, $column, \@filters);
71 if ($output eq "screen"){
72 # Printing results to screen
73 $template->param(mainloop => $results);
74 output_html_with_http_headers $input, $cookie, $template->output;
75 exit;
76 } else {
77 # Printing to a csv file
78 print $input->header(-type => 'application/vnd.sun.xml.calc',
79 -encoding => 'utf-8',
80 -attachment=>"$basename.csv",
81 -filename=>"$basename.csv" );
82 my $cols = @$results[0]->{loopcol};
83 my $lines = @$results[0]->{looprow};
84 # header top-right
85 print "num /". @$results[0]->{column} .$sep;
86 # Other header
87 foreach my $col ( @$cols ) {
88 print $col->{coltitle}.$sep;
90 print "Total\n";
91 # Table
92 foreach my $line ( @$lines ) {
93 my $x = $line->{loopcell};
94 print $line->{rowtitle}.$sep;
95 foreach my $cell (@$x) {
96 my $cellvalue = defined $cell->{value} ? $cell->{value}.$sep : ''.$sep;
97 print $cellvalue;
99 # print $line->{totalrow};
100 print "\n";
102 # footer
103 print "TOTAL";
104 $cols = @$results[0]->{loopfooter};
105 foreach my $col ( @$cols ) {
106 print $sep.$col->{totalcol};
108 print $sep.@$results[0]->{total};
109 exit;
111 # Displaying choices
112 } else {
113 my $dbh = C4::Context->dbh;
114 my @values;
115 my %labels;
116 my %select;
117 my $req;
119 my $CGIextChoice = ( 'CSV' ); # FIXME translation
120 my $CGIsepChoice = GetDelimiterChoices;
122 my $patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['categorycode']});
123 $template->param(
124 CGIextChoice => $CGIextChoice,
125 CGIsepChoice => $CGIsepChoice,
126 patron_categories => $patron_categories,
128 output_html_with_http_headers $input, $cookie, $template->output;
132 sub calculate {
133 my ($line, $column, $filters) = @_;
134 my @mainloop;
135 my @loopfooter;
136 my @loopcol;
137 my @loopline;
138 my @looprow;
139 my %globalline;
140 my $grantotal =0;
141 # extract parameters
142 my $dbh = C4::Context->dbh;
144 # Filters
145 # Checking filters
147 my @loopfilter;
148 for (my $i=0;$i<=2;$i++) {
149 my %cell;
150 if ( @$filters[$i] ) {
151 if (($i==1) and (@$filters[$i-1])) {
152 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
154 $cell{filter} .= @$filters[$i];
155 $cell{crit} .="Bor Cat" if ($i==0);
156 $cell{crit} .="Without issues since" if ($i==1);
157 push @loopfilter, \%cell;
160 my $colfield;
161 my $colorder;
162 if ($column){
163 $column = "borrowers.".$column if $column=~/categorycode/ || $column=~/branchcode/;
164 my @colfilter ;
165 $colfilter[0] = @$filters[0] if ($column =~ /category/ ) ;
166 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
167 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
169 # loop cols.
170 $colfield .= $column;
171 $colorder .= $column;
173 my $strsth2;
174 $strsth2 .= "select distinct " . $dbh->quote($colfield) . " FROM borrowers WHERE 1";
175 my @query_args;
176 if ( $colfilter[0] ) {
177 $colfilter[0] =~ s/\*/%/g;
178 $strsth2 .= " and " . $dbh->quote($column) . "LIKE ?" ;
179 push @query_args, $colfilter[0];
181 $strsth2 .=" group by " . $dbh->quote($colfield);
182 $strsth2 .=" order by " . $dbh->quote($colorder);
183 # warn "". $strsth2;
185 my $sth2 = $dbh->prepare( $strsth2 );
186 $sth2->execute( @query_args );
187 while (my ($celvalue) = $sth2->fetchrow) {
188 my %cell;
189 # my %ft;
190 # warn "coltitle :".$celvalue;
191 $cell{coltitle} = $celvalue;
192 # $ft{totalcol} = 0;
193 push @loopcol, \%cell;
195 # warn "fin des titres colonnes";
198 my $i=0;
199 # my @totalcol;
201 #Initialization of cell values.....
202 my @table;
204 # warn "init table";
205 if($line) {
206 for (my $i=1;$i<=$line;$i++) {
207 foreach my $col ( @loopcol ) {
208 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"Global"}=0;
214 # preparing calculation
215 my $strcalc ;
217 # Processing calculation
218 $strcalc .= "SELECT CONCAT( borrowers.surname , \"\\t\",borrowers.firstname, \"\\t\", borrowers.cardnumber)";
219 $strcalc .= " , " . $dbh->quote($colfield) if ($colfield);
220 $strcalc .= " FROM borrowers ";
221 $strcalc .= "WHERE 1 ";
222 my @query_args;
223 if ( @$filters[0] ) {
224 @$filters[0]=~ s/\*/%/g;
225 $strcalc .= " AND borrowers.categorycode like ?";
226 push @query_args, @$filters[0];
228 $strcalc .= " AND NOT EXISTS (SELECT * FROM issues WHERE issues.borrowernumber=borrowers.borrowernumber ";
229 if ( @$filters[1] ) {
230 $strcalc .= " AND issues.timestamp > ?";
231 push @query_args, @$filters[1];
233 $strcalc .= ") ";
234 $strcalc .= " AND NOT EXISTS (SELECT * FROM old_issues WHERE old_issues.borrowernumber=borrowers.borrowernumber ";
235 if ( @$filters[1] ) {
236 $strcalc .= " AND old_issues.timestamp > ?";
237 push @query_args, @$filters[1];
239 $strcalc .= ") ";
240 $strcalc .= " group by borrowers.borrowernumber";
241 $strcalc .= ", " . $dbh->quote($colfield) if ($column);
242 $strcalc .= " order by " . $dbh->quote($colfield) if ($colfield);
243 my $max;
244 if ($line) {
245 if (@loopcol) {
246 $max = $line*@loopcol;
247 } else { $max=$line;}
248 $strcalc .= " LIMIT 0,$max";
251 my $dbcalc = $dbh->prepare($strcalc);
252 $dbcalc->execute( @query_args );
253 # warn "filling table";
254 my $previous_col;
255 $i=1;
256 while (my @data = $dbcalc->fetchrow) {
257 my ($row, $col )=@data;
258 $col = "zzEMPTY" if (!defined($col));
259 $i=1 if (($previous_col) and not($col eq $previous_col));
260 $table[$i]->{$col}=$row;
261 # warn " $i $col $row";
262 $i++;
263 $previous_col=$col;
266 push @loopcol,{coltitle => "Global"} if not($column);
268 $max =(($line)?$line:@table -1);
269 for ($i=1; $i<=$max;$i++) {
270 my @loopcell;
271 #@loopcol ensures the order for columns is common with column titles
272 # and the number matches the number of columns
273 my $colcount=0;
274 foreach my $col ( @loopcol ) {
275 my $value;
276 if (@loopcol){
277 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}};
278 } else {
279 $value =$table[$i]->{"zzEMPTY"};
281 push @loopcell, {value => $value} ;
283 push @looprow,{ 'rowtitle' => $i ,
284 'loopcell' => \@loopcell,
290 # the header of the table
291 $globalline{loopfilter}=\@loopfilter;
292 # the core of the table
293 $globalline{looprow} = \@looprow;
294 $globalline{loopcol} = \@loopcol;
295 # # the foot (totals by borrower type)
296 $globalline{loopfooter} = \@loopfooter;
297 $globalline{total}= $grantotal;
298 $globalline{line} = $line;
299 $globalline{column} = $column;
300 push @mainloop,\%globalline;
301 return \@mainloop;
305 __END__