Bug 15233: Cataloging subfield editors: Clean up html and streamline text for better...
[koha.git] / misc / cronjobs / membership_expiry.pl
blob0cf52c8b716b6a386119113c736170a24db475d2
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 =back
66 =head1 CONFIGURATION
68 The content of the messages is configured in Tools -> Notices and slips. Use the MEMBERSHIP_EXPIRY notice.
70 Typically, messages are prepared for each patron when the memberships are going to expire.
72 These emails are staged in the outgoing message queue, as are messages
73 produced by other features of Koha. This message queue must be
74 processed regularly by the
75 F<misc/cronjobs/process_message_queue.pl> program.
77 In the event that the C<-n> flag is passed to this program, no emails
78 are sent. Instead, messages are sent on standard output from this
79 program.
81 Notices can contain variables enclosed in double angle brackets like
82 E<lt>E<lt>thisE<gt>E<gt>. Those variables will be replaced with values
83 specific to the soon expiring members.
84 Available variables are:
86 =over
88 =item E<lt>E<lt>borrowers.*E<gt>E<gt>
90 any field from the borrowers table
92 =item E<lt>E<lt>branches.*E<gt>E<gt>
94 any field from the branches table
96 =back
98 =cut
100 use Modern::Perl;
101 use Getopt::Long;
102 use Pod::Usage;
103 use Data::Dumper;
104 BEGIN {
105 # find Koha's Perl modules
106 # test carefully before changing this
107 use FindBin;
108 eval { require "$FindBin::Bin/../kohalib.pl" };
111 use C4::Context;
112 use C4::Letters;
113 use C4::Log;
115 # These are defaults for command line options.
116 my $confirm; # -c: Confirm that the user has read and configured this script.
117 my $nomail; # -n: No mail. Will not send any emails.
118 my $verbose= 0; # -v: verbose
119 my $help = 0;
120 my $man = 0;
122 GetOptions(
123 'help|?' => \$help,
124 'man' => \$man,
125 'c' => \$confirm,
126 'n' => \$nomail,
127 'v' => \$verbose,
128 ) or pod2usage(2);
130 pod2usage( -verbose => 2 ) if $man;
131 pod2usage(1) if $help || !$confirm;
133 cronlogaction();
135 my $expdays = C4::Context->preference('MembershipExpiryDaysNotice');
136 if( !$expdays ) {
137 #If the pref is not set, we will exit
138 warn 'Exiting membership_expiry.pl: MembershipExpiryDaysNotice not set'
139 if $verbose;
140 exit;
143 my $admin_adress = C4::Context->preference('KohaAdminEmailAddress');
144 warn 'getting upcoming membership expires' if $verbose;
145 my $upcoming_mem_expires = C4::Members::GetUpcomingMembershipExpires();
146 warn 'found ' . scalar( @$upcoming_mem_expires ) . ' soon expiring members'
147 if $verbose;
149 # main loop
150 foreach my $recent ( @$upcoming_mem_expires ) {
151 my $from_address = $recent->{'branchemail'} || $admin_adress;
152 my $letter_type = 'MEMBERSHIP_EXPIRY';
153 my $letter = C4::Letters::getletter( 'members', $letter_type,
154 $recent->{'branchcode'} );
155 die "no letter of type '$letter_type' found. Please see sample_notices.sql"
156 unless $letter;
158 $letter = parse_letter({
159 letter => $letter,
160 borrowernumber => $recent->{'borrowernumber'},
161 firstname => $recent->{'firstname'},
162 categorycode => $recent->{'categorycode'},
163 branchcode => $recent->{'branchcode'},
165 if ($letter) {
166 if ($nomail) {
167 print $letter->{'content'};
168 } else {
169 C4::Letters::EnqueueLetter({
170 letter => $letter,
171 borrowernumber => $recent->{'borrowernumber'},
172 from_address => $from_address,
173 message_transport_type => 'email',
179 =head1 SUBROUTINES
181 =head2 parse_letter
183 =cut
185 sub parse_letter {
186 my $params = shift;
187 foreach my $required ( qw( letter borrowernumber ) ) {
188 return unless exists $params->{$required};
190 my $letter = C4::Letters::GetPreparedLetter (
191 module => 'members',
192 letter_code => 'MEMBERSHIP_EXPIRY',
193 tables => {
194 'borrowers', $params->{'borrowernumber'},
195 'branches', $params->{'branchcode'}