msg_template improvements, RT#8324
[freeside.git] / FS / FS / svc_acct.pm
index 0458bb9..3b26688 100644 (file)
@@ -721,82 +721,89 @@ sub insert {
     }
 
     #welcome email
-    my ($to,$welcome_template,$welcome_from,$welcome_subject,$welcome_subject_template,$welcome_mimetype)
-      = ('','','','','','');
-
-    if ( $conf->exists('welcome_email', $agentnum) ) {
-      $welcome_template = new Text::Template (
-        TYPE   => 'ARRAY',
-        SOURCE => [ map "$_\n", $conf->config('welcome_email', $agentnum) ]
-      ) or warn "can't create welcome email template: $Text::Template::ERROR";
-      $welcome_from = $conf->config('welcome_email-from', $agentnum);
-        # || 'your-isp-is-dum'
-      $welcome_subject = $conf->config('welcome_email-subject', $agentnum)
-        || 'Welcome';
-      $welcome_subject_template = new Text::Template (
-        TYPE   => 'STRING',
-        SOURCE => $welcome_subject,
-      ) or warn "can't create welcome email subject template: $Text::Template::ERROR";
-      $welcome_mimetype = $conf->config('welcome_email-mimetype', $agentnum)
-        || 'text/plain';
+    my $error = '';
+    my $msgnum = $conf->config('welcome_msgnum', $agentnum);
+    if ( $msgnum ) {
+      my $msg_template = qsearchs('msg_template', { msgnum => $msgnum });
+      $error = $msg_template->send('cust_main' => $cust_main);
     }
-    if ( $welcome_template && $cust_pkg ) {
-      my $to = join(', ', grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list );
-      if ( $to ) {
-
-        my %hash = (
-                     'custnum'  => $self->custnum,
-                     'username' => $self->username,
-                     'password' => $self->_password,
-                     'first'    => $cust_main->first,
-                     'last'     => $cust_main->getfield('last'),
-                     'pkg'      => $cust_pkg->part_pkg->pkg,
-                   );
-        my $wqueue = new FS::queue {
-          'svcnum' => $self->svcnum,
-          'job'    => 'FS::svc_acct::send_email'
-        };
-        my $error = $wqueue->insert(
-          'to'       => $to,
-          'from'     => $welcome_from,
-          'subject'  => $welcome_subject_template->fill_in( HASH => \%hash, ),
-          'mimetype' => $welcome_mimetype,
-          'body'     => $welcome_template->fill_in( HASH => \%hash, ),
-        );
-        if ( $error ) {
-          $dbh->rollback if $oldAutoCommit;
-          return "error queuing welcome email: $error";
-        }
+    else { #!$msgnum
+      my ($to,$welcome_template,$welcome_from,$welcome_subject,$welcome_subject_template,$welcome_mimetype)
+        = ('','','','','','');
+
+      if ( $conf->exists('welcome_email', $agentnum) ) {
+        $welcome_template = new Text::Template (
+          TYPE   => 'ARRAY',
+          SOURCE => [ map "$_\n", $conf->config('welcome_email', $agentnum) ]
+        ) or warn "can't create welcome email template: $Text::Template::ERROR";
+        $welcome_from = $conf->config('welcome_email-from', $agentnum);
+          # || 'your-isp-is-dum'
+        $welcome_subject = $conf->config('welcome_email-subject', $agentnum)
+          || 'Welcome';
+        $welcome_subject_template = new Text::Template (
+          TYPE   => 'STRING',
+          SOURCE => $welcome_subject,
+        ) or warn "can't create welcome email subject template: $Text::Template::ERROR";
+        $welcome_mimetype = $conf->config('welcome_email-mimetype', $agentnum)
+          || 'text/plain';
+      }
+      if ( $welcome_template ) {
+        my $to = join(', ', grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list );
+        if ( $to ) {
+
+          my %hash = (
+                       'custnum'  => $self->custnum,
+                       'username' => $self->username,
+                       'password' => $self->_password,
+                       'first'    => $cust_main->first,
+                       'last'     => $cust_main->getfield('last'),
+                       'pkg'      => $cust_pkg->part_pkg->pkg,
+                     );
+          my $wqueue = new FS::queue {
+            'svcnum' => $self->svcnum,
+            'job'    => 'FS::svc_acct::send_email'
+          };
+          my $error = $wqueue->insert(
+            'to'       => $to,
+            'from'     => $welcome_from,
+            'subject'  => $welcome_subject_template->fill_in( HASH => \%hash, ),
+            'mimetype' => $welcome_mimetype,
+            'body'     => $welcome_template->fill_in( HASH => \%hash, ),
+          );
+          if ( $error ) {
+            $dbh->rollback if $oldAutoCommit;
+            return "error queuing welcome email: $error";
+          }
 
-        if ( $options{'depend_jobnum'} ) {
-          warn "$me depend_jobnum found; adding to welcome email dependancies"
-            if $DEBUG;
-          if ( ref($options{'depend_jobnum'}) ) {
-            warn "$me adding jobs ". join(', ', @{$options{'depend_jobnum'}} ).
-                 "to welcome email dependancies"
+          if ( $options{'depend_jobnum'} ) {
+            warn "$me depend_jobnum found; adding to welcome email dependancies"
               if $DEBUG;
-            push @jobnums, @{ $options{'depend_jobnum'} };
-          } else {
-            warn "$me adding job $options{'depend_jobnum'} ".
-                 "to welcome email dependancies"
-              if $DEBUG;
-            push @jobnums, $options{'depend_jobnum'};
+            if ( ref($options{'depend_jobnum'}) ) {
+              warn "$me adding jobs ". join(', ', @{$options{'depend_jobnum'}} ).
+                   "to welcome email dependancies"
+                if $DEBUG;
+              push @jobnums, @{ $options{'depend_jobnum'} };
+            } else {
+              warn "$me adding job $options{'depend_jobnum'} ".
+                   "to welcome email dependancies"
+                if $DEBUG;
+              push @jobnums, $options{'depend_jobnum'};
+            }
           }
-        }
 
-        foreach my $jobnum ( @jobnums ) {
-          my $error = $wqueue->depend_insert($jobnum);
-          if ( $error ) {
-            $dbh->rollback if $oldAutoCommit;
-            return "error queuing welcome email job dependancy: $error";
+          foreach my $jobnum ( @jobnums ) {
+            my $error = $wqueue->depend_insert($jobnum);
+            if ( $error ) {
+              $dbh->rollback if $oldAutoCommit;
+              return "error queuing welcome email job dependancy: $error";
+            }
           }
-        }
 
-      }
-
-    }
+        }
 
-  } # if ( $cust_pkg )
+      } # if $welcome_template
+    } # if !$msgnum
+  } # if $cust_pkg
 
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
   ''; #no error