Bug 22755: Fix pod in patron_emailer.pl
[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 BEGIN {
22     # find Koha's Perl modules
23     # test carefully before changing this
24     use FindBin;
25     eval { require "$FindBin::Bin/../kohalib.pl" };
26 }
27
28 use Koha::Script -cron;
29 use Getopt::Long;
30 use Pod::Usage;
31
32 use C4::Log;
33 use C4::Reports::Guided;
34
35 cronlogaction();
36
37 =head1 NAME
38
39 patron_emailer.pl
40
41 =head1 SYNOPSIS
42
43 patron_emailer.pl
44     [--report ][--notice][--module] --library  --from
45
46  Options:
47     --help                       brief help
48     --report                     report ID to use as data for email template
49     --notice                     specific notice code to use
50     --module                     which module to find the above notice in
51     --library                    specified branch for selecting notice, will use all libraries by default
52     --from                       specified email for 'from' address, report column 'from' used if not specified
53     --email                      specified column to use as 'to' email address, report column 'email' used if not specified
54     --verbose                    increased verbosity, will print notices and errors
55     --commit                     send emails, without this script will only report
56
57 =head1 OPTIONS
58
59 =over 8
60
61 =item B<-help>
62
63 Print brief help and exit.
64
65 =item B<-man>
66
67 Print full documentation and exit.
68
69 =item B<-report>
70
71 Specify a saved SQL report id in the Koha system to user for the emails. All, and only,
72     columns in the report will be available for notice template variables
73
74 =item B<-notice>
75
76 Specific notice (CODE) to select
77
78 =item B<-module>
79
80 Which module to find the specified notice in
81
82 =item B<-library>
83
84 Option to specify which branches notice should be used, 'All libraries' is used if not specified
85
86 =item B<-from>
87
88 Specify the sender address of the email, if not specified a 'from' column in the report will be used.
89
90 =item B<-email>
91
92 Specify the column to find recipient address of the email, if not specified an 'email' column in the report will be used.
93
94 =item B<-verbose>
95
96 Increased verbosity, reports successes and errors.
97
98 =item B<-commit>
99
100 Send emails, if omitted script will report as verbose.
101
102 =back
103
104 =cut
105
106 my $help     = 0;
107 my $report_id;
108 my $notice;
109 my $module;  #this is only for selecting correct notice - report itself defines available columns, not module
110 my $library; #as above, determines which notice to use, will use 'all libraries' if not specified
111 my $email;   #to specify which column should be used as email in report will use 'email' from borrwers table
112 my $from;    #to specify from address, will expect 'from' column in report if not specified
113 my $verbose  = 0;
114 my $commit   = 0;
115
116 my $error_msgs = {
117     MISSING_PARAMS => "You must supply a report ID, letter module and code at minimum\n",
118     NO_LETTER      => "The specified letter was not found, please check your input\n",
119     NO_REPORT      => "The specified report was not found, please check your input\n",
120     REPORT_FAIL    => "There was an error running the report, please check your SQL\n",
121     NO_BOR_COL     => "There was no borrowernumber found for row ",
122     NO_EMAIL_COL   => "There was no email found for row ",
123     NO_FROM_COL    => "No from email was specified for row ",
124     NO_BOR         => "There is no borrower with borrowernumber "
125 };
126
127 GetOptions(
128     'help|?'      => \$help,
129     'report=i'    => \$report_id,
130     'notice=s'    => \$notice,
131     'module=s'    => \$module,
132     'library=s'   => \$library,
133     'email=s'     => \$email,
134     'from=s'      => \$from,
135     'verbose'     => \$verbose,
136     'commit'      => \$commit
137 ) or pod2usage(1);
138 pod2usage(1) if $help;
139 pod2usage(1) unless $report_id && $notice && $module;
140
141 my ( $emails, $errors ) = C4::Reports::Guided::EmailReport({
142     email      => $email,
143     from       => $from,
144     report_id  => $report_id,
145     module     => $module,
146     code       => $notice,
147     branch     => $library,
148     verbose    => $verbose,
149     commit     => $commit,
150 });
151
152 foreach my $email (@$emails){
153     print "No emails will be sent!\n" unless $commit;
154     if( $verbose || !$commit ){
155         print "Email generated to $email->{to_address} from $email->{from_address}\n";
156         print "Content:\n";
157         print $email->{letter}->{content} ."\n";
158     }
159     C4::Letters::EnqueueLetter({
160         letter => $email->{letter},
161         borrowernumber         => $email->{borrowernumber},
162         message_transport_type => 'email',
163         from_address           => $email->{from_address},
164         to_address             => $email->{to_address},
165     }) if $commit;
166 }
167
168 if( $verbose || !$commit ){
169     foreach my $error ( @$errors ){
170         foreach ( keys %{$error} ){
171             print "$_\n";
172             if ( $_ eq 'FATAL' ) { print $error_msgs->{ ${$error}{$_} } }
173             else { print $error_msgs->{$_} . ${$error}{$_} . "\n" }
174         }
175     }
176 }