Bug 6679: Fixing admin/aqplain :encoding(UTF-8) gives strict validation
[koha.git] / reports / borrowers_stats.pl
blobc17a32683dfe312b4a45185204a639c1909c9b6c
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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use strict;
21 #use warnings; FIXME - Bug 2505
22 use CGI;
23 use C4::Auth;
24 use C4::Context;
25 use C4::Branch; # GetBranches
26 use C4::Koha;
27 use C4::Dates;
28 use C4::Acquisition;
29 use C4::Output;
30 use C4::Reports;
31 use C4::Circulation;
32 use C4::Dates qw/format_date format_date_in_iso/;
33 use Date::Calc qw(
34 Today
35 Add_Delta_YM
38 =head1 NAME
40 plugin that shows a stats on borrowers
42 =head1 DESCRIPTION
44 =over 2
46 =cut
48 my $input = new CGI;
49 my $do_it=$input->param('do_it');
50 my $fullreportname = "reports/borrowers_stats.tmpl";
51 my $line = $input->param("Line");
52 my $column = $input->param("Column");
53 my @filters = $input->param("Filter");
54 $filters[3]=format_date_in_iso($filters[3]);
55 $filters[4]=format_date_in_iso($filters[4]);
56 my $digits = $input->param("digits");
57 my $period = $input->param("period");
58 my $borstat = $input->param("status");
59 my $borstat1 = $input->param("activity");
60 my $output = $input->param("output");
61 my $basename = $input->param("basename");
62 our $sep = $input->param("sep");
63 $sep = "\t" if ($sep eq 'tabulation');
64 my $selected_branch; # = $input->param("?");
66 our $branches = GetBranches;
68 my ($template, $borrowernumber, $cookie)
69 = get_template_and_user({template_name => $fullreportname,
70 query => $input,
71 type => "intranet",
72 authnotrequired => 0,
73 flagsrequired => {reports => '*'},
74 debug => 1,
75 });
76 $template->param(do_it => $do_it);
77 if ($do_it) {
78 my $results = calculate($line, $column, $digits, $borstat,$borstat1 ,\@filters);
79 if ($output eq "screen"){
80 $template->param(mainloop => $results);
81 output_html_with_http_headers $input, $cookie, $template->output;
82 } else {
83 print $input->header(-type => 'application/vnd.sun.xml.calc',
84 -encoding => 'utf-8',
85 -name => "$basename.csv",
86 -attachment => "$basename.csv");
87 my $cols = @$results[0]->{loopcol};
88 my $lines = @$results[0]->{looprow};
89 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
90 foreach my $col ( @$cols ) {
91 print $col->{coltitle}.$sep;
93 print "Total\n";
94 foreach my $line ( @$lines ) {
95 my $x = $line->{loopcell};
96 print $line->{rowtitle}.$sep;
97 foreach my $cell (@$x) {
98 print $cell->{value}.$sep;
100 print $line->{totalrow};
101 print "\n";
103 print "TOTAL";
104 $cols = @$results[0]->{loopfooter};
105 foreach my $col ( @$cols ) {
106 print $sep.$col->{totalcol};
108 print $sep.@$results[0]->{total};
110 exit(1); # exit after do_it, regardless
111 } else {
112 my $dbh = C4::Context->dbh;
113 my $req;
114 $template->param( CAT_LOOP => &catcode_aref);
115 my @branchloop;
116 foreach (sort {$branches->{$a}->{branchname} cmp $branches->{$b}->{branchname}} keys %$branches) {
117 my $line = {branchcode => $_, branchname => $branches->{$_}->{branchname} || 'UNKNOWN'};
118 $line->{selected} = 'selected' if ($selected_branch and $selected_branch eq $_);
119 push @branchloop, $line;
121 $template->param(BRANCH_LOOP => \@branchloop);
122 $req = $dbh->prepare("SELECT DISTINCTROW zipcode FROM borrowers WHERE zipcode IS NOT NULL AND zipcode <> '' ORDER BY zipcode");
123 $req->execute;
124 $template->param( ZIP_LOOP => $req->fetchall_arrayref({}));
125 $req = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category='Bsort1' ORDER BY lib");
126 $req->execute;
127 $template->param( SORT1_LOOP => $req->fetchall_arrayref({}));
128 $req = $dbh->prepare("SELECT DISTINCTROW sort2 AS value FROM borrowers WHERE sort2 IS NOT NULL AND sort2 <> '' ORDER BY sort2 LIMIT 200");
129 # More than 200 items in a dropdown is not going to be useful anyway, and w/ 50,000 patrons we can destory DB performance.
130 $req->execute;
131 $template->param( SORT2_LOOP => $req->fetchall_arrayref({}));
133 my $CGIextChoice=CGI::scrolling_list(
134 -name => 'MIME',
135 -id => 'MIME',
136 -values => ['CSV'], # FIXME translation
137 -size => 1,
138 -multiple => 0 );
139 my $CGIsepChoice=GetDelimiterChoices;
140 $template->param(
141 CGIextChoice => $CGIextChoice,
142 CGIsepChoice => $CGIsepChoice,
143 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
147 output_html_with_http_headers $input, $cookie, $template->output;
149 sub catcode_aref() {
150 my $req = C4::Context->dbh->prepare("SELECT categorycode, description FROM categories ORDER BY description");
151 $req->execute;
152 return $req->fetchall_arrayref({});
154 sub catcodes_hash() {
155 my %cathash;
156 my $catcodes = &catcode_aref;
157 foreach (@$catcodes) {
158 $cathash{$_->{categorycode}} = ($_->{description} || 'NO_DESCRIPTION') . " ($_->{categorycode})";
160 return %cathash;
163 sub calculate {
164 my ($line, $column, $digits, $status, $activity, $filters) = @_;
165 my @mainloop;
166 my @loopfooter;
167 my @loopcol;
168 my @loopline;
169 my @looprow;
170 my %globalline;
171 my $grantotal =0;
172 # extract parameters
173 my $dbh = C4::Context->dbh;
175 # Filters
176 my $linefilter = "";
177 # warn "filtres ".@filters[0];
178 # warn "filtres ".@filters[4];
179 # warn "filtres ".@filters[5];
180 # warn "filtres ".@filters[6];
182 $linefilter = @$filters[0] if ($line =~ /categorycode/ ) ;
183 $linefilter = @$filters[1] if ($line =~ /zipcode/ ) ;
184 $linefilter = @$filters[2] if ($line =~ /branchcode/ ) ;
185 $linefilter = @$filters[5] if ($line =~ /sex/);
186 $linefilter = @$filters[6] if ($line =~ /sort1/ ) ;
187 $linefilter = @$filters[7] if ($line =~ /sort2/ ) ;
189 my $colfilter = "";
190 $colfilter = @$filters[0] if ($column =~ /categorycode/);
191 $colfilter = @$filters[1] if ($column =~ /zipcode/);
192 $colfilter = @$filters[2] if ($column =~ /branchcode/);
193 $colfilter = @$filters[5] if ($column =~ /sex/);
194 $colfilter = @$filters[6] if ($column =~ /sort1/);
195 $colfilter = @$filters[7] if ($column =~ /sort2/);
197 my @loopfilter;
198 for (my $i=0;$i<=7;$i++) {
199 my %cell;
200 if ( @$filters[$i] ) {
201 if($i == 3 or $i == 4){
202 $cell{filter} .= format_date(@$filters[$i]);
203 }else{
204 $cell{filter} .= @$filters[$i];
207 $cell{crit} .="Cat Code " if ($i==0);
208 $cell{crit} .="Zip Code" if ($i==1);
209 $cell{crit} .="Branchcode" if ($i==2);
210 $cell{crit} .="Date of Birth" if ($i==3);
211 $cell{crit} .="Date of Birth" if ($i==4);
212 $cell{crit} .="Sex" if ($i==5);
213 $cell{crit} .="Sort1" if ($i==6);
214 $cell{crit} .="Sort2" if ($i==7);
215 push @loopfilter, \%cell;
218 ($status ) and push @loopfilter,{crit=>"Status", filter=>$status };
219 ($activity) and push @loopfilter,{crit=>"Activity",filter=>$activity};
220 push @loopfilter,{debug=>1, crit=>"Branches",filter=>join(" ", sort keys %$branches)};
221 push @loopfilter,{debug=>1, crit=>"(line, column)", filter=>"($line,$column)"};
222 # year of activity
223 my ( $period_year, $period_month, $period_day )=Add_Delta_YM( Today(),-$period, 0);
224 my $newperioddate=$period_year."-".$period_month."-".$period_day;
225 # 1st, loop rows.
226 my $linefield;
227 if (($line =~/zipcode/) and ($digits)) {
228 $linefield .="left($line,$digits)";
229 } else{
230 $linefield .= $line;
233 my %cathash = ($line eq 'categorycode' or $column eq 'categorycode') ? &catcodes_hash : ();
234 push @loopfilter, {debug=>1, crit=>"\%cathash", filter=>join(", ", map {$cathash{$_}} sort keys %cathash)};
236 my $strsth = "SELECT distinctrow $linefield FROM borrowers WHERE $line IS NOT NULL ";
237 $linefilter =~ s/\*/%/g;
238 if ( $linefilter ) {
239 $strsth .= " AND $linefield LIKE ? " ;
241 $strsth .= " AND $status='1' " if ($status);
242 $strsth .=" order by $linefield";
244 push @loopfilter, {sql=>1, crit=>"Query", filter=>$strsth};
245 my $sth = $dbh->prepare($strsth);
246 if ( $linefilter ) {
247 $sth->execute($linefilter);
248 } else {
249 $sth->execute;
251 while (my ($celvalue) = $sth->fetchrow) {
252 my %cell;
253 if ($celvalue) {
254 $cell{rowtitle} = $celvalue;
255 # $cell{rowtitle_display} = ($linefield eq 'branchcode') ? $branches->{$celvalue}->{branchname} : $celvalue;
256 $cell{rowtitle_display} = ($cathash{$celvalue} || "$celvalue\*") if ($line eq 'categorycode');
258 $cell{totalrow} = 0;
259 push @loopline, \%cell;
262 # 2nd, loop cols.
263 my $colfield;
264 if (($column =~/zipcode/) and ($digits)) {
265 $colfield = "left($column,$digits)";
266 } else {
267 $colfield = $column;
269 my $strsth2 = "select distinctrow $colfield from borrowers where $column is not null";
270 if ($colfilter) {
271 $colfilter =~ s/\*/%/g;
272 $strsth2 .= " AND $colfield LIKE ? ";
274 $strsth2 .= " AND $status='1' " if ($status);
275 $strsth2 .= " order by $colfield";
276 push @loopfilter, {sql=>1, crit=>"Query", filter=>$strsth2};
277 my $sth2 = $dbh->prepare($strsth2);
278 if ($colfilter) {
279 $sth2->execute($colfilter);
280 } else {
281 $sth2->execute;
283 while (my ($celvalue) = $sth2->fetchrow) {
284 my %cell;
285 if ($celvalue) {
286 $cell{coltitle} = $celvalue;
287 # $cell{coltitle_display} = ($colfield eq 'branchcode') ? $branches->{$celvalue}->{branchname} : $celvalue;
288 $cell{coltitle_display} = $cathash{$celvalue} if ($column eq 'categorycode');
290 push @loopcol, \%cell;
293 my $i=0;
294 #Initialization of cell values.....
295 my %table;
296 # warn "init table";
297 foreach my $row (@loopline) {
298 foreach my $col ( @loopcol ) {
299 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
300 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
302 $table{$row->{rowtitle}}->{totalrow}=0;
303 $table{$row->{rowtitle}}->{rowtitle_display} = $row->{rowtitle_display};
306 # preparing calculation
307 my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM borrowers WHERE 1 ";
308 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
309 $strcalc .= " AND categorycode like '" . @$filters[0] ."'" if ( @$filters[0] );
310 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
311 $strcalc .= " AND zipcode like '" . @$filters[1] ."'" if ( @$filters[1] );
312 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
313 $strcalc .= " AND branchcode like '" . @$filters[2] ."'" if ( @$filters[2] );
314 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
315 $strcalc .= " AND dateofbirth > '" . @$filters[3] ."'" if ( @$filters[3] );
316 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
317 $strcalc .= " AND dateofbirth < '" . @$filters[4] ."'" if ( @$filters[4] );
318 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
319 $strcalc .= " AND sex like '" . @$filters[5] ."'" if ( @$filters[5] );
320 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
321 $strcalc .= " AND sort1 like '" . @$filters[6] ."'" if ( @$filters[6] );
322 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
323 $strcalc .= " AND sort2 like '" . @$filters[7] ."'" if ( @$filters[7] );
324 $strcalc .= " AND borrowernumber in (select distinct(borrowernumber) from old_issues where issuedate > '" . $newperioddate . "')" if ($activity eq 'active');
325 $strcalc .= " AND borrowernumber not in (select distinct(borrowernumber) from old_issues where issuedate > '" . $newperioddate . "')" if ($activity eq 'nonactive');
326 $strcalc .= " AND $status='1' " if ($status);
327 $strcalc .= " group by $linefield, $colfield";
328 push @loopfilter, {sql=>1, crit=>"Query", filter=>$strcalc};
329 my $dbcalc = $dbh->prepare($strcalc);
330 $dbcalc->execute;
332 my $emptycol;
333 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
334 # warn "filling table $row / $col / $value ";
335 $emptycol = 1 if (!defined($col));
336 $col = "zzEMPTY" if (!defined($col));
337 $row = "zzEMPTY" if (!defined($row));
339 $table{$row}->{$col}+=$value;
340 $table{$row}->{totalrow}+=$value;
341 $grantotal += $value;
344 push @loopcol,{coltitle => "NULL"} if ($emptycol);
346 foreach my $row (sort keys %table) {
347 my @loopcell;
348 #@loopcol ensures the order for columns is common with column titles
349 # and the number matches the number of columns
350 foreach my $col ( @loopcol ) {
351 my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
352 push @loopcell, {value => $value};
354 push @looprow,{
355 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
356 'rowtitle_display' => $table{$row}->{rowtitle_display} || $row,
357 'loopcell' => \@loopcell,
358 'totalrow' => $table{$row}->{totalrow}
362 foreach my $col ( @loopcol ) {
363 my $total=0;
364 foreach my $row ( @looprow ) {
365 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
366 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
368 # warn "summ for column ".$col->{coltitle}." = ".$total;
369 push @loopfooter, {'totalcol' => $total};
373 # the header of the table
374 $globalline{loopfilter}=\@loopfilter;
375 # the core of the table
376 $globalline{looprow} = \@looprow;
377 $globalline{loopcol} = \@loopcol;
378 # # the foot (totals by borrower type)
379 $globalline{loopfooter} = \@loopfooter;
380 $globalline{total}= $grantotal;
381 $globalline{line} = $line;
382 $globalline{column} = $column;
383 push @mainloop,\%globalline;
384 return \@mainloop;