3 # This file is part of Koha.
5 # Copyright 2023 Koha development team
6 # Copyright 2015 Amit Gupta (amitddng135@gmail.com)
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23 membership_expiry.pl - cron script to put membership expiry reminders into the message queue
27 ./membership_expiry.pl -c
31 0 1 * * * membership_expiry.pl -c
34 --help brief help message
35 --man full documentation
36 --where <conditions> where clause to add to the query
37 -v -verbose verbose mode
38 -n --nomail if supplied, messages will be output to STDOUT and no email or sms will be sent
39 -c --confirm commit changes to db, no action will be taken unless this switch is included
40 -b --branch <branchname> only deal with patrons from this library/branch
41 --before=X include patrons expiring a number of days BEFORE the date set by the preference
42 --after=X include patrons expiring a number of days AFTER the date set by the preference
43 -l --letter <lettercode> use a specific notice rather than the default
44 --active=X only deal with active patrons (active within X months)
45 --inactive=X only deal with inactive patrons (inactive within X months)
46 --renew renew patrons and send notice (instead of expiry notice only)
50 This script sends membership expiry reminder notices to patrons, by email and sms.
51 It queues them in the message queue, which is processed by
52 the process_message_queue.pl cronjob.
60 Print a brief help message and exits.
64 Prints the manual page and exits.
68 Verbose. Without this flag set, only fatal errors are reported.
72 Do not send any email. Membership expire notices that would have been sent to
73 the patrons are printed to standard out.
77 Confirm flag: Add this option. The script will only print a usage
82 Optional branchcode to restrict the cronjob to that branch.
86 Optional parameter to extend the selection with a number of days BEFORE
87 the date set by the preference.
91 Optional parameter to extend the selection with a number of days AFTER
92 the date set by the preference.
96 Use this option to specify a condition built with columns from the borrowers table
99 --where 'lastseen IS NOT NULL'
100 will only notify patrons who have been seen.
104 Optional parameter to use another notice than the default: MEMBERSHIP_EXPIRY
108 Optional parameter to include active patrons only (active within passed number of months).
112 Optional parameter to include inactive patrons only (inactive within passed number of months).
113 This allows you to e.g. send expiry warnings only to inactive patrons.
117 Optional parameter to automatically renew patrons instead of sending them an expiry notice.
118 They will be informed by a patron renewal notice.
124 The content of the messages is configured in Tools -> Notices and slips. Use the MEMBERSHIP_EXPIRY notice or
125 supply another via the parameters.
127 Typically, messages are prepared for each patron when the memberships are going to expire.
129 These emails are staged in the outgoing message queue, as are messages
130 produced by other features of Koha. This message queue must be
131 processed regularly by the
132 F<misc/cronjobs/process_message_queue.pl> program.
134 In the event that the C<-n> flag is passed to this program, no emails
135 are sent. Instead, messages are sent on standard output from this
138 Notices can contain variables enclosed in double angle brackets like
139 E<lt>E<lt>thisE<gt>E<gt>. Those variables will be replaced with values
140 specific to the soon expiring members.
141 Available variables are:
145 =item E<lt>E<lt>borrowers.*E<gt>E<gt>
147 any field from the borrowers table
149 =item E<lt>E<lt>branches.*E<gt>E<gt>
151 any field from the branches table
158 use Getopt::Long qw( GetOptions );
159 use Pod::Usage qw( pod2usage );
161 use Koha::Script -cron;
164 use C4::Log qw( cronlogaction );
168 # These are defaults for command line options.
169 my $confirm; # -c: Confirm that the user has read and configured this script.
170 my $nomail; # -n: No mail. Will not send any emails.
171 my $verbose = 0; # -v: verbose
176 my ( $branch, $letter_type );
182 my $command_line_options = join(" ",@ARGV);
190 'branch:s' => \$branch,
191 'before:i' => \$before,
192 'after:i' => \$after,
193 'letter:s' => \$letter_type,
194 'where=s' => \@where,
195 'active:i' => \$active,
196 'inactive:i' => \$inactive,
200 pod2usage( -verbose => 2 ) if $man;
201 pod2usage(1) if $help || !$confirm;
202 if ( defined($active) && defined($inactive) ) {
203 print "Sorry, it is not possible to pass both -active as well as -inactive.\n";
207 cronlogaction({ info => $command_line_options });
209 my $expdays = C4::Context->preference('MembershipExpiryDaysNotice');
211 #If the pref is not set, we will exit
212 warn 'Exiting membership_expiry.pl: MembershipExpiryDaysNotice not set'
217 warn 'getting upcoming membership expires' if $verbose;
218 my $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires(
220 ( $branch ? ( 'me.branchcode' => $branch ) : () ),
226 my $where_literal = join ' AND ', @where;
227 $upcoming_mem_expires = $upcoming_mem_expires->search( \$where_literal ) if @where;
229 warn 'found ' . $upcoming_mem_expires->count . ' soon expiring members'
233 $letter_type = 'MEMBERSHIP_EXPIRY' if !$letter_type;
234 my ( $count_skipped, $count_renewed, $count_enqueued ) = ( 0, 0, 0 );
235 while ( my $recent = $upcoming_mem_expires->next ) {
236 if ( $active && !$recent->is_active( { months => $active } ) ) {
239 } elsif ( $inactive && $recent->is_active( { months => $inactive } ) ) {
246 $recent->renew_account;
247 $which_notice = 'MEMBERSHIP_RENEWED';
250 $which_notice = $letter_type;
253 my $from_address = $recent->library->from_email_address;
254 my $letter = C4::Letters::GetPreparedLetter(
256 letter_code => $which_notice,
257 branchcode => $recent->branchcode,
258 lang => $recent->lang,
260 borrowers => $recent->borrowernumber,
261 branches => $recent->branchcode,
264 last if !$letter; # Letters.pm already warned, just exit
266 print $letter->{'content'}."\n";
270 C4::Letters::EnqueueLetter({
272 borrowernumber => $recent->borrowernumber,
273 from_address => $from_address,
274 message_transport_type => 'email',
278 if ($recent->smsalertnumber) {
279 my $smsletter = C4::Letters::GetPreparedLetter(
281 letter_code => $which_notice,
282 branchcode => $recent->branchcode,
283 lang => $recent->lang,
285 borrowers => $recent->borrowernumber,
286 branches => $recent->branchcode,
288 message_transport_type => 'sms',
291 C4::Letters::EnqueueLetter({
292 letter => $smsletter,
293 borrowernumber => $recent->borrowernumber,
294 message_transport_type => 'sms',
301 print "Membership renewed for $count_renewed patrons\n" if $count_renewed;
302 print "Enqueued notices for $count_enqueued patrons\n";
303 print "Skipped $count_skipped inactive patrons\n" if $active;
304 print "Skipped $count_skipped active patrons\n" if $inactive;
307 cronlogaction({ action => 'End', info => "COMPLETED" });