Including null values to statistics reports
authorhdl <hdl>
Wed, 9 Mar 2005 14:21:56 +0000 (14:21 +0000)
committerhdl <hdl>
Wed, 9 Mar 2005 14:21:56 +0000 (14:21 +0000)
koha-tmpl/intranet-tmpl/default/en/reports/acquisitions_stats.tmpl
koha-tmpl/intranet-tmpl/default/en/reports/borrowers_stats.tmpl
koha-tmpl/intranet-tmpl/default/en/reports/catalogue_stats.tmpl
koha-tmpl/intranet-tmpl/default/en/reports/issues_stats.tmpl
reports/acquisitions_stats.pl
reports/borrowers_stats.pl
reports/issues_stats.pl

index a9518f9..e8bad61 100644 (file)
@@ -11,7 +11,7 @@
                        <!-- /TMPL_LOOP -->
                <!-- /TMPL_IF NAME="loopfilter"-->
                <br>
-               <table>
+               <table border="1">
                        <tr >
                                <th class="reports"><!-- TMPL_VAR name="line" --> / <!-- TMPL_VAR name="column" --></th>
                                <!-- TMPL_LOOP NAME="loopcol" -->
@@ -25,7 +25,9 @@
                                                <!-- TMPL_VAR NAME="rowtitle" --></td>
                                                <!-- TMPL_LOOP NAME="loopcell" -->
                                                        <!-- TMPL_IF NAME="hilighted" --><td class="numberhilighted"><!-- TMPL_ELSE --><td class="number"><!-- /TMPL_IF -->
-                                                               <!-- TMPL_VAR NAME="value" -->
+                                                               <!-- TMPL_IF Name="value" --><!-- TMPL_VAR NAME="value" -->
+                                                               <!-- TMPL_ELSE--> &nbsp;
+                                                               <!-- /TMPL_IF -->
                                                        </td>
                                                <!-- /TMPL_LOOP -->
                                                <!-- TMPL_IF NAME="hilighted" --><td class="numberhilighted"><!-- TMPL_ELSE --><td class="number"><!-- /TMPL_IF -->
index 378c92a..6a45b88 100644 (file)
@@ -10,7 +10,7 @@
                                        <p><label> <!-- TMPL_VAR NAME="crit"--> =</label><!-- TMPL_VAR NAME="filter"--></p>
                        <!-- /TMPL_LOOP -->
                <!-- /TMPL_IF NAME="loopfilter"-->
-               <table>
+               <table border="1">
                        <tr >
                                <th class="reports"><!-- TMPL_VAR name="line" --> / <!-- TMPL_VAR name="column" --></th>
                                <!-- TMPL_LOOP NAME="loopcol" -->
                                                <!-- TMPL_VAR NAME="rowtitle" --></td>
                                                <!-- TMPL_LOOP NAME="loopcell" -->
                                                        <!-- TMPL_IF NAME="hilighted" --><td class="numberhilighted"><!-- TMPL_ELSE --><td class="number"><!-- /TMPL_IF -->
-                                                               <!-- TMPL_VAR NAME="value" -->
-                                                       </td>
+                                                               <!--TMPL_IF NAME="value" --><!-- TMPL_VAR NAME="value" -->
+                                                               <!--TMPL_ELSE-->
+                                                                       &nbsp;
+                                                               <!--/TMPL_IF-->
+                                                               </td>
                                                <!-- /TMPL_LOOP -->
                                                <!-- TMPL_IF NAME="hilighted" --><td class="numberhilighted"><!-- TMPL_ELSE --><td class="number"><!-- /TMPL_IF -->
                                                        <!-- TMPL_VAR NAME="totalrow" -->
index 307422a..a038f5e 100644 (file)
                                                <!-- TMPL_IF NAME="hilighted" --><td class="hilighted"><!-- TMPL_ELSE --><td><!-- /TMPL_IF -->
                                                <!-- TMPL_VAR NAME="rowtitle" --></td>
                                                <!-- TMPL_LOOP NAME="loopcell" -->
-                                                       <!--TMPL_IF NAME="value" -->
-                                                               <!-- TMPL_IF NAME="hilighted" --><td class="numberhilighted"><!-- TMPL_ELSE --><td class="number"><!-- /TMPL_IF -->
-                                                                       <!-- TMPL_VAR NAME="value" -->
-                                                               </td>
-                                                       <!--TMPL_ELSE-->
-                                                               <!-- TMPL_IF NAME="hilighted" --><td class="numberhilighted"><!-- TMPL_ELSE --><td class="number"><!-- /TMPL_IF -->
-                                                                       &nbsp;
-                                                               </td>
-                                                       <!--/TMPL_IF-->
+                                                       <!-- TMPL_IF NAME="hilighted" --><td class="numberhilighted"><!-- TMPL_ELSE --><td class="number"><!-- /TMPL_IF -->
+                                                               <!--TMPL_IF NAME="value" --><!-- TMPL_VAR NAME="value" -->
+                                                               <!--TMPL_ELSE-->&nbsp;
+                                                               <!--/TMPL_IF-->
+                                                       </td>
                                                <!-- /TMPL_LOOP -->
                                                <!-- TMPL_IF NAME="hilighted" --><td class="numberhilighted"><!-- TMPL_ELSE --><td class="number"><!-- /TMPL_IF -->
                                                        <!-- TMPL_VAR NAME="totalrow" -->
