5 # Copyright 2000-2002 Katipo Communications
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA 02111-1307 USA
26 use C4::Branch; # GetBranches
35 plugin that shows a stats on borrowers
45 my $do_it=$input->param('do_it');
46 my $fullreportname = "reports/borrowers_stats.tmpl";
47 my $line = $input->param("Line");
48 my $column = $input->param("Column");
49 my @filters = $input->param("Filter");
50 my $digits = $input->param("digits");
51 my $borstat = $input->param("status");
52 my $output = $input->param("output");
53 my $basename = $input->param("basename");
54 my $mime = $input->param("MIME");
55 my $del = $input->param("sep");
57 my ($template, $borrowernumber, $cookie)
58 = get_template_and_user({template_name => $fullreportname,
62 flagsrequired => {reports=> 1},
65 $template->param(do_it => $do_it);
67 my $results = calculate($line, $column, $digits, $borstat, \@filters);
68 if ($output eq "screen"){
69 $template->param(mainloop => $results);
70 output_html_with_http_headers $input, $cookie, $template->output;
73 print $input->header(-type => 'application/vnd.sun.xml.calc',
75 -name=>"$basename.csv",
76 -attachment=>"$basename.csv");
77 my $cols = @$results[0]->{loopcol};
78 my $lines = @$results[0]->{looprow};
80 $sep =C4::Context->preference("delimiter");
81 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
82 foreach my $col ( @$cols ) {
83 print $col->{coltitle}.$sep;
86 foreach my $line ( @$lines ) {
87 my $x = $line->{loopcell};
88 print $line->{rowtitle}.$sep;
89 foreach my $cell (@$x) {
90 print $cell->{value}.$sep;
92 print $line->{totalrow};
96 $cols = @$results[0]->{loopfooter};
97 foreach my $col ( @$cols ) {
98 print $sep.$col->{totalcol};
100 print $sep.@$results[0]->{total};
104 my $dbh = C4::Context->dbh;
108 $req = $dbh->prepare( "select categorycode, description from categories order by description");
112 push @select_catcode,"";
113 $select_catcode{""} ="";
114 while (my ($catcode, $description) =$req->fetchrow) {
115 push @select_catcode, $catcode;
116 $select_catcode{$catcode} = $description;
118 my $CGICatCode=CGI::scrolling_list( -name => 'Filter',
120 -values => \@select_catcode,
121 -labels => \%select_catcode,
126 my $branches = GetBranches;
128 foreach my $thisbranch (keys %$branches) {
129 # my $selected = 1 if $thisbranch eq $branch;
130 my %row =(value => $thisbranch,
131 # selected => $selected,
132 branchname => $branches->{$thisbranch}->{'branchname'},
134 push @branchloop, \%row;
136 $req = $dbh->prepare( "select distinctrow zipcode from borrowers order by zipcode");
139 push @select_zipcode,"";
140 while (my ($value) =$req->fetchrow) {
142 push @select_zipcode, $value;
146 my $CGIZipCode=CGI::scrolling_list( -name => 'Filter',
148 -values => \@select_zipcode,
152 $req = $dbh->prepare( "SELECT authorised_value,lib FROM authorised_values WHERE category='Bsort1' order by lib");
156 push @select_sort1,"";
157 $select_sort1{""}="";
159 while (my ($auth_value,$lib) =$req->fetchrow) {
162 push @select_sort1, $auth_value;
163 $select_sort1{$auth_value}=$lib
167 my $CGIsort1=CGI::scrolling_list( -name => 'Filter',
169 -values => \@select_sort1,
170 -labels =>\%select_sort1,
174 $req = $dbh->prepare( "select distinctrow sort2 from borrowers order by sort2");
177 push @select_sort2,"";
179 while (my ($value) =$req->fetchrow) {
182 push @select_sort2, $value;
185 my $CGIsort2=CGI::scrolling_list( -name => 'Filter',
187 -values => \@select_sort2,
191 my @mime = ( C4::Context->preference("MIME") );
192 foreach my $mime (@mime){
196 my $CGIextChoice=CGI::scrolling_list(
203 my @dels = ( C4::Context->preference("delimiter") );
204 my $CGIsepChoice=CGI::scrolling_list(
211 $template->param( CGICatCode => $CGICatCode,
212 CGIZipCode => $CGIZipCode,
213 CGISort1 => $CGIsort1,
214 hassort1 => $hassort1,
215 CGISort2 => $CGIsort2,
216 hassort2 => $hassort2,
217 CGIextChoice => $CGIextChoice,
218 CGIsepChoice => $CGIsepChoice,
219 CGIBranch => @branchloop
223 output_html_with_http_headers $input, $cookie, $template->output;
228 my ($line, $column, $digits, $status, $filters) = @_;
237 my $dbh = C4::Context->dbh;
241 # warn "filtres ".@filters[0];
242 # warn "filtres ".@filters[1];
243 # warn "filtres ".@filters[2];
244 # warn "filtres ".@filters[3];
246 $linefilter = @$filters[0] if ($line =~ /categorycode/ ) ;
247 $linefilter = @$filters[1] if ($line =~ /zipcode/ ) ;
248 $linefilter = @$filters[2] if ($line =~ /branccode/ ) ;
249 $linefilter = @$filters[3] if ($line =~ /sort1/ ) ;
250 $linefilter = @$filters[4] if ($line =~ /sort2/ ) ;
253 $colfilter = @$filters[0] if ($column =~ /categorycode/);
254 $colfilter = @$filters[1] if ($column =~ /zipcode/);
255 $colfilter = @$filters[2] if ($column =~ /branchcode/);
256 $colfilter = @$filters[3] if ($column =~ /sort1/);
257 $colfilter = @$filters[4] if ($column =~ /sort2/);
260 for (my $i=0;$i<=3;$i++) {
262 if ( @$filters[$i] ) {
263 $cell{filter} .= @$filters[$i];
264 $cell{crit} .="Cat Code " if ($i==0);
265 $cell{crit} .="Zip Code" if ($i==1);
266 $cell{crit} .="Branchcode" if ($i==2);
267 $cell{crit} .="Sort1" if ($i==3);
268 $cell{crit} .="Sort2" if ($i==4);
269 push @loopfilter, \%cell;
273 push @loopfilter,{crit=>"Status",filter=>$status}
277 if (($line =~/zipcode/) and ($digits)) {
278 $linefield .="left($line,$digits)";
284 $strsth .= "select distinctrow $linefield from borrowers where $line is not null ";
285 $linefilter =~ s/\*/%/g;
287 $strsth .= " and $linefield LIKE ? " ;
289 $strsth .= " and $status='1' " if ($status);
290 $strsth .=" order by $linefield";
293 my $sth = $dbh->prepare( $strsth );
295 $sth->execute($linefilter);
299 while ( my ($celvalue) = $sth->fetchrow) {
302 $cell{rowtitle} = $celvalue;
304 # $cell{rowtitle} = "";
307 push @loopline, \%cell;
312 if (($column =~/zipcode/) and ($digits)) {
313 $colfield .= "left($column,$digits)";
315 $colfield .= $column;
318 $colfilter =~ s/\*/%/g;
319 $strsth2 .= "select distinctrow $colfield from borrowers where $column is not null";
321 $strsth2 .= " and $colfield LIKE ? ";
323 $strsth2 .= " and $status='1' " if ($status);
324 $strsth2 .= " order by $colfield";
326 my $sth2 = $dbh->prepare( $strsth2 );
328 $sth2->execute($colfilter);
332 while (my ($celvalue) = $sth2->fetchrow) {
336 $cell{coltitle} = $celvalue;
338 push @loopcol, \%cell;
346 #Initialization of cell values.....
349 foreach my $row ( @loopline ) {
350 foreach my $col ( @loopcol ) {
351 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
352 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
354 $table{$row->{rowtitle}}->{totalrow}=0;
357 # preparing calculation
358 my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM borrowers WHERE 1 ";
359 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
360 $strcalc .= " AND categorycode like '" . @$filters[0] ."'" if ( @$filters[0] );
361 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
362 $strcalc .= " AND zipcode like '" . @$filters[1] ."'" if ( @$filters[1] );
363 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
364 $strcalc .= " AND sort1 like '" . @$filters[2] ."'" if ( @$filters[2] );
365 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
366 $strcalc .= " AND sort2 like '" . @$filters[3] ."'" if ( @$filters[3] );
367 $strcalc .= " AND $status='1' " if ($status);
368 $strcalc .= " group by $linefield, $colfield";
370 my $dbcalc = $dbh->prepare($strcalc);
372 # warn "filling table";
375 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
376 # warn "filling table $row / $col / $value ";
377 $emptycol = 1 if ($col eq undef);
378 $col = "zzEMPTY" if ($col eq undef);
379 $row = "zzEMPTY" if ($row eq undef);
381 $table{$row}->{$col}+=$value;
382 $table{$row}->{totalrow}+=$value;
383 $grantotal += $value;
386 push @loopcol,{coltitle => "NULL"} if ($emptycol);
388 foreach my $row ( sort keys %table ) {
390 #@loopcol ensures the order for columns is common with column titles
391 # and the number matches the number of columns
392 foreach my $col ( @loopcol ) {
393 my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
394 push @loopcell, {value => $value } ;
396 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
397 'loopcell' => \@loopcell,
398 'hilighted' => ($hilighted >0),
399 'totalrow' => $table{$row}->{totalrow}
401 $hilighted = -$hilighted;
404 foreach my $col ( @loopcol ) {
406 foreach my $row ( @looprow ) {
407 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
408 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
410 # warn "summ for column ".$col->{coltitle}." = ".$total;
411 push @loopfooter, {'totalcol' => $total};
415 # the header of the table
416 $globalline{loopfilter}=\@loopfilter;
417 # the core of the table
418 $globalline{looprow} = \@looprow;
419 $globalline{loopcol} = \@loopcol;
420 # # the foot (totals by borrower type)
421 $globalline{loopfooter} = \@loopfooter;
422 $globalline{total}= $grantotal;
423 $globalline{line} = $line;
424 $globalline{column} = $column;
425 push @mainloop,\%globalline;