Bug 35171: Add send_empty option to runreport

This patch adds a new 'send_empty' option to runreport.pl

To test:
1 - Create a report in Koha that will not return any results:
    SELECT barcode FROM items WHERE 1=2
2 - perl misc/cronjobs/runreport.pl 1
3 - Output is: NO OUTPUT: 0 results from execute_query
4 - perl misc/cronjobs/runreport.pl 1 --send_empty
5 - Output is: no results were returned for the report
6 - perl misc/cronjobs/runreport.pl 1 --send_empty --email
7 - It will die on an email error unless you have SMTP configured - this si good, it means we sent an email
8 - Bonus points: Test on a system that can correctly send emails, confirm it works :-)

Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
(cherry picked from commit 4bfd1e21c6)
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
This commit is contained in:
Nick Clemens 2023-10-26 18:03:40 +00:00 committed by Fridolin Somers
parent baf56d81b7
commit 42fdb17d68

View file

@ -54,6 +54,7 @@ runreport.pl [ -h | -m ] [ -v ] reportID [ reportID ... ]
--format=s selects format. Choice of text, html, csv or tsv
-e --email whether to use e-mail (implied by --to or --from)
--send_empty whether to send an email when there are no results from the specified report
-a --attachment additionally attach the report as a file. cannot be used with html format
--username username to pass to the SMTP server for authentication
--password password to pass to the SMTP server for authentication
@ -173,20 +174,21 @@ binmode STDOUT, ":encoding(UTF-8)";
# These variables can be set by command line options,
# initially set to default values.
my $help = 0;
my $man = 0;
my $verbose = 0;
my $send_email = 0;
my $attachment = 0;
my $format = "text";
my $to = "";
my $from = "";
my $subject = "";
my @params = ();
my $separator = ',';
my $quote = '"';
my $help = 0;
my $man = 0;
my $verbose = 0;
my $send_email = 0;
my $send_empty = 0;
my $attachment = 0;
my $format = "text";
my $to = "";
my $from = "";
my $subject = "";
my @params = ();
my $separator = ',';
my $quote = '"';
my $store_results = 0;
my $csv_header = 0;
my $csv_header = 0;
my $csv_separator = "";
my $username = undef;
@ -206,6 +208,7 @@ GetOptions(
'subject=s' => \$subject,
'param=s' => \@params,
'email' => \$send_email,
'send_empty' => \$send_empty,
'a|attachment' => \$attachment,
'username:s' => \$username,
'password:s' => \$password,
@ -288,7 +291,7 @@ foreach my $report_id (@ARGV) {
}
);
my $count = scalar($sth->rows);
unless ($count) {
unless ($count || $send_empty ) {
print "NO OUTPUT: 0 results from execute_query\n";
next;
}
@ -296,7 +299,9 @@ foreach my $report_id (@ARGV) {
my $message;
my @rows_to_store;
if ($format eq 'html') {
if( !$count ){
$message = "No results were returned for the report\n";
} elsif ($format eq 'html') {
my $cgi = CGI->new();
my @rows;
while (my $line = $sth->fetchrow_arrayref) {
@ -327,7 +332,7 @@ foreach my $report_id (@ARGV) {
}
$message = Encode::decode_utf8($message);
}
if ( $store_results ) {
if ( $store_results && $count ) {
my $json = to_json( \@rows_to_store );
C4::Reports::Guided::store_results( $report_id, $json );
}