index db8c10f..e6acdf5 100644 (file)
@@ -11,7 +11,7 @@
                        <!-- /TMPL_LOOP -->
                <!-- /TMPL_IF NAME="loopfilter"-->
                <br>
-               <table>
+               <table border="1"> 
                        <tr >
                                <th class="reports"><!-- TMPL_VAR name="line" --> / <!-- TMPL_VAR name="column" --></th>
                                <!-- TMPL_LOOP NAME="loopcol" -->
@@ -25,7 +25,9 @@
                                                <!-- TMPL_VAR NAME="rowtitle" --></td>
                                                <!-- TMPL_LOOP NAME="loopcell" -->
                                                        <!-- TMPL_IF NAME="hilighted" --><td class="numberhilighted"><!-- TMPL_ELSE --><td class="number"><!-- /TMPL_IF -->
-                                                               <!-- TMPL_VAR NAME="value" -->
+                                                               <!-- TMPL_IF NAME="value" --><!-- TMPL_VAR NAME="value" -->
+                                                               <!-- TMPL_ELSE --> &nbsp;
+                                                               <!-- /TMPL_IF -->
                                                        </td>
                                                <!-- /TMPL_LOOP -->
                                                <!-- TMPL_IF NAME="hilighted" --><td class="numberhilighted"><!-- TMPL_ELSE --><td class="number"><!-- /TMPL_IF -->
