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
31 use C4::Interface::CGI::Output;
32 use C4::Circulation::Circ2;
36 plugin that shows a stats on borrowers
46 my $do_it=$input->param('do_it');
47 my $fullreportname = "reports/borrowers_stats.tmpl";
48 my $line = $input->param("Line");
49 my $column = $input->param("Column");
50 my @filters = $input->param("Filter");
51 my $digits = $input->param("digits");
52 my $borstat = $input->param("status");
53 my $output = $input->param("output");
54 my $basename = $input->param("basename");
55 my $mime = $input->param("MIME");
56 my $del = $input->param("sep");
58 my ($template, $borrowernumber, $cookie)
59 = get_template_and_user({template_name => $fullreportname,
63 flagsrequired => {editcatalogue => 1},
66 $template->param(do_it => $do_it);
68 my $results = calculate($line, $column, $digits, $borstat, \@filters);
69 if ($output eq "screen"){
70 $template->param(mainloop => $results);
71 output_html_with_http_headers $input, $cookie, $template->output;
74 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,
128 my ($count2,@branches)=branches();
129 push @select_branch,"";
130 $select_branches{''}='';
131 for (my $i=0;$i<$count2;$i++){
132 push @select_branch, $branches[$i]->{'branchcode'};#
133 $select_branches{$branches[$i]->{'branchcode'}} = $branches[$i]->{'branchname'};
135 my $CGIbranch=CGI::scrolling_list( -name => 'pickup',
136 -values => \@select_branch,
137 -labels => \%select_branches,
141 $req = $dbh->prepare( "select distinctrow sort1 from borrowers order by sort1");
144 push @select_sort1,"";
146 while (my ($value) =$req->fetchrow) {
149 push @select_sort1, $value;
152 my $CGIsort1=CGI::scrolling_list( -name => 'Filter',
154 -values => \@select_sort1,
158 $req = $dbh->prepare( "select distinctrow sort2 from borrowers order by sort2");
161 push @select_sort2,"";
163 while (my ($value) =$req->fetchrow) {
166 push @select_sort2, $value;
169 my $CGIsort2=CGI::scrolling_list( -name => 'Filter',
171 -values => \@select_sort2,
175 my @mime = ( C4::Context->preference("MIME") );
176 foreach my $mime (@mime){
180 my $CGIextChoice=CGI::scrolling_list(
187 my @dels = ( C4::Context->preference("delimiter") );
188 my $CGIsepChoice=CGI::scrolling_list(
194 $template->param(CGICatcode => $CGICatCode,
195 CGISort1 => $CGIsort1,
196 hassort1 => $hassort1,
197 CGISort2 => $CGIsort2,
198 hassort2 => $hassort2,
199 CGIextChoice => $CGIextChoice,
200 CGIsepChoice => $CGIsepChoice,
201 CGIBranch => $CGIbranch
205 output_html_with_http_headers $input, $cookie, $template->output;
210 my ($line, $column, $digits, $status, $filters) = @_;
219 my $dbh = C4::Context->dbh;
223 # warn "filtres ".@filters[0];
224 # warn "filtres ".@filters[1];
225 # warn "filtres ".@filters[2];
226 # warn "filtres ".@filters[3];
228 $linefilter = @$filters[0] if ($line =~ /categorycode/ ) ;
229 $linefilter = @$filters[1] if ($line =~ /zipcode/ ) ;
230 $linefilter = @$filters[2] if ($line =~ /branccode/ ) ;
231 $linefilter = @$filters[3] if ($line =~ /sort1/ ) ;
232 $linefilter = @$filters[4] if ($line =~ /sort2/ ) ;
235 $colfilter = @$filters[0] if ($column =~ /categorycode/);
236 $colfilter = @$filters[1] if ($column =~ /zipcode/);
237 $colfilter = @$filters[2] if ($column =~ /branchcode/);
238 $colfilter = @$filters[3] if ($column =~ /sort1/);
239 $colfilter = @$filters[4] if ($column =~ /sort2/);
242 for (my $i=0;$i<=3;$i++) {
244 if ( @$filters[$i] ) {
245 $cell{filter} .= @$filters[$i];
246 $cell{crit} .="Cat Code " if ($i==0);
247 $cell{crit} .="Zip Code" if ($i==1);
248 $cell{crit} .="Branchcode" if ($i==2);
249 $cell{crit} .="Sort1" if ($i==3);
250 $cell{crit} .="Sort2" if ($i==4);
251 push @loopfilter, \%cell;
255 push @loopfilter,{crit=>"Status",filter=>$status}
259 if (($line =~/zipcode/) and ($digits)) {
260 $linefield .="left($line,$digits)";
266 $strsth .= "select distinctrow $linefield from borrowers where $line is not null ";
267 $linefilter =~ s/\*/%/g;
269 $strsth .= " and $linefield LIKE ? " ;
271 $strsth .= " and $status='1' " if ($status);
272 $strsth .=" order by $linefield";
275 my $sth = $dbh->prepare( $strsth );
277 $sth->execute($linefilter);
281 while ( my ($celvalue) = $sth->fetchrow) {
284 $cell{rowtitle} = $celvalue;
286 # $cell{rowtitle} = "";
289 push @loopline, \%cell;
294 if (($column =~/zipcode/) and ($digits)) {
295 $colfield .= "left($column,$digits)";
297 $colfield .= $column;
300 $colfilter =~ s/\*/%/g;
301 $strsth2 .= "select distinctrow $colfield from borrowers where $column is not null";
303 $strsth2 .= " and $colfield LIKE ? ";
305 $strsth2 .= " and $status='1' " if ($status);
306 $strsth2 .= " order by $colfield";
308 my $sth2 = $dbh->prepare( $strsth2 );
310 $sth2->execute($colfilter);
314 while (my ($celvalue) = $sth2->fetchrow) {
318 $cell{coltitle} = $celvalue;
320 push @loopcol, \%cell;
328 #Initialization of cell values.....
331 foreach my $row ( @loopline ) {
332 foreach my $col ( @loopcol ) {
333 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
334 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
336 $table{$row->{rowtitle}}->{totalrow}=0;
339 # preparing calculation
340 my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM borrowers WHERE 1 ";
341 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
342 $strcalc .= " AND categorycode like '" . @$filters[0] ."'" if ( @$filters[0] );
343 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
344 $strcalc .= " AND zipcode like '" . @$filters[1] ."'" if ( @$filters[1] );
345 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
346 $strcalc .= " AND sort1 like '" . @$filters[2] ."'" if ( @$filters[2] );
347 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
348 $strcalc .= " AND sort2 like '" . @$filters[3] ."'" if ( @$filters[3] );
349 $strcalc .= " AND $status='1' " if ($status);
350 $strcalc .= " group by $linefield, $colfield";
352 my $dbcalc = $dbh->prepare($strcalc);
354 # warn "filling table";
357 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
358 # warn "filling table $row / $col / $value ";
359 $emptycol = 1 if ($col eq undef);
360 $col = "zzEMPTY" if ($col eq undef);
361 $row = "zzEMPTY" if ($row eq undef);
363 $table{$row}->{$col}+=$value;
364 $table{$row}->{totalrow}+=$value;
365 $grantotal += $value;
368 push @loopcol,{coltitle => "NULL"} if ($emptycol);
370 foreach my $row ( sort keys %table ) {
372 #@loopcol ensures the order for columns is common with column titles
373 # and the number matches the number of columns
374 foreach my $col ( @loopcol ) {
375 my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
376 push @loopcell, {value => $value } ;
378 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
379 'loopcell' => \@loopcell,
380 'hilighted' => ($hilighted >0),
381 'totalrow' => $table{$row}->{totalrow}
383 $hilighted = -$hilighted;
386 foreach my $col ( @loopcol ) {
388 foreach my $row ( @looprow ) {
389 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
390 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
392 # warn "summ for column ".$col->{coltitle}." = ".$total;
393 push @loopfooter, {'totalcol' => $total};
397 # the header of the table
398 $globalline{loopfilter}=\@loopfilter;
399 # the core of the table
400 $globalline{looprow} = \@looprow;
401 $globalline{loopcol} = \@loopcol;
402 # # the foot (totals by borrower type)
403 $globalline{loopfooter} = \@loopfooter;
404 $globalline{total}= $grantotal;
405 $globalline{line} = $line;
406 $globalline{column} = $column;
407 push @mainloop,\%globalline;