Bug 22323: cronjob runreport.pl CSV add encoding
[koha.git] / misc / cronjobs / runreport.pl
1 #!/usr/bin/perl
2 #
3 # Copyright 2008 Liblime
4 # Copyright 2014 Foundations Bible College, Inc.
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22
23 use C4::Reports::Guided; # 0.12
24 use Koha::Reports;
25 use C4::Context;
26 use C4::Log;
27 use Koha::Email;
28 use Koha::DateUtils;
29
30 use Getopt::Long qw(:config auto_help auto_version);
31 use Pod::Usage;
32 use MIME::Lite;
33 use Text::CSV::Encoded;
34 use CGI qw ( -utf8 );
35 use Carp;
36 use Encode;
37 use JSON qw( to_json );
38
39 BEGIN {
40     # find Koha's Perl modules
41     # test carefully before changing this
42     use FindBin;
43     eval { require "$FindBin::Bin/../kohalib.pl" };
44 }
45
46 =head1 NAME
47
48 runreport.pl - Run pre-existing saved reports
49
50 =head1 SYNOPSIS
51
52 runreport.pl [ -h | -m ] [ -v ] reportID [ reportID ... ]
53
54  Options:
55    -h --help       brief help message
56    -m --man        full documentation, same as --help --verbose
57    -v --verbose    verbose output
58
59    --format=s      selects format. Choice of text, html, csv or tsv
60
61    -e --email      whether to use e-mail (implied by --to or --from)
62    -a --attachment additionally attach the report as a file. cannot be used with html format
63    --username      username to pass to the SMTP server for authentication
64    --password      password to pass to the SMTP server for authentication
65    --method        method is the type of authentication. Ie. LOGIN, DIGEST-MD5, etc.
66    --to=s          e-mail address to send report to
67    --from=s        e-mail address to send report from
68    --subject=s     subject for the e-mail
69    --store-results store the result of the report
70    --csv-header    add column names as first line of csv output
71
72
73  Arguments:
74    reportID        report ID Number from saved_sql.id, multiple ID's may be specified
75
76 =head1 OPTIONS
77
78 =over
79
80 =item B<--help>
81
82 Print a brief help message and exits.
83
84 =item B<--man>
85
86 Prints the manual page and exits.
87
88 =item B<-v>
89
90 Verbose. Without this flag set, only fatal errors are reported.
91
92 =item B<--format>
93
94 Current options are text, html, csv, and tsv. At the moment, text and tsv both produce tab-separated tab-separated output.
95
96 =item B<--email>
97
98 Whether to use e-mail (implied by --to or --from).
99
100 =item B<--username>
101
102 Username to pass to the SMTP server for authentication
103
104 =item B<--password>
105
106 Password to pass to the SMTP server for authentication
107
108 =item B<--method>
109
110 Method is the type of authentication. Ie. LOGIN, DIGEST-MD5, etc.
111
112 =item B<--to>
113
114 E-mail address to send report to. Defaults to KohaAdminEmailAddress.
115
116 =item B<--from>
117
118 E-mail address to send report from. Defaults to KohaAdminEmailAddress.
119
120 =item B<--subject>
121
122 Subject for the e-mail message. Defaults to "Koha Saved Report"
123
124 =item B<--store-results>
125
126 Store the result of the report into the saved_reports DB table.
127
128 To access the results, go on Reports > Guided reports > Saved report.
129
130 =back
131
132 =head1 DESCRIPTION
133
134 This script is designed to run existing Saved Reports.
135
136 =head1 USAGE EXAMPLES
137
138 B<runreport.pl 16>
139
140 In the most basic form, runs the report specified by ID number from 
141 saved_sql.id, in this case #16, outputting the results to STDOUT.  
142
143 B<runreport.pl 16 17>
144
145 Same as above, but also runs report #17. 
146
147 =head1 TO DO
148
149 =over
150
151
152 =item *
153
154 Allow Saved Results option.
155
156
157 =back
158
159 =head1 SEE ALSO
160
161 Reports - Guided Reports
162
163 =cut
164
165 # These variables can be set by command line options,
166 # initially set to default values.
167
168 my $help    = 0;
169 my $man     = 0;
170 my $verbose = 0;
171 my $email   = 0;
172 my $attachment = 0;
173 my $format  = "text";
174 my $to      = "";
175 my $from    = "";
176 my $subject = "";
177 my $separator = ',';
178 my $quote = '"';
179 my $store_results = 0;
180 my $csv_header = 0;
181
182 my $username = undef;
183 my $password = undef;
184 my $method = 'LOGIN';
185
186 GetOptions(
187     'help|?'            => \$help,
188     'man'               => \$man,
189     'verbose'           => \$verbose,
190     'format=s'          => \$format,
191     'to=s'              => \$to,
192     'from=s'            => \$from,
193     'subject=s'         => \$subject,
194     'email'             => \$email,
195     'a|attachment'      => \$attachment,
196     'username:s'        => \$username,
197     'password:s'        => \$password,
198     'method:s'          => \$method,
199     'store-results'     => \$store_results,
200     'csv-header'        => \$csv_header,
201
202 ) or pod2usage(2);
203 pod2usage( -verbose => 2 ) if ($man);
204 pod2usage( -verbose => 2 ) if ($help and $verbose);
205 pod2usage(1) if $help;
206
207 cronlogaction();
208
209 unless ($format) {
210     $verbose and print STDERR "No format specified, assuming 'text'\n";
211     $format = 'text';
212 }
213
214 if ($format eq 'tsv' || $format eq 'text') {
215     $format = 'csv';
216     $separator = "\t";
217 }
218
219 if ($to or $from or $email) {
220     $email = 1;
221     $from or $from = C4::Context->preference('KohaAdminEmailAddress');
222     $to   or $to   = C4::Context->preference('KohaAdminEmailAddress');
223 }
224
225 unless (scalar(@ARGV)) {
226     print STDERR "ERROR: No reportID(s) specified\n";
227     pod2usage(1);
228 }
229 ($verbose) and print scalar(@ARGV), " argument(s) after options: " . join(" ", @ARGV) . "\n";
230
231 my $today = dt_from_string();
232 my $date = $today->ymd();
233
234 foreach my $report_id (@ARGV) {
235     my $report = Koha::Reports->find( $report_id );
236     unless ($report) {
237         warn "ERROR: No saved report $report_id found";
238         next;
239     }
240     my $sql         = $report->savedsql;
241     my $report_name = $report->report_name;
242     my $type        = $report->type;
243
244     $verbose and print "SQL: $sql\n\n";
245     if ( $subject eq "" )
246     {
247         if ( defined($report_name) and $report_name ne "")
248         {
249             $subject = $report_name ;
250         }
251         else
252         {
253             $subject = 'Koha Saved Report';
254         }
255     }
256     my ($sth) = execute_query( $sql, undef, undef, undef, $report_id );
257     my $count = scalar($sth->rows);
258     unless ($count) {
259         print "NO OUTPUT: 0 results from execute_query\n";
260         next;
261     }
262     $verbose and print "$count results from execute_query\n";
263
264     my $message;
265     my @rows_to_store;
266     if ($format eq 'html') {
267         my $cgi = CGI->new();
268         my @rows;
269         while (my $line = $sth->fetchrow_arrayref) {
270             foreach (@$line) { defined($_) or $_ = ''; }    # catch undef values, replace w/ ''
271             push @rows, $cgi->TR( join('', $cgi->td($line)) ) . "\n";
272             push @rows_to_store, [@$line] if $store_results;
273         }
274         $message = $cgi->table(join "", @rows);
275     } elsif ($format eq 'csv') {
276         my $csv = Text::CSV::Encoded->new({
277             encoding_out => 'utf8',
278             binary      => 1,
279             quote_char  => $quote,
280             sep_char    => $separator,
281             });
282
283         if ( $csv_header ) {
284             my @fields = map { decode( 'utf8', $_ ) } @{ $sth->{NAME} };
285             $csv->combine( @fields );
286             $message .= $csv->string() . "\n";
287             push @rows_to_store, [@fields] if $store_results;
288         }
289
290         while (my $line = $sth->fetchrow_arrayref) {
291             $csv->combine(@$line);
292             $message .= $csv->string() . "\n";
293             push @rows_to_store, [@$line] if $store_results;
294         }
295     }
296     if ( $store_results ) {
297         my $json = to_json( \@rows_to_store );
298         C4::Reports::Guided::store_results( $report_id, $json );
299     }
300     if ($email) {
301         my $args = { to => $to, from => $from, subject => $subject };
302         if ( $format eq 'html' ) {
303             $message = "<html><head><style>tr:nth-child(2n+1) { background-color: #ccc;}</style></head><body>$message</body></html>";
304             $args->{contenttype} = 'text/html';
305         }
306         my $email = Koha::Email->new();
307         my %mail  = $email->create_message_headers($args);
308         $mail{Data} = $message;
309         $mail{Auth} = { user => $username, pass => $password, method => $method } if $username;
310
311         my $msg = MIME::Lite->new(%mail);
312
313         $msg->attach(
314             Type        => "text/$format",
315             Data        => encode( 'utf8', $message ),
316             Filename    => "report$report_id-$date.$format",
317             Disposition => 'attachment',
318         ) if $attachment;
319
320         $msg->send();
321         carp "Mail not sent" unless $msg->last_send_successful();
322     }
323     else {
324         print $message;
325     }
326 }