Bug 28456: Add WHERE option to membership_expiry cronjob
[koha.git] / misc / cronjobs / membership_expiry.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright (C) 2015 Amit Gupta (amitddng135@gmail.com)
6 #
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.
11 #
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.
16 #
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>.
19
20 =head1 NAME
21
22 membership_expiry.pl - cron script to put membership expiry reminders into the message queue
23
24 =head1 SYNOPSIS
25
26 ./membership_expiry.pl -c
27
28 or, in crontab:
29
30 0 1 * * * membership_expiry.pl -c
31
32 Options:
33    --help                   brief help message
34    --man                    full documentation
35    --where <conditions>     where clause to add to the query
36    -v -verbose              verbose mode
37    -n --nomail              if supplied messages will be output to STDOUT and not sent
38    -c --confirm             commit changes to db, no action will be taken unless this switch is included
39    -b --branch <branchname> only deal with patrons from this library/branch
40    --before=X               include patrons expiring a number of days BEFORE the date set by the preference
41    --after=X                include patrons expiring a number of days AFTER  the date set by the preference
42    -l --letter <lettercode> use a specific notice rather than the default
43
44 =head1 DESCRIPTION
45
46 This script sends membership expiry reminder notices to patrons.
47 It queues them in the message queue, which is processed by
48 the process_message_queue.pl cronjob.
49
50 =head1 OPTIONS
51
52 =over 8
53
54 =item B<--help>
55
56 Print a brief help message and exits.
57
58 =item B<--man>
59
60 Prints the manual page and exits.
61
62 =item B<-v>
63
64 Verbose. Without this flag set, only fatal errors are reported.
65
66 =item B<-n>
67
68 Do not send any email. Membership expire notices that would have been sent to
69 the patrons are printed to standard out.
70
71 =item B<-c>
72
73 Confirm flag: Add this option. The script will only print a usage
74 statement otherwise.
75
76 =item B<-branch>
77
78 Optional branchcode to restrict the cronjob to that branch.
79
80 =item B<-before>
81
82 Optional parameter to extend the selection with a number of days BEFORE
83 the date set by the preference.
84
85 =item B<-after>
86
87 Optional parameter to extend the selection with a number of days AFTER
88 the date set by the preference.
89
90 =item B<-where>
91
92 Use this option to specify a condition built with columns from the borrowers table
93
94 e.g.
95 --where 'lastseen IS NOT NULL'
96 will only notify patrons who have been seen.
97
98 =item B<-letter>
99
100 Optional parameter to use another notice than the default: MEMBERSHIP_EXPIRY
101
102 =back
103
104 =head1 CONFIGURATION
105
106 The content of the messages is configured in Tools -> Notices and slips. Use the MEMBERSHIP_EXPIRY notice or
107 supply another via the parameters.
108
109 Typically, messages are prepared for each patron when the memberships are going to expire.
110
111 These emails are staged in the outgoing message queue, as are messages
112 produced by other features of Koha. This message queue must be
113 processed regularly by the
114 F<misc/cronjobs/process_message_queue.pl> program.
115
116 In the event that the C<-n> flag is passed to this program, no emails
117 are sent. Instead, messages are sent on standard output from this
118 program.
119
120 Notices can contain variables enclosed in double angle brackets like
121 E<lt>E<lt>thisE<gt>E<gt>. Those variables will be replaced with values
122 specific to the soon expiring members.
123 Available variables are:
124
125 =over
126
127 =item E<lt>E<lt>borrowers.*E<gt>E<gt>
128
129 any field from the borrowers table
130
131 =item E<lt>E<lt>branches.*E<gt>E<gt>
132
133 any field from the branches table
134
135 =back
136
137 =cut
138
139 use Modern::Perl;
140 use Getopt::Long qw( GetOptions );
141 use Pod::Usage qw( pod2usage );
142 BEGIN {
143     # find Koha's Perl modules
144     # test carefully before changing this
145     use FindBin ();
146     eval { require "$FindBin::Bin/../kohalib.pl" };
147 }
148
149 use Koha::Script -cron;
150 use C4::Context;
151 use C4::Letters;
152 use C4::Log qw( cronlogaction );
153
154 use Koha::Patrons;
155
156 # These are defaults for command line options.
157 my $confirm;                              # -c: Confirm that the user has read and configured this script.
158 my $nomail;                               # -n: No mail. Will not send any emails.
159 my $verbose = 0;                           # -v: verbose
160 my $help    = 0;
161 my $man     = 0;
162 my $before  = 0;
163 my $after   = 0;
164 my ( $branch, $letter_type );
165 my @where;
166
167 GetOptions(
168     'help|?'         => \$help,
169     'man'            => \$man,
170     'c'              => \$confirm,
171     'n'              => \$nomail,
172     'v'              => \$verbose,
173     'branch:s'       => \$branch,
174     'before:i'       => \$before,
175     'after:i'        => \$after,
176     'letter:s'       => \$letter_type,
177     'where=s'        => \@where,
178 ) or pod2usage(2);
179
180 pod2usage( -verbose => 2 ) if $man;
181 pod2usage(1) if $help || !$confirm;
182
183 cronlogaction();
184
185 my $expdays = C4::Context->preference('MembershipExpiryDaysNotice');
186 if( !$expdays ) {
187     #If the pref is not set, we will exit
188     warn 'Exiting membership_expiry.pl: MembershipExpiryDaysNotice not set'
189         if $verbose;
190     exit;
191 }
192
193 warn 'getting upcoming membership expires' if $verbose;
194 my $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires(
195     {
196         ( $branch ? ( 'me.branchcode' => $branch ) : () ),
197         before => $before,
198         after  => $after,
199     }
200 );
201
202 my $where_literal = join ' AND ', @where;
203 $upcoming_mem_expires = $upcoming_mem_expires->search( \$where_literal ) if @where;
204
205 warn 'found ' . $upcoming_mem_expires->count . ' soon expiring members'
206     if $verbose;
207
208 # main loop
209 $letter_type = 'MEMBERSHIP_EXPIRY' if !$letter_type;
210 while ( my $recent = $upcoming_mem_expires->next ) {
211     my $from_address = $recent->library->from_email_address;
212     my $letter =  C4::Letters::GetPreparedLetter(
213         module      => 'members',
214         letter_code => $letter_type,
215         branchcode  => $recent->branchcode,
216         lang        => $recent->lang,
217         tables      => {
218             borrowers => $recent->borrowernumber,
219             branches  => $recent->branchcode,
220         },
221     );
222     last if !$letter; # Letters.pm already warned, just exit
223     if( $nomail ) {
224         print $letter->{'content'}."\n";
225     } else {
226         C4::Letters::EnqueueLetter({
227             letter                 => $letter,
228             borrowernumber         =>  $recent->borrowernumber,
229             from_address           => $from_address,
230             message_transport_type => 'email',
231         });
232     }
233 }