<commit_before>021-cronjob-send-only-one-message-per-notice.patch

This makes sure only ONE message_transport_type is used when running the overdue notices cronjob.
It tries sms if smsalertnumber exists but emails are missing.
Otherwise it tries email if any emails exists but smsalertnumber is missing.
Fallback is 'print' if any other fails.

diff --git a/misc/cronjobs/overdue_notices.pl b/misc/cronjobs/overdue_notices.pl
old mode 100644
new mode 100755
index 2e7346f..5a99d1a
--- a/misc/cronjobs/overdue_notices.pl
+++ b/misc/cronjobs/overdue_notices.pl
@@ -680,8 +680,19 @@ END_SQL
                 my $print_sent = 0; # A print notice is not yet sent for this patron
                 for my $mtt ( @message_transport_types ) {
                     my $effective_mtt = $mtt;
-                    if ( ($mtt eq 'email' and not scalar @emails_to_use) or ($mtt eq 'sms' and not $data->{smsalertnumber}) ) {
-                        # email or sms is requested but not exist, do a print.
+
+                    if ( ($mtt eq 'email' and not scalar @emails_to_use) and ($data->{smsalertnumber}) ) {
+                        # Missing email but smsalertnumber is present - use sms
+                        $effective_mtt = 'sms';
+                    }
+
+                    if ( ($mtt eq 'sms' and not $data->{smsalertnumber}) and (scalar @emails_to_use) ) {
+                        # Missing smsalertnumber, but some email address present - use email
+                        $effective_mtt = 'email';
+                    }
+
+                    if ( (not scalar @emails_to_use) and (not $data->{smsalertnumber}) ) {
+                        # fallback to 'print' as last resort if all is missing
                         $effective_mtt = 'print';
                     }

@@ -759,18 +770,19 @@ END_SQL
                               );
                         }
                         unless ( $effective_mtt eq 'print' and $print_sent == 1 ) {
-                            # Just sent a print if not already done.
-                            C4::Letters::EnqueueLetter(
-                                {   letter                 => $letter,
-                                    borrowernumber         => $borrowernumber,
-                                    message_transport_type => $effective_mtt,
-                                    from_address           => $admin_email_address,
-                                    to_address             => join(',', @emails_to_use),
-                                }
-                            ) unless $test_mode;
+                            # Enqueue exactly ONE notice.
                             # A print notice should be sent only once per overdue level.
-                            # Without this check, a print could be sent twice or more if the library checks sms and email and print and the patron has no email or sms number.
-                            $print_sent = 1;
+                            if ($print_sent == 0) {
+                              C4::Letters::EnqueueLetter(
+                                  {   letter                 => $letter,
+                                      borrowernumber         => $borrowernumber,
+                                      message_transport_type => $effective_mtt,
+                                      from_address           => $admin_email_address,
+                                      to_address             => join(',', @emails_to_use),
+                                  }
+                              ) unless $test_mode;
+                              $print_sent = 1;
+                            }
                         }
                     }
                 }<commit_msg>Fix overdue cronjob patch against original<commit_after>021-cronjob-send-only-one-message-per-notice.patch

This makes sure only ONE message_transport_type is used when running the overdue notices cronjob.
It tries sms if smsalertnumber exists but emails are missing.
Otherwise it tries email if any emails exists but smsalertnumber is missing.
Fallback is 'print' if any other fails.

diff --git a/misc/cronjobs/overdue_notices.pl b/misc/cronjobs/overdue_notices.pl
old mode 100644
new mode 100755
index 7504c7a..5a99d1a
--- a/misc/cronjobs/overdue_notices.pl
+++ b/misc/cronjobs/overdue_notices.pl
@@ -680,8 +680,19 @@ END_SQL
                 my $print_sent = 0; # A print notice is not yet sent for this patron
                 for my $mtt ( @message_transport_types ) {
                     my $effective_mtt = $mtt;
-                    if ( ($mtt eq 'email' and not scalar @emails_to_use) or ($mtt eq 'sms' and not $data->{smsalertnumber}) ) {
-                        # email or sms is requested but not exist, do a print.
+
+                    if ( ($mtt eq 'email' and not scalar @emails_to_use) and ($data->{smsalertnumber}) ) {
+                        # Missing email but smsalertnumber is present - use sms
+                        $effective_mtt = 'sms';
+                    }
+
+                    if ( ($mtt eq 'sms' and not $data->{smsalertnumber}) and (scalar @emails_to_use) ) {
+                        # Missing smsalertnumber, but some email address present - use email
+                        $effective_mtt = 'email';
+                    }
+
+                    if ( (not scalar @emails_to_use) and (not $data->{smsalertnumber}) ) {
+                        # fallback to 'print' as last resort if all is missing
                         $effective_mtt = 'print';
                     }

@@ -759,18 +770,19 @@ END_SQL
                               );
                         }
                         unless ( $effective_mtt eq 'print' and $print_sent == 1 ) {
-                            # Just sent a print if not already done.
-                            C4::Letters::EnqueueLetter(
-                                {   letter                 => $letter,
-                                    borrowernumber         => $borrowernumber,
-                                    message_transport_type => $effective_mtt,
-                                    from_address           => $admin_email_address,
-                                    to_address             => join(',', @emails_to_use),
-                                }
-                            ) unless $test_mode;
+                            # Enqueue exactly ONE notice.
                             # A print notice should be sent only once per overdue level.
-                            # Without this check, a print could be sent twice or more if the library checks sms and email and print and the patron has no email or sms number.
-                            $print_sent = 1 if $effective_mtt eq 'print';
+                            if ($print_sent == 0) {
+                              C4::Letters::EnqueueLetter(
+                                  {   letter                 => $letter,
+                                      borrowernumber         => $borrowernumber,
+                                      message_transport_type => $effective_mtt,
+                                      from_address           => $admin_email_address,
+                                      to_address             => join(',', @emails_to_use),
+                                  }
+                              ) unless $test_mode;
+                              $print_sent = 1;
+                            }
                         }
                     }
                 }
{'fraction_non_alphanumeric': 0.08364461045891142, 'fraction_numerical': 0.013340448239060833, 'mean_word_length': 1.10471645143178, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3524329', 'n_tokens_mistral': 2010, 'n_tokens_neox': 1851, 'n_words': 537}