Bug 14834: Make membership_expiry cronjob more flexible
[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 =head1 DESCRIPTION
33
34 This script sends membership expiry reminder notices to patrons.
35 It queues them in the message queue, which is processed by
36 the process_message_queue.pl cronjob.
37
38 =head1 OPTIONS
39
40 =over 8
41
42 =item B<--help>
43
44 Print a brief help message and exits.
45
46 =item B<--man>
47
48 Prints the manual page and exits.
49
50 =item B<-v>
51
52 Verbose. Without this flag set, only fatal errors are reported.
53
54 =item B<-n>
55
56 Do not send any email. Membership expire notices that would have been sent to
57 the patrons are printed to standard out.
58
59 =item B<-c>
60
61 Confirm flag: Add this option. The script will only print a usage
62 statement otherwise.
63
64 =item B<-branch>
65
66 Optional branchcode to restrict the cronjob to that branch.
67
68 =item B<-before>
69
70 Optional parameter to extend the selection with a number of days BEFORE
71 the date set by the preference.
72
73 =item B<-after>
74
75 Optional parameter to extend the selection with a number of days AFTER
76 the date set by the preference.
77
78 =back
79
80 =head1 CONFIGURATION
81
82 The content of the messages is configured in Tools -> Notices and slips. Use the MEMBERSHIP_EXPIRY notice.
83
84 Typically, messages are prepared for each patron when the memberships are going to expire.
85
86 These emails are staged in the outgoing message queue, as are messages
87 produced by other features of Koha. This message queue must be
88 processed regularly by the
89 F<misc/cronjobs/process_message_queue.pl> program.
90
91 In the event that the C<-n> flag is passed to this program, no emails
92 are sent. Instead, messages are sent on standard output from this
93 program.
94
95 Notices can contain variables enclosed in double angle brackets like
96 E<lt>E<lt>thisE<gt>E<gt>. Those variables will be replaced with values
97 specific to the soon expiring members.
98 Available variables are:
99
100 =over
101
102 =item E<lt>E<lt>borrowers.*E<gt>E<gt>
103
104 any field from the borrowers table
105
106 =item E<lt>E<lt>branches.*E<gt>E<gt>
107
108 any field from the branches table
109
110 =back
111
112 =cut
113
114 use Modern::Perl;
115 use Getopt::Long;
116 use Pod::Usage;
117 use Data::Dumper;
118 BEGIN {
119     # find Koha's Perl modules
120     # test carefully before changing this
121     use FindBin;
122     eval { require "$FindBin::Bin/../kohalib.pl" };
123 }
124
125 use C4::Context;
126 use C4::Letters;
127 use C4::Log;
128
129 # These are defaults for command line options.
130 my $confirm;                              # -c: Confirm that the user has read and configured this script.
131 my $nomail;                               # -n: No mail. Will not send any emails.
132 my $verbose = 0;                           # -v: verbose
133 my $help    = 0;
134 my $man     = 0;
135 my $before  = 0;
136 my $after   = 0;
137 my $branch;
138
139 GetOptions(
140     'help|?'         => \$help,
141     'man'            => \$man,
142     'c'              => \$confirm,
143     'n'              => \$nomail,
144     'v'              => \$verbose,
145     'branch:s'       => \$branch,
146     'before:i'       => \$before,
147     'after:i'        => \$after,
148 ) or pod2usage(2);
149
150 pod2usage( -verbose => 2 ) if $man;
151 pod2usage(1) if $help || !$confirm;
152
153 cronlogaction();
154
155 my $expdays = C4::Context->preference('MembershipExpiryDaysNotice');
156 if( !$expdays ) {
157     #If the pref is not set, we will exit
158     warn 'Exiting membership_expiry.pl: MembershipExpiryDaysNotice not set'
159         if $verbose;
160     exit;
161 }
162
163 my $admin_adress = C4::Context->preference('KohaAdminEmailAddress');
164 warn 'getting upcoming membership expires' if $verbose;
165 my $upcoming_mem_expires = C4::Members::GetUpcomingMembershipExpires({ branch => $branch, before => $before, after => $after });
166 warn 'found ' . scalar( @$upcoming_mem_expires ) . ' soon expiring members'
167     if $verbose;
168
169 # main loop
170 foreach my $recent ( @$upcoming_mem_expires ) {
171     my $from_address = $recent->{'branchemail'} || $admin_adress;
172     my $letter_type = 'MEMBERSHIP_EXPIRY';
173     my $letter = C4::Letters::getletter( 'members', $letter_type,
174         $recent->{'branchcode'} );
175     die "no letter of type '$letter_type' found. Please see sample_notices.sql"
176         unless $letter;
177
178     $letter = parse_letter({
179         letter         => $letter,
180         borrowernumber => $recent->{'borrowernumber'},
181         firstname      => $recent->{'firstname'},
182         categorycode   => $recent->{'categorycode'},
183         branchcode     => $recent->{'branchcode'},
184     });
185     if ($letter) {
186         if ($nomail) {
187             print $letter->{'content'}."\n";
188         } else {
189             C4::Letters::EnqueueLetter({
190                 letter                 => $letter,
191                 borrowernumber         =>  $recent->{'borrowernumber'},
192                 from_address           => $from_address,
193                 message_transport_type => 'email',
194             });
195         }
196     }
197 }
198
199 =head1 SUBROUTINES
200
201 =head2 parse_letter
202
203 =cut
204
205 sub parse_letter {
206     my $params = shift;
207     foreach my $required ( qw( letter borrowernumber ) ) {
208         return unless exists $params->{$required};
209     }
210     my $letter =  C4::Letters::GetPreparedLetter (
211         module => 'members',
212         letter_code => 'MEMBERSHIP_EXPIRY',
213         tables => {
214             'borrowers', $params->{'borrowernumber'},
215             'branches', $params->{'branchcode'}
216         },
217     );
218 }