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', -name=>"$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");
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");
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");
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) {
294 $cell{coltitle} = $celvalue;
296 push @loopcol, \%cell;
304 #Initialization of cell values.....
307 foreach my $row ( @loopline ) {
308 foreach my $col ( @loopcol ) {
309 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
310 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
312 $table{$row->{rowtitle}}->{totalrow}=0;
315 # preparing calculation
316 my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM borrowers WHERE $line is not null AND $column is not null";
317 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
318 $strcalc .= " AND categorycode like '" . @$filters[0] ."'" if ( @$filters[0] );
319 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
320 $strcalc .= " AND zipcode like '" . @$filters[1] ."'" if ( @$filters[1] );
321 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
322 $strcalc .= " AND sort1 like '" . @$filters[2] ."'" if ( @$filters[2] );
323 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
324 $strcalc .= " AND sort2 like '" . @$filters[3] ."'" if ( @$filters[3] );
325 $strcalc .= " AND $status='1' " if ($status);
326 $strcalc .= " group by $linefield, $colfield";
328 my $dbcalc = $dbh->prepare($strcalc);
330 # warn "filling table";
331 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
332 # warn "filling table $row / $col / $value ";
333 $table{$row}->{$col}=$value;
334 $table{$row}->{totalrow}+=$value;
335 $grantotal += $value;
338 foreach my $row ( keys %table ) {
340 #@loopcol ensures the order for columns is common with column titles
341 foreach my $col ( @loopcol ) {
342 push @loopcell, {value => $table{$row}->{$col->{coltitle}}} ;
344 push @looprow,{ 'rowtitle' => $row,
345 'loopcell' => \@loopcell,
347 'totalrow' => $table{$row}->{totalrow}
349 $hilighted = -$hilighted;
352 foreach my $col ( @loopcol ) {
354 foreach my $row ( @loopline ) {
355 $total += $table{$row->{rowtitle}}->{$col->{coltitle}};
357 push @loopfooter, {'totalcol' => $total};
361 # the header of the table
362 $globalline{loopfilter}=\@loopfilter;
363 # the core of the table
364 $globalline{looprow} = \@looprow;
365 $globalline{loopcol} = \@loopcol;
366 # # the foot (totals by borrower type)
367 $globalline{loopfooter} = \@loopfooter;
368 $globalline{total}= $grantotal;
369 $globalline{line} = $line;
370 $globalline{column} = $column;
371 push @mainloop,\%globalline;