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
34 plugin that shows a stats on borrowers
43 my $do_it=$input->param('do_it');
44 my $fullreportname = "reports/borrowers_out.tmpl";
45 my $limit = $input->param("Limit");
46 my $column = $input->param("Criteria");
47 my @filters = $input->param("Filter");
48 my $output = $input->param("output");
49 my $basename = $input->param("basename");
50 my $mime = $input->param("MIME");
51 my $del = $input->param("sep");
52 #warn "calcul : ".$calc;
53 my ($template, $borrowernumber, $cookie)
54 = get_template_and_user
({template_name
=> $fullreportname,
58 flagsrequired
=> {reports
=> 1},
61 $template->param(do_it
=> $do_it,
62 intranetcolorstylesheet
=> C4
::Context
->preference("intranetcolorstylesheet"),
63 intranetstylesheet
=> C4
::Context
->preference("intranetstylesheet"),
64 IntranetNav
=> C4
::Context
->preference("IntranetNav"),
68 my $results = calculate
($limit, $column, \
@filters);
69 if ($output eq "screen"){
70 # Printing results to screen
71 $template->param(mainloop
=> $results);
72 output_html_with_http_headers
$input, $cookie, $template->output;
75 # Printing to a csv file
76 print $input->header(-type
=> 'application/vnd.sun.xml.calc',
78 -attachment
=>"$basename.csv",
79 -filename
=>"$basename.csv" );
80 my $cols = @
$results[0]->{loopcol
};
81 my $lines = @
$results[0]->{looprow
};
83 $sep =C4
::Context
->preference("delimiter");
85 print "num /". @
$results[0]->{column
} .$sep;
87 foreach my $col ( @
$cols ) {
88 print $col->{coltitle
}.$sep;
92 foreach my $line ( @
$lines ) {
93 my $x = $line->{loopcell
};
94 print $line->{rowtitle
}.$sep;
95 foreach my $cell (@
$x) {
96 print $cell->{value
}.$sep;
98 print $line->{totalrow
};
103 $cols = @
$results[0]->{loopfooter
};
104 foreach my $col ( @
$cols ) {
105 print $sep.$col->{totalcol
};
107 print $sep.@
$results[0]->{total
};
112 my $dbh = C4
::Context
->dbh;
118 my @mime = ( C4
::Context
->preference("MIME") );
119 # foreach my $mime (@mime){
123 my $CGIextChoice=CGI
::scrolling_list
(
130 my @dels = ( C4
::Context
->preference("delimiter") );
131 my $CGIsepChoice=CGI
::scrolling_list
(
138 my ($codes,$labels) = GetborCatFromCatType
(undef,undef);
140 foreach my $thisborcat (sort keys %$labels) {
141 # my $selected = 1 if $thisbranch eq $branch;
142 my %row =(value
=> $thisborcat,
143 # selected => $selected,
144 description
=> $labels->{$thisborcat},
146 push @borcatloop, \
%row;
151 CGIextChoice
=> $CGIextChoice,
152 CGIsepChoice
=> $CGIsepChoice,
153 borcatloop
=>\
@borcatloop,
155 output_html_with_http_headers
$input, $cookie, $template->output;
162 my ($line, $column, $filters) = @_;
171 my $dbh = C4
::Context
->dbh;
177 for (my $i=0;$i<=2;$i++) {
179 if ( @
$filters[$i] ) {
180 if (($i==1) and (@
$filters[$i-1])) {
181 $cell{err
} = 1 if (@
$filters[$i]<@
$filters[$i-1]) ;
183 $cell{filter
} .= @
$filters[$i];
184 $cell{crit
} .="Bor Cat" if ($i==0);
185 $cell{crit
} .="Without issues since" if ($i==1);
186 push @loopfilter, \
%cell;
192 $column = "borrowers.".$column if $column=~/categorycode/;
194 $colfilter[0] = @
$filters[0] if ($column =~ /category/ ) ;
195 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
196 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
199 $colfield .= $column;
200 $colorder .= $column;
203 $strsth2 .= "select distinctrow $colfield FROM borrowers LEFT JOIN `issues` ON issues.borrowernumber=borrowers.borrowernumber";
205 $colfilter[0] =~ s/\*/%/g;
206 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
208 $strsth2 .=" group by $colfield";
209 $strsth2 .=" order by $colorder";
212 my $sth2 = $dbh->prepare( $strsth2 );
217 while (my ($celvalue) = $sth2->fetchrow) {
220 # warn "coltitle :".$celvalue;
221 $cell{coltitle
} = $celvalue;
223 push @loopcol, \
%cell;
225 # warn "fin des titres colonnes";
232 #Initialization of cell values.....
236 for (my $i=1;$i<=$line;$i++) {
237 foreach my $col ( @loopcol ) {
238 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
239 $table[$i]->{($col->{coltitle
})?
$col->{coltitle
}:"Global"}=0;
244 # preparing calculation
247 # Processing calculation
248 $strcalc .= "SELECT CONCAT( borrowers.surname , \"\\t\",borrowers.firstname, \"\\t\", borrowers.cardnumber)";
249 $strcalc .= " , $colfield " if ($colfield);
250 $strcalc .= " FROM borrowers ";
251 $strcalc .= "WHERE 1 ";
252 @
$filters[0]=~ s/\*/%/g if (@
$filters[0]);
253 $strcalc .= " AND borrowers.categorycode like '" . @
$filters[0] ."'" if ( @
$filters[0] );
255 my $strqueryfilter="SELECT DISTINCT borrowernumber FROM issues where issues.timestamp> @$filters[1] ";
256 my $queryfilter = $dbh->prepare("SELECT DISTINCT borrowernumber FROM issues where issues.timestamp> @$filters[1] ");
257 $strcalc .= " AND borrowers.borrowernumber not in ($strqueryfilter)";
259 # $queryfilter->execute(@$filters[1]);
260 # while (my ($borrowernumber)=$queryfilter->fetchrow){
261 # $strcalc .= " AND borrowers.borrowernumber <> $borrowernumber ";
264 my $strqueryfilter="SELECT DISTINCT borrowernumber FROM issues ";
265 my $queryfilter = $dbh->prepare("SELECT DISTINCT borrowernumber FROM issues ");
266 $queryfilter->execute;
267 $strcalc .= " AND borrowers.borrowernumber not in ($strqueryfilter)";
268 # while (my ($borrowernumber)=$queryfilter->fetchrow){
269 # $strcalc .= " AND borrowers.borrowernumber <> $borrowernumber ";
272 $strcalc .= " group by borrowers.borrowernumber";
273 $strcalc .= ", $colfield" if ($column);
274 $strcalc .= " order by $colfield " if ($colfield);
277 $max = $line*@loopcol;
278 } else { $max=$line;}
279 $strcalc .= " LIMIT 0,$max" if ($line);
280 warn "SQL :". $strcalc;
282 my $dbcalc = $dbh->prepare($strcalc);
284 # warn "filling table";
287 while (my @data = $dbcalc->fetchrow) {
288 my ($row, $col )=@data;
289 $col = "zzEMPTY" if ($col eq undef);
290 $i=1 if (($previous_col) and not($col eq $previous_col));
291 $table[$i]->{$col}=$row;
292 # warn " ".$i." ".$col. " ".$row;
297 push @loopcol,{coltitle
=> "Global"} if not($column);
299 my $max =(($line)?
$line:@table -1);
300 for ($i=1; $i<=$max;$i++) {
302 #@loopcol ensures the order for columns is common with column titles
303 # and the number matches the number of columns
305 foreach my $col ( @loopcol ) {
308 $value =$table[$i]->{(($col->{coltitle
} eq "NULL") or ($col->{coltitle
} eq "Global"))?
"zzEMPTY":$col->{coltitle
}};
310 $value =$table[$i]->{"zzEMPTY"};
312 push @loopcell, {value
=> $value} ;
314 push @looprow,{ 'rowtitle' => $i ,
315 'loopcell' => \
@loopcell,
316 'hilighted' => ($hilighted >0),
318 $hilighted = -$hilighted;
323 # the header of the table
324 $globalline{loopfilter
}=\
@loopfilter;
325 # the core of the table
326 $globalline{looprow
} = \
@looprow;
327 $globalline{loopcol
} = \
@loopcol;
328 # # the foot (totals by borrower type)
329 $globalline{loopfooter
} = \
@loopfooter;
330 $globalline{total
}= $grantotal;
331 $globalline{line
} = $line;
332 $globalline{column
} = $column;
333 push @mainloop,\
%globalline;