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 DHTMLcalendar_dateformat => get_date_format_string_for_DHTMLcalendar(),
66 my $results = calculate($limit, $column, \@filters);
67 if ($output eq "screen"){
68 # Printing results to screen
69 $template->param(mainloop => $results);
70 output_html_with_http_headers $input, $cookie, $template->output;
73 # Printing to a csv file
74 print $input->header(-type => 'application/vnd.sun.xml.calc',
76 -attachment=>"$basename.csv",
77 -filename=>"$basename.csv" );
78 my $cols = @$results[0]->{loopcol};
79 my $lines = @$results[0]->{looprow};
81 $sep =C4::Context->preference("delimiter");
83 print "num /". @$results[0]->{column} .$sep;
85 foreach my $col ( @$cols ) {
86 print $col->{coltitle}.$sep;
90 foreach my $line ( @$lines ) {
91 my $x = $line->{loopcell};
92 print $line->{rowtitle}.$sep;
93 foreach my $cell (@$x) {
94 print $cell->{value}.$sep;
96 print $line->{totalrow};
101 $cols = @$results[0]->{loopfooter};
102 foreach my $col ( @$cols ) {
103 print $sep.$col->{totalcol};
105 print $sep.@$results[0]->{total};
110 my $dbh = C4::Context->dbh;
116 my @mime = ( C4::Context->preference("MIME") );
117 # foreach my $mime (@mime){
121 my $CGIextChoice=CGI::scrolling_list(
128 my @dels = ( C4::Context->preference("delimiter") );
129 my $CGIsepChoice=CGI::scrolling_list(
136 my ($codes,$labels) = GetborCatFromCatType(undef,undef);
138 foreach my $thisborcat (sort keys %$labels) {
139 # my $selected = 1 if $thisbranch eq $branch;
140 my %row =(value => $thisborcat,
141 # selected => $selected,
142 description => $labels->{$thisborcat},
144 push @borcatloop, \%row;
147 CGIextChoice => $CGIextChoice,
148 CGIsepChoice => $CGIsepChoice,
149 borcatloop =>\@borcatloop,
151 output_html_with_http_headers $input, $cookie, $template->output;
158 my ($line, $column, $filters) = @_;
167 my $dbh = C4::Context->dbh;
173 for (my $i=0;$i<=2;$i++) {
175 if ( @$filters[$i] ) {
176 if (($i==1) and (@$filters[$i-1])) {
177 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
179 $cell{filter} .= @$filters[$i];
180 $cell{crit} .="Bor Cat" if ($i==0);
181 $cell{crit} .="Without issues since" if ($i==1);
182 push @loopfilter, \%cell;
188 $column = "borrowers.".$column if $column=~/categorycode/;
190 $colfilter[0] = @$filters[0] if ($column =~ /category/ ) ;
191 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
192 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
195 $colfield .= $column;
196 $colorder .= $column;
199 $strsth2 .= "select distinctrow $colfield FROM borrowers LEFT JOIN `issues` ON issues.borrowernumber=borrowers.borrowernumber";
201 $colfilter[0] =~ s/\*/%/g;
202 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
204 $strsth2 .=" group by $colfield";
205 $strsth2 .=" order by $colorder";
208 my $sth2 = $dbh->prepare( $strsth2 );
213 while (my ($celvalue) = $sth2->fetchrow) {
216 # warn "coltitle :".$celvalue;
217 $cell{coltitle} = $celvalue;
219 push @loopcol, \%cell;
221 # warn "fin des titres colonnes";
228 #Initialization of cell values.....
232 for (my $i=1;$i<=$line;$i++) {
233 foreach my $col ( @loopcol ) {
234 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
235 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"Global"}=0;
240 # preparing calculation
243 # Processing calculation
244 $strcalc .= "SELECT CONCAT( borrowers.surname , \"\\t\",borrowers.firstname, \"\\t\", borrowers.cardnumber)";
245 $strcalc .= " , $colfield " if ($colfield);
246 $strcalc .= " FROM borrowers ";
247 $strcalc .= "WHERE 1 ";
248 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
249 $strcalc .= " AND borrowers.categorycode like '" . @$filters[0] ."'" if ( @$filters[0] );
251 my $strqueryfilter="SELECT DISTINCT borrowernumber FROM issues where issues.timestamp> @$filters[1] ";
252 my $queryfilter = $dbh->prepare("SELECT DISTINCT borrowernumber FROM issues where issues.timestamp> ".format_date_in_iso(@$filters[1]));
253 $strcalc .= " AND borrowers.borrowernumber not in ($strqueryfilter)";
255 # $queryfilter->execute(@$filters[1]);
256 # while (my ($borrowernumber)=$queryfilter->fetchrow){
257 # $strcalc .= " AND borrowers.borrowernumber <> $borrowernumber ";
260 my $strqueryfilter="SELECT DISTINCT borrowernumber FROM issues ";
261 my $queryfilter = $dbh->prepare("SELECT DISTINCT borrowernumber FROM issues ");
262 $queryfilter->execute;
263 $strcalc .= " AND borrowers.borrowernumber not in ($strqueryfilter)";
264 # while (my ($borrowernumber)=$queryfilter->fetchrow){
265 # $strcalc .= " AND borrowers.borrowernumber <> $borrowernumber ";
268 $strcalc .= " group by borrowers.borrowernumber";
269 $strcalc .= ", $colfield" if ($column);
270 $strcalc .= " order by $colfield " if ($colfield);
273 $max = $line*@loopcol;
274 } else { $max=$line;}
275 $strcalc .= " LIMIT 0,$max" if ($line);
276 warn "SQL :". $strcalc;
278 my $dbcalc = $dbh->prepare($strcalc);
280 # warn "filling table";
283 while (my @data = $dbcalc->fetchrow) {
284 my ($row, $col )=@data;
285 $col = "zzEMPTY" if ($col eq undef);
286 $i=1 if (($previous_col) and not($col eq $previous_col));
287 $table[$i]->{$col}=$row;
288 # warn " ".$i." ".$col. " ".$row;
293 push @loopcol,{coltitle => "Global"} if not($column);
295 my $max =(($line)?$line:@table -1);
296 for ($i=1; $i<=$max;$i++) {
298 #@loopcol ensures the order for columns is common with column titles
299 # and the number matches the number of columns
301 foreach my $col ( @loopcol ) {
304 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}};
306 $value =$table[$i]->{"zzEMPTY"};
308 push @loopcell, {value => $value} ;
310 push @looprow,{ 'rowtitle' => $i ,
311 'loopcell' => \@loopcell,
312 'hilighted' => ($hilighted >0),
314 $hilighted = -$hilighted;
319 # the header of the table
320 $globalline{loopfilter}=\@loopfilter;
321 # the core of the table
322 $globalline{looprow} = \@looprow;
323 $globalline{loopcol} = \@loopcol;
324 # # the foot (totals by borrower type)
325 $globalline{loopfooter} = \@loopfooter;
326 $globalline{total}= $grantotal;
327 $globalline{line} = $line;
328 $globalline{column} = $column;
329 push @mainloop,\%globalline;