Merge remote branch 'kc/new/bug_4908' into kcmaster
[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
32 use vars qw($VERSION);
33
34 BEGIN {
35     # find Koha's Perl modules
36     # test carefully before changing this
37     use FindBin;
38     eval { require "$FindBin::Bin/../kohalib.pl" };
39     $VERSION = 0.22;
40 }
41
42 =head1 NAME
43
44 runreport.pl - Run pre-existing saved reports
45
46 =head1 SYNOPSIS
47
48 runreport.pl [ -h | -m ] [ -v ] reportID [ reportID ... ]
49
50  Options:
51    -h --help       brief help message
52    -m --man        full documentation, same as --help --verbose
53    -v --verbose    verbose output
54
55  Arguments:
56    reportID        report ID Number from saved_sql.id, multiple ID's may be specified
57
58 =head1 OPTIONS
59
60 =over
61
62 =item B<-help>
63
64 Print a brief help message and exits.
65
66 =item B<-man>
67
68 Prints the manual page and exits.
69
70 =item B<-v>
71
72 Verbose. Without this flag set, only fatal errors are reported.
73
74 =back
75
76 =head1 DESCRIPTION
77
78 This script is designed to run existing Saved Reports.
79
80 =head1 USAGE EXAMPLES
81
82 B<runreport.pl 16>
83
84 In the most basic form, runs the report specified by ID number from 
85 saved_sql.id, in this case #16, outputting the results to STDOUT.  
86
87 B<runreport.pl 16 17>
88
89 Same as above, but also runs report #17. 
90
91 =head1 TO DO
92
93 =over
94
95 =item * 
96
97 Complete testing for Sendmail related options: --email, --to, and --from.
98
99 =item *
100
101 Allow Saved Results option.
102
103 =item *
104
105 Possible --format option for CSV or tab-delimited output.
106
107 =back
108
109 =head1 SEE ALSO
110
111 Reports - Guided Reports
112
113 =cut
114
115 # These variables can be set by command line options,
116 # initially set to default values.
117
118 my $help    = 0;
119 my $man     = 0;
120 my $verbose = 0;
121 my $email   = 0;
122 my $format  = "";
123 my $to      = "";
124 my $from    = "";
125 my $subject = 'Koha Saved Report';
126
127 GetOptions(
128     'help|?'     => \$help,
129     'man'        => \$man,
130     'verbose'    => \$verbose,
131     'format'     => \$format,
132     'to'         => \$to,
133     'from'       => \$from,
134     'email'      => \$email,
135 ) or pod2usage(2);
136 pod2usage( -verbose => 2 ) if ($man);
137 pod2usage( -verbose => 2 ) if ($help and $verbose);
138 pod2usage(1) if $help;
139
140 unless ($format) {
141     $verbose and print STDERR "No format specified, assuming 'text'\n";
142     $format = '';
143     # $format = 'text';
144 }
145
146 if ($to or $from or $email) {
147     $email = 1;
148     $from or $from = C4::Context->preference('KohaAdminEmailAddress');
149     $to   or $to   = C4::Context->preference('KohaAdminEmailAddress');
150 }
151
152 unless (scalar(@ARGV)) {
153     print STDERR "ERROR: No reportID(s) specified\n";
154     pod2usage(1);
155 }
156 ($verbose) and print scalar(@ARGV), " argument(s) after options: " . join(" ", @ARGV) . "\n";
157
158
159 foreach my $report (@ARGV) {
160     my ($sql, $type) = get_saved_report($report);
161     unless ($sql) {
162         warn "ERROR: No saved report $report found";
163         next;
164     }
165     $verbose and print "SQL: $sql\n\n";
166     # my $results = execute_query($sql, undef, 0, 99999, $format, $report); 
167     my ($sth) = execute_query($sql);
168     # execute_query(sql, , 0, 20, , )
169     my $count = scalar($sth->rows);
170     unless ($count) {
171         print "NO OUTPUT: 0 results from execute_query\n";
172         next;
173     }
174     $verbose and print "$count results from execute_query\n";
175
176     my $cgi = CGI->new();
177     my @rows = ();
178     while (my $line = $sth->fetchrow_arrayref) {
179         foreach (@$line) { defined($_) or $_ = ''; }    # catch undef values, replace w/ ''
180         push @rows, $cgi->TR( join('', $cgi->td($line)) ) . "\n";
181     }
182     my $message = $cgi->table(join "", @rows);
183
184     if ($email){
185         my %mail = (
186             To      => $to,
187             From    => $from,
188             Subject => $subject,
189             Message => $message 
190         );
191         sendmail(%mail) or warn "mail not sent";
192     } else {
193         print $message;
194     }
195     # my @xmlarray = ... ;
196     # my $url = "/cgi-bin/koha/reports/guided_reports.pl?phase=retrieve%20results&id=$id";
197     # my $xml = XML::Dumper->new()->pl2xml( \@xmlarray );
198     # store_results($id,$xml);
199 }