From f684ff84028e3e98027465e78f24760802986f35 Mon Sep 17 00:00:00 2001 From: Olivier Crouzet Date: Thu, 22 Dec 2016 18:06:07 +0100 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. Rebased-on: 2021-09-24 Victor Grousset/tuxayo --- misc/cronjobs/overdue_notices.pl | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/misc/cronjobs/overdue_notices.pl b/misc/cronjobs/overdue_notices.pl index a1c2211628..95c16e2882 100755 --- a/misc/cronjobs/overdue_notices.pl +++ b/misc/cronjobs/overdue_notices.pl @@ -78,6 +78,7 @@ overdue_notices.pl -email Type of email that will be used. Can be 'email', 'emailpro' or 'B_email'. Repeatable. --frombranch Set the from address for the notice to one of 'item-homebranch' or 'item-issuebranch'. + -a Create only one message per borrower and delay level including overdues from all branches. =head1 OPTIONS @@ -192,6 +193,13 @@ Use the address information from the item homebranch library instead of the issu Defaults to '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 @@ -314,6 +322,7 @@ my $itemscontent = join( ',', qw( date_due title barcode author itemnumber ) ); my @myborcat; my @myborcatout; my ( $date_input, $today ); +my $all_in_one; GetOptions( 'help|?' => \$help, @@ -334,6 +343,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; @@ -459,6 +469,8 @@ elsif ( defined $text_filename ) { } } +my %already_queued; +my %seen = map { $_ => 1 } @branches; foreach my $branchcode (@branches) { my $calendar; if ( C4::Context->preference('OverdueNoticeCalendar') ) { @@ -468,7 +480,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; @@ -487,6 +499,8 @@ SELECT biblio.*, items.*, issues.*, biblioitems.itemtype, branchname AND items.itemlost = 0 AND TO_DAYS($date)-TO_DAYS(issues.date_due) >= 0 END_SQL + $sql2.= " AND issues.branchcode = ?" unless $all_in_one; + my $sth2 = $dbh->prepare($sql2); if($owning_library) { $sql2 .= ' AND items.homebranch = ? '; @@ -601,6 +615,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'} ? ', ' : '', @@ -652,7 +667,7 @@ END_SQL ) unless $test_mode; $verbose and warn "debarring $borr\n"; } - my @params = ($borrowernumber,$branchcode); + my @params = $all_in_one ? ($borrowernumber) : ($borrowernumber, $branchcode); $verbose and warn "STH2 PARAMS: borrowernumber = $borrowernumber"; $sth2->execute(@params); @@ -698,6 +713,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 } ); @@ -819,6 +835,7 @@ END_SQL } } } + $already_queued{"$borrowernumber$i"} = 1; } $sth->finish; } -- 2.33.0