Merge remote branch 'kc/master' into new/bug_5817
[koha.git] / misc / cronjobs / runreport.pl
1 #!/usr/bin/perl
2 #
3 # Copyright 2008 Liblime
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22
23 use C4::Reports::Guided; # 0.12
24 use C4::Context;
25
26 use Getopt::Long qw(:config auto_help auto_version);
27 use Pod::Usage;
28 use Mail::Sendmail;
29 use Text::CSV_XS;
30 use CGI;
31 use Carp;
32
33 use vars qw($VERSION);
34
35 BEGIN {
36     # find Koha's Perl modules
37     # test carefully before changing this
38     use FindBin;
39     eval { require "$FindBin::Bin/../kohalib.pl" };
40     $VERSION = 0.22;
41 }
42
43 =head1 NAME
44
45 runreport.pl - Run pre-existing saved reports
46
47 =head1 SYNOPSIS
48
49 runreport.pl [ -h | -m ] [ -v ] reportID [ reportID ... ]
50
51  Options:
52    -h --help       brief help message
53    -m --man        full documentation, same as --help --verbose
54    -v --verbose    verbose output
55
56  Arguments:
57    reportID        report ID Number from saved_sql.id, multiple ID's may be specified
58
59 =head1 OPTIONS
60
61 =over
62
63 =item B<-help>
64
65 Print a brief help message and exits.
66
67 =item B<-man>
68
69 Prints the manual page and exits.
70
71 =item B<-v>
72
73 Verbose. Without this flag set, only fatal errors are reported.
74
75 =back
76
77 =head1 DESCRIPTION
78
79 This script is designed to run existing Saved Reports.
80
81 =head1 USAGE EXAMPLES
82
83 B<runreport.pl 16>
84
85 In the most basic form, runs the report specified by ID number from 
86 saved_sql.id, in this case #16, outputting the results to STDOUT.  
87
88 B<runreport.pl 16 17>
89
90 Same as above, but also runs report #17. 
91
92 =head1 TO DO
93
94 =over
95
96 =item * 
97
98 Complete testing for Sendmail related options: --email, --to, and --from.
99
100 =item *
101
102 Allow Saved Results option.
103
104 =item *
105
106 Possible --format option for CSV or tab-delimited output.
107
108 =back
109
110 =head1 SEE ALSO
111
112 Reports - Guided Reports
113
114 =cut
115
116 # These variables can be set by command line options,
117 # initially set to default values.
118
119 my $help    = 0;
120 my $man     = 0;
121 my $verbose = 0;
122 my $email   = 0;
123 my $format  = "";
124 my $to      = "";
125 my $from    = "";
126 my $subject = 'Koha Saved Report';
127
128 GetOptions(
129     'help|?'     => \$help,
130     'man'        => \$man,
131     'verbose'    => \$verbose,
132     'format'     => \$format,
133     'to'         => \$to,
134     'from'       => \$from,
135     'email'      => \$email,
136 ) or pod2usage(2);
137 pod2usage( -verbose => 2 ) if ($man);
138 pod2usage( -verbose => 2 ) if ($help and $verbose);
139 pod2usage(1) if $help;
140
141 unless ($format) {
142     $verbose and print STDERR "No format specified, assuming 'text'\n";
143     $format = '';
144     # $format = 'text';
145 }
146
147 if ($to or $from or $email) {
148     $email = 1;
149     $from or $from = C4::Context->preference('KohaAdminEmailAddress');
150     $to   or $to   = C4::Context->preference('KohaAdminEmailAddress');
151 }
152
153 unless (scalar(@ARGV)) {
154     print STDERR "ERROR: No reportID(s) specified\n";
155     pod2usage(1);
156 }
157 ($verbose) and print scalar(@ARGV), " argument(s) after options: " . join(" ", @ARGV) . "\n";
158
159
160 foreach my $report (@ARGV) {
161     my ($sql, $type) = get_saved_report($report);
162     unless ($sql) {
163         carp "ERROR: No saved report $report found";
164         next;
165     }
166     $verbose and print "SQL: $sql\n\n";
167     # my $results = execute_query($sql, undef, 0, 99999, $format, $report); 
168     my ($sth) = execute_query($sql);
169     # execute_query(sql, , 0, 20, , )
170     my $count = scalar($sth->rows);
171     unless ($count) {
172         print "NO OUTPUT: 0 results from execute_query\n";
173         next;
174     }
175     $verbose and print "$count results from execute_query\n";
176
177     my $cgi = CGI->new();
178     my @rows = ();
179     while (my $line = $sth->fetchrow_arrayref) {
180         foreach (@$line) { defined($_) or $_ = ''; }    # catch undef values, replace w/ ''
181         push @rows, $cgi->TR( join('', $cgi->td($line)) ) . "\n";
182     }
183     my $message = $cgi->table(join "", @rows);
184
185     if ($email){
186         my %mail = (
187             To      => $to,
188             From    => $from,
189             Subject => $subject,
190             Message => $message 
191         );
192         sendmail(%mail) or carp 'mail not sent:' . $Mail::Sendmail::error;
193     } else {
194         print $message;
195     }
196     # my @xmlarray = ... ;
197     # my $url = "/cgi-bin/koha/reports/guided_reports.pl?phase=retrieve%20results&id=$id";
198     # my $xml = XML::Dumper->new()->pl2xml( \@xmlarray );
199     # store_results($id,$xml);
200 }