Bug 28553: Patrons can be set to receive auto_renew notices as SMS, but Koha does...
authorKyle M Hall <kyle@bywatersolutions.com>
Mon, 18 Jul 2022 16:37:48 +0000 (12:37 -0400)
committerLucas Gass <lucas@bywatersolutions.com>
Mon, 31 Oct 2022 21:47:37 +0000 (21:47 +0000)
Following bug 18532, one can set a patron's messaging prefs to deliver an auto-renew notice via SMS.
However, the auto_renewals cron only knows how to generate email notices. We should not allow the selection of non-functional transport types.

Test Plan:
 1 - Set pref AutoRenewalNotices - 'according to patron messaging preferences'
 2 - Set circ rule with:
    Loan period: 5 days
    No renewal before: 5
    AutoRenewal: Yes
    Renewals allowed: 5
 3 - Set SMS Send driver to Email
 4 - Find a patron and set them to receive auto renewal notices via SMS and no other transport
 5 - Provide a proivder and sms number for the patron
 6 - Checkout an item to the patron, set due date to yesterday
 7 - Run auto renewals:
    perl misc/cronjobs/automatic_renewals.pl -v --send-notices -c
 8 - Item renewed, no message sent
 9 - Check in item, checkout again due yesterday
10 - Apply patch
11 - Browse to Tools->notices and slips
12 - Edit AUTO_RENEWALS and AUTO_RENEWALS_DIGEST to copy email template to sms and add a title
13 - perl misc/cronjobs/automatic_renewals.pl -v --send-notices -c
14 - Item renewed, confirm patron has a notice in their account
15 - Check the digest option, check in item, checkout again as due, repeat cronjob and note digest notic
16 - sign off

https://bugs.koha-community.org/show_bug.cgi?id=30355

Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: David Cook <dcook@prosentient.com.au>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
(cherry picked from commit fef29151e0dd6287f8e4e49effc9d9666bf87582)

Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
misc/cronjobs/automatic_renewals.pl

index 1af18c7..923015a 100755 (executable)
@@ -133,7 +133,15 @@ $verbose = 1 unless $verbose or $confirm;
 print "Test run only\n" unless $confirm;
 
 print "getting auto renewals\n" if $verbose;
-my $auto_renews = Koha::Checkouts->search({ auto_renew => 1, 'patron.autorenew_checkouts' => 1 },{ join => 'patron'});
+my $auto_renews = Koha::Checkouts->search(
+    {
+        auto_renew                   => 1,
+        'patron.autorenew_checkouts' => 1,
+    },
+    {
+        join => ['patron','item']
+    }
+);
 print "found " . $auto_renews->count . " auto renewals\n" if $verbose;
 
 my $renew_digest = {};
@@ -141,7 +149,7 @@ my %report;
 while ( my $auto_renew = $auto_renews->next ) {
     print "examining item '" . $auto_renew->itemnumber . "' to auto renew\n" if $verbose;
 
-    my ( $borrower_preferences, $wants_email, $wants_digest ) = ( undef, 0, 0 );
+    my ( $borrower_preferences, $wants_messages, $wants_digest ) = ( undef, 0, 0 );
     if ( $send_notices_pref eq 'preferences' ){
         $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences(
             {
@@ -149,15 +157,14 @@ while ( my $auto_renew = $auto_renews->next ) {
                 message_name   => 'auto_renewals'
             }
         );
-        $wants_email = 1
+        $wants_messages = 1
             if $borrower_preferences
-            && $borrower_preferences->{transports}
-            && $borrower_preferences->{transports}->{email};
+            && $borrower_preferences->{transports};
         $wants_digest = 1
-            if $wants_email
+            if $wants_messages
             && $borrower_preferences->{wants_digest};
     } else { # Preference is never or cron
-        $wants_email = $send_notices;
+        $wants_messages = $send_notices;
         $wants_digest = 0;
     }
 
@@ -175,7 +182,7 @@ while ( my $auto_renew = $auto_renews->next ) {
             $auto_renew->auto_renew_error(undef)->store;
         }
         push @{ $report{ $auto_renew->borrowernumber } }, $auto_renew
-            if ( $wants_email ) && !$wants_digest;
+            if ( $wants_messages ) && !$wants_digest;
     } elsif ( $error eq 'too_many'
         or $error eq 'on_reserve'
         or $error eq 'restriction'
@@ -194,7 +201,7 @@ while ( my $auto_renew = $auto_renews->next ) {
         if ( $updated ) {
             $auto_renew->auto_renew_error($error)->store if $confirm;
             push @{ $report{ $auto_renew->borrowernumber } }, $auto_renew
-              if $error ne 'auto_too_soon' && ( $wants_email  && !$wants_digest );    # Do not notify if it's too soon
+              if $error ne 'auto_too_soon' && ( $wants_messages  && !$wants_digest );    # Do not notify if it's too soon
         }
     }
 
@@ -218,30 +225,45 @@ while ( my $auto_renew = $auto_renews->next ) {
 if ( $send_notices && $confirm ) {
     for my $borrowernumber ( keys %report ) {
         my $patron = Koha::Patrons->find($borrowernumber);
-        for my $issue ( @{ $report{$borrowernumber} } ) {
-            my $item   = Koha::Items->find( $issue->itemnumber );
-            my $letter = C4::Letters::GetPreparedLetter(
-                module      => 'circulation',
-                letter_code => 'AUTO_RENEWALS',
-                tables      => {
-                    borrowers => $patron->borrowernumber,
-                    issues    => $issue->itemnumber,
-                    items     => $issue->itemnumber,
-                    biblio    => $item->biblionumber,
-                },
-                lang => $patron->lang,
+        my $borrower_preferences =
+            C4::Members::Messaging::GetMessagingPreferences(
+                {
+                    borrowernumber => $borrowernumber,
+                    message_name   => 'auto_renewals'
+                }
             );
+        for my $issue ( @{ $report{$borrowernumber} } ) {
+            my $item = $issue->item;
+            # Force sending of email and only email if pref is set to "cron"
+            my @transports = $send_notices_pref eq 'preferences' ? keys %{ $borrower_preferences->{'transports'} } : 'sms';
+            foreach my $transport ( @transports ) {
+                my $letter = C4::Letters::GetPreparedLetter (
+                    module      => 'circulation',
+                    letter_code => 'AUTO_RENEWALS',
+                    tables      => {
+                        borrowers => $patron->borrowernumber,
+                        issues    => $issue->itemnumber,
+                        items     => $issue->itemnumber,
+                        biblio    => $item->biblionumber,
+                    },
+                    lang => $patron->lang,
+                    message_transport_type => $transport,
+                );
 
-            my $library = Koha::Libraries->find( $patron->branchcode );
-            my $admin_email_address = $library->from_email_address;
-
-            C4::Letters::EnqueueLetter(
-                {   letter                 => $letter,
-                    borrowernumber         => $borrowernumber,
-                    message_transport_type => 'email',
-                    from_address           => $admin_email_address,
+                if ($letter) {
+                    my $library = $patron->library;
+                    my $admin_email_address = $library->from_email_address;
+
+                    C4::Letters::EnqueueLetter(
+                        {
+                            letter                 => $letter,
+                            borrowernumber         => $borrowernumber,
+                            from_address           => $admin_email_address,
+                            message_transport_type => $transport
+                        }
+                    );
                 }
-            );
+            }
         }
     }