From a57dc4fc659c0581b119c699a66988932b679144 Mon Sep 17 00:00:00 2001 From: mark Date: Wed, 29 Dec 2010 00:02:29 +0000 Subject: [PATCH] send_email export, RT#10884 --- FS/FS/Misc.pm | 4 +- FS/FS/msg_template.pm | 15 ++-- FS/FS/part_export/send_email.pm | 160 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 171 insertions(+), 8 deletions(-) create mode 100644 FS/FS/part_export/send_email.pm diff --git a/FS/FS/Misc.pm b/FS/FS/Misc.pm index fe8ac6082..6108f778a 100644 --- a/FS/FS/Misc.pm +++ b/FS/FS/Misc.pm @@ -323,9 +323,9 @@ sub generate_email { my $data; if ( ref($args{'text_body'}) eq 'ARRAY' ) { - $data = $args{'text_body'}; + $data = join("\n", @{ $args{'text_body'} }); } else { - $data = [ split(/\n/, $args{'text_body'}) ]; + $data = $args{'text_body'}; } $alternative->attach( diff --git a/FS/FS/msg_template.pm b/FS/FS/msg_template.pm index f0cda41bf..a35b2d19f 100644 --- a/FS/FS/msg_template.pm +++ b/FS/FS/msg_template.pm @@ -178,7 +178,7 @@ rt_ticket export when exporting "replace" events. =item to Destination address. The default is to use the customer's -invoicing_list addresses. +invoicing_list addresses. Multiple addresses may be comma-separated. =back @@ -281,11 +281,14 @@ sub prepare { # and email ### - my @to = ($opt{'to'}) || $cust_main->invoicing_list_emailonly; - #warn "prepared msg_template with no email destination (custnum ". - # $cust_main->custnum.")\n" - # if !@to; - # warning is not appropriate now that we use these for tickets + my @to; + if ( exists($opt{'to'}) ) { + @to = split(/\s*,\s*/, $opt{'to'}); + } + else { + @to = $cust_main->invoicing_list_emailonly; + } + # no warning when preparing with no destination my $conf = new FS::Conf; diff --git a/FS/FS/part_export/send_email.pm b/FS/FS/part_export/send_email.pm new file mode 100644 index 000000000..05f623633 --- /dev/null +++ b/FS/FS/part_export/send_email.pm @@ -0,0 +1,160 @@ +package FS::part_export::send_email; + +use vars qw(@ISA %info); +use Tie::IxHash; +use FS::part_export; +use FS::Record qw(qsearch qsearchs); +use FS::Conf; +use FS::msg_template; +use FS::Misc qw(send_email); + +@ISA = qw(FS::part_export); + +my %templates; +my %template_select = ( + type => 'select', + freeform => 1, + option_label => sub { + $templates{$_[0]}; + }, + option_values => sub { + %templates = (0 => '', + map { $_->msgnum, $_->msgname } + qsearch({ table => 'msg_template', + hashref => {}, + order_by => 'ORDER BY msgnum ASC' + }) + ); + sort keys (%templates); + }, +); + +tie my %options, 'Tie::IxHash', ( + 'insert_template' => { + before => ' + + + + +', + }, + 'delete_template' => { + before => ' + +', + }, + 'replace_template' => { + before => ' + +', + }, + 'suspend_template' => { + before => ' + +', + }, + 'unsuspend_template' => { + before => ' + +
Template
New service', + %template_select, + after => '
Delete', + %template_select, + after => '
Modify', + %template_select, + after => '
Suspend', + %template_select, + after => '
Unsuspend', + %template_select, + after => '
+', + }, + 'to_customer' => { + label => 'Send to customer', + type => 'checkbox', + }, + 'to_address' => { + label => 'Send to other address: ', + type => 'text', + }, +); + +%info = ( + 'svc' => [qw( svc_acct svc_broadband svc_phone svc_domain )], + 'desc' => + 'Send an email message', + 'options' => \%options, + 'nodomain' => '', + 'notes' => ' + Send an email message. The subject and body of the message + will be generated from a message template.' +); + +sub _export { + my( $self, $action, $svc ) = (shift, shift, shift); + my $conf = new FS::Conf; + + my $msgnum = $self->option($action.'_template'); + return if !$msgnum; + + my $msg_template = FS::msg_template->by_key($msgnum); + return "Template $msgnum not found\n" if !$msg_template; + + my $cust_pkg = $svc->cust_svc->cust_pkg; + my $cust_main = $svc->cust_svc->cust_pkg->cust_main if $cust_pkg; + my $custnum = $cust_main->custnum if $cust_main; + my $svcnum = $svc->svcnum if $action ne 'delete'; + + my @to = split(',', $self->option('to_address') || ''); + push @to, $cust_main->invoicing_list_emailonly + if $self->option('to_customer') and $cust_main; + if ( !@to ) { + warn 'No destination address for send_email export: custnum '.$cust_main->custnum; + # warn, don't die, but also avoid sending the template with _no_ 'to'=> + # param, which would send to the customer by default. + return; + } + + if ( $action eq 'replace' ) { + my $old = shift; + return $msg_template->send( + 'cust_main' => $cust_main, + 'object' => [ $svc, $old ], + 'to' => join(',', @to), + ); + } + else { + return $msg_template->send( + 'cust_main' => $cust_main, + 'object' => $svc, + 'to' => join(',', @to), + ); + } +} + +sub _export_insert { + my($self, $svc) = (shift, shift); + $self->_export('insert', $svc); +} + +sub _export_replace { + my($self, $new, $old) = (shift, shift, shift); + $self->_export('replace', $new, $old); +} + +sub _export_delete { + my($self, $svc) = (shift, shift); + $self->_export('delete', $svc); +} + +sub _export_suspend { + my($self, $svc) = (shift, shift); + $self->_export('suspend', $svc); +} + +sub _export_unsuspend { + my($self, $svc) = (shift, shift); + $self->_export('unsuspend', $svc); +} + +1; -- 2.11.0