From dfab054de3fc799fdbae4aea2719297b2e8cd475 Mon Sep 17 00:00:00 2001 From: Thibaud Guillot Date: Thu, 18 Aug 2022 16:38:19 +0200 Subject: [PATCH] Bug 31487: Add an option to send by mail longoverdue items to borrowers Test plan: 1) You must have longoverdue items to one or many borrowers 2) You must create a notice template (Notices & slips section) 3) You can run the script misc/cronjobs/longoverdue.pl manually without --mail option first (see -h or --help for more info about the script) 4) You must see an output with some longoverdues (step 1 required) 5) Now you can run the script with --mail option but like its specify in "help" option you must add option --code="" (relative to your code template) and --branchcode="". 6) Normally you will see after the classic output a new block with emails added to the message queue and it will be sent on the next misc/cronjobs/process_message_queue.pl script iteration --- misc/cronjobs/longoverdue.pl | 86 +++++++++++++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 2 deletions(-) diff --git a/misc/cronjobs/longoverdue.pl b/misc/cronjobs/longoverdue.pl index 457ddf4465..10aea5582b 100755 --- a/misc/cronjobs/longoverdue.pl +++ b/misc/cronjobs/longoverdue.pl @@ -34,6 +34,7 @@ use Pod::Usage qw( pod2usage ); use C4::Circulation qw( LostItem MarkIssueReturned ); use C4::Context; use C4::Log qw( cronlogaction ); +use C4::Letters; use Koha::ItemTypes; use Koha::Patron::Categories; use Koha::Patrons; @@ -52,6 +53,7 @@ my $man=0; my $list_categories = 0; my $list_itemtypes = 0; my @skip_lost_values; +my ($mail, $code, $branchcode, $letter_template); GetOptions( 'l|lost=s%' => \$lost, @@ -70,6 +72,9 @@ GetOptions( 'skip-itemtype=s' => $skip_itemtype, 'list-itemtypes' => \$list_itemtypes, 'skip-lost-value=s' => \@skip_lost_values, + 'mail' => \$mail, + 'code=s' => \$code, + 'branchcode=s' => \$branchcode, ); if ( $man ) { @@ -213,6 +218,18 @@ Display short help message an exit. Display entire manual and exit. +=item B<--mail > + +Send a mail based on configured letter template. + +=item B<--code > + +Get notice template code for longoverdue mail. + +=item B<--branchcode > + +Specify particular branchcode for longoverdue mail. + =back =cut @@ -275,8 +292,7 @@ if ( ! defined($charge) ) { } unless ($confirm) { $verbose = 1; # If you're not running it for real, then the whole point is the print output. - print "### TEST MODE -- NO ACTIONS TAKEN ###\n"; -} + } cronlogaction(); @@ -368,6 +384,18 @@ if ( @$skip_itemtype ) { %itemtype_to_process = ( %itemtype_to_process, map { $_ => 0 } @$skip_itemtype ); } +if ( $mail && !$code || $mail && !$branchcode) { + print "Argument -code or -branchcode missing for mail option\n"; +} else { + $letter_template = Koha::Notice::Templates->find_effective_template( + { + module => 'circulation', + code => $code, + branchcode => $branchcode, + } + ); +} + my $filter_itemtypes = ( scalar @$itemtype || scalar @$skip_itemtype ); my $count; @@ -379,6 +407,9 @@ my $i = 0; # We need a better way to handle this. # my $sth_items = longoverdue_sth({ skip_lost_values => \@skip_lost_values }); +my $reminders_email; +my $letter; +my $admin_email_address; foreach my $startrange (sort keys %$lost) { if( my $lostvalue = $lost->{$startrange} ) { @@ -410,6 +441,28 @@ foreach my $startrange (sort keys %$lost) { MarkIssueReturned($row->{borrowernumber},$row->{itemnumber},undef,$patron->privacy) } } + if ($letter_template) { + + $letter = $letter_template->unblessed; + my $library = Koha::Libraries->find($branchcode); + $admin_email_address = $library->from_email_address; + + my $borrowernumber = $row->{borrowernumber}; + my $itemnumber = $row->{itemnumber}; + my $patron = Koha::Patrons->find($borrowernumber); + my $notice_email = $patron->email; + + if ($notice_email) { + $reminders_email->{$borrowernumber}->{'email'} = + $notice_email; + push + @{ $reminders_email->{$borrowernumber}->{'itemnumbers'} }, + $itemnumber; + } + } elsif ($mail) { + print "\nLetter template not found\n"; + } + $count++; } push @report, { @@ -443,3 +496,32 @@ if (!$quiet){ summarize (\@report, 1); print "\nTOTAL: $total items\n"; } + +if ($reminders_email) { + foreach ( keys %$reminders_email ) { + my $items_due = + join( ";", @{ %$reminders_email{$_}->{'itemnumbers'} } ); + + C4::Letters::EnqueueLetter( + { + letter => $letter, + borrowernumber => $_, + message_transport_type => 'email', + from_address => $admin_email_address, + to_address => %$reminders_email{$_}->{'email'}, + items => $items_due, + } + ); + + printf( +"\nEnqueue mail to %s - borrowernumber %s for longoverdue on item(s): %s\n", + %$reminders_email{$_}->{'email'}, + $_, $items_due + ); + } +} +else { + if ($mail) { + print "\nNo mail add to enqueue list\n"; + } +} -- 2.25.1