3 # This file is part of Koha.
5 # Copyright (C) 2015 Amit Gupta (amitddng135@gmail.com)
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.
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.
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>.
22 membership_expiry.pl - cron script to put membership expiry reminder into message queues
26 ./membership_expiry.pl -c
30 0 1 * * * membership_expiry.pl -c
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.
46 # find Koha's Perl modules
47 # test carefully before changing this
49 eval { require "$FindBin::Bin/../kohalib.pl" };
54 use C4::Dates qw/format_date/;
60 membership_expiry.pl - prepare messages to be sent to membership expiry reminder notices to patrons.
64 membership_expiry.pl [-c]
72 Print a brief help message and exits.
76 Prints the manual page and exits.
80 Verbose. Without this flag set, only fatal errors are reported.
84 Do not send any email. Membership expire notices that would have been sent to
85 the patrons are printed to standard out.
89 Confirm flag: Add this option. The script will only print a usage
96 This script is designed to alert send membership expire notices
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
103 =head2 Outgoing emails
105 Typically, messages are prepared for each patron when the memberships are going to expire
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.
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
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:
126 =item E<lt>E<lt>borrowers.*E<gt>E<gt>
128 any field from the borrowers table
130 =item E<lt>E<lt>branches.*E<gt>E<gt>
132 any field from the branches table
138 The F<misc/cronjobs/membership_expiry.pl> program allows you to send
139 messages to patrons when the membership are going to expires.
142 # These are defaults for command line options.
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
158 pod2usage(1) if $help;
159 pod2usage( -verbose => 2 ) if $man;;
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.
174 print "Do you wish to continue? (y/n)";
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;
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;
193 $letter = parse_letter({ letter => $letter,
194 borrowernumber => $recent->{'borrowernumber'},
195 firstname => $recent->{'firstname'},
196 categorycode => $recent->{'categorycode'},
197 branchcode => $recent->{'branchcode'},
201 print $letter->{'content'};
203 C4::Letters::EnqueueLetter( { letter => $letter,
204 borrowernumber => $recent->{'borrowernumber'},
205 from_address => $from_address,
206 message_transport_type => 'email',
221 foreach my $required ( qw( letter borrowernumber ) ) {
222 return unless exists $params->{$required};
224 my $letter = C4::Letters::GetPreparedLetter (
226 letter_code => 'MEMBERSHIP_EXPIRY',
227 tables => {'borrowers', $params->{'borrowernumber'}, 'branches', $params->{'branchcode'}},