Bug 6810: Fix QA failures
[koha.git] / misc / cronjobs / membership_expiry.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright (C) 2015 Amit Gupta (amitddng135@gmail.com)
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 =head1 NAME
21
22 membership_expiry.pl - cron script to put membership expiry reminder into message queues
23
24 =head1 SYNOPSIS
25
26 ./membership_expiry.pl -c
27
28 or, in crontab:
29
30 0 1 * * * membership_expiry.pl -c
31
32 =head1 DESCRIPTION
33
34 This script sends membership expiry reminder notices to patrons.
35 It queues them in the message queue, which is processed by
36 the process_message_queue.pl cronjob.
37
38 =cut
39
40
41 use Modern::Perl;
42 use Getopt::Long;
43 use Pod::Usage;
44 use Data::Dumper;
45 BEGIN {
46     # find Koha's Perl modules
47     # test carefully before changing this
48     use FindBin;
49     eval { require "$FindBin::Bin/../kohalib.pl" };
50 }
51
52 use C4::Context;
53 use C4::Letters;
54 use C4::Dates qw/format_date/;
55 use C4::Log;
56
57
58 =head1 NAME
59
60 membership_expiry.pl - prepare messages to be sent to membership expiry reminder notices to patrons.
61
62 =head1 SYNOPSIS
63
64 membership_expiry.pl [-c]
65
66 =head1 OPTIONS
67
68 =over 8
69
70 =item B<--help>
71
72 Print a brief help message and exits.
73
74 =item B<--man>
75
76 Prints the manual page and exits.
77
78 =item B<-v>
79
80 Verbose. Without this flag set, only fatal errors are reported.
81
82 =item B<-n>
83
84 Do not send any email. Membership expire notices that would have been sent to
85 the patrons are printed to standard out.
86
87 =item B<-c>
88
89 Confirm flag: Add this option. The script will only print a usage
90 statement otherwise.
91
92 =back
93
94 =head1 DESCRIPTION
95
96 This script is designed to alert send membership expire notices
97
98 =head2 Configuration
99
100 This script pays attention to send the membership expire notices
101 The content of the messages is configured in Tools -> Notices and slips. Use the MEMBERSHIP_EXPIRY template
102
103 =head2 Outgoing emails
104
105 Typically, messages are prepared for each patron when the memberships are going to expire
106
107
108 These emails are staged in the outgoing message queue, as are messages
109 produced by other features of Koha. This message queue must be
110 processed regularly by the
111 F<misc/cronjobs/process_message_queue.pl> program.
112
113 In the event that the C<-n> flag is passed to this program, no emails
114 are sent. Instead, messages are sent on standard output from this
115 program.
116
117 =head2 Templates
118
119 Templates can contain variables enclosed in double angle brackets like
120 E<lt>E<lt>thisE<gt>E<gt>. Those variables will be replaced with values
121 specific to the members there membership expiry date is coming.
122 Available variables are:
123
124 =over
125
126 =item E<lt>E<lt>borrowers.*E<gt>E<gt>
127
128 any field from the borrowers table
129
130 =item E<lt>E<lt>branches.*E<gt>E<gt>
131
132 any field from the branches table
133
134 =back
135
136 =head1 SEE ALSO
137
138 The F<misc/cronjobs/membership_expiry.pl> program allows you to send
139 messages to patrons when the membership are going to expires.
140 =cut
141
142 # These are defaults for command line options.
143
144 my $confirm;                              # -c: Confirm that the user has read and configured this script.
145 my $nomail;                               # -n: No mail. Will not send any emails.
146 my $verbose= 0;                           # -v: verbose
147
148 my $help    = 0;
149 my $man     = 0;
150
151 GetOptions(
152             'help|?'         => \$help,
153             'man'            => \$man,
154             'c'              => \$confirm,
155             'n'              => \$nomail,
156             'v'              => \$verbose,
157        ) or pod2usage(2);
158 pod2usage(1) if $help;
159 pod2usage( -verbose => 2 ) if $man;;
160
161 my $usage = << 'ENDUSAGE';
162 This script prepares for membership expiry reminders to be sent to
163 patrons. It queues them in the message queue, which is processed by
164 the process_message_queue.pl cronjob.
165 See the comments in the script for directions on changing the script.
166 This script has the following parameters :
167     -c Confirm and remove this help & warning
168     -n send No mail. Instead, all mail messages are printed on screen. Useful for testing purposes.
169     -v verbose
170 ENDUSAGE
171
172 unless ($confirm) {
173     print $usage;
174     print "Do you wish to continue? (y/n)";
175     chomp($_ = <STDIN>);
176     exit unless (/^y/i);
177 }
178
179 cronlogaction();
180
181 my $admin_adress = C4::Context->preference('KohaAdminEmailAddress');
182 warn 'getting upcoming membership expires' if $verbose;
183 my $upcoming_mem_expires = C4::Members::GetUpcomingMembershipExpires();
184 warn 'found ' . scalar( @$upcoming_mem_expires ) . ' issues' if $verbose;
185
186
187 UPCOMINGMEMEXP: foreach my $recent ( @$upcoming_mem_expires ) {
188     my $from_address = $recent->{'branchemail'} || $admin_adress;
189     my $letter_type = 'MEMBERSHIP_EXPIRY';
190     my $letter = C4::Letters::getletter( 'members', $letter_type, $recent->{'branchcode'} );
191     die "no letter of type '$letter_type' found. Please see sample_notices.sql" unless $letter;
192
193     $letter = parse_letter({  letter    => $letter,
194                               borrowernumber => $recent->{'borrowernumber'},
195                               firstname => $recent->{'firstname'},
196                               categorycode  => $recent->{'categorycode'},
197                               branchcode => $recent->{'branchcode'},
198                           });
199     if ($letter) {
200         if ($nomail) {
201             print $letter->{'content'};
202         } else {
203              C4::Letters::EnqueueLetter( {  letter               => $letter,
204                                             borrowernumber       =>  $recent->{'borrowernumber'},
205                                             from_address           => $from_address,
206                                             message_transport_type => 'email',
207                                         } );
208          }
209        }
210     }
211
212
213 =head1 METHODS
214
215 =head2 parse_letter
216
217 =cut
218
219 sub parse_letter {
220     my $params = shift;
221     foreach my $required ( qw( letter borrowernumber ) ) {
222         return unless exists $params->{$required};
223     }
224     my $letter =  C4::Letters::GetPreparedLetter (
225             module => 'members',
226             letter_code => 'MEMBERSHIP_EXPIRY',
227             tables => {'borrowers', $params->{'borrowernumber'}, 'branches', $params->{'branchcode'}},
228     );
229 }
230
231 1;
232
233 __END__