# This file is part of Koha.
#
-# Copyright (C) 2013 Amit Gupta (amitddng135@gmail.com)
+# Copyright (C) 2015 Amit Gupta (amitddng135@gmail.com)
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
=head1 NAME
-membership_expires.pl - cron script to put membership expiry reminders into message queue
+membership_expiry.pl - cron script to put membership expiry reminders into the message queue
=head1 SYNOPSIS
-./membership_expires.pl -c
+./membership_expiry.pl -c
or, in crontab:
-0 1 * * * membership_expires.pl -c
+0 1 * * * membership_expiry.pl -c
+
+=head1 DESCRIPTION
+
+This script sends membership expiry reminder notices to patrons.
+It queues them in the message queue, which is processed by
+the process_message_queue.pl cronjob.
+
+=head1 OPTIONS
+
+=over 8
+
+=item B<--help>
+
+Print a brief help message and exits.
+
+=item B<--man>
+
+Prints the manual page and exits.
+
+=item B<-v>
+
+Verbose. Without this flag set, only fatal errors are reported.
+
+=item B<-n>
+
+Do not send any email. Membership expire notices that would have been sent to
+the patrons are printed to standard out.
+
+=item B<-c>
+
+Confirm flag: Add this option. The script will only print a usage
+statement otherwise.
+
+=item B<-branch>
+
+Optional branchcode to restrict the cronjob to that branch.
+
+=item B<-before>
+
+Optional parameter to extend the selection with a number of days BEFORE
+the date set by the preference.
+
+=item B<-after>
+
+Optional parameter to extend the selection with a number of days AFTER
+the date set by the preference.
+
+=item B<-letter>
+
+Optional parameter to use another notice than the default one.
+
+=back
+
+=head1 CONFIGURATION
+
+The content of the messages is configured in Tools -> Notices and slips. Use the MEMBERSHIP_EXPIRY notice.
+
+Typically, messages are prepared for each patron when the memberships are going to expire.
+
+These emails are staged in the outgoing message queue, as are messages
+produced by other features of Koha. This message queue must be
+processed regularly by the
+F<misc/cronjobs/process_message_queue.pl> program.
+
+In the event that the C<-n> flag is passed to this program, no emails
+are sent. Instead, messages are sent on standard output from this
+program.
+
+Notices can contain variables enclosed in double angle brackets like
+E<lt>E<lt>thisE<gt>E<gt>. Those variables will be replaced with values
+specific to the soon expiring members.
+Available variables are:
+
+=over
+
+=item E<lt>E<lt>borrowers.*E<gt>E<gt>
+
+any field from the borrowers table
+
+=item E<lt>E<lt>branches.*E<gt>E<gt>
+
+any field from the branches table
+
+=back
=cut
-use strict;
-use warnings;
-use Getopt::Long;
-use Data::Dumper;
+use Modern::Perl;
+use Getopt::Long qw( GetOptions );
+use Pod::Usage qw( pod2usage );
BEGIN {
# find Koha's Perl modules
# test carefully before changing this
- use FindBin;
+ use FindBin ();
eval { require "$FindBin::Bin/../kohalib.pl" };
}
+use Koha::Script -cron;
use C4::Context;
use C4::Letters;
-use C4::Dates qw/format_date/;
-use C4::Log;
+use C4::Log qw( cronlogaction );
-cronlogaction();
+use Koha::Patrons;
# These are defaults for command line options.
my $confirm; # -c: Confirm that the user has read and configured this script.
my $nomail; # -n: No mail. Will not send any emails.
-my $verbose= 0; # -v: verbose
-
-GetOptions( 'c' => \$confirm,
- 'n' => \$nomail,
- 'v' => \$verbose,
- );
+my $verbose = 0; # -v: verbose
+my $help = 0;
+my $man = 0;
+my $before = 0;
+my $after = 0;
+my ( $branch, $letter_type );
+
+GetOptions(
+ 'help|?' => \$help,
+ 'man' => \$man,
+ 'c' => \$confirm,
+ 'n' => \$nomail,
+ 'v' => \$verbose,
+ 'branch:s' => \$branch,
+ 'before:i' => \$before,
+ 'after:i' => \$after,
+ 'letter:s' => \$letter_type,
+) or pod2usage(2);
+
+pod2usage( -verbose => 2 ) if $man;
+pod2usage(1) if $help || !$confirm;
+cronlogaction();
-my $usage = << 'ENDUSAGE';
-This script prepares for membership expiry reminders to be sent to
-patrons. It queues them in the message queue, which is processed by
-the process_message_queue.pl cronjob.
-See the comments in the script for directions on changing the script.
-This script has the following parameters :
- -c Confirm and remove this help & warning
- -n send No mail. Instead, all mail messages are printed on screen. Usefull for testing purposes.
- -v verbose
-ENDUSAGE
-
-unless ($confirm) {
- print $usage;
- print "Do you wish to continue? (y/n)";
- chomp($_ = <STDIN>);
- exit unless (/^y/i);
+my $expdays = C4::Context->preference('MembershipExpiryDaysNotice');
+if( !$expdays ) {
+ #If the pref is not set, we will exit
+ warn 'Exiting membership_expiry.pl: MembershipExpiryDaysNotice not set'
+ if $verbose;
+ exit;
}
-my $admin_adress = C4::Context->preference('KohaAdminEmailAddress');
warn 'getting upcoming membership expires' if $verbose;
-my $upcoming_mem_expires = C4::Members::GetUpcomingMembershipExpires();
-warn 'found ' . scalar( @$upcoming_mem_expires ) . ' issues' if $verbose;
-
-
-UPCOMINGMEMEXP: foreach my $recent ( @$upcoming_mem_expires ) {
- my $from_address = $recent->{'branchemail'} || $admin_adress;
- my $letter_type = 'MEMEXP';
- my $letter = C4::Letters::getletter( 'members', $letter_type, $recent->{'branchcode'} );
- die "no letter of type '$letter_type' found. Please see sample_notices.sql" unless $letter;
-
- $letter = parse_letter({ letter => $letter,
- borrowernumber => $recent->{'borrowernumber'},
- firstname => $recent->{'firstname'},
- categorycode => $recent->{'categorycode'},
- branchcode => $recent->{'branchcode'},
- });
- if ($letter) {
- if ($nomail) {
- print $letter->{'content'};
- } else {
- C4::Letters::EnqueueLetter( { letter => $letter,
- borrowernumber => $recent->{'borrowernumber'},
- from_address => $from_address,
- message_transport_type => 'email',
- } );
- }
- }
- }
-
-
-=head1 METHODS
-
-=head2 parse_letter
-
-=cut
-
-sub parse_letter {
- my $params = shift;
- foreach my $required ( qw( letter borrowernumber ) ) {
- return unless exists $params->{$required};
+my $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires(
+ {
+ ( $branch ? ( 'me.branchcode' => $branch ) : () ),
+ before => $before,
+ after => $after,
}
- my $letter = C4::Letters::GetPreparedLetter (
- module => 'members',
- letter_code => 'MEMEXP',
- branchcode => $params->{'branchcode'},
- tables => {'borrowers', $params->{'borrowernumber'},},
+);
+warn 'found ' . $upcoming_mem_expires->count . ' soon expiring members'
+ if $verbose;
+
+# main loop
+$letter_type = 'MEMBERSHIP_EXPIRY' if !$letter_type;
+while ( my $recent = $upcoming_mem_expires->next ) {
+ my $from_address = $recent->library->from_email_address;
+ my $letter = C4::Letters::GetPreparedLetter(
+ module => 'members',
+ letter_code => $letter_type,
+ branchcode => $recent->branchcode,
+ lang => $recent->lang,
+ tables => {
+ borrowers => $recent->borrowernumber,
+ branches => $recent->branchcode,
+ },
);
+ last if !$letter; # Letters.pm already warned, just exit
+ if( $nomail ) {
+ print $letter->{'content'}."\n";
+ } else {
+ C4::Letters::EnqueueLetter({
+ letter => $letter,
+ borrowernumber => $recent->borrowernumber,
+ from_address => $from_address,
+ message_transport_type => 'email',
+ });
+ }
}
-
-1;
-
-__END__