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;
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 => {editcatalogue => 1},
64 $template->param(do_it => $do_it);
67 my $results = calculate($limit, $column, \@filters);
68 if ($output eq "screen"){
69 # Printing results to screen
70 $template->param(mainloop => $results);
71 output_html_with_http_headers $input, $cookie, $template->output;
74 # Printing to a csv file
75 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) = borrowercategories;
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;
149 CGIextChoice => $CGIextChoice,
150 CGIsepChoice => $CGIsepChoice,
151 borcatloop =>\@borcatloop,
153 output_html_with_http_headers $input, $cookie, $template->output;
160 my ($line, $column, $filters) = @_;
169 my $dbh = C4::Context->dbh;
175 for (my $i=0;$i<=2;$i++) {
177 if ( @$filters[$i] ) {
178 if (($i==1) and (@$filters[$i-1])) {
179 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
181 $cell{filter} .= @$filters[$i];
182 $cell{crit} .="Bor Cat" if ($i==0);
183 $cell{crit} .="Without issues since" if ($i==1);
184 push @loopfilter, \%cell;
190 $column = "borrowers.".$column if $column=~/categorycode/;
192 $colfilter[0] = @$filters[0] if ($column =~ /category/ ) ;
193 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
194 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
197 $colfield .= $column;
198 $colorder .= $column;
201 $strsth2 .= "select distinctrow $colfield FROM borrowers LEFT JOIN `issues` ON issues.borrowernumber=borrowers.borrowernumber";
203 $colfilter[0] =~ s/\*/%/g;
204 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
206 $strsth2 .=" group by $colfield";
207 $strsth2 .=" order by $colorder";
210 my $sth2 = $dbh->prepare( $strsth2 );
215 while (my ($celvalue) = $sth2->fetchrow) {
218 # warn "coltitle :".$celvalue;
219 $cell{coltitle} = $celvalue;
221 push @loopcol, \%cell;
223 # warn "fin des titres colonnes";
230 #Initialization of cell values.....
234 for (my $i=1;$i<=$line;$i++) {
235 foreach my $col ( @loopcol ) {
236 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
237 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"Global"}=0;
242 # preparing calculation
245 # Processing calculation
246 $strcalc .= "SELECT CONCAT( borrowers.surname , \"\\t\",borrowers.firstname, \"\\t\", borrowers.cardnumber)";
247 $strcalc .= " , $colfield " if ($colfield);
248 $strcalc .= " FROM borrowers ";
249 $strcalc .= "WHERE 1 ";
250 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
251 $strcalc .= " AND borrowers.categorycode like '" . @$filters[0] ."'" if ( @$filters[0] );
253 my $strqueryfilter="SELECT DISTINCT borrowernumber FROM issues where issues.timestamp> @$filters[1] ";
254 my $queryfilter = $dbh->prepare("SELECT DISTINCT borrowernumber FROM issues where issues.timestamp> @$filters[1] ");
255 $strcalc .= " AND borrowers.borrowernumber not in ($strqueryfilter)";
257 # $queryfilter->execute(@$filters[1]);
258 # while (my ($bornum)=$queryfilter->fetchrow){
259 # $strcalc .= " AND borrowers.borrowernumber <> $bornum ";
262 my $strqueryfilter="SELECT DISTINCT borrowernumber FROM issues ";
263 my $queryfilter = $dbh->prepare("SELECT DISTINCT borrowernumber FROM issues ");
264 $queryfilter->execute;
265 $strcalc .= " AND borrowers.borrowernumber not in ($strqueryfilter)";
266 # while (my ($bornum)=$queryfilter->fetchrow){
267 # $strcalc .= " AND borrowers.borrowernumber <> $bornum ";
270 $strcalc .= " group by borrowers.borrowernumber";
271 $strcalc .= ", $colfield" if ($column);
272 $strcalc .= " order by $colfield " if ($colfield);
275 $max = $line*@loopcol;
276 } else { $max=$line;}
277 $strcalc .= " LIMIT 0,$max" if ($line);
278 warn "SQL :". $strcalc;
280 my $dbcalc = $dbh->prepare($strcalc);
282 # warn "filling table";
285 while (my @data = $dbcalc->fetchrow) {
286 my ($row, $col )=@data;
287 $col = "zzEMPTY" if ($col eq undef);
288 $i=1 if (($previous_col) and not($col eq $previous_col));
289 $table[$i]->{$col}=$row;
290 # warn " ".$i." ".$col. " ".$row;
295 push @loopcol,{coltitle => "Global"} if not($column);
297 my $max =(($line)?$line:@table -1);
298 for ($i=1; $i<=$max;$i++) {
300 #@loopcol ensures the order for columns is common with column titles
301 # and the number matches the number of columns
303 foreach my $col ( @loopcol ) {
306 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}};
308 $value =$table[$i]->{"zzEMPTY"};
310 push @loopcell, {value => $value} ;
312 push @looprow,{ 'rowtitle' => $i ,
313 'loopcell' => \@loopcell,
314 'hilighted' => ($hilighted >0),
316 $hilighted = -$hilighted;
321 # the header of the table
322 $globalline{loopfilter}=\@loopfilter;
323 # the core of the table
324 $globalline{looprow} = \@looprow;
325 $globalline{loopcol} = \@loopcol;
326 # # the foot (totals by borrower type)
327 $globalline{loopfooter} = \@loopfooter;
328 $globalline{total}= $grantotal;
329 $globalline{line} = $line;
330 $globalline{column} = $column;
331 push @mainloop,\%globalline;