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
28 use C4::Interface::CGI::Output;
29 use C4::Circulation::Circ2;
33 plugin that shows a stats on borrowers
43 my $do_it=$input->param('do_it');
44 my $fullreportname = "reports/borrowers_stats.tmpl";
45 my $line = $input->param("Line");
46 my $column = $input->param("Column");
47 my @filters = $input->param("Filter");
48 my $digits = $input->param("digits");
49 my $borstat = $input->param("status");
50 my $output = $input->param("output");
51 my $basename = $input->param("basename");
52 my $mime = $input->param("MIME");
53 my $del = $input->param("sep");
55 my ($template, $borrowernumber, $cookie)
56 = get_template_and_user({template_name => $fullreportname,
60 flagsrequired => {editcatalogue => 1},
63 $template->param(do_it => $do_it);
65 my $results = calculate($line, $column, $digits, $borstat, \@filters);
66 if ($output eq "screen"){
67 $template->param(mainloop => $results);
68 output_html_with_http_headers $input, $cookie, $template->output;
71 print $input->header(-type => 'application/vnd.sun.xml.calc',
72 -name=>"$basename.csv",
73 -attachment=>"$basename.csv");
74 my $cols = @$results[0]->{loopcol};
75 my $lines = @$results[0]->{looprow};
77 $sep =C4::Context->preference("delimiter");
78 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
79 foreach my $col ( @$cols ) {
80 print $col->{coltitle}.$sep;
83 foreach my $line ( @$lines ) {
84 my $x = $line->{loopcell};
85 print $line->{rowtitle}.$sep;
86 foreach my $cell (@$x) {
87 print $cell->{value}.$sep;
89 print $line->{totalrow};
93 $cols = @$results[0]->{loopfooter};
94 foreach my $col ( @$cols ) {
95 print $sep.$col->{totalcol};
97 print $sep.@$results[0]->{total};
101 my $dbh = C4::Context->dbh;
105 $req = $dbh->prepare( "select categorycode, description from categories order by description");
109 push @select_catcode,"";
110 $select_catcode{""} = "";
111 while (my ($catcode, $description) =$req->fetchrow) {
112 push @select_catcode, $catcode;
113 $select_catcode{$catcode} = $description
115 my $CGICatCode=CGI::scrolling_list( -name => 'Filter',
117 -values => \@select_catcode,
118 -labels => \%select_catcode,
122 $req = $dbh->prepare( "select distinctrow sort1 from borrowers order by sort1");
125 push @select_sort1,"";
127 while (my ($value) =$req->fetchrow) {
130 push @select_sort1, $value;
133 my $CGIsort1=CGI::scrolling_list( -name => 'Filter',
135 -values => \@select_sort1,
139 $req = $dbh->prepare( "select distinctrow sort2 from borrowers order by sort2");
142 push @select_sort2,"";
144 while (my ($value) =$req->fetchrow) {
147 push @select_sort2, $value;
150 my $CGIsort2=CGI::scrolling_list( -name => 'Filter',
152 -values => \@select_sort2,
156 my @mime = ( C4::Context->preference("MIME") );
157 foreach my $mime (@mime){
161 my $CGIextChoice=CGI::scrolling_list(
168 my @dels = ( C4::Context->preference("delimiter") );
169 my $CGIsepChoice=CGI::scrolling_list(
175 $template->param(CGICatCode => $CGICatCode,
176 CGISort1 => $CGIsort1,
177 hassort1 => $hassort1,
178 CGISort2 => $CGIsort2,
179 hassort2 => $hassort2,
180 CGIextChoice => $CGIextChoice,
181 CGIsepChoice => $CGIsepChoice
185 output_html_with_http_headers $input, $cookie, $template->output;
190 my ($line, $column, $digits, $status, $filters) = @_;
199 my $dbh = C4::Context->dbh;
203 # warn "filtres ".@filters[0];
204 # warn "filtres ".@filters[1];
205 # warn "filtres ".@filters[2];
206 # warn "filtres ".@filters[3];
208 $linefilter = @$filters[0] if ($line =~ /categorycode/ ) ;
209 $linefilter = @$filters[1] if ($line =~ /zipcode/ ) ;
210 $linefilter = @$filters[2] if ($line =~ /sort1/ ) ;
211 $linefilter = @$filters[3] if ($line =~ /sort2/ ) ;
214 $colfilter = @$filters[0] if ($column =~ /categorycode/);
215 $colfilter = @$filters[1] if ($column =~ /zipcode/);
216 $colfilter = @$filters[2] if ($column =~ /sort1/);
217 $colfilter = @$filters[3] if ($column =~ /sort2/);
220 for (my $i=0;$i<=3;$i++) {
222 if ( @$filters[$i] ) {
223 $cell{filter} .= @$filters[$i];
224 $cell{crit} .="Cat Code " if ($i==0);
225 $cell{crit} .="Zip Code" if ($i==1);
226 $cell{crit} .="Sort1" if ($i==2);
227 $cell{crit} .="Sort2" if ($i==3);
228 push @loopfilter, \%cell;
232 push @loopfilter,{crit=>"Status",filter=>$status}
236 if (($line =~/zipcode/) and ($digits)) {
237 $linefield .="left($line,$digits)";
243 $strsth .= "select distinctrow $linefield from borrowers where $line is not null ";
244 $linefilter =~ s/\*/%/g;
246 $strsth .= " and $linefield LIKE ? " ;
248 $strsth .= " and $status='1' " if ($status);
249 $strsth .=" order by $linefield";
252 my $sth = $dbh->prepare( $strsth );
254 $sth->execute($linefilter);
258 while ( my ($celvalue) = $sth->fetchrow) {
261 $cell{rowtitle} = $celvalue;
263 # $cell{rowtitle} = "";
266 push @loopline, \%cell;
271 if (($column =~/zipcode/) and ($digits)) {
272 $colfield .= "left($column,$digits)";
274 $colfield .= $column;
277 $colfilter =~ s/\*/%/g;
278 $strsth2 .= "select distinctrow $colfield from borrowers where $column is not null";
280 $strsth2 .= " and $colfield LIKE ? ";
282 $strsth2 .= " and $status='1' " if ($status);
283 $strsth2 .= " order by $colfield";
285 my $sth2 = $dbh->prepare( $strsth2 );
287 $sth2->execute($colfilter);
291 while (my ($celvalue) = $sth2->fetchrow) {
295 $cell{coltitle} = $celvalue;
297 push @loopcol, \%cell;
305 #Initialization of cell values.....
308 foreach my $row ( @loopline ) {
309 foreach my $col ( @loopcol ) {
310 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
311 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
313 $table{$row->{rowtitle}}->{totalrow}=0;
316 # preparing calculation
317 my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM borrowers WHERE 1 ";
318 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
319 $strcalc .= " AND categorycode like '" . @$filters[0] ."'" if ( @$filters[0] );
320 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
321 $strcalc .= " AND zipcode like '" . @$filters[1] ."'" if ( @$filters[1] );
322 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
323 $strcalc .= " AND sort1 like '" . @$filters[2] ."'" if ( @$filters[2] );
324 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
325 $strcalc .= " AND sort2 like '" . @$filters[3] ."'" if ( @$filters[3] );
326 $strcalc .= " AND $status='1' " if ($status);
327 $strcalc .= " group by $linefield, $colfield";
329 my $dbcalc = $dbh->prepare($strcalc);
331 # warn "filling table";
334 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
335 # warn "filling table $row / $col / $value ";
336 $emptycol = 1 if ($col eq undef);
337 $col = "zzEMPTY" if ($col eq undef);
338 $row = "zzEMPTY" if ($row eq undef);
340 $table{$row}->{$col}+=$value;
341 $table{$row}->{totalrow}+=$value;
342 $grantotal += $value;
345 push @loopcol,{coltitle => "NULL"} if ($emptycol);
347 foreach my $row ( sort keys %table ) {
349 #@loopcol ensures the order for columns is common with column titles
350 # and the number matches the number of columns
351 foreach my $col ( @loopcol ) {
352 my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
353 push @loopcell, {value => $value } ;
355 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
356 'loopcell' => \@loopcell,
357 'hilighted' => ($hilighted >0),
358 'totalrow' => $table{$row}->{totalrow}
360 $hilighted = -$hilighted;
363 foreach my $col ( @loopcol ) {
365 foreach my $row ( @looprow ) {
366 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
367 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
369 # warn "summ for column ".$col->{coltitle}." = ".$total;
370 push @loopfooter, {'totalcol' => $total};
374 # the header of the table
375 $globalline{loopfilter}=\@loopfilter;
376 # the core of the table
377 $globalline{looprow} = \@looprow;
378 $globalline{loopcol} = \@loopcol;
379 # # the foot (totals by borrower type)
380 $globalline{loopfooter} = \@loopfooter;
381 $globalline{total}= $grantotal;
382 $globalline{line} = $line;
383 $globalline{column} = $column;
384 push @mainloop,\%globalline;