From 2103a18a3be93d38f2c1e69bea14a2681e382007 Mon Sep 17 00:00:00 2001 From: mark Date: Fri, 3 Feb 2012 01:08:11 +0000 Subject: [PATCH] email to search results, #16246 --- FS/FS/cust_main_Mixin.pm | 40 ++++++++++++++++++++---------------- httemplate/misc/email-customers.html | 5 ++++- httemplate/search/svc_acct.cgi | 4 ++++ 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/FS/FS/cust_main_Mixin.pm b/FS/FS/cust_main_Mixin.pm index d493060f0..7fcb5b5c7 100644 --- a/FS/FS/cust_main_Mixin.pm +++ b/FS/FS/cust_main_Mixin.pm @@ -388,6 +388,7 @@ in the job fail, the entire job will abort and return an error. use Storable qw(thaw); use MIME::Base64; use Data::Dumper qw(Dumper); +use Digest::SHA qw(sha1); # for duplicate checking sub email_search_result { my($class, $param) = @_; @@ -438,32 +439,23 @@ sub email_search_result { } my $cust_main = $obj->cust_main; - my @message; + tie my %message, 'Tie::IxHash'; if ( !$cust_main ) { next; # unlinked object; nothing else we can do } - if( $sent_to{$cust_main->custnum} ) { - # avoid duplicates - $dups++; - next; - } - - $sent_to{$cust_main->custnum} = 1; - if ( $msg_template ) { - # XXX add support for other context objects? - # If we do that, handling of "duplicates" will - # have to be smarter. Currently we limit to - # one message per custnum because they'd all - # be identical. - @message = $msg_template->prepare( 'cust_main' => $cust_main ); + # Now supports other context objects. + %message = $msg_template->prepare( + 'cust_main' => $cust_main, + 'object' => $obj, + ); } else { my @to = $cust_main->invoicing_list_emailonly; next if !@to; - @message = ( + %message = ( 'from' => $from, 'to' => \@to, 'subject' => $subject, @@ -473,7 +465,19 @@ sub email_search_result { ); } #if $msg_template - $error = send_email( generate_email( @message ) ); + # For non-cust_main searches, we avoid duplicates based on message + # body text. + my $unique = $cust_main->custnum; + $unique .= sha1($message{'text_body'}) if $class ne 'FS::cust_main'; + if( $sent_to{$unique} ) { + # avoid duplicates + $dups++; + next; + } + + $sent_to{$unique} = 1; + + $error = send_email( generate_email( %message ) ); if($error) { # queue the sending of this message so that the user can see what we @@ -484,7 +488,7 @@ sub email_search_result { 'status' => 'failed', 'statustext' => $error, }; - $queue->insert(@message); + $queue->insert(%message); push @retry_jobs, $queue; } else { diff --git a/httemplate/misc/email-customers.html b/httemplate/misc/email-customers.html index d981aeae9..b87a1bfbb 100644 --- a/httemplate/misc/email-customers.html +++ b/httemplate/misc/email-customers.html @@ -191,7 +191,10 @@ if ( $cgi->param('action') eq 'preview' ) { $sql_query->{'order_by'} = ''; my $object = qsearchs($sql_query); my $cust = $object->cust_main; - my %message = $msg_template->prepare( 'cust_main' => $cust ); + my %message = $msg_template->prepare( + 'cust_main' => $cust, + 'object' => $object + ); ($from, $subject, $html_body) = @message{'from', 'subject', 'html_body'}; } } diff --git a/httemplate/search/svc_acct.cgi b/httemplate/search/svc_acct.cgi index be649a5f8..92e1c500c 100755 --- a/httemplate/search/svc_acct.cgi +++ b/httemplate/search/svc_acct.cgi @@ -11,6 +11,10 @@ 'color' => \@color, 'style' => \@style, 'footer' => \@footer, + 'html_init' => include('/elements/email-link.html', + 'search_hash' => \%search_hash, + 'table' => 'svc_acct' + ), &> <%once> -- 2.11.0