From 6e7e454e4a612ca5323911029a8928642dc61f27 Mon Sep 17 00:00:00 2001 From: Hammat Wele Date: Thu, 26 Jan 2023 14:48:54 +0000 Subject: [PATCH] Bug 17649: Only one message per borrower and overdue level The added option (-a|--all_in_one) allows to create only one message per borrower and delay level including overdues from whatever branches. A branchcode value (branches.branchcode) has to be given to choose which sender address will be set. == test plan == 0/ Have (or create) at least two branches B1 and B2. Have (or create) a default notice for all branches in Notices and Slips Module 1/ Make sure you're logged in with an account belonging to B1, or switch to branch B1 2/ checkout item I1 (belonging to B1) to patron P (specifying as due date the day before (1 day overdue)) 3/ switch to branch B2 4/ checkout item I2 (belonging to B2) to patron P (specifying as due date the day before (1 day overdue)) 5/ make sure you have defined "Overdue notice/status triggers" for that patron category, ie with Delay=1 so that "Overdue Notice" is selected and "Email" is checked. 6/ from cli run 'perl ./misc/cronjobs/overdue_notices.pl' (optionally specifying also '-library B1 -library B2' ) 7/ Two new entries will be generated in message_queue DB table, coming from different branches that have the SAME list of overdue items 8/ either see the 'content' field in these entries, or run 'perl ./cronjobs/process_message_queue.pl' to receive the actual emails (provided the email is properly setup). Apply patch redo step 6 running the script with the new option -a (all_in_one). this option expect as mandatory parameter the branchcode of the library that you want to make sender of the message. say that 'B2CODE' is the branchcode of B2 branch, it will run like this : perl ./misc/cronjobs/overdue_notices.pl -a B2CODE You will see that only one message had been created. Its content shows both B1 and B2 overdue items and the sender address matches B2 address as well. https://bugs.koha-community.org/show_bug.cgi?id=32511 --- misc/cronjobs/overdue_notices.pl | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/misc/cronjobs/overdue_notices.pl b/misc/cronjobs/overdue_notices.pl index f8fc79203c..59c283c941 100755 --- a/misc/cronjobs/overdue_notices.pl +++ b/misc/cronjobs/overdue_notices.pl @@ -72,6 +72,7 @@ overdue_notices.pl --frombranch Organize and send overdue notices by home library (item-homebranch) or checkout library (item-issuebranch). This option is only used, if the OverdueNoticeFrom system preference is set to 'command-line option'. Defaults to item-issuebranch. + -a Create only one message per borrower and delay level including overdues from all branches. =head1 OPTIONS @@ -188,6 +189,13 @@ Organize overdue notices either by checkout library (item-issuebranch) or item h This option is only used, if the OverdueNoticeFrom system preference is set to use 'command-line option'. Defaults to checkout library (item-issuebranch). +=item B<-a> | B<--all_in_one> + +Create only one message per borrower and for each delay level including overdues from all branches. +Branchcode value (branches.branchcode) has to be given to choose which sender address will be set. +It's recommended to use this option only with default notice(s) defined for 'All branches' +in Notices & Slips and no specific ones. + =back =head1 DESCRIPTION @@ -310,6 +318,7 @@ my $itemscontent = join( ',', qw( date_due title barcode author itemnumber ) ); my @myborcat; my @myborcatout; my ( $date_input, $today ); +my $all_in_one; my $command_line_options = join(" ",@ARGV); @@ -332,6 +341,7 @@ GetOptions( 'borcatout=s' => \@myborcatout, 'email=s' => \@emails, 'frombranch=s' => \$frombranch, + 'a|all_in_one=s' => \$all_in_one, ) or pod2usage(2); pod2usage(1) if $help; pod2usage( -verbose => 2 ) if $man; @@ -457,6 +467,8 @@ elsif ( defined $text_filename ) { } } +my %already_queued; +my %seen = map { $_ => 1 } @branches; foreach my $branchcode (@branches) { my $calendar; if ( C4::Context->preference('OverdueNoticeCalendar') ) { @@ -466,7 +478,7 @@ foreach my $branchcode (@branches) { } } - my $library = Koha::Libraries->find($branchcode); + my $library = $all_in_one ? Koha::Libraries->find($all_in_one) : Koha::Libraries->find($branchcode); my $admin_email_address = $library->from_email_address; my $branch_email_address = C4::Context->preference('AddressForFailedOverdueNotices') || $library->inbound_email_address; @@ -490,7 +502,7 @@ END_SQL if($owning_library) { $sql2 .= ' AND items.homebranch = ? '; } else { - $sql2 .= ' AND issues.branchcode = ? '; + $sql2 .= ' AND issues.branchcode = ? ' unless $all_in_one; } my $sth2 = $dbh->prepare($sql2); @@ -606,6 +618,7 @@ END_SQL next; } $borrowernumber = $data->{'borrowernumber'}; + next if ($all_in_one && $already_queued{"$borrowernumber$i"}); my $borr = sprintf( "%s%s%s (%s)", $data->{'surname'} || '', $data->{'firstname'} && $data->{'surname'} ? ', ' : '', @@ -656,7 +669,7 @@ END_SQL ) unless $test_mode; $verbose and warn "debarring $borr\n"; } - my @params = ($borrowernumber,$branchcode); + my @params = $all_in_one ? ($borrowernumber) : ($borrowernumber, $branchcode); $sth2->execute(@params); my $itemcount = 0; @@ -701,6 +714,7 @@ END_SQL $exceededPrintNoticesMaxLines = 1; last; } + next if $all_in_one and ! grep {$seen{$item_info->{branchcode}}} @branches; $j++; $titles .= C4::Letters::get_item_content( { item => $item_info, item_content_fields => \@item_content_fields, dateonly => 1 } ); @@ -824,6 +838,7 @@ END_SQL } } } + $already_queued{"$borrowernumber$i"} = 1; } $sth->finish; } -- 2.34.1