Head & rel_2_2 merged
[koha_fer] / reports / cat_issues_top.pl
index dc956d8..6044b19 100755 (executable)
@@ -30,6 +30,7 @@ use C4::Koha;
 use C4::Interface::CGI::Output;
 use C4::Circulation::Circ2;
 use Date::Manip;
+use C4::Members;
 
 =head1 NAME
 
@@ -61,7 +62,11 @@ my ($template, $borrowernumber, $cookie)
                                flagsrequired => {editcatalogue => 1},
                                debug => 1,
                                });
-$template->param(do_it => $do_it);
+$template->param(do_it => $do_it,
+               intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
+               intranetstylesheet => C4::Context->preference("intranetstylesheet"),
+               IntranetNav => C4::Context->preference("IntranetNav"),
+               );
 if ($do_it) {
 # Displaying results
        my $results = calculate($limit, $column, \@filters);
@@ -133,10 +138,50 @@ if ($do_it) {
                                -values   => \@dels,
                                -size     => 1,
                                -multiple => 0 );
+       #branch
+       my $branches = getallbranches;
+       my @branchloop;
+       foreach my $thisbranch (keys %$branches) {
+#                      my $selected = 1 if $thisbranch eq $branch;
+                       my %row =(value => $thisbranch,
+#                                                                      selected => $selected,
+                                                                       branchname => $branches->{$thisbranch}->{'branchname'},
+                                                       );
+                       push @branchloop, \%row;
+       }
+
+       #doctype
+       my $itemtypes = getitemtypes;
+       my @itemtypeloop;
+       foreach my $thisitemtype (keys %$itemtypes) {
+#                      my $selected = 1 if $thisbranch eq $branch;
+                       my %row =(value => $thisitemtype,
+#                                                                      selected => $selected,
+                                                                       description => $itemtypes->{$thisitemtype}->{'description'},
+                                                       );
+                       push @itemtypeloop, \%row;
+       }
+       
+       #borcat
+       my ($codes,$labels) = borrowercategories;
+       my @borcatloop;
+       foreach my $thisborcat (sort keys %$labels) {
+ #                     my $selected = 1 if $thisbranch eq $branch;
+                       my %row =(value => $thisborcat,
+ #                                                                     selected => $selected,
+                                                                       description => $labels->{$thisborcat},
+                                                       );
+                       push @borcatloop, \%row;
+       }
        
+       #Day
+       #Month
        $template->param(
                                        CGIextChoice => $CGIextChoice,
-                                       CGIsepChoice => $CGIsepChoice
+                                       CGIsepChoice => $CGIsepChoice,
+                                       branchloop =>\@branchloop,
+                                       itemtypeloop =>\@itemtypeloop,
+                                       borcatloop =>\@borcatloop,
                                        );
 output_html_with_http_headers $input, $cookie, $template->output;
 }
@@ -251,10 +296,7 @@ sub calculate {
        
                while (my ($celvalue) = $sth2->fetchrow) {
                        my %cell;
-       #               my %ft;
-       #               warn "coltitle :".$celvalue;
-                       $cell{coltitle} = $celvalue;
-       #               $ft{totalcol} = 0;
+                       $cell{coltitle} = ($celvalue?$celvalue:"NULL");
                        push @loopcol, \%cell;
                }
        #       warn "fin des titres colonnes";
@@ -280,7 +322,7 @@ sub calculate {
        my $strcalc ;
        
 # Processing average loanperiods
-       $strcalc .= "SELECT biblio.title, COUNT(*) AS RANK, biblio.biblionumber AS ID";
+       $strcalc .= "SELECT DISTINCT biblio.title, COUNT(biblio.biblionumber) AS RANK, biblio.biblionumber AS ID";
        $strcalc .= " , $colfield " if ($colfield);
        $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";
 
@@ -305,33 +347,31 @@ sub calculate {
        @$filters[9]=~ s/\*/%/g if (@$filters[9]);
        $strcalc .= " AND year(issues.timestamp) like '" . @$filters[9] ."'" if ( @$filters[9] );
        
-       $strcalc .= " group by borrowers.borrowernumber";
+       $strcalc .= " group by biblio.biblionumber";
        $strcalc .= ", $colfield" if ($column);
-       $strcalc .= " order by ";
-       $strcalc .= "$colfield, " if ($colfield);
-       $strcalc .= "RANK DESC ";
-       my $max;
-       if (@loopcol) {
-               $max = $line*@loopcol;
-       } else { $max=$line;}
-       $strcalc .= " LIMIT 0,$max";
+       $strcalc .= " order by RANK DESC";
+       $strcalc .= ", $colfield " if ($colfield);
+#      my $max;
+#      if (@loopcol) {
+#              $max = $line*@loopcol;
+#      } else { $max=$line;}
+#      $strcalc .= " LIMIT 0,$max";
        warn "SQL :". $strcalc;
        
        my $dbcalc = $dbh->prepare($strcalc);
        $dbcalc->execute;
 #      warn "filling table";
        my $previous_col;
-       my $i=1;
+       my %indice;
        while (my  @data = $dbcalc->fetchrow) {
                my ($row, $rank, $id, $col )=@data;
                $col = "zzEMPTY" if ($col eq undef);
-               $i=1 if (($previous_col) and not($col eq $previous_col));
-               $table[$i]->{$col}->{'name'}=$row;
-               $table[$i]->{$col}->{'count'}=$rank;
-               $table[$i]->{$col}->{'link'}=$id;
+               $indice{$col}=1 if (not($indice{$col}));
+               $table[$indice{$col}]->{$col}->{'name'}=$row;
+               $table[$indice{$col}]->{$col}->{'count'}=$rank;
+               $table[$indice{$col}]->{$col}->{'link'}=$id;
 #              warn " ".$i." ".$col. " ".$row;
-               $i++;
-               $previous_col=$col;
+               $indice{$col}++;
        }
        
        push @loopcol,{coltitle => "Global"} if not($column);
@@ -385,4 +425,4 @@ sub calculate {
        return \@mainloop;
 }
 
-1;
\ No newline at end of file
+1;