From f23d7670f5def80f5ab6b369f3456b6cf4f40626 Mon Sep 17 00:00:00 2001 From: PerplexedTheta Date: Tue, 27 Aug 2024 15:45:06 +0100 Subject: [PATCH] Bug 32216: Add print notices to membership_expiry.pl This patch adds print notice functionality to the membership_expiry.pl script, by adding both conditional and and forced modes. When passing only --confirm, the script will check the patron's record for an email address. If one is found, an email notice is generated. If one is not, a print notice is used instead. By adding a -p flag, print notices will be generated, even if an email address is present. To test: a) set the MembershipExpiryDaysNotice syspref to 7 days b) choose or set up half a dozen patrons with expiry dates 1) take heed: these expiry dates will need to be T+7 days c) add fake email addresses to two of the patrons 1) take heed: the other patrons should have no primary or secondary email d) set up the MEMBERSHIP_EXPIRY notice a) set content & subject in the email notice to contain the word email b) set content & subject in the print notice to contain the word print e) run ./misc/cronjobs/membership_expiry.pl -n -c 1) notice how the printed notices contain the word email f) run ./misc/cronjobs/membership_expiry.pl -c g) check the notices for the patrons from step b 1) notice how only email notices are generated h) APPLY PATCH i) repeat step e 1) notice how a mix of email and print notices are now generated j) run ./misc/cronjobs/membership_expiry.pl -n -p -c 1) notice how the output is the same as in step i - this is by design k) repeat steps f-g 1) notice how some patrons will only get an email notice, whilst others will only get a print notice 2) this should occur based on which patrons do, or do not have a valid primary or secondary email l) run ./misc/cronjobs/membership_expiry.pl -p -c m) repeat step g 1) notice that all patrons now get a print notice, whilst some get both a print notice, and an email notice. Again, this should occur based on whether the patron has a valid primary or secondary email n) SIGN OFF Signed-off-by: Andrew Fuerste Henry --- misc/cronjobs/membership_expiry.pl | 121 ++++++++++++++++++++--------- 1 file changed, 86 insertions(+), 35 deletions(-) diff --git a/misc/cronjobs/membership_expiry.pl b/misc/cronjobs/membership_expiry.pl index 3d0a53a05e..5dfb1e8175 100755 --- a/misc/cronjobs/membership_expiry.pl +++ b/misc/cronjobs/membership_expiry.pl @@ -24,7 +24,7 @@ membership_expiry.pl - cron script to put membership expiry reminders into the m =head1 SYNOPSIS -./membership_expiry.pl -c [-v] [-n] [-branch CODE] [-before DAYS] [-after DAYS] [-where COND] [-renew] [-letter X] [-letter-renew Y] [-active|-inactive] +./membership_expiry.pl -c [-v] [-n] [-p] [-branch CODE] [-before DAYS] [-after DAYS] [-where COND] [-renew] [-letter X] [-letter-renew Y] [-active|-inactive] or, in crontab: @@ -54,9 +54,14 @@ 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 +Do not send any notices. Membership expire notices that would have been sent to the patrons are printed to standard out. +=item B<-p> + +Force the generation of print notices, even if the borrower has an email address. +Note that this flag cannot be used in combination with -n + =item B<-c> Confirm flag: Add this option. The script will only print a usage @@ -132,6 +137,10 @@ 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. +When using the C<-p> flag, print notices are generated regardless of whether or +not the borrower has an email address. This can be useful for libraries that +prefer to deal with print notices. + Notices can contain variables enclosed in double angle brackets like EEthisEE. Those variables will be replaced with values specific to the soon expiring members. @@ -165,6 +174,7 @@ 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 $forceprint; # -p: Force print notices, even if email is found my $verbose = 0; # -v: verbose my $help = 0; my $man = 0; @@ -185,6 +195,7 @@ GetOptions( 'man' => \$man, 'c' => \$confirm, 'n' => \$nomail, + 'p' => \$forceprint, 'v' => \$verbose, 'branch:s' => \$branch, 'before:i' => \$before, @@ -254,64 +265,104 @@ warn 'found ' . $upcoming_mem_expires->count . ' soon expiring members' # main loop my ( $count_skipped, $count_renewed, $count_enqueued ) = ( 0, 0, 0 ); while ( my $recent = $upcoming_mem_expires->next ) { - if ( $active && !$recent->is_active( { months => $active } ) ) { + my $patron = Koha::Patrons->find( $recent->borrowernumber ); + my $user_email = $patron->notice_email_address; + my $from_address = $patron->library->from_email_address; + + if ( $active && !$patron->is_active( { months => $active } ) ) { $count_skipped++; next; - } elsif ( $inactive && $recent->is_active( { months => $inactive } ) ) { + } elsif ( $inactive && $patron->is_active( { months => $inactive } ) ) { $count_skipped++; next; } my $which_notice; if ($renew) { - $recent->renew_account; + $patron->renew_account; $which_notice = $letter_renew; $count_renewed++; } else { $which_notice = $letter_expiry; } - my $from_address = $recent->library->from_email_address; - my $letter = C4::Letters::GetPreparedLetter( - module => 'members', - letter_code => $which_notice, - 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"; - next; + if ($user_email) { + my $email_letter = C4::Letters::GetPreparedLetter( + module => 'members', + letter_code => $which_notice, + branchcode => $patron->branchcode, + lang => $patron->lang, + tables => { + borrowers => $patron->borrowernumber, + branches => $patron->branchcode, + }, + message_transport_type => 'email', + ); + + if ($nomail) { + print $email_letter->{'content'} . "\n"; + next; + } + + if ($email_letter) { + C4::Letters::EnqueueLetter( + { + letter => $email_letter, + borrowernumber => $patron->borrowernumber, + from_address => $from_address, + message_transport_type => 'email', + } + ); + } + } + + if ( !$user_email || $forceprint ) { + my $print_letter = C4::Letters::GetPreparedLetter( + module => 'members', + letter_code => $which_notice, + branchcode => $patron->branchcode, + lang => $patron->lang, + tables => { + borrowers => $patron->borrowernumber, + branches => $patron->branchcode, + }, + message_transport_type => 'print', + ); + + if ($nomail) { + print $print_letter->{'content'} . "\n"; + next; + } + + if ($print_letter) { + C4::Letters::EnqueueLetter( + { + letter => $print_letter, + borrowernumber => $patron->borrowernumber, + message_transport_type => 'print', + } + ); + } } - C4::Letters::EnqueueLetter({ - letter => $letter, - borrowernumber => $recent->borrowernumber, - from_address => $from_address, - message_transport_type => 'email', - }); $count_enqueued++; - if ($recent->smsalertnumber) { - my $smsletter = C4::Letters::GetPreparedLetter( + if ($patron->smsalertnumber) { + my $sms_letter = C4::Letters::GetPreparedLetter( module => 'members', letter_code => $which_notice, - branchcode => $recent->branchcode, - lang => $recent->lang, + branchcode => $patron->branchcode, + lang => $patron->lang, tables => { - borrowers => $recent->borrowernumber, - branches => $recent->branchcode, + borrowers => $patron->borrowernumber, + branches => $patron->branchcode, }, message_transport_type => 'sms', ); - if ($smsletter) { + if ($sms_letter) { C4::Letters::EnqueueLetter({ - letter => $smsletter, - borrowernumber => $recent->borrowernumber, + letter => $sms_letter, + borrowernumber => $patron->borrowernumber, message_transport_type => 'sms', }); } -- 2.39.5