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/cat_issues_top.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,
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',
77 -attachment=>"$basename.csv",
78 -filename=>"$basename.csv" );
79 my $cols = @$results[0]->{loopcol};
80 my $lines = @$results[0]->{looprow};
82 $sep =C4::Context->preference("delimiter");
84 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
86 foreach my $col ( @$cols ) {
87 print $col->{coltitle}.$sep;
91 foreach my $line ( @$lines ) {
92 my $x = $line->{loopcell};
93 print $line->{rowtitle}.$sep;
94 foreach my $cell (@$x) {
95 print $cell->{value}.$sep;
97 print $line->{totalrow};
102 $cols = @$results[0]->{loopfooter};
103 foreach my $col ( @$cols ) {
104 print $sep.$col->{totalcol};
106 print $sep.@$results[0]->{total};
111 my $dbh = C4::Context->dbh;
117 my @mime = ( C4::Context->preference("MIME") );
118 # foreach my $mime (@mime){
122 my $CGIextChoice=CGI::scrolling_list(
129 my @dels = ( C4::Context->preference("delimiter") );
130 my $CGIsepChoice=CGI::scrolling_list(
138 CGIextChoice => $CGIextChoice,
139 CGIsepChoice => $CGIsepChoice
141 output_html_with_http_headers $input, $cookie, $template->output;
148 my ($line, $column, $filters) = @_;
157 my $dbh = C4::Context->dbh;
163 for (my $i=0;$i<=6;$i++) {
165 if ( @$filters[$i] ) {
166 if (($i==1) and (@$filters[$i-1])) {
167 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
169 $cell{filter} .= @$filters[$i];
170 $cell{crit} .="Issue From" if ($i==0);
171 $cell{crit} .="Issue To" if ($i==1);
172 $cell{crit} .="Return From" if ($i==2);
173 $cell{crit} .="Return To" if ($i==3);
174 $cell{crit} .="Branch" if ($i==4);
175 $cell{crit} .="Doc Type" if ($i==5);
176 $cell{crit} .="Bor Cat" if ($i==6);
177 $cell{crit} .="Day" if ($i==7);
178 $cell{crit} .="Month" if ($i==8);
179 $cell{crit} .="Year" if ($i==9);
180 push @loopfilter, \%cell;
186 $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
187 $column = "biblioitems.".$column if $column=~/itemtype/;
188 $column = "borrowers.".$column if $column=~/categorycode/;
190 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
191 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
192 $colfilter[0] = @$filters[2] if ($column =~ /returndate/ ) ;
193 $colfilter[1] = @$filters[3] if ($column =~ /returndate/ ) ;
194 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
195 $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ ) ;
196 $colfilter[0] = @$filters[6] if ($column =~ /category/ ) ;
197 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
198 $colfilter[0] = @$filters[7] if ($column =~ /timestamp/ ) ;
199 $colfilter[0] = @$filters[8] if ($column =~ /timestamp/ ) ;
200 $colfilter[0] = @$filters[9] if ($column =~ /timestamp/ ) ;
201 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
204 if ($column eq "Day") {
206 $column = "issues.timestamp";
207 $colfield .="dayname($column)";
208 $colorder .="weekday($column)";
209 } elsif ($column eq "Month") {
211 $column = "issues.timestamp";
212 $colfield .="monthname($column)";
213 $colorder .="month($column)";
214 } elsif ($column eq "Year") {
216 $column = "issues.timestamp";
217 $colfield .="Year($column)";
218 $colorder .= $column;
220 $colfield .= $column;
221 $colorder .= $column;
225 $strsth2 .= "select distinctrow $colfield FROM `issues`,borrowers,biblioitems LEFT JOIN items ON (biblioitems.biblioitemnumber=items.biblioitemnumber) WHERE issues.itemnumber=items.itemnumber AND issues.borrowernumber=borrowers.borrowernumber and returndate is not null";
226 if (($column=~/timestamp/) or ($column=~/returndate/)){
227 if ($colfilter[1] and ($colfilter[0])){
228 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
229 } elsif ($colfilter[1]) {
230 $strsth2 .= " and $column < '$colfilter[1]' " ;
231 } elsif ($colfilter[0]) {
232 $strsth2 .= " and $column > '$colfilter[0]' " ;
234 } elsif ($colfilter[0]) {
235 $colfilter[0] =~ s/\*/%/g;
236 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
238 $strsth2 .=" group by $colfield";
239 $strsth2 .=" order by $colorder";
242 my $sth2 = $dbh->prepare( $strsth2 );
243 if (( @colfilter ) and ($colfilter[1])){
244 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
245 } elsif ($colfilter[0]) {
246 $sth2->execute($colfilter[0]);
252 while (my ($celvalue) = $sth2->fetchrow) {
255 # warn "coltitle :".$celvalue;
256 $cell{coltitle} = $celvalue;
258 push @loopcol, \%cell;
260 # warn "fin des titres colonnes";
267 #Initialization of cell values.....
271 for (my $i=1;$i<=$line;$i++) {
272 foreach my $col ( @loopcol ) {
273 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
274 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"total"}=0;
279 # preparing calculation
282 # Processing average loanperiods
283 $strcalc .= "SELECT CONCAT( biblioitems.biblioitemnumber) , COUNT(*) AS RANK";
284 $strcalc .= " , $colfield " if ($colfield);
285 $strcalc .= " FROM `issues`,borrowers,biblioitems LEFT JOIN items ON (biblioitems.biblioitemnumber=items.biblioitemnumber) LEFT JOIN issuingrules ON (issuingrules.branchcode=issues.branchcode AND issuingrules.itemtype=biblioitems.itemtype AND issuingrules.categorycode=borrowers.categorycode) WHERE issues.itemnumber=items.itemnumber AND issues.borrowernumber=borrowers.borrowernumber and returndate is not null";
287 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
288 $strcalc .= " AND issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
289 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
290 $strcalc .= " AND issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
291 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
292 $strcalc .= " AND issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
293 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
294 $strcalc .= " AND issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
295 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
296 $strcalc .= " AND issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
297 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
298 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[5] ."'" if ( @$filters[5] );
299 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
300 $strcalc .= " AND borrowers.categorycode like '" . @$filters[6] ."'" if ( @$filters[6] );
301 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
302 $strcalc .= " AND dayname(issues.timestamp) like '" . @$filters[7]."'" if (@$filters[7]);
303 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
304 $strcalc .= " AND monthname(issues.timestamp) like '" . @$filters[8]."'" if (@$filters[8]);
305 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
306 $strcalc .= " AND year(issues.timestamp) like '" . @$filters[9] ."'" if ( @$filters[9] );
308 $strcalc .= " group by borrowers.borrowernumber";
309 $strcalc .= ", $colfield" if ($column);
310 $strcalc .= " order by ";
311 $strcalc .= "$colfield, " if ($colfield);
312 $strcalc .= "RANK DESC ";
315 $max = $line*@loopcol;
316 } else { $max=$line;}
317 $strcalc .= " LIMIT 0,$max";
318 warn "SQL :". $strcalc;
320 my $dbcalc = $dbh->prepare($strcalc);
322 # warn "filling table";
325 while (my @data = $dbcalc->fetchrow) {
326 my ($row, $rank, $col )=@data;
327 # warn "filling table $row / $col / $issuedate / $returndate /$weight";
328 $col = "zzEMPTY" if ($col eq undef);
329 $i=1 if (($previous_col) and not($col eq $previous_col));
330 $table[$i]->{$col}=$row;
331 # warn " ".$i." ".$col. " ".$row;
334 # $table{$row}->{totalrow}+=$weight*$loanlength;
337 push @loopcol,{coltitle => "Global"} if not ($column);
339 for ($i=1; $i<=$line;$i++) {
342 #@loopcol ensures the order for columns is common with column titles
343 # and the number matches the number of columns
345 foreach my $col ( @loopcol ) {
348 #warn " test ".(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle};
349 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}};
351 $value =$table[$i]->{"zzEMPTY"};
353 # $table{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}} = $value;
354 #$table{$row}->{totalrow}+=$value;
355 #warn "row : $row col:$col $cnttable{$row}->{(($col->{coltitle} eq \"NULL\")or ($col->{coltitle} eq \"\"))?\"zzEMPTY\":$col->{coltitle}}";
356 # $colcount+=$cnttable{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}};
357 push @loopcell, {value => $value} ;
359 #warn "row : $row colcount:$colcount";
360 #my $total = $table[$i]->{totalrow}/$colcount if ($colcount>0);
361 push @looprow,{ 'rowtitle' => $i ,
362 'loopcell' => \@loopcell,
363 'hilighted' => ($hilighted >0),
364 #'totalrow' => ($total)?sprintf("%.2f",$total):0
366 $hilighted = -$hilighted;
371 # the header of the table
372 $globalline{loopfilter}=\@loopfilter;
373 # the core of the table
374 $globalline{looprow} = \@looprow;
375 $globalline{loopcol} = \@loopcol;
376 # # the foot (totals by borrower type)
377 $globalline{loopfooter} = \@loopfooter;
378 $globalline{total}= $grantotal;
379 $globalline{line} = $line;
380 $globalline{column} = $column;
381 push @mainloop,\%globalline;