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;
34 plugin that shows a stats on borrowers
44 my $do_it=$input->param('do_it');
45 my $fullreportname = "reports/borrowers_out.tmpl";
46 my $limit = $input->param("Limit");
47 my $column = $input->param("Criteria");
48 my @filters = $input->param("Filter");
49 my $output = $input->param("output");
50 my $basename = $input->param("basename");
51 my $mime = $input->param("MIME");
52 my $del = $input->param("sep");
53 #warn "calcul : ".$calc;
54 my ($template, $borrowernumber, $cookie)
55 = get_template_and_user({template_name => $fullreportname,
59 flagsrequired => {editcatalogue => 1},
62 $template->param(do_it => $do_it);
65 my $results = calculate($limit, $column, \@filters);
66 if ($output eq "screen"){
67 # Printing results to screen
68 $template->param(mainloop => $results);
69 output_html_with_http_headers $input, $cookie, $template->output;
72 # Printing to a csv file
73 print $input->header(-type => 'application/vnd.sun.xml.calc',
74 -attachment=>"$basename.csv",
75 -filename=>"$basename.csv" );
76 my $cols = @$results[0]->{loopcol};
77 my $lines = @$results[0]->{looprow};
79 $sep =C4::Context->preference("delimiter");
81 print "num /". @$results[0]->{column} .$sep;
83 foreach my $col ( @$cols ) {
84 print $col->{coltitle}.$sep;
88 foreach my $line ( @$lines ) {
89 my $x = $line->{loopcell};
90 print $line->{rowtitle}.$sep;
91 foreach my $cell (@$x) {
92 print $cell->{value}.$sep;
94 print $line->{totalrow};
99 $cols = @$results[0]->{loopfooter};
100 foreach my $col ( @$cols ) {
101 print $sep.$col->{totalcol};
103 print $sep.@$results[0]->{total};
108 my $dbh = C4::Context->dbh;
114 my @mime = ( C4::Context->preference("MIME") );
115 # foreach my $mime (@mime){
119 my $CGIextChoice=CGI::scrolling_list(
126 my @dels = ( C4::Context->preference("delimiter") );
127 my $CGIsepChoice=CGI::scrolling_list(
135 CGIextChoice => $CGIextChoice,
136 CGIsepChoice => $CGIsepChoice
138 output_html_with_http_headers $input, $cookie, $template->output;
145 my ($line, $column, $filters) = @_;
154 my $dbh = C4::Context->dbh;
160 for (my $i=0;$i<=6;$i++) {
162 if ( @$filters[$i] ) {
163 if (($i==1) and (@$filters[$i-1])) {
164 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
166 $cell{filter} .= @$filters[$i];
167 $cell{crit} .="Bor Cat" if ($i==0);
168 push @loopfilter, \%cell;
174 $column = "borrowers.".$column if $column=~/categorycode/;
176 $colfilter[0] = @$filters[0] if ($column =~ /category/ ) ;
177 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
178 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
181 $colfield .= $column;
182 $colorder .= $column;
185 $strsth2 .= "select distinctrow $colfield FROM borrowers LEFT JOIN `issues` ON issues.borrowernumber=borrowers.borrowernumber";
187 $colfilter[0] =~ s/\*/%/g;
188 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
190 $strsth2 .=" group by $colfield";
191 $strsth2 .=" order by $colorder";
194 my $sth2 = $dbh->prepare( $strsth2 );
199 while (my ($celvalue) = $sth2->fetchrow) {
202 # warn "coltitle :".$celvalue;
203 $cell{coltitle} = $celvalue;
205 push @loopcol, \%cell;
207 # warn "fin des titres colonnes";
214 #Initialization of cell values.....
218 for (my $i=1;$i<=$line;$i++) {
219 foreach my $col ( @loopcol ) {
220 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
221 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"Global"}=0;
226 # preparing calculation
229 # Processing average loanperiods
230 $strcalc .= "SELECT CONCAT( borrowers.surname , \"\\t\",borrowers.firstname, \"\\t\", borrowers.cardnumber)";
231 $strcalc .= " , $colfield " if ($colfield);
232 $strcalc .= " FROM borrowers LEFT JOIN issues ON issues.borrowernumber=borrowers.borrowernumber WHERE issues.borrowernumber is null";
233 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
234 $strcalc .= " AND borrowers.categorycode like '" . @$filters[0] ."'" if ( @$filters[0] );
236 $strcalc .= " group by borrowers.borrowernumber";
237 $strcalc .= ", $colfield" if ($column);
238 $strcalc .= " order by $colfield " if ($colfield);
241 $max = $line*@loopcol;
242 } else { $max=$line;}
243 $strcalc .= " LIMIT 0,$max" if ($line);
244 warn "SQL :". $strcalc;
246 my $dbcalc = $dbh->prepare($strcalc);
248 # warn "filling table";
251 while (my @data = $dbcalc->fetchrow) {
252 my ($row, $col )=@data;
253 $col = "zzEMPTY" if ($col eq undef);
254 $i=1 if (($previous_col) and not($col eq $previous_col));
255 $table[$i]->{$col}=$row;
256 # warn " ".$i." ".$col. " ".$row;
261 push @loopcol,{coltitle => "Global"} if not($column);
263 my $max =(($line)?$line:@table);
264 for ($i=1; $i<=$max;$i++) {
266 #@loopcol ensures the order for columns is common with column titles
267 # and the number matches the number of columns
269 foreach my $col ( @loopcol ) {
272 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}};
274 $value =$table[$i]->{"zzEMPTY"};
276 push @loopcell, {value => $value} ;
278 push @looprow,{ 'rowtitle' => $i ,
279 'loopcell' => \@loopcell,
280 'hilighted' => ($hilighted >0),
282 $hilighted = -$hilighted;
287 # the header of the table
288 $globalline{loopfilter}=\@loopfilter;
289 # the core of the table
290 $globalline{looprow} = \@looprow;
291 $globalline{loopcol} = \@loopcol;
292 # # the foot (totals by borrower type)
293 $globalline{loopfooter} = \@loopfooter;
294 $globalline{total}= $grantotal;
295 $globalline{line} = $line;
296 $globalline{column} = $column;
297 push @mainloop,\%globalline;