Bug 28688: Add renew option to membership_expiry.pl
[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
28
29 or, in crontab:
30
31 0 1 * * * membership_expiry.pl -c
32
33 Options:
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)
47
48 =head1 DESCRIPTION
49
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.
53
54 =head1 OPTIONS
55
56 =over 8
57
58 =item B<--help>
59
60 Print a brief help message and exits.
61
62 =item B<--man>
63
64 Prints the manual page and exits.
65
66 =item B<-v>
67
68 Verbose. Without this flag set, only fatal errors are reported.
69
70 =item B<-n>
71
72 Do not send any email. Membership expire notices that would have been sent to
73 the patrons are printed to standard out.
74
75 =item B<-c>
76
77 Confirm flag: Add this option. The script will only print a usage
78 statement otherwise.
79
80 =item B<-branch>
81
82 Optional branchcode to restrict the cronjob to that branch.
83
84 =item B<-before>
85
86 Optional parameter to extend the selection with a number of days BEFORE
87 the date set by the preference.
88
89 =item B<-after>
90
91 Optional parameter to extend the selection with a number of days AFTER
92 the date set by the preference.
93
94 =item B<-where>
95
96 Use this option to specify a condition built with columns from the borrowers table
97
98 e.g.
99 --where 'lastseen IS NOT NULL'
100 will only notify patrons who have been seen.
101
102 =item B<-letter>
103
104 Optional parameter to use another notice than the default: MEMBERSHIP_EXPIRY
105
106 =item B<-active>
107
108 Optional parameter to include active patrons only (active within passed number of months).
109
110 =item B<-inactive>
111
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.
114
115 =item B<-renew>
116
117 Optional parameter to automatically renew patrons instead of sending them an expiry notice.
118 They will be informed by a patron renewal notice.
119
120 =back
121
122 =head1 CONFIGURATION
123
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.
126
127 Typically, messages are prepared for each patron when the memberships are going to expire.
128
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.
133
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
136 program.
137
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:
142
143 =over
144
145 =item E<lt>E<lt>borrowers.*E<gt>E<gt>
146
147 any field from the borrowers table
148
149 =item E<lt>E<lt>branches.*E<gt>E<gt>
150
151 any field from the branches table
152
153 =back
154
155 =cut
156
157 use Modern::Perl;
158 use Getopt::Long qw( GetOptions );
159 use Pod::Usage qw( pod2usage );
160
161 use Koha::Script -cron;
162 use C4::Context;
163 use C4::Letters;
164 use C4::Log qw( cronlogaction );
165
166 use Koha::Patrons;
167
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
172 my $help    = 0;
173 my $man     = 0;
174 my $before  = 0;
175 my $after   = 0;
176 my ( $branch, $letter_type );
177 my @where;
178 my $active;
179 my $inactive;
180 my $renew;
181
182 my $command_line_options = join(" ",@ARGV);
183
184 GetOptions(
185     'help|?'         => \$help,
186     'man'            => \$man,
187     'c'              => \$confirm,
188     'n'              => \$nomail,
189     'v'              => \$verbose,
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,
197     'renew'          => \$renew,
198 ) or pod2usage(2);
199
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";
204     exit;
205 }
206
207 cronlogaction({ info => $command_line_options });
208
209 my $expdays = C4::Context->preference('MembershipExpiryDaysNotice');
210 if( !$expdays ) {
211     #If the pref is not set, we will exit
212     warn 'Exiting membership_expiry.pl: MembershipExpiryDaysNotice not set'
213         if $verbose;
214     exit;
215 }
216
217 warn 'getting upcoming membership expires' if $verbose;
218 my $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires(
219     {
220         ( $branch ? ( 'me.branchcode' => $branch ) : () ),
221         before => $before,
222         after  => $after,
223     }
224 );
225
226 my $where_literal = join ' AND ', @where;
227 $upcoming_mem_expires = $upcoming_mem_expires->search( \$where_literal ) if @where;
228
229 warn 'found ' . $upcoming_mem_expires->count . ' soon expiring members'
230     if $verbose;
231
232 # main loop
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 } ) ) {
237         $count_skipped++;
238         next;
239     } elsif ( $inactive && $recent->is_active( { months => $inactive } ) ) {
240         $count_skipped++;
241         next;
242     }
243
244     my $which_notice;
245     if ($renew) {
246         $recent->renew_account;
247         $which_notice = 'MEMBERSHIP_RENEWED';
248         $count_renewed++;
249     } else {
250         $which_notice = $letter_type;
251     }
252
253     my $from_address = $recent->library->from_email_address;
254     my $letter =  C4::Letters::GetPreparedLetter(
255         module      => 'members',
256         letter_code => $which_notice,
257         branchcode  => $recent->branchcode,
258         lang        => $recent->lang,
259         tables      => {
260             borrowers => $recent->borrowernumber,
261             branches  => $recent->branchcode,
262         },
263     );
264     last if !$letter;    # Letters.pm already warned, just exit
265     if ($nomail) {
266         print $letter->{'content'}."\n";
267         next;
268     }
269
270     C4::Letters::EnqueueLetter({
271         letter                 => $letter,
272         borrowernumber         =>  $recent->borrowernumber,
273         from_address           => $from_address,
274         message_transport_type => 'email',
275     });
276     $count_enqueued++;
277
278     if ($recent->smsalertnumber) {
279         my $smsletter = C4::Letters::GetPreparedLetter(
280             module      => 'members',
281             letter_code => $which_notice,
282             branchcode  => $recent->branchcode,
283             lang        => $recent->lang,
284             tables      => {
285                 borrowers => $recent->borrowernumber,
286                 branches  => $recent->branchcode,
287             },
288             message_transport_type => 'sms',
289         );
290         if ($smsletter) {
291             C4::Letters::EnqueueLetter({
292                 letter                 => $smsletter,
293                 borrowernumber         => $recent->borrowernumber,
294                 message_transport_type => 'sms',
295             });
296         }
297     }
298 }
299
300 if ($verbose) {
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;
305 }
306
307 cronlogaction({ action => 'End', info => "COMPLETED" });