X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_main_Mixin.pm;h=212c04e0f520eaf98de00075ff95fd93c0105278;hb=3d0a1bb06b895c5be6e3f0517d355442a6b1e125;hp=8c8553c091d7631e7bc39f5f6858e3265d073c0c;hpb=eb02bd7d44776cadc4c17f72df508afd223b142f;p=freeside.git diff --git a/FS/FS/cust_main_Mixin.pm b/FS/FS/cust_main_Mixin.pm index 8c8553c09..212c04e0f 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) = @_; @@ -408,10 +409,8 @@ sub email_search_result { or die "msgnum $msgnum not found\n"; } - $param->{'payby'} = [ split(/\0/, $param->{'payby'}) ] - unless ref($param->{'payby'}); - my $sql_query = $class->search($param->{'search'}); + $sql_query->{'select'} = $sql_query->{'table'} . '.*'; my $count_query = delete($sql_query->{'count_query'}); my $count_sth = dbh->prepare($count_query) @@ -441,41 +440,45 @@ 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_scalar; - next if !$to; + my @to = $cust_main->invoicing_list_emailonly; + next if !@to; - @message = ( + %message = ( 'from' => $from, - 'to' => $to, + 'to' => \@to, 'subject' => $subject, 'html_body' => $html_body, 'text_body' => $text_body, + 'custnum' => $cust_main->custnum, ); } #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 @@ -486,7 +489,7 @@ sub email_search_result { 'status' => 'failed', 'statustext' => $error, }; - $queue->insert(@message); + $queue->insert(%message); push @retry_jobs, $queue; } else { @@ -536,10 +539,46 @@ sub process_email_search_result { die "error loading FS::$table: $@\n" if $@; my $error = "FS::$table"->email_search_result( $param ); + dbh->commit; # save failed jobs before rethrowing the error die $error if $error; } +=item conf + +Returns a configuration handle (L) set to the customer's locale, +if they have one. If not, returns an FS::Conf with no locale. + +=cut + +sub conf { + my $self = shift; + return $self->{_conf} if (ref $self and $self->{_conf}); + my $cust_main = $self->cust_main; + my $conf = new FS::Conf { + 'locale' => ($cust_main ? $cust_main->locale : '') + }; + $self->{_conf} = $conf if ref $self; + return $conf; +} + +=item mt TEXT [, ARGS ] + +Localizes a text string (see L) for the customer's locale, +if they have one. + +=cut + +sub mt { + my $self = shift; + return $self->{_lh}->maketext(@_) if (ref $self and $self->{_lh}); + my $cust_main = $self->cust_main; + my $locale = $cust_main ? $cust_main->locale : ''; + my $lh = FS::L10N->get_handle($locale); + $self->{_lh} = $lh if ref $self; + return $lh->maketext(@_); +} + =back =head1 BUGS