moving itemslost to reports from tools
[koha_fer] / reports / issues_avg_stats.pl
index 8721230..8cb2176 100755 (executable)
@@ -23,13 +23,11 @@ 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 Date::Manip;
+use C4::Circulation;
+use Date::Calc qw(Delta_Days);
 
 =head1 NAME
 
@@ -37,8 +35,7 @@ plugin that shows a stats on borrowers
 
 =head1 DESCRIPTION
 
-
-=over2
+=over 2
 
 =cut
 
@@ -61,10 +58,14 @@ my ($template, $borrowernumber, $cookie)
                                query => $input,
                                type => "intranet",
                                authnotrequired => 0,
-                               flagsrequired => {editcatalogue => 1},
+                               flagsrequired => {reports => 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, $rodsp, $podsp, $calc, \@filters);
@@ -76,6 +77,7 @@ if ($do_it) {
        } else {
 # Printing to a csv file
                print $input->header(-type => 'application/vnd.sun.xml.calc',
+                                     -encoding    => 'utf-8',
                        -attachment=>"$basename.csv",
                        -filename=>"$basename.csv" );
                my $cols = @$results[0]->{loopcol};
@@ -125,7 +127,7 @@ if ($do_it) {
                $select{$value}=$desc;
        }
        my $CGIBorCat=CGI::scrolling_list( -name     => 'Filter',
-                               -id => 'Filter',
+                               -id => 'borcat',
                                -values   => \@select,
                                -labels   => \%select,
                                -size     => 1,
@@ -142,7 +144,7 @@ if ($do_it) {
                $select{$value}=$desc;
        }
        my $CGIItemTypes=CGI::scrolling_list( -name     => 'Filter',
-                               -id => 'Filter',
+                               -id => 'itemtypes',
                                -values   => \@select,
                                -labels    => \%select,
                                -size     => 1,
@@ -157,7 +159,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,"";
@@ -167,14 +169,14 @@ if ($do_it) {
                $select_branches{$branch} = $branches->{$branch}->{'branchname'};
        }
        my $CGIBranch=CGI::scrolling_list( -name     => 'Filter',
-                               -id => 'Filter',
+                               -id => 'branch',
                                -values   => \@select_branch,
                                -labels   => \%select_branches,
                                -size     => 1,
                                -multiple => 0 );
        
        my $CGISort1=CGI::scrolling_list( -name     => 'Filter',
-                               -id => 'Filter',
+                               -id => 'sort1',
                                -values   => \@select,
                                -size     => 1,
                                -multiple => 0 );
@@ -191,7 +193,7 @@ if ($do_it) {
                push @select, $value;
        }
        my $CGISort2=CGI::scrolling_list( -name     => 'Filter',
-                               -id => 'Filter',
+                               -id => 'sort2',
                                -values   => \@select,
                                -size     => 1,
                                -multiple => 0 );
@@ -337,7 +339,7 @@ sub calculate {
                $linefield .="Year($line)";
                $lineorder .= $line;  
        } elsif (($line=~/timestamp/) or ($line=~/returndate/)){
-               $linefield .= "date_format('$line',\"%Y-%m-%d\")";
+               $linefield .= "date_format(\'$line\',\"%Y-%m-%d\")";
                $lineorder .= $line;  
        } else {
                $linefield .= $line;
@@ -351,9 +353,9 @@ sub calculate {
                if ($linefilter[1] and ($linefilter[0])){
                        $strsth .= " and $line between '$linefilter[0]' and '$linefilter[1]' " ;
                } elsif ($linefilter[1]) {
-                               $strsth .= " and $line < '$linefilter[1]' " ;
+                               $strsth .= " and $line < \'$linefilter[1]\' " ;
                } elsif ($linefilter[0]) {
-                       $strsth .= " and $line > '$linefilter[0]' " ;
+                       $strsth .= " and $line > \'$linefilter[0]\' " ;
                }
                if ($linefilter[2]){
                        $strsth .= " and dayname($line) = '$linefilter[2]' " ;
@@ -475,7 +477,7 @@ sub calculate {
        
 # 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";
+       $strcalc .= " 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.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
@@ -521,10 +523,10 @@ sub calculate {
                $col = "zzEMPTY" if ($col eq undef);
                $row = "zzEMPTY" if ($row eq undef);
 #              warn "506 row :".$row." column :".$col;
-               my @result =split /:/,DateCalc($returndate,$issuedate) ;
+               my $result =Delta_Days(split(/-/,$issuedate),split (/-/,$returndate)) ;
 #  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];
+               $loanlength = $result;
 #              warn "512 Same row and col DateCalc returns :$loanlength with return ". $returndate ."issue ". $issuedate ."weight : ". $weight;
 #              warn "513 row :".$row." column :".$col;
                $table{$row}->{$col}+=$weight*$loanlength;
@@ -589,4 +591,4 @@ sub calculate {
        return \@mainloop;
 }
 
-1;
\ No newline at end of file
+1;