use Modern::Perl;
use C4::Reports::Guided; # 0.12
+use Koha::Reports;
use C4::Context;
use C4::Log;
use Koha::Email;
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;
--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:
my $separator = ',';
my $quote = '"';
my $store_results = 0;
+my $csv_header = 0;
my $username = undef;
my $password = undef;
'password:s' => \$password,
'method:s' => \$method,
'store-results' => \$store_results,
+ 'csv-header' => \$csv_header,
) or pod2usage(2);
pod2usage( -verbose => 2 ) if ($man);
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 "" )
$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";
}
$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";