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
26 use C4::Branch; # GetBranches
35 plugin that shows a stats on borrowers
45 my $do_it=$input->param('do_it');
46 my $fullreportname = "reports/cat_issues_top.tmpl";
47 my $limit = $input->param("Limit");
48 my $column = $input->param("Criteria");
49 my @filters = $input->param("Filter");
50 my $output = $input->param("output");
51 my $basename = $input->param("basename");
52 my $mime = $input->param("MIME");
53 my $del = $input->param("sep");
54 #warn "calcul : ".$calc;
55 my ($template, $borrowernumber, $cookie)
56 = get_template_and_user({template_name => $fullreportname,
60 flagsrequired => { reports => 1},
63 $template->param(do_it => $do_it,
64 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
65 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
66 IntranetNav => C4::Context->preference("IntranetNav"),
70 my $results = calculate($limit, $column, \@filters);
71 if ($output eq "screen"){
72 # Printing results to screen
73 $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 @$results[0]->{line} ."/". @$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 $branches = GetBranches;
143 foreach my $thisbranch (keys %$branches) {
144 # my $selected = 1 if $thisbranch eq $branch;
145 my %row =(value => $thisbranch,
146 # selected => $selected,
147 branchname => $branches->{$thisbranch}->{'branchname'},
149 push @branchloop, \%row;
153 my $itemtypes = GetItemTypes;
155 foreach my $thisitemtype (keys %$itemtypes) {
156 # my $selected = 1 if $thisbranch eq $branch;
157 my %row =(value => $thisitemtype,
158 # selected => $selected,
159 description => $itemtypes->{$thisitemtype}->{'description'},
161 push @itemtypeloop, \%row;
165 my ($codes,$labels) = GetborCatFromCatType(undef,undef);
167 foreach my $thisborcat (sort keys %$labels) {
168 # my $selected = 1 if $thisbranch eq $branch;
169 my %row =(value => $thisborcat,
170 # selected => $selected,
171 description => $labels->{$thisborcat},
173 push @borcatloop, \%row;
179 CGIextChoice => $CGIextChoice,
180 CGIsepChoice => $CGIsepChoice,
181 branchloop =>\@branchloop,
182 itemtypeloop =>\@itemtypeloop,
183 borcatloop =>\@borcatloop,
185 output_html_with_http_headers $input, $cookie, $template->output;
192 my ($line, $column, $filters) = @_;
201 my $dbh = C4::Context->dbh;
207 for (my $i=0;$i<=6;$i++) {
209 if ( @$filters[$i] ) {
210 if (($i==1) and (@$filters[$i-1])) {
211 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
213 $cell{filter} .= @$filters[$i];
214 $cell{crit} .="Issue From" if ($i==0);
215 $cell{crit} .="Issue To" if ($i==1);
216 $cell{crit} .="Return From" if ($i==2);
217 $cell{crit} .="Return To" if ($i==3);
218 $cell{crit} .="Branch" if ($i==4);
219 $cell{crit} .="Doc Type" if ($i==5);
220 $cell{crit} .="Bor Cat" if ($i==6);
221 $cell{crit} .="Day" if ($i==7);
222 $cell{crit} .="Month" if ($i==8);
223 $cell{crit} .="Year" if ($i==9);
224 push @loopfilter, \%cell;
230 $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
231 $column = "biblioitems.".$column if $column=~/itemtype/;
232 $column = "borrowers.".$column if $column=~/categorycode/;
234 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
235 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
236 $colfilter[0] = @$filters[2] if ($column =~ /returndate/ ) ;
237 $colfilter[1] = @$filters[3] if ($column =~ /returndate/ ) ;
238 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
239 $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ ) ;
240 $colfilter[0] = @$filters[6] if ($column =~ /category/ ) ;
241 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
242 $colfilter[0] = @$filters[7] if ($column =~ /timestamp/ ) ;
243 $colfilter[0] = @$filters[8] if ($column =~ /timestamp/ ) ;
244 $colfilter[0] = @$filters[9] if ($column =~ /timestamp/ ) ;
245 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
248 if ($column eq "Day") {
250 $column = "issues.timestamp";
251 $colfield .="dayname($column)";
252 $colorder .="weekday($column)";
253 } elsif ($column eq "Month") {
255 $column = "issues.timestamp";
256 $colfield .="monthname($column)";
257 $colorder .="month($column)";
258 } elsif ($column eq "Year") {
260 $column = "issues.timestamp";
261 $colfield .="Year($column)";
262 $colorder .= $column;
264 $colfield .= $column;
265 $colorder .= $column;
269 $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";
270 if (($column=~/timestamp/) or ($column=~/returndate/)){
271 if ($colfilter[1] and ($colfilter[0])){
272 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
273 } elsif ($colfilter[1]) {
274 $strsth2 .= " and $column < '$colfilter[1]' " ;
275 } elsif ($colfilter[0]) {
276 $strsth2 .= " and $column > '$colfilter[0]' " ;
278 } elsif ($colfilter[0]) {
279 $colfilter[0] =~ s/\*/%/g;
280 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
282 $strsth2 .=" group by $colfield";
283 $strsth2 .=" order by $colorder";
286 my $sth2 = $dbh->prepare( $strsth2 );
287 if (( @colfilter ) and ($colfilter[1])){
288 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
289 } elsif ($colfilter[0]) {
290 $sth2->execute($colfilter[0]);
296 while (my ($celvalue) = $sth2->fetchrow) {
298 $cell{coltitle} = ($celvalue?$celvalue:"NULL");
299 push @loopcol, \%cell;
301 # warn "fin des titres colonnes";
308 #Initialization of cell values.....
312 for (my $i=1;$i<=$line;$i++) {
313 foreach my $col ( @loopcol ) {
314 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
315 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"total"}->{'name'}=0;
320 # preparing calculation
323 # Processing average loanperiods
324 $strcalc .= "SELECT DISTINCT biblio.title, COUNT(biblio.biblionumber) AS RANK, biblio.biblionumber AS ID";
325 $strcalc .= " , $colfield " if ($colfield);
326 $strcalc .= " FROM `issues`,borrowers,(items LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber) LEFT JOIN biblio ON (biblio.biblionumber=items.biblionumber) WHERE issues.itemnumber=items.itemnumber AND issues.borrowernumber=borrowers.borrowernumber and returndate is not null";
328 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
329 $strcalc .= " AND issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
330 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
331 $strcalc .= " AND issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
332 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
333 $strcalc .= " AND issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
334 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
335 $strcalc .= " AND issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
336 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
337 $strcalc .= " AND issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
338 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
339 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[5] ."'" if ( @$filters[5] );
340 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
341 $strcalc .= " AND borrowers.categorycode like '" . @$filters[6] ."'" if ( @$filters[6] );
342 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
343 $strcalc .= " AND dayname(issues.timestamp) like '" . @$filters[7]."'" if (@$filters[7]);
344 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
345 $strcalc .= " AND monthname(issues.timestamp) like '" . @$filters[8]."'" if (@$filters[8]);
346 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
347 $strcalc .= " AND year(issues.timestamp) like '" . @$filters[9] ."'" if ( @$filters[9] );
349 $strcalc .= " group by biblio.biblionumber";
350 $strcalc .= ", $colfield" if ($column);
351 $strcalc .= " order by RANK DESC";
352 $strcalc .= ", $colfield " if ($colfield);
355 # $max = $line*@loopcol;
356 # } else { $max=$line;}
357 # $strcalc .= " LIMIT 0,$max";
358 warn "SQL :". $strcalc;
360 my $dbcalc = $dbh->prepare($strcalc);
362 # warn "filling table";
365 while (my @data = $dbcalc->fetchrow) {
366 my ($row, $rank, $id, $col )=@data;
367 $col = "zzEMPTY" if ($col eq undef);
368 $indice{$col}=1 if (not($indice{$col}));
369 $table[$indice{$col}]->{$col}->{'name'}=$row;
370 $table[$indice{$col}]->{$col}->{'count'}=$rank;
371 $table[$indice{$col}]->{$col}->{'link'}=$id;
372 # warn " ".$i." ".$col. " ".$row;
376 push @loopcol,{coltitle => "Global"} if not($column);
378 for ($i=1; $i<=$line;$i++) {
381 #@loopcol ensures the order for columns is common with column titles
382 # and the number matches the number of columns
384 foreach my $col ( @loopcol ) {
385 # warn " colonne :$col->{coltitle}";
390 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'name'};
391 $count =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'count'};
392 $link =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'link'};
394 $value =$table[$i]->{"zzEMPTY"}->{'name'};
395 $count =$table[$i]->{"zzEMPTY"}->{'count'};
396 $link =$table[$i]->{"zzEMPTY"}->{'link'};
398 # warn " ".$i ." value:$value count:$count reference:$link";
399 push @loopcell, {value => $value, count =>$count, reference => $link} ;
401 #warn "row : $row colcount:$colcount";
402 #my $total = $table[$i]->{totalrow}/$colcount if ($colcount>0);
403 push @looprow,{ 'rowtitle' => $i ,
404 'loopcell' => \@loopcell,
405 'hilighted' => ($hilighted >0),
406 #'totalrow' => ($total)?sprintf("%.2f",$total):0
408 $hilighted = -$hilighted;
413 # the header of the table
414 $globalline{loopfilter}=\@loopfilter;
415 # the core of the table
416 $globalline{looprow} = \@looprow;
417 $globalline{loopcol} = \@loopcol;
418 # # the foot (totals by borrower type)
419 $globalline{loopfooter} = \@loopfooter;
420 $globalline{total}= $grantotal;
421 $globalline{line} = $line;
422 $globalline{column} = $column;
423 push @mainloop,\%globalline;