removed C4::Dates from koha-ffzg.psgi
[koha.git] / misc / cronjobs / runreport.pl
index 370c479..c89a6d4 100755 (executable)
@@ -21,6 +21,7 @@
 use Modern::Perl;
 
 use C4::Reports::Guided; # 0.12
+use Koha::Reports;
 use C4::Context;
 use C4::Log;
 use Koha::Email;
@@ -29,7 +30,7 @@ use Koha::DateUtils;
 use Getopt::Long qw(:config auto_help auto_version);
 use Pod::Usage;
 use MIME::Lite;
-use Text::CSV_XS;
+use Text::CSV::Encoded;
 use CGI qw ( -utf8 );
 use Carp;
 use Encode;
@@ -66,6 +67,7 @@ runreport.pl [ -h | -m ] [ -v ] reportID [ reportID ... ]
    --from=s        e-mail address to send report from
    --subject=s     subject for the e-mail
    --store-results store the result of the report
+   --csv-header    add column names as first line of csv output
 
 
  Arguments:
@@ -175,6 +177,7 @@ my $subject = "";
 my $separator = ',';
 my $quote = '"';
 my $store_results = 0;
+my $csv_header = 0;
 
 my $username = undef;
 my $password = undef;
@@ -194,6 +197,7 @@ GetOptions(
     'password:s'        => \$password,
     'method:s'          => \$method,
     'store-results'     => \$store_results,
+    'csv-header'        => \$csv_header,
 
 ) or pod2usage(2);
 pod2usage( -verbose => 2 ) if ($man);
@@ -228,14 +232,14 @@ my $today = dt_from_string();
 my $date = $today->ymd();
 
 foreach my $report_id (@ARGV) {
-    my $report = get_saved_report($report_id);
+    my $report = Koha::Reports->find( $report_id );
     unless ($report) {
         warn "ERROR: No saved report $report_id found";
         next;
     }
-    my $sql         = $report->{savedsql};
-    my $report_name = $report->{report_name};
-    my $type        = $report->{type};
+    my $sql         = $report->savedsql;
+    my $report_name = $report->report_name;
+    my $type        = $report->type;
 
     $verbose and print "SQL: $sql\n\n";
     if ( $subject eq "" )
@@ -249,8 +253,7 @@ foreach my $report_id (@ARGV) {
             $subject = 'Koha Saved Report';
         }
     }
-    # my $results = execute_query($sql, undef, 0, 99999, $format, $report_id);
-    my ($sth) = execute_query($sql);
+    my ($sth) = execute_query( $sql, undef, undef, undef, $report_id );
     my $count = scalar($sth->rows);
     unless ($count) {
         print "NO OUTPUT: 0 results from execute_query\n";
@@ -270,10 +273,20 @@ foreach my $report_id (@ARGV) {
         }
         $message = $cgi->table(join "", @rows);
     } elsif ($format eq 'csv') {
-        my $csv = Text::CSV_XS->new({
+        my $csv = Text::CSV::Encoded->new({
+            encoding_out => 'utf8',
+            binary      => 1,
             quote_char  => $quote,
             sep_char    => $separator,
             });
+
+        if ( $csv_header ) {
+            my @fields = map { decode( 'utf8', $_ ) } @{ $sth->{NAME} };
+            $csv->combine( @fields );
+            $message .= $csv->string() . "\n";
+            push @rows_to_store, [@fields] if $store_results;
+        }
+
         while (my $line = $sth->fetchrow_arrayref) {
             $csv->combine(@$line);
             $message .= $csv->string() . "\n";