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
29 use C4::Interface::CGI::Output;
36 plugin that shows a stats on borrowers
46 my $do_it=$input->param('do_it');
47 my $fullreportname = "reports/borrowers_out.tmpl";
48 my $limit = $input->param("Limit");
49 my $column = $input->param("Criteria");
50 my @filters = $input->param("Filter");
51 my $output = $input->param("output");
52 my $basename = $input->param("basename");
53 my $mime = $input->param("MIME");
54 my $del = $input->param("sep");
55 #warn "calcul : ".$calc;
56 my ($template, $borrowernumber, $cookie)
57 = get_template_and_user({template_name => $fullreportname,
61 flagsrequired => {reports => 1},
64 $template->param(do_it => $do_it,
65 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
66 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
67 IntranetNav => C4::Context->preference("IntranetNav"),
71 my $results = calculate($limit, $column, \@filters);
72 if ($output eq "screen"){
73 # Printing results to screen
74 $template->param(mainloop => $results);
75 output_html_with_http_headers $input, $cookie, $template->output;
78 # Printing to a csv file
79 print $input->header(-type => 'application/vnd.sun.xml.calc',
81 -attachment=>"$basename.csv",
82 -filename=>"$basename.csv" );
83 my $cols = @$results[0]->{loopcol};
84 my $lines = @$results[0]->{looprow};
86 $sep =C4::Context->preference("delimiter");
88 print "num /". @$results[0]->{column} .$sep;
90 foreach my $col ( @$cols ) {
91 print $col->{coltitle}.$sep;
95 foreach my $line ( @$lines ) {
96 my $x = $line->{loopcell};
97 print $line->{rowtitle}.$sep;
98 foreach my $cell (@$x) {
99 print $cell->{value}.$sep;
101 print $line->{totalrow};
106 $cols = @$results[0]->{loopfooter};
107 foreach my $col ( @$cols ) {
108 print $sep.$col->{totalcol};
110 print $sep.@$results[0]->{total};
115 my $dbh = C4::Context->dbh;
121 my @mime = ( C4::Context->preference("MIME") );
122 # foreach my $mime (@mime){
126 my $CGIextChoice=CGI::scrolling_list(
133 my @dels = ( C4::Context->preference("delimiter") );
134 my $CGIsepChoice=CGI::scrolling_list(
141 my ($codes,$labels) = GetborCatFromCatType(undef,undef);
143 foreach my $thisborcat (sort keys %$labels) {
144 # my $selected = 1 if $thisbranch eq $branch;
145 my %row =(value => $thisborcat,
146 # selected => $selected,
147 description => $labels->{$thisborcat},
149 push @borcatloop, \%row;
154 CGIextChoice => $CGIextChoice,
155 CGIsepChoice => $CGIsepChoice,
156 borcatloop =>\@borcatloop,
158 output_html_with_http_headers $input, $cookie, $template->output;
165 my ($line, $column, $filters) = @_;
174 my $dbh = C4::Context->dbh;
180 for (my $i=0;$i<=2;$i++) {
182 if ( @$filters[$i] ) {
183 if (($i==1) and (@$filters[$i-1])) {
184 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
186 $cell{filter} .= @$filters[$i];
187 $cell{crit} .="Bor Cat" if ($i==0);
188 $cell{crit} .="Without issues since" if ($i==1);
189 push @loopfilter, \%cell;
195 $column = "borrowers.".$column if $column=~/categorycode/;
197 $colfilter[0] = @$filters[0] if ($column =~ /category/ ) ;
198 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
199 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
202 $colfield .= $column;
203 $colorder .= $column;
206 $strsth2 .= "select distinctrow $colfield FROM borrowers LEFT JOIN `issues` ON issues.borrowernumber=borrowers.borrowernumber";
208 $colfilter[0] =~ s/\*/%/g;
209 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
211 $strsth2 .=" group by $colfield";
212 $strsth2 .=" order by $colorder";
215 my $sth2 = $dbh->prepare( $strsth2 );
220 while (my ($celvalue) = $sth2->fetchrow) {
223 # warn "coltitle :".$celvalue;
224 $cell{coltitle} = $celvalue;
226 push @loopcol, \%cell;
228 # warn "fin des titres colonnes";
235 #Initialization of cell values.....
239 for (my $i=1;$i<=$line;$i++) {
240 foreach my $col ( @loopcol ) {
241 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
242 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"Global"}=0;
247 # preparing calculation
250 # Processing calculation
251 $strcalc .= "SELECT CONCAT( borrowers.surname , \"\\t\",borrowers.firstname, \"\\t\", borrowers.cardnumber)";
252 $strcalc .= " , $colfield " if ($colfield);
253 $strcalc .= " FROM borrowers ";
254 $strcalc .= "WHERE 1 ";
255 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
256 $strcalc .= " AND borrowers.categorycode like '" . @$filters[0] ."'" if ( @$filters[0] );
258 my $strqueryfilter="SELECT DISTINCT borrowernumber FROM issues where issues.timestamp> @$filters[1] ";
259 my $queryfilter = $dbh->prepare("SELECT DISTINCT borrowernumber FROM issues where issues.timestamp> @$filters[1] ");
260 $strcalc .= " AND borrowers.borrowernumber not in ($strqueryfilter)";
262 # $queryfilter->execute(@$filters[1]);
263 # while (my ($borrowernumber)=$queryfilter->fetchrow){
264 # $strcalc .= " AND borrowers.borrowernumber <> $borrowernumber ";
267 my $strqueryfilter="SELECT DISTINCT borrowernumber FROM issues ";
268 my $queryfilter = $dbh->prepare("SELECT DISTINCT borrowernumber FROM issues ");
269 $queryfilter->execute;
270 $strcalc .= " AND borrowers.borrowernumber not in ($strqueryfilter)";
271 # while (my ($borrowernumber)=$queryfilter->fetchrow){
272 # $strcalc .= " AND borrowers.borrowernumber <> $borrowernumber ";
275 $strcalc .= " group by borrowers.borrowernumber";
276 $strcalc .= ", $colfield" if ($column);
277 $strcalc .= " order by $colfield " if ($colfield);
280 $max = $line*@loopcol;
281 } else { $max=$line;}
282 $strcalc .= " LIMIT 0,$max" if ($line);
283 warn "SQL :". $strcalc;
285 my $dbcalc = $dbh->prepare($strcalc);
287 # warn "filling table";
290 while (my @data = $dbcalc->fetchrow) {
291 my ($row, $col )=@data;
292 $col = "zzEMPTY" if ($col eq undef);
293 $i=1 if (($previous_col) and not($col eq $previous_col));
294 $table[$i]->{$col}=$row;
295 # warn " ".$i." ".$col. " ".$row;
300 push @loopcol,{coltitle => "Global"} if not($column);
302 my $max =(($line)?$line:@table -1);
303 for ($i=1; $i<=$max;$i++) {
305 #@loopcol ensures the order for columns is common with column titles
306 # and the number matches the number of columns
308 foreach my $col ( @loopcol ) {
311 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}};
313 $value =$table[$i]->{"zzEMPTY"};
315 push @loopcell, {value => $value} ;
317 push @looprow,{ 'rowtitle' => $i ,
318 'loopcell' => \@loopcell,
319 'hilighted' => ($hilighted >0),
321 $hilighted = -$hilighted;
326 # the header of the table
327 $globalline{loopfilter}=\@loopfilter;
328 # the core of the table
329 $globalline{looprow} = \@looprow;
330 $globalline{loopcol} = \@loopcol;
331 # # the foot (totals by borrower type)
332 $globalline{loopfooter} = \@loopfooter;
333 $globalline{total}= $grantotal;
334 $globalline{line} = $line;
335 $globalline{column} = $column;
336 push @mainloop,\%globalline;