Moved/renamed kohastructure_pg.sql to data/Pg
[koha.git] / reports / catalogue_stats.pl
index 87344b4..35e56c8 100755 (executable)
@@ -1,6 +1,5 @@
 #!/usr/bin/perl
 
-# $Id$
 
 # Copyright 2000-2002 Katipo Communications
 #
@@ -23,12 +22,10 @@ use strict;
 use C4::Auth;
 use CGI;
 use C4::Context;
-use HTML::Template;
-use C4::Search;
+use C4::Branch; # GetBranches
 use C4::Output;
 use C4::Koha;
-use C4::Interface::CGI::Output;
-use C4::Circulation::Circ2;
+use C4::Circulation;
 
 =head1 NAME
 
@@ -36,8 +33,7 @@ plugin that shows a stats on borrowers
 
 =head1 DESCRIPTION
 
-
-=over2
+=over 2
 
 =cut
 
@@ -60,7 +56,7 @@ my ($template, $borrowernumber, $cookie)
                                query => $input,
                                type => "intranet",
                                authnotrequired => 0,
-                               flagsrequired => {editcatalogue => 1},
+                               flagsrequired => {reports => 1},
                                debug => 1,
                                });
 $template->param(do_it => $do_it);
@@ -71,7 +67,10 @@ if ($do_it) {
                output_html_with_http_headers $input, $cookie, $template->output;
                exit(1);
        } else {
-               print $input->header(-type => 'application/vnd.sun.xml.calc', -name=>"$basename.csv" );
+               print $input->header(-type => 'application/vnd.sun.xml.calc',
+                                     -encoding    => 'utf-8',
+                                                        -attachment=>"$basename.csv",
+                                                        -name=>"$basename.csv" );
                my $cols = @$results[0]->{loopcol};
                my $lines = @$results[0]->{looprow};
                my $sep;
@@ -80,7 +79,7 @@ if ($do_it) {
                foreach my $col ( @$cols ) {
                        print $col->{coltitle}.$sep;
                }
-               print "\n";
+               print "Total\n";
                foreach my $line ( @$lines ) {
                        my $x = $line->{loopcell};
                        print $line->{rowtitle}.$sep;
@@ -104,57 +103,57 @@ if ($do_it) {
        my %labels;
        my $count=0;
        my $req;
-       $req = $dbh->prepare("select distinctrow left(dewey,3) from biblioitems order by dewey");
+       $req = $dbh->prepare("select count(dewey) from biblioitems ");
        $req->execute;
        my $hasdewey;
        my @select;
-       push @select,"";
+#      push @select,"";
        while (my ($value) =$req->fetchrow) {
-               $hasdewey =1 if (($value) and (! $hasdewey));
-               $count++ if (($value) and (! $hasdewey));
-               push @select, $value;
+               $hasdewey =1 if (($value>2) and (! $hasdewey));
+               $count++ if (($value>2) and (! $hasdewey));
+#              push @select, $value;
        }
-       my $CGIdewey=CGI::scrolling_list( -name     => 'Filter',
-                               -id => 'Filter',
-                               -values   => \@select,
-                               -size     => 1,
-                               -multiple => 0 );
+#      my $CGIdewey=CGI::scrolling_list( -name     => 'Filter',
+#                              -id => 'Filter',
+#                              -values   => \@select,
+#                              -size     => 1,
+#                              -multiple => 0 );
        
-       $req = $dbh->prepare( "select distinctrow left(lccn,3) from biblioitems order by lccn");
+       $req = $dbh->prepare( "select count(lccn) from biblioitems ");
        $req->execute;
-       undef @select;
-       push @select,"";
+#      undef @select;
+#      push @select,"";
        my $haslccn;
        my $hlghtlccn;
        while (my ($value) =$req->fetchrow) {
                $hlghtlccn = !($hasdewey);
-               $haslccn =1 if (($value) and (! $haslccn));
+               $haslccn =1 if (($value>2) and (! $haslccn));
                $count++ if (($value) and (! $haslccn));
-               push @select, $value;
+#              push @select, $value;
        }
-       my $CGIlccn=CGI::scrolling_list( -name     => 'Filter',
-                               -id => 'Filter',
-                               -values   => \@select,
-                               -size     => 1,
-                               -multiple => 0 );
+#      my $CGIlccn=CGI::scrolling_list( -name     => 'Filter',
+#                              -id => 'Filter',
+#                              -values   => \@select,
+#                              -size     => 1,
+#                              -multiple => 0 );
        
-       $req = $dbh->prepare("select distinctrow left(itemcallnumber,5) from items order by itemcallnumber");
+       $req = $dbh->prepare("select count(itemcallnumber) from items");
        $req->execute;
-       undef @select;
-       push @select,"";
+#      undef @select;
+#      push @select,"";
        my $hascote;
        my $hlghtcote;
        while (my ($value) =$req->fetchrow) {
-               $hascote =1 if (($value) and (! $hascote));
+               $hascote =1 if (($value>2) and (! $hascote));
                $count++ if (($value) and (! $hascote));
                $hlghtcote = (($hasdewey) and ($haslccn)) or (!($hasdewey) and !($haslccn));
-               push @select, $value;
+#              push @select, $value;
        }
-       my $CGIcote=CGI::scrolling_list( -name     => 'Filter',
-                               -id => 'Filter',
-                               -values   => \@select,
-                               -size     => 1,
-                               -multiple => 0 );
+#      my $CGIcote=CGI::scrolling_list( -name     => 'Filter',
+#                              -id => 'Filter',
+#                              -values   => \@select,
+#                              -size     => 1,
+#                              -multiple => 0 );
        $count++;
        my $hglghtDT =$count % 2;
 #      warn "highlightDT ".$hglghtDT;
@@ -171,18 +170,8 @@ if ($do_it) {
        my $hglghtLOC =$count % 2;
 #      warn "highlightLOC ".$hglghtLOC;
        
-#      undef @select;
-#      push @select,"";
-#      for (my $i=1950;$i<=2050;$i++) {
-#              push @select, $i;
-#      }
-#      my $CGIpublicationyear=CGI::scrolling_list( -name     => 'Filter',
-#                              -id => 'Filter',
-#                              -values   => \@select,
-#                              -size     => 1,
-#                              -multiple => 0 );
        
-       $req = $dbh->prepare("select distinctrow itemtype from biblioitems order by itemtype");
+       $req = $dbh->prepare("select itemtype from itemtypes order by itemtype");
        $req->execute;
        undef @select;
        push @select,"";
@@ -190,27 +179,27 @@ if ($do_it) {
                push @select, $value;
        }
        my $CGIitemtype=CGI::scrolling_list( -name     => 'Filter',
-                               -id => 'Filter',
+                               -id => 'itemtype',
                                -values   => \@select,
                                -size     => 1,
                                -multiple => 0 );
        
-       $req = $dbh->prepare("select distinctrow publishercode from biblioitems order by publishercode");
-       $req->execute;
-       undef @select;
-       push @select,"";
-       while (my ($value) =$req->fetchrow) {
-               push @select, $value;
-       }
-       my $CGIpublisher=CGI::scrolling_list( -name     => 'Filter',
-                               -id => 'Filter',
-                               -values   => \@select,
-                               -size     => 1,
-                               -multiple => 0 );
+#      $req = $dbh->prepare("select distinctrow left(publishercode,75) from biblioitems order by publishercode");
+#      $req->execute;
+#      undef @select;
+#      push @select,"";
+#      while (my ($value) =$req->fetchrow) {
+#              push @select, $value;
+#      }
+#      my $CGIpublisher=CGI::scrolling_list( -name     => 'Filter',
+#                              -id => 'Filter',
+#                              -values   => \@select,
+#                              -size     => 1,
+#                              -multiple => 0 );
 
        undef @select;
        push @select,"";
-       my $branches=getbranches();
+       my $branches=GetBranches();
        my %select_branches;
        $select_branches{""} = "";
        foreach my $branch (keys %$branches) {
@@ -218,7 +207,7 @@ if ($do_it) {
                $select_branches{$branch} = $branches->{$branch}->{'branchname'};
        }
        my $CGIbranch=CGI::scrolling_list( -name     => 'Filter',
-                               -id => 'Filter',
+                               -id => 'branch',
                                -values   => \@select,
                                -labels   => \%select_branches,
                                -size     => 1,
@@ -228,8 +217,11 @@ if ($do_it) {
        $req->execute;
        undef @select;
        push @select,"";
+       while (my ($value) =$req->fetchrow) {
+               push @select, $value;
+       }
        my $CGIlocation=CGI::scrolling_list( -name     => 'Filter',
-                               -id => 'Filter',
+                               -id => 'location',
                                -values   => \@select,
                                -size     => 1,
                                -multiple => 0 );
@@ -255,12 +247,12 @@ if ($do_it) {
                                -multiple => 0 );
        
        $template->param(hasdewey=>$hasdewey,
-                                       CGIFromDeweyClass => $CGIdewey,
-                                       CGIToDeweyClass => $CGIdewey,
+#                                      CGIFromDeweyClass => $CGIdewey,
+#                                      CGIToDeweyClass => $CGIdewey,
                                        haslccn=> $haslccn,
                                        hlghtlccn => $hlghtlccn,
-                                       CGIFromLoCClass => $CGIlccn,
-                                       CGIToLoCClass => $CGIlccn,
+#                                      CGIFromLoCClass => $CGIlccn,
+#                                      CGIToLoCClass => $CGIlccn,
                                        hascote=> $hascote,
                                        hlghtcote => $hlghtcote,
                                        hglghtDT => $hglghtDT,
@@ -268,12 +260,12 @@ if ($do_it) {
                                        hglghtPY => $hglghtPY,
                                        hglghtHB => $hglghtHB,
                                        hglghtLOC => $hglghtLOC,
-                                       CGIFromCoteClass => $CGIcote,
-                                       CGIToCoteClass => $CGIcote,
+#                                      CGIFromCoteClass => $CGIcote,
+#                                      CGIToCoteClass => $CGIcote,
                                        CGIItemType => $CGIitemtype,
 #                                      CGIFromPublicationYear => $CGIpublicationyear,
 #                                      CGIToPublicationYear => $CGIpublicationyear,
-                                       CGIPublisher => $CGIpublisher,
+#                                      CGIPublisher => $CGIpublisher,
                                        CGIBranch => $CGIbranch,
                                        CGILocation => $CGIlocation,
                                        CGIextChoice => $CGIextChoice,
@@ -336,8 +328,8 @@ sub calculate {
        $linefilter[1] = @$filters[3] if ($line =~ /lccn/ )  ;
        $linefilter[0] = @$filters[4] if ($line =~ /items.itemcallnumber/ )  ;
        $linefilter[1] = @$filters[5] if ($line =~ /items.itemcallnumber/ )  ;
-       @linefilter[0] = @$filters[6] if ($line =~ /itemtype/ )  ;
-       @linefilter[0] = @$filters[7] if ($line =~ /publishercode/ ) ;
+       $linefilter[0] = @$filters[6] if ($line =~ /itemtype/ )  ;
+       $linefilter[0] = @$filters[7] if ($line =~ /publishercode/ ) ;
        $linefilter[0] = @$filters[8] if ($line =~ /publicationyear/ ) ;
        $linefilter[1] = @$filters[9] if ($line =~ /publicationyear/ ) ;
        @linefilter[0] = @$filters[10] if ($line =~ /items.homebranch/ ) ;
@@ -350,8 +342,8 @@ sub calculate {
        $colfilter[1] = @$filters[3] if ($column =~ /lccn/ )  ;
        $colfilter[0] = @$filters[4] if ($column =~ /itemcallnumber/ )  ;
        $colfilter[1] = @$filters[5] if ($column =~ /itemcallnumber/ )  ;
-       @colfilter[0] = @$filters[6] if ($column =~ /itemtype/ )  ;
-       @colfilter[0] = @$filters[7] if ($column =~ /publishercode/ ) ;
+       $colfilter[0] = @$filters[6] if ($column =~ /itemtype/ )  ;
+       $colfilter[0] = @$filters[7] if ($column =~ /publishercode/ ) ;
        $colfilter[0] = @$filters[8] if ($column =~ /publicationyear/ ) ;
        $colfilter[1] = @$filters[9] if ($column =~ /publicationyear/ ) ;
        @colfilter[0] = @$filters[10] if ($column =~ /items.homebranch/ ) ;
@@ -371,7 +363,7 @@ sub calculate {
        
        
        my $strsth;
-       $strsth .= "select distinctrow $linefield from biblioitems, items where (items.biblioitemnumber = biblioitems.biblioitemnumber) and $line is not null ";
+       $strsth .= "select distinctrow $linefield from biblioitems left join items on (items.biblioitemnumber = biblioitems.biblioitemnumber) where $line is not null ";
        if ( @linefilter ) {
                if ($linefilter[1]){
                        $strsth .= " and $line >= ? " ;
@@ -382,7 +374,7 @@ sub calculate {
                }
        }
        $strsth .=" order by $linefield";
-#      warn "". $strsth;
+       warn "". $strsth;
        
        my $sth = $dbh->prepare( $strsth );
        if (( @linefilter ) and ($linefilter[1])){
@@ -396,8 +388,8 @@ sub calculate {
                my %cell;
                if ($celvalue) {
                        $cell{rowtitle} = $celvalue;
-               } else {
-                       $cell{rowtitle} = "";
+#              } else {
+#                      $cell{rowtitle} = "";
                }
                $cell{totalrow} = 0;
                push @loopline, \%cell;
@@ -416,7 +408,7 @@ sub calculate {
        }
        
        my $strsth2;
-       $strsth2 .= "select distinctrow $colfield from biblioitems, items where (items.biblioitemnumber = biblioitems.biblioitemnumber) and $column is not null ";
+       $strsth2 .= "select distinctrow $colfield from biblioitems left join items on (items.biblioitemnumber = biblioitems.biblioitemnumber) where $column is not null ";
        if (( @colfilter ) and ($colfilter[1])) {
                $strsth2 .= " and $column> ? and $column< ?";
        }elsif ($colfilter[0]){
@@ -424,7 +416,7 @@ sub calculate {
                $strsth2 .= " and $column LIKE ? ";
        } 
        $strsth2 .= " order by $colfield";
-#      warn "". $strsth2;
+       warn "". $strsth2;
        my $sth2 = $dbh->prepare( $strsth2 );
        if ((@colfilter) and ($colfilter[1])) {
                $sth2->execute($colfilter[0],$colfilter[1]);
@@ -436,7 +428,11 @@ sub calculate {
        while (my ($celvalue) = $sth2->fetchrow) {
                my %cell;
                my %ft;
-               $cell{coltitle} = $celvalue;
+               if ($celvalue) {
+                       $cell{coltitle} = $celvalue;
+#              } else {
+#                      $cell{coltitle} = "";
+               }
                $ft{totalcol} = 0;
                push @loopcol, \%cell;
        }
@@ -458,52 +454,97 @@ sub calculate {
        }
 
 # preparing calculation
-       my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM biblioitems, items WHERE (items.biblioitemnumber = biblioitems.biblioitemnumber) AND $line is not null AND $column is not null";
-       @$filters[0]=~ s/\*/%/g if (@$filters[0]);
-       $strcalc .= " AND dewey >" . @$filters[0] ."" if ( @$filters[0] );
-       @$filters[1]=~ s/\*/%/g if (@$filters[1]);
-       $strcalc .= " AND dewey <" . @$filters[1] ."" if ( @$filters[1] );
-       @$filters[2]=~ s/\*/%/g if (@$filters[2]);
-       $strcalc .= " AND lccn >" . @$filters[2] ."" if ( @$filters[2] );
-       @$filters[3]=~ s/\*/%/g if (@$filters[3]);
-       $strcalc .= " AND lccn <" . @$filters[3] ."" if ( @$filters[3] );
-       @$filters[4]=~ s/\*/%/g if (@$filters[4]);
-       $strcalc .= " AND items.itemcallnumber >" . @$filters[4] ."" if ( @$filters[4] );
-       @$filters[5]=~ s/\*/%/g if (@$filters[5]);
-       $strcalc .= " AND items.itemcallnumber <" . @$filters[5] ."" if ( @$filters[5] );
-       @$filters[6]=~ s/\*/%/g if (@$filters[6]);
-       $strcalc .= " AND biblioitems.itemtype like '" . @$filters[6] ."'" if ( @$filters[6] );
-       @$filters[7]=~ s/\*/%/g if (@$filters[7]);
-       $strcalc .= " AND biblioitems.publishercode like '" . @$filters[7] ."'" if ( @$filters[7] );
-       @$filters[8]=~ s/\*/%/g if (@$filters[8]);
-       $strcalc .= " AND publicationyear >" . @$filters[8] ."" if ( @$filters[8] );
-       @$filters[9]=~ s/\*/%/g if (@$filters[9]);
-       $strcalc .= " AND publicationyear <" . @$filters[9] ."" if ( @$filters[9] );
-       @$filters[10]=~ s/\*/%/g if (@$filters[10]);
-       $strcalc .= " AND items.homebranch like '" . @$filters[10] ."'" if ( @$filters[10] );
-       @$filters[11]=~ s/\*/%/g if (@$filters[11]);
-       $strcalc .= " AND items.location like '" . @$filters[11] ."'" if ( @$filters[11] );
+       my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM biblioitems LEFT JOIN  items ON (items.biblioitemnumber = biblioitems.biblioitemnumber) WHERE 1";
+       if (@$filters[0]){
+               @$filters[0]=~ s/\*/%/g;
+               $strcalc .= " AND dewey >" . @$filters[0] ."";
+       }
+       if (@$filters[1]){
+               @$filters[1]=~ s/\*/%/g ;
+               $strcalc .= " AND dewey <" . @$filters[1] ."";
+               
+       }
+       if (@$filters[2]){
+               @$filters[2]=~ s/\*/%/g ;
+               $strcalc .= " AND lccn >" . @$filters[2] ."" ;
+       }
+       if (@$filters[3]){
+               @$filters[3]=~ s/\*/%/g;
+               $strcalc .= " AND lccn <" . @$filters[3] ."" ;
+       }
+       if (@$filters[4]){
+               @$filters[4]=~ s/\*/%/g ;
+               $strcalc .= " AND items.itemcallnumber >=" . $dbh->quote(@$filters[4]) ."" ;
+       }
+       
+       if (@$filters[5]){
+               @$filters[5]=~ s/\*/%/g;
+               $strcalc .= " AND items.itemcallnumber <=" . $dbh->quote(@$filters[5]) ."" ;
+       }
+       
+       if (@$filters[6]){
+               @$filters[6]=~ s/\*/%/g;
+               $strcalc .= " AND biblioitems.itemtype like '" . @$filters[6] ."'";
+       }
+       
+       if (@$filters[7]){
+               @$filters[7]=~ s/\*/%/g;
+               @$filters[7].="%" unless @$filters[7]=~/%/;
+               $strcalc .= " AND biblioitems.publishercode like \"" . @$filters[7] ."\"";
+       }
+       if (@$filters[8]){
+               @$filters[8]=~ s/\*/%/g;
+               $strcalc .= " AND publicationyear >" . @$filters[8] ."" ;
+       }
+       if (@$filters[9]){
+               @$filters[9]=~ s/\*/%/g;
+               $strcalc .= " AND publicationyear <" . @$filters[9] ."";
+       }
+       if (@$filters[10]){
+               @$filters[10]=~ s/\*/%/g;
+               $strcalc .= " AND items.homebranch like '" . @$filters[10] ."'";
+       }
+       if (@$filters[11]){
+               @$filters[11]=~ s/\*/%/g;
+               $strcalc .= " AND items.location like '" . @$filters[11] ."'" if ( @$filters[11] );
+       }
+       
        $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
-#      warn "". $strcalc;
+       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;
+               $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;
        }
+
+#      my %cell = {rowtitle => 'zzROWEMPTY'};
+#      push @loopline,\%cell;
+#      undef %cell;
+#      my %cell;
+#      %cell = {coltitle => "zzEMPTY"};
+       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;
@@ -513,7 +554,7 @@ sub calculate {
        foreach my $col ( @loopcol ) {
                my $total=0;
                foreach my $row ( @looprow ) {
-                       $total += $table{$row->{rowtitle}}->{$col->{coltitle}};
+                       $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;
@@ -535,4 +576,4 @@ sub calculate {
        return \@mainloop;
 }
 
-1;
\ No newline at end of file
+1;