index a54ff87..abb8f18 100755 (executable)
@@ -328,8 +328,8 @@ sub calculate {
                my %cell;
                if ($celvalue) {
                        $cell{rowtitle} = $celvalue;
-               } else {
-                       $cell{rowtitle} = "";
+#              } else {
+#                      $cell{rowtitle} = "";
                }
                $cell{totalrow} = 0;
                push @loopline, \%cell;
@@ -392,10 +392,10 @@ sub calculate {
 
        while (my ($celvalue) = $sth2->fetchrow) {
                my %cell;
-               my %ft;
+               if ($celvalue){
 #              warn "coltitle :".$celvalue;
-               $cell{coltitle} = $celvalue;
-               $ft{totalcol} = 0;
+                       $cell{coltitle} = $celvalue;
+               }
                push @loopcol, \%cell;
        }
 #      warn "fin des titres colonnes";
@@ -420,7 +420,7 @@ sub calculate {
        $strcalc .= "SELECT $linefield, $colfield, ";
        $strcalc .= "COUNT( aqorders.ordernumber ) " if ($process ==1);
        $strcalc .= "SUM( aqorders.quantity * aqorders.listprice ) " if ($process ==2);
-       $strcalc .= "FROM aqorders, aqbasket,aqorderbreakdown left join aqorderdelivery on (aqorders.ordernumber =aqorderdelivery.ordernumber ) left join aqbooksellers on (aqbasket.booksellerid=aqbooksellers.id) where (aqorders.basketno=aqbasket.basketno) and (aqorderbreakdown.ordernumber=aqorders.ordernumber) and $column is not null and $line is not null ";
+       $strcalc .= "FROM aqorders, aqbasket,aqorderbreakdown left join aqorderdelivery on (aqorders.ordernumber =aqorderdelivery.ordernumber ) left join aqbooksellers on (aqbasket.booksellerid=aqbooksellers.id) where (aqorders.basketno=aqbasket.basketno) and (aqorderbreakdown.ordernumber=aqorders.ordernumber) ";
 
        @$filters[0]=~ s/\*/%/g if (@$filters[0]);
        $strcalc .= " AND aqbasket.closedate > '" . @$filters[0] ."'" if ( @$filters[0] );
@@ -442,36 +442,46 @@ sub calculate {
        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 ";
-               $table{$row}->{$col}=$value;
+#              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;
        }
+
+       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 ) {
-                       push @loopcell, {value => $table{$row}->{$col->{coltitle}}} ;
+                       my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
+                       push @loopcell, {value => $value  } ;
                }
-               push @looprow,{ 'rowtitle' => $row,
+               push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
                                                'loopcell' => \@loopcell,
-                                               'hilighted' => ,
+                                               'hilighted' => ($hilighted >0),
                                                'totalrow' => $table{$row}->{totalrow}
                                        };
                $hilighted = -$hilighted;
        }
        
-#      warn "footer processing";
+#      warn "footer processing";
        foreach my $col ( @loopcol ) {
                my $total=0;
                foreach my $row ( @looprow ) {
-                       $total += $table{$row->{rowtitle}}->{$col->{coltitle}};
-#                      warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
+                       $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;
+#              warn "summ for column ".$col->{coltitle}."  = ".$total;
                push @loopfooter, {'totalcol' => $total};
        }
                        
index 9fce669..1d05600 100755 (executable)
@@ -261,8 +261,8 @@ sub calculate {
                my %cell;
                if ($celvalue) {
                        $cell{rowtitle} = $celvalue;
-               } else {
-                       $cell{rowtitle} = "";
+#              } else {
+#                      $cell{rowtitle} = "";
                }
                $cell{totalrow} = 0;
                push @loopline, \%cell;
@@ -293,8 +293,9 @@ sub calculate {
        while (my ($celvalue) = $sth2->fetchrow) {
                my %cell;
                my %ft;
-               $cell{coltitle} = $celvalue;
-               $ft{totalcol} = 0;
+               if ($celvalue) {
+                       $cell{coltitle} = $celvalue;
+               }
                push @loopcol, \%cell;
        }
        
@@ -315,7 +316,7 @@ sub calculate {
        }
 
 # preparing calculation
-       my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM borrowers WHERE $line is not null AND $column is not null";
+       my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM borrowers WHERE ";
        @$filters[0]=~ s/\*/%/g if (@$filters[0]);
        $strcalc .= " AND categorycode like '" . @$filters[0] ."'" if ( @$filters[0] );
        @$filters[1]=~ s/\*/%/g if (@$filters[1]);
@@ -330,22 +331,32 @@ sub calculate {
        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 ";
-               $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;
        }
        
-       foreach my $row ( keys %table ) {
+       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 ) {
-                       push @loopcell, {value => $table{$row}->{$col->{coltitle}}} ;
+                       my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
+                       push @loopcell, {value => $value  } ;
                }
-               push @looprow,{ 'rowtitle' => $row,
+               push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
                                                'loopcell' => \@loopcell,
-                                               'hilighted' => ,
+                                               'hilighted' => ($hilighted >0),
                                                'totalrow' => $table{$row}->{totalrow}
                                        };
                $hilighted = -$hilighted;
@@ -353,9 +364,11 @@ sub calculate {
        
        foreach my $col ( @loopcol ) {
                my $total=0;
-               foreach my $row ( @loopline ) {
-                       $total += $table{$row->{rowtitle}}->{$col->{coltitle}};
+               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};
        }
                        
index 5da297a..b9fe363 100755 (executable)
@@ -29,6 +29,7 @@ use C4::Output;
 use C4::Koha;
 use C4::Interface::CGI::Output;
 use C4::Circulation::Circ2;
+use Date::Manip;
 
 =head1 NAME
 
@@ -435,68 +436,146 @@ sub calculate {
 
 # preparing calculation
        my $strcalc ;
-       $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 " ;
+       if ($process ==2) {
+       # Processing average loanperiods
+               $strcalc .= "SELECT $linefield, $colfield, ";
+               $strcalc .= " DATE_SUB(date_due, INTERVAL CAST(issues.renewals 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 ";
+       
+               @$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";
+               
+               my $dbcalc = $dbh->prepare($strcalc);
+               $dbcalc->execute;
+       #       warn "filling table";
+               my $emptycol;
+               my $issues_count; 
+               while (my ($row, $col, $issuedate, $returndate) = $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;
+               }
        }
-       $strcalc .= "FROM statistics,borrowers where (statistics.borrowernumber=borrowers.borrowernumber) and $column is not null and $line is not null ";
-
-       @$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 );
+       push @loopcol,{coltitle => "NULL"} if ($emptycol);
        
-       $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
-#      warn "". $strcalc;
-       my $dbcalc = $dbh->prepare($strcalc);
-       $dbcalc->execute;
-#      warn "filling table";
-       while (my ($row, $col, $value) = $dbcalc->fetchrow) {
-#              warn "filling table $row / $col / $value ";
-               $table{$row}->{$col}=$value;
-               $table{$row}->{totalrow}+=$value;
-               $grantotal += $value;
+       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,
+                                               'loopcell' => \@loopcell,
+                                               'hilighted' => ($hilighted >0),
+                                               'totalrow' => $table{$row}->{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};
+       
+       }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) ";
+       
+               @$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 $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;
+               }
        }
+       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 ) {
-                       push @loopcell, {value => $table{$row}->{$col->{coltitle}}} ;
+                       my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
+                       push @loopcell, {value => $value  } ;
                }
-               push @looprow,{ 'rowtitle' => $row,
+               push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
                                                'loopcell' => \@loopcell,
-                                               'hilighted' => ,
+                                               'hilighted' => ($hilighted >0),
                                                'totalrow' => $table{$row}->{totalrow}
                                        };
                $hilighted = -$hilighted;
        }
        
-#      warn "footer processing";
+#      warn "footer processing";
        foreach my $col ( @loopcol ) {
                my $total=0;
                foreach my $row ( @looprow ) {
-                       $total += $table{$row->{rowtitle}}->{$col->{coltitle}};
-#                      warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
+                       $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;
+#              warn "summ for column ".$col->{coltitle}."  = ".$total;
                push @loopfooter, {'totalcol' => $total};
        }