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
44 my $do_it=$input->param('do_it');
45 my $fullreportname = "reports/cat_issues_top.tmpl";
46 my $limit = $input->param("Limit");
47 my $column = $input->param("Criteria");
48 my @filters = $input->param("Filter");
49 my $output = $input->param("output");
50 my $basename = $input->param("basename");
51 my $mime = $input->param("MIME");
52 my $del = $input->param("sep");
53 #warn "calcul : ".$calc;
54 my ($template, $borrowernumber, $cookie)
55 = get_template_and_user({template_name => $fullreportname,
59 flagsrequired => { reports => 1},
62 $template->param(do_it => $do_it,
63 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
64 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
65 IntranetNav => C4::Context->preference("IntranetNav"),
69 my $results = calculate($limit, $column, \@filters);
70 if ($output eq "screen"){
71 # Printing results to screen
72 $template->param(mainloop => $results,
74 output_html_with_http_headers $input, $cookie, $template->output;
77 # Printing to a csv file
78 print $input->header(-type => 'application/vnd.sun.xml.calc',
80 -attachment=>"$basename.csv",
81 -filename=>"$basename.csv" );
82 my $cols = @$results[0]->{loopcol};
83 my $lines = @$results[0]->{looprow};
85 $sep =C4::Context->preference("delimiter");
87 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
89 foreach my $col ( @$cols ) {
90 print $col->{coltitle}.$sep;
94 foreach my $line ( @$lines ) {
95 my $x = $line->{loopcell};
96 print $line->{rowtitle}.$sep;
97 foreach my $cell (@$x) {
98 print $cell->{value}.$sep;
100 print $line->{totalrow};
105 $cols = @$results[0]->{loopfooter};
106 foreach my $col ( @$cols ) {
107 print $sep.$col->{totalcol};
109 print $sep.@$results[0]->{total};
114 my $dbh = C4::Context->dbh;
120 my @mime = ( C4::Context->preference("MIME") );
121 # foreach my $mime (@mime){
125 my $CGIextChoice=CGI::scrolling_list(
132 my @dels = ( C4::Context->preference("delimiter") );
133 my $CGIsepChoice=CGI::scrolling_list(
140 my $branches = GetBranches;
142 foreach my $thisbranch (keys %$branches) {
143 # my $selected = 1 if $thisbranch eq $branch;
144 my %row =(value => $thisbranch,
145 # selected => $selected,
146 branchname => $branches->{$thisbranch}->{'branchname'},
148 push @branchloop, \%row;
152 my $itemtypes = GetItemTypes;
154 foreach my $thisitemtype (keys %$itemtypes) {
155 # my $selected = 1 if $thisbranch eq $branch;
156 my %row =(value => $thisitemtype,
157 # selected => $selected,
158 description => $itemtypes->{$thisitemtype}->{'description'},
160 push @itemtypeloop, \%row;
164 my ($codes,$labels) = GetborCatFromCatType(undef,undef);
166 foreach my $thisborcat (sort keys %$labels) {
167 # my $selected = 1 if $thisbranch eq $branch;
168 my %row =(value => $thisborcat,
169 # selected => $selected,
170 description => $labels->{$thisborcat},
172 push @borcatloop, \%row;
178 CGIextChoice => $CGIextChoice,
179 CGIsepChoice => $CGIsepChoice,
180 branchloop =>\@branchloop,
181 itemtypeloop =>\@itemtypeloop,
182 borcatloop =>\@borcatloop,
184 output_html_with_http_headers $input, $cookie, $template->output;
191 my ($line, $column, $filters) = @_;
200 my $dbh = C4::Context->dbh;
206 for (my $i=0;$i<=6;$i++) {
208 if ( @$filters[$i] ) {
209 if (($i==1) and (@$filters[$i-1])) {
210 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
212 $cell{filter} .= @$filters[$i];
213 $cell{crit} .="Issue From" if ($i==0);
214 $cell{crit} .="Issue To" if ($i==1);
215 $cell{crit} .="Return From" if ($i==2);
216 $cell{crit} .="Return To" if ($i==3);
217 $cell{crit} .="Branch" if ($i==4);
218 $cell{crit} .="Doc Type" if ($i==5);
219 $cell{crit} .="Bor Cat" if ($i==6);
220 $cell{crit} .="Day" if ($i==7);
221 $cell{crit} .="Month" if ($i==8);
222 $cell{crit} .="Year" if ($i==9);
223 push @loopfilter, \%cell;
229 $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
230 $column = "biblioitems.".$column if $column=~/itemtype/;
231 $column = "borrowers.".$column if $column=~/categorycode/;
233 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
234 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
235 $colfilter[0] = @$filters[2] if ($column =~ /returndate/ ) ;
236 $colfilter[1] = @$filters[3] if ($column =~ /returndate/ ) ;
237 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
238 $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ ) ;
239 $colfilter[0] = @$filters[6] if ($column =~ /category/ ) ;
240 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
241 $colfilter[0] = @$filters[7] if ($column =~ /timestamp/ ) ;
242 $colfilter[0] = @$filters[8] if ($column =~ /timestamp/ ) ;
243 $colfilter[0] = @$filters[9] if ($column =~ /timestamp/ ) ;
244 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
247 if ($column eq "Day") {
249 $column = "issues.timestamp";
250 $colfield .="dayname($column)";
251 $colorder .="weekday($column)";
252 } elsif ($column eq "Month") {
254 $column = "issues.timestamp";
255 $colfield .="monthname($column)";
256 $colorder .="month($column)";
257 } elsif ($column eq "Year") {
259 $column = "issues.timestamp";
260 $colfield .="Year($column)";
261 $colorder .= $column;
263 $colfield .= $column;
264 $colorder .= $column;
268 $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";
269 if (($column=~/timestamp/) or ($column=~/returndate/)){
270 if ($colfilter[1] and ($colfilter[0])){
271 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
272 } elsif ($colfilter[1]) {
273 $strsth2 .= " and $column < '$colfilter[1]' " ;
274 } elsif ($colfilter[0]) {
275 $strsth2 .= " and $column > '$colfilter[0]' " ;
277 } elsif ($colfilter[0]) {
278 $colfilter[0] =~ s/\*/%/g;
279 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
281 $strsth2 .=" group by $colfield";
282 $strsth2 .=" order by $colorder";
285 my $sth2 = $dbh->prepare( $strsth2 );
286 if (( @colfilter ) and ($colfilter[1])){
287 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
288 } elsif ($colfilter[0]) {
289 $sth2->execute($colfilter[0]);
295 while (my ($celvalue) = $sth2->fetchrow) {
297 $cell{coltitle} = ($celvalue?$celvalue:"NULL");
298 push @loopcol, \%cell;
300 # warn "fin des titres colonnes";
307 #Initialization of cell values.....
311 for (my $i=1;$i<=$line;$i++) {
312 foreach my $col ( @loopcol ) {
313 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
314 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"total"}->{'name'}=0;
319 # preparing calculation
322 # Processing average loanperiods
323 $strcalc .= "SELECT DISTINCT biblio.title, COUNT(biblio.biblionumber) AS RANK, biblio.biblionumber AS ID";
324 $strcalc .= " , $colfield " if ($colfield);
325 $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";
327 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
328 $strcalc .= " AND issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
329 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
330 $strcalc .= " AND issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
331 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
332 $strcalc .= " AND issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
333 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
334 $strcalc .= " AND issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
335 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
336 $strcalc .= " AND issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
337 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
338 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[5] ."'" if ( @$filters[5] );
339 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
340 $strcalc .= " AND borrowers.categorycode like '" . @$filters[6] ."'" if ( @$filters[6] );
341 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
342 $strcalc .= " AND dayname(issues.timestamp) like '" . @$filters[7]."'" if (@$filters[7]);
343 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
344 $strcalc .= " AND monthname(issues.timestamp) like '" . @$filters[8]."'" if (@$filters[8]);
345 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
346 $strcalc .= " AND year(issues.timestamp) like '" . @$filters[9] ."'" if ( @$filters[9] );
348 $strcalc .= " group by biblio.biblionumber";
349 $strcalc .= ", $colfield" if ($column);
350 $strcalc .= " order by RANK DESC";
351 $strcalc .= ", $colfield " if ($colfield);
354 # $max = $line*@loopcol;
355 # } else { $max=$line;}
356 # $strcalc .= " LIMIT 0,$max";
357 warn "SQL :". $strcalc;
359 my $dbcalc = $dbh->prepare($strcalc);
361 # warn "filling table";
364 while (my @data = $dbcalc->fetchrow) {
365 my ($row, $rank, $id, $col )=@data;
366 $col = "zzEMPTY" if ($col eq undef);
367 $indice{$col}=1 if (not($indice{$col}));
368 $table[$indice{$col}]->{$col}->{'name'}=$row;
369 $table[$indice{$col}]->{$col}->{'count'}=$rank;
370 $table[$indice{$col}]->{$col}->{'link'}=$id;
371 # warn " ".$i." ".$col. " ".$row;
375 push @loopcol,{coltitle => "Global"} if not($column);
377 for ($i=1; $i<=$line;$i++) {
380 #@loopcol ensures the order for columns is common with column titles
381 # and the number matches the number of columns
383 foreach my $col ( @loopcol ) {
384 # warn " colonne :$col->{coltitle}";
389 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'name'};
390 $count =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'count'};
391 $link =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'link'};
393 $value =$table[$i]->{"zzEMPTY"}->{'name'};
394 $count =$table[$i]->{"zzEMPTY"}->{'count'};
395 $link =$table[$i]->{"zzEMPTY"}->{'link'};
397 # warn " ".$i ." value:$value count:$count reference:$link";
398 push @loopcell, {value => $value, count =>$count, reference => $link} ;
400 #warn "row : $row colcount:$colcount";
401 #my $total = $table[$i]->{totalrow}/$colcount if ($colcount>0);
402 push @looprow,{ 'rowtitle' => $i ,
403 'loopcell' => \@loopcell,
404 'hilighted' => ($hilighted >0),
405 #'totalrow' => ($total)?sprintf("%.2f",$total):0
407 $hilighted = -$hilighted;
412 # the header of the table
413 $globalline{loopfilter}=\@loopfilter;
414 # the core of the table
415 $globalline{looprow} = \@looprow;
416 $globalline{loopcol} = \@loopcol;
417 # # the foot (totals by borrower type)
418 $globalline{loopfooter} = \@loopfooter;
419 $globalline{total}= $grantotal;
420 $globalline{line} = $line;
421 $globalline{column} = $column;
422 push @mainloop,\%globalline;