DB and syspref update for Tags and BakerTaylor
[koha.git] / reports / borrowers_stats.pl
index 9b2d878..45e6bc7 100755 (executable)
@@ -1,7 +1,5 @@
 #!/usr/bin/perl
 
-# $Id$
-
 # Copyright 2000-2002 Katipo Communications
 #
 # This file is part of Koha.
@@ -25,9 +23,14 @@ use C4::Auth;
 use C4::Context;
 use C4::Branch; # GetBranches
 use C4::Koha;
+use C4::Dates;
 use C4::Acquisition;
 use C4::Output;
 use C4::Circulation;
+use Date::Calc qw(
+  Today
+  Add_Delta_YM
+  );
 
 =head1 NAME
 
@@ -46,6 +49,7 @@ my $line = $input->param("Line");
 my $column = $input->param("Column");
 my @filters = $input->param("Filter");
 my $digits = $input->param("digits");
+my $period = $input->param("period");
 my $borstat = $input->param("status");
 my $borstat1 = $input->param("activity");
 my $output = $input->param("output");
@@ -115,7 +119,7 @@ if ($do_it) {
                $select_catcode{$catcode} = $description;
        }
        my $CGICatCode=CGI::scrolling_list( -name     => 'Filter',
-                               -id => 'Filter',
+                               -id => 'catcode',
                                -values   => \@select_catcode,
                                -labels   => \%select_catcode,
                                -size     => 1,
@@ -128,7 +132,7 @@ my @select_branch;
 #my %select_branches;
 push @select_branch,"";
 #$select_branches{""}="";
-foreach my $thisbranch (keys %$branches) {
+foreach my $thisbranch (sort keys %$branches) {
        push @select_branch,$thisbranch;
    # my $selected = 1 if $thisbranch eq $branch;
     my %row =(value => $thisbranch,
@@ -138,7 +142,7 @@ foreach my $thisbranch (keys %$branches) {
     push @branchloop, \%row;
 }
     my $CGIBranch=CGI::scrolling_list( -name     => 'Filter',
-                             -id => 'Filter',
+                             -id => 'branch',
                              -values   => \@select_branch,
 #                             -labels   => \%select_branches,
                              -size     => 1,
@@ -156,7 +160,7 @@ foreach my $thisbranch (keys %$branches) {
        }
 # 
        my $CGIZipCode=CGI::scrolling_list( -name     => 'Filter',
-                               -id => 'Filter',
+                               -id => 'zipcode',
                                -values   => \@select_zipcode,
                                -size     => 1,
                                -multiple => 0 );
@@ -178,7 +182,7 @@ foreach my $thisbranch (keys %$branches) {
        }
 # 
        my $CGIsort1=CGI::scrolling_list( -name     => 'Filter',
-                               -id => 'Filter',
+                               -id => 'sort1',
                                -values   => \@select_sort1,
                                -labels =>\%select_sort1,
                                -size     => 1,
@@ -196,16 +200,13 @@ foreach my $thisbranch (keys %$branches) {
                }
        }
        my $CGIsort2=CGI::scrolling_list( -name     => 'Filter',
-                               -id => 'Filter',
+                               -id => 'sort2',
                                -values   => \@select_sort2,
                                -size     => 1,
                                -multiple => 0 );
        
        my @mime = ( C4::Context->preference("MIME") );
-       foreach my $mime (@mime){
-               warn "".$mime;
-       }
-       
+       # warn 'MIME(s): ' . join ' ', @mime;
        my $CGIextChoice=CGI::scrolling_list(
                                -name => 'MIME',
                                -id => 'MIME',
@@ -223,16 +224,15 @@ foreach my $thisbranch (keys %$branches) {
 
        $template->param(               CGICatCode => $CGICatCode,
                                        CGIZipCode => $CGIZipCode,
-#                                      CGIBranch => $CGIBranch,
                                        CGISort1 => $CGIsort1,
                                        hassort1 => $hassort1,
                                        CGISort2 => $CGIsort2,
                                        hassort2 => $hassort2,
                                        CGIextChoice => $CGIextChoice,
                                        CGIsepChoice => $CGIsepChoice,
-                                       CGIBranch => $CGIBranch
-#                                      CGIBranch => \@branchloop
-                                       );
+                                       CGIBranch => $CGIBranch,
+                                       DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
+                    );
 
 }
 output_html_with_http_headers $input, $cookie, $template->output;
@@ -294,6 +294,10 @@ sub calculate {
        if ($activity) {
                push @loopfilter,{crit=>"Activity",filter=>$activity};
        }
+# year of activity
+       my ( $period_year, $period_month, $period_day )=Add_Delta_YM( Today(),-$period, 0);
+       my $newperioddate=$period_year."-".$period_month."-".$period_day;
+#      warn "PERIOD".$period;
 # 1st, loop rows.
        my $linefield;
        if (($line =~/zipcode/) and ($digits)) {
@@ -392,11 +396,11 @@ sub calculate {
        $strcalc .= " AND sort1 like '" . @$filters[5] ."'" if ( @$filters[5] );
        @$filters[6]=~ s/\*/%/g if (@$filters[6]);
        $strcalc .= " AND sort2 like '" . @$filters[6] ."'" if ( @$filters[6] );
-       $strcalc .= " AND borrowernumber in (select distinct(borrowernumber) from issues where timestamp > ' 2007-01-01')" if ($activity eq 'active');
-       $strcalc .= " AND borrowernumber not in (select distinct(borrowernumber) from issues where timestamp > ' 2007-01-01')" if ($activity eq 'nonactive');
+       $strcalc .= " AND borrowernumber in (select distinct(borrowernumber) from old_issues where issuedate > '" . $newperioddate . "')" if ($activity eq 'active');
+       $strcalc .= " AND borrowernumber not in (select distinct(borrowernumber) from old_issues where issuedate > '" . $newperioddate . "')" if ($activity eq 'nonactive');
        $strcalc .= " AND $status='1' " if ($status);
        $strcalc .= " group by $linefield, $colfield";
-       warn "". $strcalc;
+#      warn "". $strcalc;
        my $dbcalc = $dbh->prepare($strcalc);
        $dbcalc->execute;
 #      warn "filling table";