From 668dfda11970313d3e876769182b87b63c336df1 Mon Sep 17 00:00:00 2001
From: Julian Maurice <julian.maurice@biblibre.com>
Date: Thu, 21 Jul 2022 11:39:07 +0200
Subject: [PATCH] Bug 23773: Send membership expiry notices by sms too
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Test plan:
1. Verify in "About" page that SMS::Send is installed.
   If not, install it.
2. Set system preferences:
   - SMSSendDriver = 'Test'
   - MembershipExpiryDaysNotice = 30
3. In "Notices and Slips" tool, edit the 'MEMBERSHIP_EXPIRY' letter and
   verify that both email and sms parts are filled. Use a different
   title and body for the sms part
4. Create a new borrower with an email address and an SMS number. Set
   their expiry date to a date next week
5. Run misc/cronjobs/membership_expiry.pl --confirm
6. Verify in the patron "Notices" tab that two notices are pending, one
   for email, one for sms

Sponsored-by: Médiathèque de Montauban
---
 misc/cronjobs/membership_expiry.pl | 40 +++++++++++++++++++++++-------
 1 file changed, 31 insertions(+), 9 deletions(-)

diff --git a/misc/cronjobs/membership_expiry.pl b/misc/cronjobs/membership_expiry.pl
index 18c40be18f..939b6e0f40 100755
--- a/misc/cronjobs/membership_expiry.pl
+++ b/misc/cronjobs/membership_expiry.pl
@@ -34,7 +34,7 @@ Options:
    --man                    full documentation
    --where <conditions>     where clause to add to the query
    -v -verbose              verbose mode
-   -n --nomail              if supplied messages will be output to STDOUT and not sent
+   -n --nomail              if supplied, messages will be output to STDOUT and no email or sms will be sent
    -c --confirm             commit changes to db, no action will be taken unless this switch is included
    -b --branch <branchname> only deal with patrons from this library/branch
    --before=X               include patrons expiring a number of days BEFORE the date set by the preference
@@ -43,7 +43,7 @@ Options:
 
 =head1 DESCRIPTION
 
-This script sends membership expiry reminder notices to patrons.
+This script sends membership expiry reminder notices to patrons, by email and sms.
 It queues them in the message queue, which is processed by
 the process_message_queue.pl cronjob.
 
@@ -218,13 +218,35 @@ while ( my $recent = $upcoming_mem_expires->next ) {
     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',
-        });
+        next;
+    }
+
+    C4::Letters::EnqueueLetter({
+        letter                 => $letter,
+        borrowernumber         =>  $recent->borrowernumber,
+        from_address           => $from_address,
+        message_transport_type => 'email',
+    });
+
+    if ($recent->smsalertnumber) {
+        my $smsletter = C4::Letters::GetPreparedLetter(
+            module      => 'members',
+            letter_code => $letter_type,
+            branchcode  => $recent->branchcode,
+            lang        => $recent->lang,
+            tables      => {
+                borrowers => $recent->borrowernumber,
+                branches  => $recent->branchcode,
+            },
+            message_transport_type => 'sms',
+        );
+        if ($smsletter) {
+            C4::Letters::EnqueueLetter({
+                letter                 => $smsletter,
+                borrowernumber         => $recent->borrowernumber,
+                message_transport_type => 'sms',
+            });
+        }
     }
 }
 
-- 
2.30.2