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
30 use C4::Interface::CGI::Output;
31 use C4::Circulation::Circ2;
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 => {editcatalogue => 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',
74 -name=>"$basename.csv",
75 -attachment=>"$basename.csv");
76 my $cols = @$results[0]->{loopcol};
77 my $lines = @$results[0]->{looprow};
79 $sep =C4::Context->preference("delimiter");
80 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
81 foreach my $col ( @$cols ) {
82 print $col->{coltitle}.$sep;
85 foreach my $line ( @$lines ) {
86 my $x = $line->{loopcell};
87 print $line->{rowtitle}.$sep;
88 foreach my $cell (@$x) {
89 print $cell->{value}.$sep;
91 print $line->{totalrow};
95 $cols = @$results[0]->{loopfooter};
96 foreach my $col ( @$cols ) {
97 print $sep.$col->{totalcol};
99 print $sep.@$results[0]->{total};
103 my $dbh = C4::Context->dbh;
107 $req = $dbh->prepare( "select categorycode, description from categories order by description");
111 push @select_catcode,"";
112 $select_catcode{""} = "";
113 while (my ($catcode, $description) =$req->fetchrow) {
114 push @select_catcode, $catcode;
115 $select_catcode{$catcode} = $description
117 my $CGICatCode=CGI::scrolling_list( -name => 'Filter',
119 -values => \@select_catcode,
120 -labels => \%select_catcode,
124 $req = $dbh->prepare( "select distinctrow sort1 from borrowers order by sort1");
127 push @select_sort1,"";
129 while (my ($value) =$req->fetchrow) {
132 push @select_sort1, $value;
135 my $CGIsort1=CGI::scrolling_list( -name => 'Filter',
137 -values => \@select_sort1,
141 $req = $dbh->prepare( "select distinctrow sort2 from borrowers order by sort2");
144 push @select_sort2,"";
146 while (my ($value) =$req->fetchrow) {
149 push @select_sort2, $value;
152 my $CGIsort2=CGI::scrolling_list( -name => 'Filter',
154 -values => \@select_sort2,
158 my @mime = ( C4::Context->preference("MIME") );
159 foreach my $mime (@mime){
163 my $CGIextChoice=CGI::scrolling_list(
170 my @dels = ( C4::Context->preference("delimiter") );
171 my $CGIsepChoice=CGI::scrolling_list(
177 $template->param(CGICatcode => $CGICatCode,
178 CGISort1 => $CGIsort1,
179 hassort1 => $hassort1,
180 CGISort2 => $CGIsort2,
181 hassort2 => $hassort2,
182 CGIextChoice => $CGIextChoice,
183 CGIsepChoice => $CGIsepChoice
187 output_html_with_http_headers $input, $cookie, $template->output;
192 my ($line, $column, $digits, $status, $filters) = @_;
201 my $dbh = C4::Context->dbh;
205 # warn "filtres ".@filters[0];
206 # warn "filtres ".@filters[1];
207 # warn "filtres ".@filters[2];
208 # warn "filtres ".@filters[3];
210 $linefilter = @$filters[0] if ($line =~ /categorycode/ ) ;
211 $linefilter = @$filters[1] if ($line =~ /zipcode/ ) ;
212 $linefilter = @$filters[2] if ($line =~ /sort1/ ) ;
213 $linefilter = @$filters[3] if ($line =~ /sort2/ ) ;
216 $colfilter = @$filters[0] if ($column =~ /categorycode/);
217 $colfilter = @$filters[1] if ($column =~ /zipcode/);
218 $colfilter = @$filters[2] if ($column =~ /sort1/);
219 $colfilter = @$filters[3] if ($column =~ /sort2/);
222 for (my $i=0;$i<=3;$i++) {
224 if ( @$filters[$i] ) {
225 $cell{filter} .= @$filters[$i];
226 $cell{crit} .="Cat Code " if ($i==0);
227 $cell{crit} .="Zip Code" if ($i==1);
228 $cell{crit} .="Sort1" if ($i==2);
229 $cell{crit} .="Sort2" if ($i==3);
230 push @loopfilter, \%cell;
234 push @loopfilter,{crit=>"Status",filter=>$status}
238 if (($line =~/zipcode/) and ($digits)) {
239 $linefield .="left($line,$digits)";
245 $strsth .= "select distinctrow $linefield from borrowers where $line is not null ";
246 $linefilter =~ s/\*/%/g;
248 $strsth .= " and $linefield LIKE ? " ;
250 $strsth .= " and $status='1' " if ($status);
251 $strsth .=" order by $linefield";
254 my $sth = $dbh->prepare( $strsth );
256 $sth->execute($linefilter);
260 while ( my ($celvalue) = $sth->fetchrow) {
263 $cell{rowtitle} = $celvalue;
265 # $cell{rowtitle} = "";
268 push @loopline, \%cell;
273 if (($column =~/zipcode/) and ($digits)) {
274 $colfield .= "left($column,$digits)";
276 $colfield .= $column;
279 $colfilter =~ s/\*/%/g;
280 $strsth2 .= "select distinctrow $colfield from borrowers where $column is not null";
282 $strsth2 .= " and $colfield LIKE ? ";
284 $strsth2 .= " and $status='1' " if ($status);
285 $strsth2 .= " order by $colfield";
287 my $sth2 = $dbh->prepare( $strsth2 );
289 $sth2->execute($colfilter);
293 while (my ($celvalue) = $sth2->fetchrow) {
297 $cell{coltitle} = $celvalue;
299 push @loopcol, \%cell;
307 #Initialization of cell values.....
310 foreach my $row ( @loopline ) {
311 foreach my $col ( @loopcol ) {
312 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
313 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
315 $table{$row->{rowtitle}}->{totalrow}=0;
318 # preparing calculation
319 my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM borrowers WHERE 1 ";
320 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
321 $strcalc .= " AND categorycode like '" . @$filters[0] ."'" if ( @$filters[0] );
322 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
323 $strcalc .= " AND zipcode like '" . @$filters[1] ."'" if ( @$filters[1] );
324 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
325 $strcalc .= " AND sort1 like '" . @$filters[2] ."'" if ( @$filters[2] );
326 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
327 $strcalc .= " AND sort2 like '" . @$filters[3] ."'" if ( @$filters[3] );
328 $strcalc .= " AND $status='1' " if ($status);
329 $strcalc .= " group by $linefield, $colfield";
331 my $dbcalc = $dbh->prepare($strcalc);
333 # warn "filling table";
336 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
337 # warn "filling table $row / $col / $value ";
338 $emptycol = 1 if ($col eq undef);
339 $col = "zzEMPTY" if ($col eq undef);
340 $row = "zzEMPTY" if ($row eq undef);
342 $table{$row}->{$col}+=$value;
343 $table{$row}->{totalrow}+=$value;
344 $grantotal += $value;
347 push @loopcol,{coltitle => "NULL"} if ($emptycol);
349 foreach my $row ( sort keys %table ) {
351 #@loopcol ensures the order for columns is common with column titles
352 # and the number matches the number of columns
353 foreach my $col ( @loopcol ) {
354 my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
355 push @loopcell, {value => $value } ;
357 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
358 'loopcell' => \@loopcell,
359 'hilighted' => ($hilighted >0),
360 'totalrow' => $table{$row}->{totalrow}
362 $hilighted = -$hilighted;
365 foreach my $col ( @loopcol ) {
367 foreach my $row ( @looprow ) {
368 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
369 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
371 # warn "summ for column ".$col->{coltitle}." = ".$total;
372 push @loopfooter, {'totalcol' => $total};
376 # the header of the table
377 $globalline{loopfilter}=\@loopfilter;
378 # the core of the table
379 $globalline{looprow} = \@looprow;
380 $globalline{loopcol} = \@loopcol;
381 # # the foot (totals by borrower type)
382 $globalline{loopfooter} = \@loopfooter;
383 $globalline{total}= $grantotal;
384 $globalline{line} = $line;
385 $globalline{column} = $column;
386 push @mainloop,\%globalline;