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