From 65cced778aa74765878afcb6f250a2997a37a306 Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
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 <david@davidnind.com>
Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net>
---
 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.42.0