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 [-v] [-n] [-branch CODE] [-before DAYS] [-after DAYS] [-where COND] [-renew] [-letter X] [-letter-renew Y] [-active|-inactive]
31 0 1 * * * membership_expiry.pl -c [other options you need as mentioned above]
35 This script sends membership expiry reminder notices to patrons, by email and sms.
36 It queues them in the message queue, which is processed by
37 the process_message_queue.pl cronjob.
45 Print a brief help message and exits.
49 Prints the manual page and exits.
53 Verbose. Without this flag set, only fatal errors are reported.
57 Do not send any email. Membership expire notices that would have been sent to
58 the patrons are printed to standard out.
62 Confirm flag: Add this option. The script will only print a usage
67 Optional branchcode to restrict the cronjob to that branch.
71 Optional parameter to extend the selection with a number of days BEFORE
72 the date set by the preference.
76 Optional parameter to extend the selection with a number of days AFTER
77 the date set by the preference.
81 Use this option to specify a condition built with columns from the borrowers table
84 --where 'lastseen IS NOT NULL'
85 will only notify patrons who have been seen.
89 Optional parameter to use another expiry notice than the default: MEMBERSHIP_EXPIRY
91 =item B<-letter_renew>
93 Optional parameter to use another renewal notice than the default: MEMBERSHIP_RENEWED
97 Optional parameter to include active patrons only (active within passed number of months).
98 This parameter needs the preference TrackLastPatronActivityTriggers.
100 IMPORTANT: You should be using those triggers already for the period that you
101 consider a user to be (in)active.
105 Optional parameter to include inactive patrons only (inactive within passed number of months).
106 This allows you to e.g. send expiry warnings only to inactive patrons.
107 This parameter needs the preference TrackLastPatronActivityTriggers.
109 IMPORTANT: You should be using those triggers already for the period that you
110 consider a user to be (in)active.
114 Optional parameter to automatically renew patrons instead of sending them an expiry notice.
115 They will be informed by a patron renewal notice.
121 The content of the messages is configured in Tools -> Notices and slips. Use the MEMBERSHIP_EXPIRY notice or
122 supply another via the parameters.
124 Typically, messages are prepared for each patron when the memberships are going to expire.
126 These emails are staged in the outgoing message queue, as are messages
127 produced by other features of Koha. This message queue must be
128 processed regularly by the
129 F<misc/cronjobs/process_message_queue.pl> program.
131 In the event that the C<-n> flag is passed to this program, no emails
132 are sent. Instead, messages are sent on standard output from this
135 Notices can contain variables enclosed in double angle brackets like
136 E<lt>E<lt>thisE<gt>E<gt>. Those variables will be replaced with values
137 specific to the soon expiring members.
138 Available variables are:
142 =item E<lt>E<lt>borrowers.*E<gt>E<gt>
144 any field from the borrowers table
146 =item E<lt>E<lt>branches.*E<gt>E<gt>
148 any field from the branches table
155 use Getopt::Long qw( GetOptions );
156 use Pod::Usage qw( pod2usage );
158 use Koha::Script -cron;
161 use C4::Log qw( cronlogaction );
165 # These are defaults for command line options.
166 my $confirm; # -c: Confirm that the user has read and configured this script.
167 my $nomail; # -n: No mail. Will not send any emails.
168 my $verbose = 0; # -v: verbose
181 my $command_line_options = join(" ",@ARGV);
189 'branch:s' => \$branch,
190 'before:i' => \$before,
191 'after:i' => \$after,
192 'letter:s' => \$letter_expiry,
193 'letter_renew:s' => \$letter_renew,
194 'where=s' => \@where,
195 'active:i' => \$active,
196 'inactive:i' => \$inactive,
199 $letter_expiry = 'MEMBERSHIP_EXPIRY' if !$letter_expiry;
200 $letter_renew = 'MEMBERSHIP_RENEWED' if !$letter_renew;
202 pod2usage( -verbose => 2 ) if $man;
203 pod2usage(1) if $help || !$confirm;
205 # Check active/inactive. Note that passing no value or zero is a no-op.
206 if ( !C4::Context->preference('TrackLastPatronActivityTriggers')
207 && ( $active || $inactive ) )
212 q{Exiting membership_expiry.pl: Using --active or --inactive needs use of TrackLastPatronActivityTriggers over specified period},
215 } elsif ( $active && $inactive ) {
218 -msg => q{The --active and --inactive flags are mutually exclusive},
221 } elsif ( ( defined $active && !$active ) || ( defined $inactive && !$inactive ) ) {
224 -msg => q{Options --active and --inactive need a number of months},
229 cronlogaction({ info => $command_line_options });
231 my $expdays = C4::Context->preference('MembershipExpiryDaysNotice');
233 #If the pref is not set, we will exit
234 warn 'Exiting membership_expiry.pl: MembershipExpiryDaysNotice not set'
239 warn 'getting upcoming membership expires' if $verbose;
240 my $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires(
242 ( $branch ? ( 'me.branchcode' => $branch ) : () ),
248 my $where_literal = join ' AND ', @where;
249 $upcoming_mem_expires = $upcoming_mem_expires->search( \$where_literal ) if @where;
251 warn 'found ' . $upcoming_mem_expires->count . ' soon expiring members'
255 my ( $count_skipped, $count_renewed, $count_enqueued ) = ( 0, 0, 0 );
256 while ( my $recent = $upcoming_mem_expires->next ) {
257 if ( $active && !$recent->is_active( { months => $active } ) ) {
260 } elsif ( $inactive && $recent->is_active( { months => $inactive } ) ) {
267 $recent->renew_account;
268 $which_notice = $letter_renew;
271 $which_notice = $letter_expiry;
274 my $from_address = $recent->library->from_email_address;
275 my $letter = C4::Letters::GetPreparedLetter(
277 letter_code => $which_notice,
278 branchcode => $recent->branchcode,
279 lang => $recent->lang,
281 borrowers => $recent->borrowernumber,
282 branches => $recent->branchcode,
285 last if !$letter; # Letters.pm already warned, just exit
287 print $letter->{'content'}."\n";
291 C4::Letters::EnqueueLetter({
293 borrowernumber => $recent->borrowernumber,
294 from_address => $from_address,
295 message_transport_type => 'email',
299 if ($recent->smsalertnumber) {
300 my $smsletter = C4::Letters::GetPreparedLetter(
302 letter_code => $which_notice,
303 branchcode => $recent->branchcode,
304 lang => $recent->lang,
306 borrowers => $recent->borrowernumber,
307 branches => $recent->branchcode,
309 message_transport_type => 'sms',
312 C4::Letters::EnqueueLetter({
313 letter => $smsletter,
314 borrowernumber => $recent->borrowernumber,
315 message_transport_type => 'sms',
322 print "Membership renewed for $count_renewed patrons\n" if $count_renewed;
323 print "Enqueued notices for $count_enqueued patrons\n";
324 print "Skipped $count_skipped inactive patrons\n" if $active;
325 print "Skipped $count_skipped active patrons\n" if $inactive;
328 cronlogaction({ action => 'End', info => "COMPLETED" });