sub getbranches renamed to GetBranches according to the coding guidelines
[koha_gimpoz] / reports / issues_stats.pl
index 3b153d9..c75e342 100755 (executable)
@@ -42,6 +42,8 @@ plugin that shows a stats on borrowers
 
 =cut
 
+
+
 my $input = new CGI;
 my $do_it=$input->param('do_it');
 my $fullreportname = "reports/issues_stats.tmpl";
@@ -66,7 +68,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($line, $column, $podsp, $type, $daysel, $monthsel, $calc, \@filters);
@@ -159,7 +165,7 @@ if ($do_it) {
                $hassort1 =1 if ($value);
                push @select, $value;
        }
-       my $branches=getbranches();
+       my $branches=GetBranches();
        my @select_branch;
        my %select_branches;
        push @select_branch,"";
@@ -272,7 +278,7 @@ sub calculate {
        push @loopfilter,{crit=>"Issue|Return ",filter=>$type};
        push @loopfilter,{crit=>"Display by ",filter=>$dsp} if ($dsp);
        push @loopfilter,{crit=>"Select Day ",filter=>$daysel} if ($daysel);
-       push @loopfilter,{crit=>"Select Month ",filter=>$daysel} if ($monthsel);
+       push @loopfilter,{crit=>"Select Month ",filter=>$monthsel} if ($monthsel);
        
        
        my @linefilter;
@@ -317,7 +323,11 @@ sub calculate {
        } else {
                $linefield .= $line;
        }  
-       
+       my $lineorder = $linefield;
+       $lineorder = "weekday($line)" if $linefield =~ /dayname/;
+       $lineorder = "month($line)" if $linefield =~ "^month";
+       $lineorder = $linefield if (not ($linefield =~ "^month") and not($linefield =~ /dayname/));
+
        my $strsth;
        $strsth .= "select distinctrow $linefield from statistics, borrowers where (statistics.borrowernumber=borrowers.borrowernumber) and $line is not null ";
        
@@ -337,8 +347,8 @@ sub calculate {
                $strsth .= " and $line LIKE ? " ;
        }
        $strsth .=" group by $linefield";
-       $strsth .=" order by $linefield";
-#      warn "". $strsth;
+       $strsth .=" order by $lineorder";
+       warn "". $strsth;
        
        my $sth = $dbh->prepare( $strsth );
        if (( @linefilter ) and ($linefilter[1])){
@@ -361,7 +371,8 @@ sub calculate {
        }
 
 # 2nd, loop cols.
-       my $colfield;                               
+       my $colfield;
+       my $colorder;                               
        if (($column =~/datetime/) and ($dsp == 1)) {
                #Display by day
                $colfield .="dayname($column)";  
@@ -376,6 +387,9 @@ sub calculate {
        } else {
                $colfield .= $column;
        }  
+       $colorder = "weekday($line)" if $colfield =~ "^dayname";
+       $colorder = "month($line)" if $colfield =~ "^month";
+       $colorder = $colfield if (not ($colfield =~ "^month") and not($colfield =~ "^dayname"));
        
        my $strsth2;
        $strsth2 .= "select distinctrow $colfield from statistics, borrowers where (statistics.borrowernumber=borrowers.borrowernumber) and $column is not null ";
@@ -396,7 +410,7 @@ sub calculate {
                $strsth2 .= " and $column LIKE ? " ;
        }
        $strsth2 .=" group by $colfield";
-       $strsth2 .=" order by $colfield";
+       $strsth2 .=" order by $colorder";
 #      warn "". $strsth2;
        
        my $sth2 = $dbh->prepare( $strsth2 );
@@ -436,203 +450,84 @@ sub calculate {
 
 # preparing calculation
        my $strcalc ;
-       if ($process ==2) {
-               $linefield=~s/datetime/issues.returndate/;
-               $linefield=~s/itemtype/biblioitems.itemtype/;
-               $colfield=~s/datetime/issues.returndate/;
-               $colfield=~s/itemtype/biblioitems.itemtype/;
-               
-       # Processing average loanperiods
-               $strcalc .= "SELECT $linefield, $colfield, ";
-               $strcalc .= " DATE_SUB(date_due, INTERVAL CAST(issuingrules.issuelength AS SIGNED INTEGER) * (CAST(issues.renewals AS SIGNED INTEGER)+1) DAY) AS issuedate, returndate, COUNT(*), date_due, issues.renewals, issuelength 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";
-#      
-               @$filters[0]=~ s/\*/%/g if (@$filters[0]);
-               $strcalc .= " AND issues.returndate > '" . @$filters[0] ."'" if ( @$filters[0] );
-               @$filters[1]=~ s/\*/%/g if (@$filters[1]);
-               $strcalc .= " AND issues.returndate < '" . @$filters[1] ."'" if ( @$filters[1] );
-               @$filters[2]=~ s/\*/%/g if (@$filters[2]);
-               $strcalc .= " AND borrowers.categorycode like '" . @$filters[2] ."'" if ( @$filters[2] );
-               @$filters[3]=~ s/\*/%/g if (@$filters[3]);
-               $strcalc .= " AND biblioitems.itemtype like '" . @$filters[3] ."'" if ( @$filters[3] );
-               @$filters[4]=~ s/\*/%/g if (@$filters[4]);
-               $strcalc .= " AND issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
-               @$filters[5]=~ s/\*/%/g if (@$filters[5]);
-               $strcalc .= " AND borrowers.sort1 like '" . @$filters[5] ."'" if ( @$filters[5] );
-               @$filters[6]=~ s/\*/%/g if (@$filters[6]);
-               $strcalc .= " AND borrowers.sort2 like '" . @$filters[6] ."'" if ( @$filters[6] );
-               $strcalc .= " AND dayname(timestamp) like '" . $daysel ."'" if ( $daysel );
-               $strcalc .= " AND monthname(timestamp) like '" . $monthsel ."'" if ( $monthsel );
-#              
-               $strcalc .= " group by issuedate, returndate, $linefield, $colfield order by $linefield,$colfield";
-               warn "SQL :". $strcalc;
-               
-               my $dbcalc = $dbh->prepare($strcalc);
-               $dbcalc->execute;
-       #       warn "filling table";
-               my $emptycol;
-               my $issues_count=0;
-               my $previous_row; 
-               my $previous_col;
-               my $loanlength; 
-               my $err;
-               my $weightrow;
-               while (my  @data = $dbcalc->fetchrow) {
-                       my ($row, $col, $issuedate, $returndate, $weight)=@data;
-                       warn "filling table $row / $col / $issuedate / $returndate /$weight";
-                       $emptycol = 1 if ($col eq undef);
-                       $col = "zzEMPTY" if ($col eq undef);
-                       $row = "zzEMPTY" if ($row eq undef);
-                       warn "row :".$row." column :".$col;
-                       if (($previous_row== $row) and ($previous_col==$col)){
-                               my @result =split /:/,DateCalc($returndate,$issuedate) ;
-#  DateCalc returns => 0:0:WK:DD:HH:MM:SS   the weeks, days, hours, minutes,
-#  and seconds between the two
-                               $loanlength = $result[2]*7+$result[3];
-                               warn "DateCalc returns :$loanlength with return ". $returndate ."issue ". $issuedate ."weight : ". $weight;
-                               
-                               $table{$row}->{$col}->{value}+=$weight*$loanlength;
-                               $issues_count+=$weight;
-                       
-                       } elsif ($previous_row==$row) {
-                               $table{$row}->{$previous_col}->{value}=$table{$row}->{$previous_col}->{value}/$issues_count;
-                               $table{$row}->{$previous_col}->{weight}=1;
-                               $table{$row}->{$previous_col}->{realweight}=$issues_count;
-                               $weightrow+=$table{$row}->{$previous_col}->{weight};
-                               $table{$row}->{totalrow}+=$table{$row}->{$previous_col}->{value};
-                               
-                               my @result =split /:/,DateCalc($returndate,$issuedate) ;
-#  DateCalc returns => 0:0:WK:DD:HH:MM:SS   the weeks, days, hours, minutes,
-#  and seconds between the two
-                               $loanlength = $result[2]*7+$result[3];
-                               $table{$row}->{$col}->{value}+=$weight*$loanlength;
-                               $issues_count=$weight;
-                               $previous_col=$col;
-                       } else {
-                               unless (($previous_row) or ($previous_col)){
-                                       $table{$previous_row}->{$previous_col}->{value}=$table{$previous_row}->{$previous_col}->{value}/$issues_count;
-                                       $table{$previous_row}->{$previous_col}->{weight}=1;
-                                       $table{$previous_row}->{$previous_col}->{realweight}=$issues_count;
-                                       $table{$previous_row}->{totalrow}+=$table{$previous_row}->{$previous_col}->{value};
-                                       $weightrow+=$table{$row}->{$previous_col}->{weight};
-                                       $table{$previous_row}->{totalrow}=$table{$previous_row}->{totalrow}/$weightrow;
-                               }
-                               my @result =split /:/,DateCalc($returndate,$issuedate) ;
-#  DateCalc returns => 0:0:WK:DD:HH:MM:SS   the weeks, days, hours, minutes,
-#  and seconds between the two
-                               $loanlength = $result[2]*7+$result[3];
-                               warn "DateCalc returns :$loanlength with return ". $returndate ."issue ". $issuedate ."weight : ". $weight;
-                               
-                               $table{$row}->{$col}->{value}=$weight*$loanlength;
-                               $issues_count=$weight;
-                               $previous_row=$row;
-                               $previous_col=$col;
-                               $weightrow=0;
-                       }
-               }
-               push @loopcol,{coltitle => "NULL"} if ($emptycol);
-               foreach my $row ( sort keys %table ) {
-                       my @loopcell;
-#              #@loopcol ensures the order for columns is common with column titles
-#              # and the number matches the number of columns
-                       foreach my $col ( @loopcol ) {
-                               my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}}->{value};
-                               push @loopcell, {value => $value  } ;
-                       }
-                       push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
-                                                       'loopcell' => \@loopcell,
-                                                       'hilighted' => ($hilighted >0),
-                                                       'totalrow' => $table{$row}->{totalrow}
-                                               };
-                       $hilighted = -$hilighted;
-               }
-#      
-# #    warn "footer processing";
-               foreach my $col ( @loopcol ) {
-                       my $total=0;
-                       my $nbrow=0;
-                       foreach my $row ( @looprow ) {
-                               $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}}->{value};
-                               $nbrow++;
-#                      warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
-                       }
-#              warn "summ for column ".$col->{coltitle}."  = ".$total;
-                       $total = $total/$nbrow if ($nbrow);
-                       push @loopfooter, {'totalcol' => $total};
-               }
-       
-       }else {
-               $strcalc .= "SELECT $linefield, $colfield, ";
-               $strcalc .= "COUNT( * ) " if ($process ==1);
-               if ($process ==3){
-                       my $rqbookcount = $dbh->prepare("SELECT count(*) FROM items");
-                       $rqbookcount->execute;
-                       my ($bookcount) = $rqbookcount->fetchrow;
-                       $strcalc .= "100*(COUNT(itemnumber))/ $bookcount " ;
-               }
-               $strcalc .= "FROM statistics,borrowers where (statistics.borrowernumber=borrowers.borrowernumber) ";
+
+       $strcalc .= "SELECT $linefield, $colfield, ";
+       $strcalc .= "COUNT( * ) " if ($process ==1);
+       if ($process ==2){
+               $strcalc .= "(COUNT(DISTINCT borrowers.borrowernumber))" ;
+       }
+       if ($process ==3){
+               $strcalc .= "(COUNT(DISTINCT issues.itemnumber))" ;
+       }
+       if ($process ==4){
+               my $rqbookcount = $dbh->prepare("SELECT count(*) FROM items");
+               $rqbookcount->execute;
+               my ($bookcount) = $rqbookcount->fetchrow;
+               $strcalc .= "100*(COUNT(DISTINCT issues.itemnumber))/ $bookcount " ;
+       }
+       $strcalc .= "FROM statistics,borrowers where (statistics.borrowernumber=borrowers.borrowernumber) ";
+
+       @$filters[0]=~ s/\*/%/g if (@$filters[0]);
+       $strcalc .= " AND statistics.datetime > '" . @$filters[0] ."'" if ( @$filters[0] );
+       @$filters[1]=~ s/\*/%/g if (@$filters[1]);
+       $strcalc .= " AND statistics.datetime < '" . @$filters[1] ."'" if ( @$filters[1] );
+       @$filters[2]=~ s/\*/%/g if (@$filters[2]);
+       $strcalc .= " AND borrowers.categorycode like '" . @$filters[2] ."'" if ( @$filters[2] );
+       @$filters[3]=~ s/\*/%/g if (@$filters[3]);
+       $strcalc .= " AND statistics.itemtype like '" . @$filters[3] ."'" if ( @$filters[3] );
+       @$filters[4]=~ s/\*/%/g if (@$filters[4]);
+       $strcalc .= " AND statistics.branch like '" . @$filters[4] ."'" if ( @$filters[4] );
+       @$filters[5]=~ s/\*/%/g if (@$filters[5]);
+       $strcalc .= " AND borrowers.sort1 like '" . @$filters[5] ."'" if ( @$filters[5] );
+       @$filters[6]=~ s/\*/%/g if (@$filters[6]);
+       $strcalc .= " AND borrowers.sort2 like '" . @$filters[6] ."'" if ( @$filters[6] );
+       $strcalc .= " AND dayname(datetime) like '" . $daysel ."'" if ( $daysel );
+       $strcalc .= " AND monthname(datetime) like '" . $monthsel ."'" if ( $monthsel );
+       $strcalc .= " AND statistics.type like '" . $type ."'" if ( $type );
        
-               @$filters[0]=~ s/\*/%/g if (@$filters[0]);
-               $strcalc .= " AND statistics.datetime > '" . @$filters[0] ."'" if ( @$filters[0] );
-               @$filters[1]=~ s/\*/%/g if (@$filters[1]);
-               $strcalc .= " AND statistics.datetime < '" . @$filters[1] ."'" if ( @$filters[1] );
-               @$filters[2]=~ s/\*/%/g if (@$filters[2]);
-               $strcalc .= " AND borrowers.categorycode like '" . @$filters[2] ."'" if ( @$filters[2] );
-               @$filters[3]=~ s/\*/%/g if (@$filters[3]);
-               $strcalc .= " AND statistics.itemtype like '" . @$filters[3] ."'" if ( @$filters[3] );
-               @$filters[4]=~ s/\*/%/g if (@$filters[4]);
-               $strcalc .= " AND statistics.branch like '" . @$filters[4] ."'" if ( @$filters[4] );
-               @$filters[5]=~ s/\*/%/g if (@$filters[5]);
-               $strcalc .= " AND borrowers.sort1 like '" . @$filters[5] ."'" if ( @$filters[5] );
-               @$filters[6]=~ s/\*/%/g if (@$filters[6]);
-               $strcalc .= " AND borrowers.sort2 like '" . @$filters[6] ."'" if ( @$filters[6] );
-               $strcalc .= " AND dayname(datetime) like '" . $daysel ."'" if ( $daysel );
-               $strcalc .= " AND monthname(datetime) like '" . $monthsel ."'" if ( $monthsel );
-               $strcalc .= " AND statistics.type like '" . $type ."'" if ( $type );
+       $strcalc .= " group by $linefield, $colfield order by $lineorder,$colorder";
+       warn "". $strcalc;
+       my $dbcalc = $dbh->prepare($strcalc);
+       $dbcalc->execute;
+#      warn "filling table";
+       my $emptycol; 
+       while (my ($row, $col, $value) = $dbcalc->fetchrow) {
+               warn "filling table $row / $col / $value ";
+               $emptycol = 1 if ($col eq undef);
+               $col = "zzEMPTY" if ($col eq undef);
+               $row = "zzEMPTY" if ($row eq undef);
                
-               $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
-       #       warn "". $strcalc;
-               my $dbcalc = $dbh->prepare($strcalc);
-               $dbcalc->execute;
-       #       warn "filling table";
-               my $emptycol; 
-               while (my ($row, $col, $value) = $dbcalc->fetchrow) {
-       #               warn "filling table $row / $col / $value ";
-                       $emptycol = 1 if ($col eq undef);
-                       $col = "zzEMPTY" if ($col eq undef);
-                       $row = "zzEMPTY" if ($row eq undef);
-                       
-                       $table{$row}->{$col}+=$value;
-                       $table{$row}->{totalrow}+=$value;
-                       $grantotal += $value;
+               $table{$row}->{$col}+=$value;
+               $table{$row}->{totalrow}+=$value;
+               $grantotal += $value;
+       }
+       push @loopcol,{coltitle => "NULL"} if ($emptycol);
+
+       foreach my $row (@loopline) {
+               my @loopcell;
+               #@loopcol ensures the order for columns is common with column titles
+               # and the number matches the number of columns
+               foreach my $col ( @loopcol ) {
+                       my $value =$table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
+                       push @loopcell, {value => $value  } ;
                }
-               push @loopcol,{coltitle => "NULL"} if ($emptycol);
-               
-               foreach my $row ( sort keys %table ) {
-                       my @loopcell;
-                       #@loopcol ensures the order for columns is common with column titles
-                       # and the number matches the number of columns
-                       foreach my $col ( @loopcol ) {
-                               my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
-                               push @loopcell, {value => $value  } ;
-                       }
-                       push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
+               push @looprow,{ 'rowtitle' => ($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle},
                                                        'loopcell' => \@loopcell,
                                                        'hilighted' => ($hilighted >0),
-                                                       'totalrow' => $table{$row}->{totalrow}
+                                                       'totalrow' => $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{totalrow}
                                                };
-                       $hilighted = -$hilighted;
-               }
-               
-       #       warn "footer processing";
-               foreach my $col ( @loopcol ) {
-                       my $total=0;
-                       foreach my $row ( @looprow ) {
-                               $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
-       #                       warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
-                       }
-       #               warn "summ for column ".$col->{coltitle}."  = ".$total;
-                       push @loopfooter, {'totalcol' => $total};
+               $hilighted = -$hilighted;
+       }
+       
+#      warn "footer processing";
+       foreach my $col ( @loopcol ) {
+               my $total=0;
+               foreach my $row ( @looprow ) {
+                       $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
+#                      warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
                }
+#              warn "summ for column ".$col->{coltitle}."  = ".$total;
+               push @loopfooter, {'totalcol' => $total};
        }
                        
 
@@ -650,4 +545,4 @@ sub calculate {
        return \@mainloop;
 }
 
-1;
\ No newline at end of file
+1;