Bug 41315 - Using patron-homelibrary option for overdue notices may not send notices to all branches
Summary: Using patron-homelibrary option for overdue notices may not send notices to a...
Status: Needs Signoff
Alias: None
Product: Koha
Classification: Unclassified
Component: Command-line Utilities (show other bugs)
Version: Main
Hardware: All All
: P5 - low major
Assignee: Nick Clemens (kidclamp)
QA Contact: Testopia
URL:
Keywords:
Depends on: 32740
Blocks: 41316
  Show dependency treegraph
 
Reported: 2025-11-26 15:31 UTC by Nick Clemens (kidclamp)
Modified: 2025-11-26 16:38 UTC (History)
1 user (show)

See Also:
GIT URL:
Initiative type: ---
Sponsorship status: ---
Comma delimited list of Sponsors:
Crowdfunding goal: 0
Patch complexity: ---
Documentation contact:
Documentation submission:
Text to go in the release notes:
Version(s) released in:
Circulation function:


Attachments
Bug 41315: Change only notice branchcode when OverDueNoticeFrom is set to patron-homelibrary (4.59 KB, patch)
2025-11-26 16:26 UTC, Nick Clemens (kidclamp)
Details | Diff | Splinter Review
Bug 41315: Remove unecessary conditional (1.54 KB, patch)
2025-11-26 16:26 UTC, Nick Clemens (kidclamp)
Details | Diff | Splinter Review
Bug 41315: (follow-up) Rename statements for clarity (4.97 KB, patch)
2025-11-26 16:26 UTC, Nick Clemens (kidclamp)
Details | Diff | Splinter Review
Bug 41315: Don't change which branch is used for getting the transports (1.21 KB, patch)
2025-11-26 16:26 UTC, Nick Clemens (kidclamp)
Details | Diff | Splinter Review

Note You need to log in before you can comment on or make changes to this bug.
Description Nick Clemens (kidclamp) 2025-11-26 15:31:26 UTC
Bug 32740 added the option to send overdue notices from the patron's homelibrary.

The branchcode variable is used for several purposes, in the new code:
 649                 if ($patron_homelibrary) {
 650                     $branchcode           = $patron->branchcode;
 651                     $library              = Koha::Libraries->find($branchcode);
 652                     $admin_email_address  = $library->from_email_address;
 653                     $branch_email_address = C4::Context->preference('AddressForFailedOverdueNotices')
 654                         || $library->inbound_email_address;
 655                 }


We change that branchcode param based on the patron, however, earlier when getting the list of patrons we limit by issue branch:
 567             my $borrower_sql = <<"END_SQL";
 568 SELECT issues.borrowernumber, firstname, surname, address, address2, city, zipcode, country, email, emailpro, B_email, smsalertnumber, phone, cardnumber, date_due
 569 FROM   issues,borrowers,categories,items
 570 WHERE  issues.borrowernumber=borrowers.borrowernumber
 571 AND    borrowers.categorycode=categories.categorycode
 572 AND    issues.itemnumber = items.itemnumber
 573 AND    items.itemlost = 0
 574 AND    TO_DAYS($date)-TO_DAYS(issues.date_due) >= 0
 575 END_SQL
 576             my @borrower_parameters;
 577             if ($branchcode) {
 578                 if ($owning_library) {
 579                     $borrower_sql .= ' AND items.homebranch=? ';
 580                 } else {
 581                     $borrower_sql .= ' AND issues.branchcode=? ';
 582                 }
 583                 push @borrower_parameters, $branchcode;
 584             }

So we may have a patron from a different branch when we go through here, and end up changing the branch we will look at during the next pass of the code above



I recreated by checking out three items to patrons from different branches, 1 day overdue each:
MariaDB [koha_kohadev]> SELECT borrowers.cardnumber, borrowers.surname, borrowers.branchcode, borrowers.categorycode, issues.issue_id,  issues.branchcode, issues.date_due FROM issues JOIN borrowers USING (borrowernumber);
+----------------+---------+------------+--------------+----------+------------+---------------------+
| cardnumber     | surname | branchcode | categorycode | issue_id | branchcode | date_due            |
+----------------+---------+------------+--------------+----------+------------+---------------------+
| 23529001000463 | Acosta  | MPL        | PT           |        1 | CPL        | 2025-11-25 23:59:00 |
| 23529000139858 | Ballard | FFL        | PT           |        2 | CPL        | 2025-11-25 23:59:00 |
| 23529000035676 | Acevedo | IPT        | PT           |        3 | CPL        | 2025-11-25 23:59:00 |
+----------------+---------+------------+--------------+----------+------------+---------------------+
3 rows in set (0.001 sec)


Setting overdue rules to trigger at 1 day, then got this output:

branch 'CPL', categorycode = PT pass 1
Using letter code 'ODUE' for pass 1
Found 3 borrowers with overdues
borrower Acosta, Edna (5) has items triggering level 1.
borrower Acevedo, Henry (19) has items triggering level 1.
borrower Ballard, Ronnie (21) has items triggering level 1.
branch 'FFL', categorycode = PT pass 2
No letter code found for pass 2
branch 'FFL', categorycode = PT pass 3
No letter code found for pass 3
Comment 1 Nick Clemens (kidclamp) 2025-11-26 16:26:23 UTC
Created attachment 189965 [details] [review]
Bug 41315: Change only notice branchcode when OverDueNoticeFrom is set to patron-homelibrary

This patch adds a new variable $notice_branchcode and uses this when getting the patron's
homelibrary to ensure we don't change the branch we are using.

To test:
 1 - At branch A - checkout three items, 1 day overdue to 3 patrons of the same category from different branches (not A)
 2 - Set the system preference OverdueNoticeFrom to 'patron home library'
 3 - Browse to More->Tools->Overdue notice/status triggers
 4 - Set overdue triggers for default library and the category used above to:
     Delay: 1
     Letter: Overdue notice (ODUE)
 5 - From the command line run the overdue notices:
     perl misc/cronjobs/overdue_notices.pl --triggered --library CPL --test --nomail -v -v
 6 - Note that we begin pass 1 with CPL and then use a different branch for passes 2 and 3
 7 - Apply patch
 8 - Repeat
 9 - Confirm branch is consistent
10 - Run without branch param:
     perl misc/cronjobs/overdue_notices.pl --triggered --library CPL --test --nomail -v -v
11 - Confirm branches are consistent
Comment 2 Nick Clemens (kidclamp) 2025-11-26 16:26:26 UTC
Created attachment 189966 [details] [review]
Bug 41315: Remove unecessary conditional

We are in a loop here:
foreach my $branchcode (@branches) {

branchcode must be defined or something has gone seriously wrong with PERL
Comment 3 Nick Clemens (kidclamp) 2025-11-26 16:26:28 UTC
Created attachment 189967 [details] [review]
Bug 41315: (follow-up) Rename statements for clarity
Comment 4 Nick Clemens (kidclamp) 2025-11-26 16:26:31 UTC
Created attachment 189968 [details] [review]
Bug 41315: Don't change which branch is used for getting the transports

See bug 41316 - the script uses the triggers of the issuing branch when sending
by patron library - we shouldn't change this here, but on the next bug