From 42fdb17d683f4d998f74e8f2aec6d9a8d97e8d4c Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Thu, 26 Oct 2023 18:03:40 +0000 Subject: [PATCH] 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 Signed-off-by: Victor Grousset/tuxayo Signed-off-by: Tomas Cohen Arazi (cherry picked from commit 4bfd1e21c6ff2415c8e36ec2e376ca1c1330fbb2) Signed-off-by: Fridolin Somers --- misc/cronjobs/runreport.pl | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/misc/cronjobs/runreport.pl b/misc/cronjobs/runreport.pl index debcf5a024..656ce34d07 100755 --- a/misc/cronjobs/runreport.pl +++ b/misc/cronjobs/runreport.pl @@ -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 ); } -- 2.39.5