Moved/renamed kohastructure_pg.sql to data/Pg
[koha.git] / reports / catalogue_stats.pl
index 55d6251..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);
@@ -72,6 +68,7 @@ if ($do_it) {
                exit(1);
        } else {
                print $input->header(-type => 'application/vnd.sun.xml.calc',
+                                     -encoding    => 'utf-8',
                                                         -attachment=>"$basename.csv",
                                                         -name=>"$basename.csv" );
                my $cols = @$results[0]->{loopcol};
@@ -106,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;
@@ -174,7 +171,7 @@ if ($do_it) {
 #      warn "highlightLOC ".$hglghtLOC;
        
        
-       $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,"";
@@ -182,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 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 );
+#      $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) {
@@ -210,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,
@@ -220,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 );
@@ -247,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,
@@ -260,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,
@@ -328,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/ ) ;
@@ -342,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/ ) ;
@@ -454,115 +454,59 @@ sub calculate {
        }
 
 # preparing calculation
-       my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM biblioitems LEFT JOIN  items ON (items.biblioitemnumber = biblioitems.biblioitemnumber)";
-       my $cond=0;
+       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 .= " WHERE dewey >" . @$filters[0] ."";
-               $cond=1; 
+               $strcalc .= " AND dewey >" . @$filters[0] ."";
        }
        if (@$filters[1]){
                @$filters[1]=~ s/\*/%/g ;
-               if ($cond){
-                       $strcalc .= " AND dewey <" . @$filters[1] ."";
-               } else {
-                       $strcalc .= " WHERE dewey <" . @$filters[1] ."" ;
-                       $cond=1;
-               }
+               $strcalc .= " AND dewey <" . @$filters[1] ."";
                
        }
        if (@$filters[2]){
                @$filters[2]=~ s/\*/%/g ;
-               if ($cond){
-                       $strcalc .= " AND lccn >" . @$filters[2] ."" ;
-               } else {
-                       $strcalc .= " WHERE lccn > " . @$filters[2] ."" ;
-                       $cond=1;
-               }
+               $strcalc .= " AND lccn >" . @$filters[2] ."" ;
        }
        if (@$filters[3]){
                @$filters[3]=~ s/\*/%/g;
-               if ($cond){
-                       $strcalc .= " AND lccn <" . @$filters[3] ."" ;
-               } else {
-                       $strcalc .= " WHERE lccn <" . @$filters[3] ."" ;
-                       $cond=1;
-               }
+               $strcalc .= " AND lccn <" . @$filters[3] ."" ;
        }
        if (@$filters[4]){
                @$filters[4]=~ s/\*/%/g ;
-               if ($cond){
-                       $strcalc .= " AND items.itemcallnumber >" . @$filters[4] ."" ;
-               } else {
-                       $strcalc .= " WHERE items.itemcallnumber >" . @$filters[4] ."" ;
-                       $cond=1;
-               }
+               $strcalc .= " AND items.itemcallnumber >=" . $dbh->quote(@$filters[4]) ."" ;
        }
        
        if (@$filters[5]){
                @$filters[5]=~ s/\*/%/g;
-               if ($cond){
-                       $strcalc .= " AND items.itemcallnumber <" . @$filters[5] ."" ;
-               } else {
-                       $strcalc .= " WHERE items.itemcallnumber <" . @$filters[5] ."" ;
-                       $cond=1;
-               }
+               $strcalc .= " AND items.itemcallnumber <=" . $dbh->quote(@$filters[5]) ."" ;
        }
        
        if (@$filters[6]){
                @$filters[6]=~ s/\*/%/g;
-               if ($cond){
-                       $strcalc .= " AND biblioitems.itemtype like '" . @$filters[6] ."'";
-               } else {
-                       $strcalc .= " WHERE biblioitems.itemtype like '" . @$filters[6] ."'";
-                       $cond=1;
-               }
+               $strcalc .= " AND biblioitems.itemtype like '" . @$filters[6] ."'";
        }
        
        if (@$filters[7]){
                @$filters[7]=~ s/\*/%/g;
                @$filters[7].="%" unless @$filters[7]=~/%/;
-               if ($cond){
-                       $strcalc .= " AND biblioitems.publishercode like \"" . @$filters[7] ."\"";
-               } else {
-                       $strcalc .= " WHERE biblioitems.publishercode like \"" . @$filters[7] ."\"";
-                       $cond=1;
-               }
+               $strcalc .= " AND biblioitems.publishercode like \"" . @$filters[7] ."\"";
        }
        if (@$filters[8]){
                @$filters[8]=~ s/\*/%/g;
-               if ($cond){
-                       $strcalc .= " AND publicationyear >" . @$filters[8] ."" ;
-               } else {
-                       $strcalc .= " WHERE publicationyear >" . @$filters[8] ."" ;
-                       $cond=1;
-               }
+               $strcalc .= " AND publicationyear >" . @$filters[8] ."" ;
        }
        if (@$filters[9]){
                @$filters[9]=~ s/\*/%/g;
-               if ($cond){
-                       $strcalc .= " AND publicationyear <" . @$filters[9] ."";
-               } else {
-                       $strcalc .= " WHERE publicationyear <" . @$filters[9] ."";
-                       $cond=1;
-               }
+               $strcalc .= " AND publicationyear <" . @$filters[9] ."";
        }
        if (@$filters[10]){
                @$filters[10]=~ s/\*/%/g;
-               if ($cond){
-                       $strcalc .= " AND items.homebranch like '" . @$filters[10] ."'";
-               } else {
-                       $strcalc .= " WHERE items.homebranch like '" . @$filters[10] ."'";
-                       $cond=1;
-               }
+               $strcalc .= " AND items.homebranch like '" . @$filters[10] ."'";
        }
        if (@$filters[11]){
                @$filters[11]=~ s/\*/%/g;
-               if ($cond){
-                       $strcalc .= " AND items.location like '" . @$filters[11] ."'" if ( @$filters[11] );
-               } else {
-                       $strcalc .= " WHERE items.location like '" . @$filters[11] ."'" if ( @$filters[11] );
-               }
+               $strcalc .= " AND items.location like '" . @$filters[11] ."'" if ( @$filters[11] );
        }
        
        $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
@@ -632,4 +576,4 @@ sub calculate {
        return \@mainloop;
 }
 
-1;
\ No newline at end of file
+1;