Bug 2662: make rand password consider minPasswordLength pref
[koha.git] / reports / borrowers_stats.pl
blob9887dfae8d89864fafa2d098dafa3957162a1344
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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
20 use strict;
21 use CGI;
22 use C4::Auth;
23 use C4::Context;
24 use C4::Branch; # GetBranches
25 use C4::Koha;
26 use C4::Dates;
27 use C4::Acquisition;
28 use C4::Output;
29 use C4::Reports;
30 use C4::Circulation;
31 use Date::Calc qw(
32 Today
33 Add_Delta_YM
36 =head1 NAME
38 plugin that shows a stats on borrowers
40 =head1 DESCRIPTION
42 =over 2
44 =cut
46 my $input = new CGI;
47 my $do_it=$input->param('do_it');
48 my $fullreportname = "reports/borrowers_stats.tmpl";
49 my $line = $input->param("Line");
50 my $column = $input->param("Column");
51 my @filters = $input->param("Filter");
52 my $digits = $input->param("digits");
53 my $period = $input->param("period");
54 my $borstat = $input->param("status");
55 my $borstat1 = $input->param("activity");
56 my $output = $input->param("output");
57 my $basename = $input->param("basename");
58 my $mime = $input->param("MIME");
59 our $sep = $input->param("sep");
60 $sep = "\t" if ($sep eq 'tabulation');
61 my $selected_branch; # = $input->param("?");
63 our $branches = GetBranches;
65 my ($template, $borrowernumber, $cookie)
66 = get_template_and_user({template_name => $fullreportname,
67 query => $input,
68 type => "intranet",
69 authnotrequired => 0,
70 flagsrequired => {reports => '*'},
71 debug => 1,
72 });
73 $template->param(do_it => $do_it);
74 if ($do_it) {
75 my $results = calculate($line, $column, $digits, $borstat,$borstat1 ,\@filters);
76 if ($output eq "screen"){
77 $template->param(mainloop => $results);
78 output_html_with_http_headers $input, $cookie, $template->output;
79 } else {
80 print $input->header(-type => 'application/vnd.sun.xml.calc',
81 -encoding => 'utf-8',
82 -name => "$basename.csv",
83 -attachment => "$basename.csv");
84 my $cols = @$results[0]->{loopcol};
85 my $lines = @$results[0]->{looprow};
86 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
87 foreach my $col ( @$cols ) {
88 print $col->{coltitle}.$sep;
90 print "Total\n";
91 foreach my $line ( @$lines ) {
92 my $x = $line->{loopcell};
93 print $line->{rowtitle}.$sep;
94 foreach my $cell (@$x) {
95 print $cell->{value}.$sep;
97 print $line->{totalrow};
98 print "\n";
100 print "TOTAL";
101 $cols = @$results[0]->{loopfooter};
102 foreach my $col ( @$cols ) {
103 print $sep.$col->{totalcol};
105 print $sep.@$results[0]->{total};
107 exit(1); # exit after do_it, regardless
108 } else {
109 my $dbh = C4::Context->dbh;
110 my $req;
111 $template->param( CAT_LOOP => &catcode_aref);
112 my @branchloop;
113 foreach (sort {$branches->{$a}->{branchname} cmp $branches->{$b}->{branchname}} keys %$branches) {
114 my $line = {branchcode => $_, branchname => $branches->{$_}->{branchname} || 'UNKNOWN'};
115 $line->{selected} = 'selected' if ($selected_branch and $selected_branch eq $_);
116 push @branchloop, $line;
118 $template->param(BRANCH_LOOP => \@branchloop);
119 $req = $dbh->prepare("SELECT DISTINCTROW zipcode FROM borrowers WHERE zipcode IS NOT NULL AND zipcode <> '' ORDER BY zipcode");
120 $req->execute;
121 $template->param( ZIP_LOOP => $req->fetchall_arrayref({}));
122 $req = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category='Bsort1' ORDER BY lib");
123 $req->execute;
124 $template->param( SORT1_LOOP => $req->fetchall_arrayref({}));
125 $req = $dbh->prepare("SELECT DISTINCTROW sort2 AS value FROM borrowers WHERE sort2 IS NOT NULL AND sort2 <> '' ORDER BY sort2 LIMIT 200");
126 # More than 200 items in a dropdown is not going to be useful anyway, and w/ 50,000 patrons we can destory DB performance.
127 $req->execute;
128 $template->param( SORT2_LOOP => $req->fetchall_arrayref({}));
130 my @mime = ( C4::Context->preference("MIME") );
131 # warn 'MIME(s): ' . join ' ', @mime;
132 my $CGIextChoice=CGI::scrolling_list(
133 -name => 'MIME',
134 -id => 'MIME',
135 -values => \@mime,
136 -size => 1,
137 -multiple => 0 );
138 my $CGIsepChoice=GetDelimiterChoices;
139 $template->param(
140 CGIextChoice => $CGIextChoice,
141 CGIsepChoice => $CGIsepChoice,
142 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
146 output_html_with_http_headers $input, $cookie, $template->output;
148 sub catcode_aref() {
149 my $req = C4::Context->dbh->prepare("SELECT categorycode, description FROM categories ORDER BY description");
150 $req->execute;
151 return $req->fetchall_arrayref({});
153 sub catcodes_hash() {
154 my %cathash;
155 my $catcodes = &catcode_aref;
156 foreach (@$catcodes) {
157 $cathash{$_->{categorycode}} = ($_->{description} || 'NO_DESCRIPTION') . " ($_->{categorycode})";
159 return %cathash;
162 sub calculate {
163 my ($line, $column, $digits, $status, $activity, $filters) = @_;
164 my @mainloop;
165 my @loopfooter;
166 my @loopcol;
167 my @loopline;
168 my @looprow;
169 my %globalline;
170 my $grantotal =0;
171 # extract parameters
172 my $dbh = C4::Context->dbh;
174 # Filters
175 my $linefilter = "";
176 # warn "filtres ".@filters[0];
177 # warn "filtres ".@filters[4];
178 # warn "filtres ".@filters[5];
179 # warn "filtres ".@filters[6];
181 $linefilter = @$filters[0] if ($line =~ /categorycode/ ) ;
182 $linefilter = @$filters[1] if ($line =~ /zipcode/ ) ;
183 $linefilter = @$filters[2] if ($line =~ /branchcode/ ) ;
184 $linefilter = @$filters[5] if ($line =~ /sex/);
185 $linefilter = @$filters[6] if ($line =~ /sort1/ ) ;
186 $linefilter = @$filters[7] if ($line =~ /sort2/ ) ;
188 my $colfilter = "";
189 $colfilter = @$filters[0] if ($column =~ /categorycode/);
190 $colfilter = @$filters[1] if ($column =~ /zipcode/);
191 $colfilter = @$filters[2] if ($column =~ /branchcode/);
192 $colfilter = @$filters[5] if ($column =~ /sex/);
193 $colfilter = @$filters[6] if ($column =~ /sort1/);
194 $colfilter = @$filters[7] if ($column =~ /sort2/);
196 my @loopfilter;
197 for (my $i=0;$i<=7;$i++) {
198 my %cell;
199 if ( @$filters[$i] ) {
200 $cell{filter} .= @$filters[$i];
201 $cell{crit} .="Cat Code " if ($i==0);
202 $cell{crit} .="Zip Code" if ($i==1);
203 $cell{crit} .="Branchcode" if ($i==2);
204 $cell{crit} .="Date of Birth" if ($i==3);
205 $cell{crit} .="Date of Birth" if ($i==4);
206 $cell{crit} .="Sex" if ($i==5);
207 $cell{crit} .="Sort1" if ($i==6);
208 $cell{crit} .="Sort2" if ($i==7);
209 push @loopfilter, \%cell;
212 ($status ) and push @loopfilter,{crit=>"Status", filter=>$status };
213 ($activity) and push @loopfilter,{crit=>"Activity",filter=>$activity};
214 push @loopfilter,{debug=>1, crit=>"Branches",filter=>join(" ", sort keys %$branches)};
215 push @loopfilter,{debug=>1, crit=>"(line, column)", filter=>"($line,$column)"};
216 # year of activity
217 my ( $period_year, $period_month, $period_day )=Add_Delta_YM( Today(),-$period, 0);
218 my $newperioddate=$period_year."-".$period_month."-".$period_day;
219 # 1st, loop rows.
220 my $linefield;
221 if (($line =~/zipcode/) and ($digits)) {
222 $linefield .="left($line,$digits)";
223 } else{
224 $linefield .= $line;
227 my %cathash = ($line eq 'categorycode' or $column eq 'categorycode') ? &catcodes_hash : ();
228 push @loopfilter, {debug=>1, crit=>"\%cathash", filter=>join(", ", map {$cathash{$_}} sort keys %cathash)};
230 my $strsth = "SELECT distinctrow $linefield FROM borrowers WHERE $line IS NOT NULL ";
231 $linefilter =~ s/\*/%/g;
232 if ( $linefilter ) {
233 $strsth .= " AND $linefield LIKE ? " ;
235 $strsth .= " AND $status='1' " if ($status);
236 $strsth .=" order by $linefield";
238 push @loopfilter, {sql=>1, crit=>"Query", filter=>$strsth};
239 my $sth = $dbh->prepare($strsth);
240 if ( $linefilter ) {
241 $sth->execute($linefilter);
242 } else {
243 $sth->execute;
245 while (my ($celvalue) = $sth->fetchrow) {
246 my %cell;
247 if ($celvalue) {
248 $cell{rowtitle} = $celvalue;
249 # $cell{rowtitle_display} = ($linefield eq 'branchcode') ? $branches->{$celvalue}->{branchname} : $celvalue;
250 $cell{rowtitle_display} = ($cathash{$celvalue} || "$celvalue\*") if ($line eq 'categorycode');
252 $cell{totalrow} = 0;
253 push @loopline, \%cell;
256 # 2nd, loop cols.
257 my $colfield;
258 if (($column =~/zipcode/) and ($digits)) {
259 $colfield = "left($column,$digits)";
260 } else {
261 $colfield = $column;
263 my $strsth2 = "select distinctrow $colfield from borrowers where $column is not null";
264 if ($colfilter) {
265 $colfilter =~ s/\*/%/g;
266 $strsth2 .= " AND $colfield LIKE ? ";
268 $strsth2 .= " AND $status='1' " if ($status);
269 $strsth2 .= " order by $colfield";
270 push @loopfilter, {sql=>1, crit=>"Query", filter=>$strsth2};
271 my $sth2 = $dbh->prepare($strsth2);
272 if ($colfilter) {
273 $sth2->execute($colfilter);
274 } else {
275 $sth2->execute;
277 while (my ($celvalue) = $sth2->fetchrow) {
278 my %cell;
279 if ($celvalue) {
280 $cell{coltitle} = $celvalue;
281 # $cell{coltitle_display} = ($colfield eq 'branchcode') ? $branches->{$celvalue}->{branchname} : $celvalue;
282 $cell{coltitle_display} = $cathash{$celvalue} if ($column eq 'categorycode');
284 push @loopcol, \%cell;
287 my $i=0;
288 #Initialization of cell values.....
289 my %table;
290 # warn "init table";
291 foreach my $row (@loopline) {
292 foreach my $col ( @loopcol ) {
293 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
294 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
296 $table{$row->{rowtitle}}->{totalrow}=0;
297 $table{$row->{rowtitle}}->{rowtitle_display} = $row->{rowtitle_display};
300 # preparing calculation
301 my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM borrowers WHERE 1 ";
302 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
303 $strcalc .= " AND categorycode like '" . @$filters[0] ."'" if ( @$filters[0] );
304 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
305 $strcalc .= " AND zipcode like '" . @$filters[1] ."'" if ( @$filters[1] );
306 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
307 $strcalc .= " AND branchcode like '" . @$filters[2] ."'" if ( @$filters[2] );
308 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
309 $strcalc .= " AND dateofbirth > '" . @$filters[3] ."'" if ( @$filters[3] );
310 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
311 $strcalc .= " AND dateofbirth < '" . @$filters[4] ."'" if ( @$filters[4] );
312 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
313 $strcalc .= " AND sex like '" . @$filters[5] ."'" if ( @$filters[5] );
314 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
315 $strcalc .= " AND sort1 like '" . @$filters[6] ."'" if ( @$filters[6] );
316 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
317 $strcalc .= " AND sort2 like '" . @$filters[7] ."'" if ( @$filters[7] );
318 $strcalc .= " AND borrowernumber in (select distinct(borrowernumber) from old_issues where issuedate > '" . $newperioddate . "')" if ($activity eq 'active');
319 $strcalc .= " AND borrowernumber not in (select distinct(borrowernumber) from old_issues where issuedate > '" . $newperioddate . "')" if ($activity eq 'nonactive');
320 $strcalc .= " AND $status='1' " if ($status);
321 $strcalc .= " group by $linefield, $colfield";
322 push @loopfilter, {sql=>1, crit=>"Query", filter=>$strcalc};
323 my $dbcalc = $dbh->prepare($strcalc);
324 $dbcalc->execute;
326 my $emptycol;
327 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
328 # warn "filling table $row / $col / $value ";
329 $emptycol = 1 if (!defined($col));
330 $col = "zzEMPTY" if (!defined($col));
331 $row = "zzEMPTY" if (!defined($row));
333 $table{$row}->{$col}+=$value;
334 $table{$row}->{totalrow}+=$value;
335 $grantotal += $value;
338 push @loopcol,{coltitle => "NULL"} if ($emptycol);
340 foreach my $row (sort keys %table) {
341 my @loopcell;
342 #@loopcol ensures the order for columns is common with column titles
343 # and the number matches the number of columns
344 foreach my $col ( @loopcol ) {
345 my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
346 push @loopcell, {value => $value};
348 push @looprow,{
349 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
350 'rowtitle_display' => $table{$row}->{rowtitle_display} || $row,
351 'loopcell' => \@loopcell,
352 'totalrow' => $table{$row}->{totalrow}
356 foreach my $col ( @loopcol ) {
357 my $total=0;
358 foreach my $row ( @looprow ) {
359 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
360 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
362 # warn "summ for column ".$col->{coltitle}." = ".$total;
363 push @loopfooter, {'totalcol' => $total};
367 # the header of the table
368 $globalline{loopfilter}=\@loopfilter;
369 # the core of the table
370 $globalline{looprow} = \@looprow;
371 $globalline{loopcol} = \@loopcol;
372 # # the foot (totals by borrower type)
373 $globalline{loopfooter} = \@loopfooter;
374 $globalline{total}= $grantotal;
375 $globalline{line} = $line;
376 $globalline{column} = $column;
377 push @mainloop,\%globalline;
378 return \@mainloop;