Bug 26745: (QA follow-up) Add Unit Tests
[koha.git] / misc / cronjobs / membership_expiry.pl
blob5038ce1f9f122ccb2a47ee65cf7eb6e6a43998cc
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Copyright (C) 2015 Amit Gupta (amitddng135@gmail.com)
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.
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.
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>.
20 =head1 NAME
22 membership_expiry.pl - cron script to put membership expiry reminders into the message queue
24 =head1 SYNOPSIS
26 ./membership_expiry.pl -c
28 or, in crontab:
30 0 1 * * * membership_expiry.pl -c
32 =head1 DESCRIPTION
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.
38 =head1 OPTIONS
40 =over 8
42 =item B<--help>
44 Print a brief help message and exits.
46 =item B<--man>
48 Prints the manual page and exits.
50 =item B<-v>
52 Verbose. Without this flag set, only fatal errors are reported.
54 =item B<-n>
56 Do not send any email. Membership expire notices that would have been sent to
57 the patrons are printed to standard out.
59 =item B<-c>
61 Confirm flag: Add this option. The script will only print a usage
62 statement otherwise.
64 =item B<-branch>
66 Optional branchcode to restrict the cronjob to that branch.
68 =item B<-before>
70 Optional parameter to extend the selection with a number of days BEFORE
71 the date set by the preference.
73 =item B<-after>
75 Optional parameter to extend the selection with a number of days AFTER
76 the date set by the preference.
78 =item B<-letter>
80 Optional parameter to use another notice than the default one.
82 =back
84 =head1 CONFIGURATION
86 The content of the messages is configured in Tools -> Notices and slips. Use the MEMBERSHIP_EXPIRY notice.
88 Typically, messages are prepared for each patron when the memberships are going to expire.
90 These emails are staged in the outgoing message queue, as are messages
91 produced by other features of Koha. This message queue must be
92 processed regularly by the
93 F<misc/cronjobs/process_message_queue.pl> program.
95 In the event that the C<-n> flag is passed to this program, no emails
96 are sent. Instead, messages are sent on standard output from this
97 program.
99 Notices can contain variables enclosed in double angle brackets like
100 E<lt>E<lt>thisE<gt>E<gt>. Those variables will be replaced with values
101 specific to the soon expiring members.
102 Available variables are:
104 =over
106 =item E<lt>E<lt>borrowers.*E<gt>E<gt>
108 any field from the borrowers table
110 =item E<lt>E<lt>branches.*E<gt>E<gt>
112 any field from the branches table
114 =back
116 =cut
118 use Modern::Perl;
119 use Getopt::Long;
120 use Pod::Usage;
121 use Data::Dumper;
122 BEGIN {
123 # find Koha's Perl modules
124 # test carefully before changing this
125 use FindBin;
126 eval { require "$FindBin::Bin/../kohalib.pl" };
129 use Koha::Script -cron;
130 use C4::Context;
131 use C4::Letters;
132 use C4::Log;
134 use Koha::Patrons;
136 # These are defaults for command line options.
137 my $confirm; # -c: Confirm that the user has read and configured this script.
138 my $nomail; # -n: No mail. Will not send any emails.
139 my $verbose = 0; # -v: verbose
140 my $help = 0;
141 my $man = 0;
142 my $before = 0;
143 my $after = 0;
144 my ( $branch, $letter_type );
146 GetOptions(
147 'help|?' => \$help,
148 'man' => \$man,
149 'c' => \$confirm,
150 'n' => \$nomail,
151 'v' => \$verbose,
152 'branch:s' => \$branch,
153 'before:i' => \$before,
154 'after:i' => \$after,
155 'letter:s' => \$letter_type,
156 ) or pod2usage(2);
158 pod2usage( -verbose => 2 ) if $man;
159 pod2usage(1) if $help || !$confirm;
161 cronlogaction();
163 my $expdays = C4::Context->preference('MembershipExpiryDaysNotice');
164 if( !$expdays ) {
165 #If the pref is not set, we will exit
166 warn 'Exiting membership_expiry.pl: MembershipExpiryDaysNotice not set'
167 if $verbose;
168 exit;
171 my $admin_adress = C4::Context->preference('KohaAdminEmailAddress');
172 warn 'getting upcoming membership expires' if $verbose;
173 my $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires(
175 ( $branch ? ( 'me.branchcode' => $branch ) : () ),
176 before => $before,
177 after => $after,
180 warn 'found ' . $upcoming_mem_expires->count . ' soon expiring members'
181 if $verbose;
183 # main loop
184 $letter_type = 'MEMBERSHIP_EXPIRY' if !$letter_type;
185 while ( my $recent = $upcoming_mem_expires->next ) {
186 my $from_address = $recent->library->branchemail || $admin_adress;
187 my $letter = C4::Letters::GetPreparedLetter(
188 module => 'members',
189 letter_code => $letter_type,
190 branchcode => $recent->branchcode,
191 lang => $recent->lang,
192 tables => {
193 borrowers => $recent->borrowernumber,
194 branches => $recent->branchcode,
197 last if !$letter; # Letters.pm already warned, just exit
198 if( $nomail ) {
199 print $letter->{'content'}."\n";
200 } else {
201 C4::Letters::EnqueueLetter({
202 letter => $letter,
203 borrowernumber => $recent->borrowernumber,
204 from_address => $from_address,
205 message_transport_type => 'email',