Bug 34587: Move everything into a ERM/EUsage subfolder
[koha.git] / misc / cronjobs / membership_expiry.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2023 Koha development team
6 # Copyright 2015 Amit Gupta (amitddng135@gmail.com)
7 #
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.
12 #
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.
17 #
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>.
20
21 =head1 NAME
22
23 membership_expiry.pl - cron script to put membership expiry reminders into the message queue
24
25 =head1 SYNOPSIS
26
27 ./membership_expiry.pl -c [-v] [-n] [-branch CODE] [-before DAYS] [-after DAYS] [-where COND] [-renew] [-letter X] [-letter-renew Y] [-active|-inactive]
28
29 or, in crontab:
30
31 0 1 * * * membership_expiry.pl -c [other options you need as mentioned above]
32
33 =head1 DESCRIPTION
34
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.
38
39 =head1 OPTIONS
40
41 =over 8
42
43 =item B<--help>
44
45 Print a brief help message and exits.
46
47 =item B<--man>
48
49 Prints the manual page and exits.
50
51 =item B<-v>
52
53 Verbose. Without this flag set, only fatal errors are reported.
54
55 =item B<-n>
56
57 Do not send any email. Membership expire notices that would have been sent to
58 the patrons are printed to standard out.
59
60 =item B<-c>
61
62 Confirm flag: Add this option. The script will only print a usage
63 statement otherwise.
64
65 =item B<-branch>
66
67 Optional branchcode to restrict the cronjob to that branch.
68
69 =item B<-before>
70
71 Optional parameter to extend the selection with a number of days BEFORE
72 the date set by the preference.
73
74 =item B<-after>
75
76 Optional parameter to extend the selection with a number of days AFTER
77 the date set by the preference.
78
79 =item B<-where>
80
81 Use this option to specify a condition built with columns from the borrowers table
82
83 e.g.
84 --where 'lastseen IS NOT NULL'
85 will only notify patrons who have been seen.
86
87 =item B<-letter>
88
89 Optional parameter to use another expiry notice than the default: MEMBERSHIP_EXPIRY
90
91 =item B<-letter_renew>
92
93 Optional parameter to use another renewal notice than the default: MEMBERSHIP_RENEWED
94
95 =item B<-active>
96
97 Optional parameter to include active patrons only (active within passed number of months).
98 This parameter needs the preference TrackLastPatronActivityTriggers.
99
100 IMPORTANT: You should be using those triggers already for the period that you
101 consider a user to be (in)active.
102
103 =item B<-inactive>
104
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.
108
109 IMPORTANT: You should be using those triggers already for the period that you
110 consider a user to be (in)active.
111
112 =item B<-renew>
113
114 Optional parameter to automatically renew patrons instead of sending them an expiry notice.
115 They will be informed by a patron renewal notice.
116
117 =back
118
119 =head1 CONFIGURATION
120
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.
123
124 Typically, messages are prepared for each patron when the memberships are going to expire.
125
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.
130
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
133 program.
134
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:
139
140 =over
141
142 =item E<lt>E<lt>borrowers.*E<gt>E<gt>
143
144 any field from the borrowers table
145
146 =item E<lt>E<lt>branches.*E<gt>E<gt>
147
148 any field from the branches table
149
150 =back
151
152 =cut
153
154 use Modern::Perl;
155 use Getopt::Long qw( GetOptions );
156 use Pod::Usage qw( pod2usage );
157
158 use Koha::Script -cron;
159 use C4::Context;
160 use C4::Letters;
161 use C4::Log qw( cronlogaction );
162
163 use Koha::Patrons;
164
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
169 my $help    = 0;
170 my $man     = 0;
171 my $before  = 0;
172 my $after   = 0;
173 my $branch;
174 my @where;
175 my $active;
176 my $inactive;
177 my $renew;
178 my $letter_expiry;
179 my $letter_renew;
180
181 my $command_line_options = join(" ",@ARGV);
182
183 GetOptions(
184     'help|?'         => \$help,
185     'man'            => \$man,
186     'c'              => \$confirm,
187     'n'              => \$nomail,
188     'v'              => \$verbose,
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,
197     'renew'          => \$renew,
198 ) or pod2usage(2);
199 $letter_expiry = 'MEMBERSHIP_EXPIRY'  if !$letter_expiry;
200 $letter_renew  = 'MEMBERSHIP_RENEWED' if !$letter_renew;
201
202 pod2usage( -verbose => 2 ) if $man;
203 pod2usage(1) if $help || !$confirm;
204
205 # Check active/inactive. Note that passing no value or zero is a no-op.
206 if ( !C4::Context->preference('TrackLastPatronActivityTriggers')
207     && ( $active || $inactive ) )
208 {
209     pod2usage(
210         -verbose => 1,
211         -msg     =>
212             q{Exiting membership_expiry.pl: Using --active or --inactive needs use of TrackLastPatronActivityTriggers over specified period},
213         -exitval => 1
214     );
215 } elsif ( $active && $inactive ) {
216     pod2usage(
217         -verbose => 1,
218         -msg     => q{The --active and --inactive flags are mutually exclusive},
219         -exitval => 1
220     );
221 } elsif ( ( defined $active && !$active ) || ( defined $inactive && !$inactive ) ) {
222     pod2usage(
223         -verbose => 1,
224         -msg     => q{Options --active and --inactive need a number of months},
225         -exitval => 1
226     );
227 }
228
229 cronlogaction({ info => $command_line_options });
230
231 my $expdays = C4::Context->preference('MembershipExpiryDaysNotice');
232 if( !$expdays ) {
233     #If the pref is not set, we will exit
234     warn 'Exiting membership_expiry.pl: MembershipExpiryDaysNotice not set'
235         if $verbose;
236     exit;
237 }
238
239 warn 'getting upcoming membership expires' if $verbose;
240 my $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires(
241     {
242         ( $branch ? ( 'me.branchcode' => $branch ) : () ),
243         before => $before,
244         after  => $after,
245     }
246 );
247
248 my $where_literal = join ' AND ', @where;
249 $upcoming_mem_expires = $upcoming_mem_expires->search( \$where_literal ) if @where;
250
251 warn 'found ' . $upcoming_mem_expires->count . ' soon expiring members'
252     if $verbose;
253
254 # main loop
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 } ) ) {
258         $count_skipped++;
259         next;
260     } elsif ( $inactive && $recent->is_active( { months => $inactive } ) ) {
261         $count_skipped++;
262         next;
263     }
264
265     my $which_notice;
266     if ($renew) {
267         $recent->renew_account;
268         $which_notice = $letter_renew;
269         $count_renewed++;
270     } else {
271         $which_notice = $letter_expiry;
272     }
273
274     my $from_address = $recent->library->from_email_address;
275     my $letter =  C4::Letters::GetPreparedLetter(
276         module      => 'members',
277         letter_code => $which_notice,
278         branchcode  => $recent->branchcode,
279         lang        => $recent->lang,
280         tables      => {
281             borrowers => $recent->borrowernumber,
282             branches  => $recent->branchcode,
283         },
284     );
285     last if !$letter;    # Letters.pm already warned, just exit
286     if ($nomail) {
287         print $letter->{'content'}."\n";
288         next;
289     }
290
291     C4::Letters::EnqueueLetter({
292         letter                 => $letter,
293         borrowernumber         =>  $recent->borrowernumber,
294         from_address           => $from_address,
295         message_transport_type => 'email',
296     });
297     $count_enqueued++;
298
299     if ($recent->smsalertnumber) {
300         my $smsletter = C4::Letters::GetPreparedLetter(
301             module      => 'members',
302             letter_code => $which_notice,
303             branchcode  => $recent->branchcode,
304             lang        => $recent->lang,
305             tables      => {
306                 borrowers => $recent->borrowernumber,
307                 branches  => $recent->branchcode,
308             },
309             message_transport_type => 'sms',
310         );
311         if ($smsletter) {
312             C4::Letters::EnqueueLetter({
313                 letter                 => $smsletter,
314                 borrowernumber         => $recent->borrowernumber,
315                 message_transport_type => 'sms',
316             });
317         }
318     }
319 }
320
321 if ($verbose) {
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;
326 }
327
328 cronlogaction({ action => 'End', info => "COMPLETED" });