Bug 31203: Alter other cronjobs that currenlty use cronlogaction
[koha.git] / misc / cronjobs / patron_emailer.pl
1 #!/usr/bin/perl
2
3 #
4 # This file is part of Koha.
5 #
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18
19 use Modern::Perl;
20
21 use Koha::Script -cron;
22 use Getopt::Long qw( GetOptions );
23 use Pod::Usage qw( pod2usage );
24
25 use C4::Log qw( cronlogaction );
26 use C4::Reports::Guided qw( EmailReport );
27
28 =head1 NAME
29
30 patron_emailer.pl
31
32 =head1 SYNOPSIS
33
34 patron_emailer.pl
35     [--report ][--notice][--module] --library  --from
36
37  Options:
38     --help                       brief help
39     --report                     report ID to use as data for email template
40     --notice                     specific notice code to use
41     --module                     which module to find the above notice in
42     --library                    specified branch for selecting notice, will use all libraries by default
43     --from                       specified email for 'from' address, report column 'from' used if not specified
44     --email                      specified column to use as 'to' email address, report column 'email' used if not specified
45     --verbose                    increased verbosity, will print notices and errors
46     --commit                     send emails, without this script will only report
47
48 =head1 OPTIONS
49
50 =over 8
51
52 =item B<-help>
53
54 Print brief help and exit.
55
56 =item B<-man>
57
58 Print full documentation and exit.
59
60 =item B<-report>
61
62 Specify a saved SQL report id in the Koha system to user for the emails. All, and only,
63     columns in the report will be available for notice template variables
64
65 =item B<-notice>
66
67 Specific notice (CODE) to select
68
69 =item B<-module>
70
71 Which module to find the specified notice in
72
73 =item B<-library>
74
75 Option to specify which branches notice should be used, 'All libraries' is used if not specified
76
77 =item B<-from>
78
79 Specify the sender address of the email, if not specified a 'from' column in the report will be used.
80
81 =item B<-email>
82
83 Specify the column to find recipient address of the email, if not specified an 'email' column in the report will be used.
84
85 =item B<-verbose>
86
87 Increased verbosity, reports successes and errors.
88
89 =item B<-commit>
90
91 Send emails, if omitted script will report as verbose.
92
93 =back
94
95 =cut
96
97 binmode( STDOUT, ":encoding(UTF-8)" );
98
99 my $help     = 0;
100 my $report_id;
101 my $notice;
102 my $module;  #this is only for selecting correct notice - report itself defines available columns, not module
103 my $library; #as above, determines which notice to use, will use 'all libraries' if not specified
104 my $email;   #to specify which column should be used as email in report will use 'email' from borrwers table
105 my $from;    #to specify from address, will expect 'from' column in report if not specified
106 my $verbose  = 0;
107 my $commit   = 0;
108
109 my $error_msgs = {
110     MISSING_PARAMS => "You must supply a report ID, letter module and code at minimum\n",
111     NO_LETTER      => "The specified letter was not found, please check your input\n",
112     NO_REPORT      => "The specified report was not found, please check your input\n",
113     REPORT_FAIL    => "There was an error running the report, please check your SQL\n",
114     NO_BOR_COL     => "There was no borrowernumber found for row ",
115     NO_EMAIL_COL   => "There was no email found for row ",
116     NO_FROM_COL    => "No from email was specified for row ",
117     NO_BOR         => "There is no borrower with borrowernumber "
118 };
119
120 my $command_line_options = join(" ",@ARGV);
121
122 GetOptions(
123     'help|?'      => \$help,
124     'report=i'    => \$report_id,
125     'notice=s'    => \$notice,
126     'module=s'    => \$module,
127     'library=s'   => \$library,
128     'email=s'     => \$email,
129     'from=s'      => \$from,
130     'verbose'     => \$verbose,
131     'commit'      => \$commit
132 ) or pod2usage(1);
133 pod2usage(1) if $help;
134 pod2usage(1) unless $report_id && $notice && $module;
135
136 cronlogaction({ info => $command_line_options });
137
138 my ( $emails, $errors ) = C4::Reports::Guided::EmailReport({
139     email      => $email,
140     from       => $from,
141     report_id  => $report_id,
142     module     => $module,
143     code       => $notice,
144     branch     => $library,
145     verbose    => $verbose,
146     commit     => $commit,
147 });
148
149 foreach my $email (@$emails){
150     print "No emails will be sent!\n" unless $commit;
151     if( $verbose || !$commit ){
152         print "Email generated to $email->{to_address} from $email->{from_address}\n";
153         print "Content:\n";
154         print $email->{letter}->{content} ."\n";
155     }
156     C4::Letters::EnqueueLetter({
157         letter => $email->{letter},
158         borrowernumber         => $email->{borrowernumber},
159         message_transport_type => 'email',
160         from_address           => $email->{from_address},
161         to_address             => $email->{to_address},
162     }) if $commit;
163 }
164
165 if( $verbose || !$commit ){
166     foreach my $error ( @$errors ){
167         foreach ( keys %{$error} ){
168             print "$_\n";
169             if ( $_ eq 'FATAL' ) { print $error_msgs->{ ${$error}{$_} } }
170             else { print $error_msgs->{$_} . ${$error}{$_} . "\n" }
171         }
172     }
173 }
174
175 cronlogaction({ action => 'End', info => "COMPLETED" });