add dsl, circuit and fiber services to email and RT ticket exports, RT#84345
[freeside.git] / FS / FS / part_export / send_email.pm
1 package FS::part_export::send_email;
2
3 use vars qw(@ISA %info);
4 use Tie::IxHash;
5 use FS::part_export;
6 use FS::Record qw(qsearch qsearchs);
7 use FS::Conf;
8 use FS::msg_template;
9
10 @ISA = qw(FS::part_export);
11
12 my %templates;
13 my %template_select = (
14   type          => 'select',
15   freeform      => 1,
16   option_label  => sub {
17     $templates{$_[0]};
18   },
19   option_values => sub {
20     %templates = (
21       0 => '',
22       map { $_->msgnum, $_->msgname } 
23         qsearch({ table    => 'msg_template',
24                   hashref  => { disabled => '', },
25                   order_by => 'ORDER BY msgnum ASC'
26                 })
27     );
28     sort keys (%templates);
29   },
30 );
31
32 tie my %options, 'Tie::IxHash', (
33   'insert_template' => {
34     before  => '
35 <TR><TD COLSPAN=2>
36 <TABLE>
37   <TR><TH></TH><TH>Template</TH></TR>
38   <TR><TD>New service</TD><TD>',
39     %template_select,
40     after   => '</TD></TR>
41 ',
42   },
43   'delete_template' => {
44     before  => '
45   <TR><TD>Delete</TD><TD>',
46     %template_select,
47     after   => '</TD></TR>
48 ',
49   },
50   'replace_template' => {
51     before => '
52   <TR><TD>Modify</TD><TD>',
53     %template_select,
54     after   => '</TD></TR>
55 ',
56   },
57   'suspend_template' => {
58     before  => '
59   <TR><TD>Suspend</TD><TD>',
60     %template_select,
61     after   => '</TD></TR>
62 ',
63   },
64   'unsuspend_template' => {
65     before  => '
66   <TR><TD>Unsuspend</TD><TD>',
67     %template_select,
68     after   => '</TD></TR>
69   </TABLE>
70 </TD></TR>',
71   },
72   'to_customer' => {
73     label   => 'Send to customer',
74     type    => 'checkbox',
75   },
76   'to_address' => {
77     label => 'Send to other address: ',
78     type      => 'text',
79   },
80 );
81
82 %info = (
83   'svc'      => [qw( svc_acct svc_hardware svc_broadband svc_phone svc_domain
84                      svc_dsl svc_circuit svc_fiber
85                 )],
86   'desc'     =>
87   'Send an email message',
88   'options'  => \%options,
89   'nodomain' => '',
90   'no_machine' => 1,
91   'notes'    => ' 
92   Send an email message.  The subject and body of the message
93   will be generated from a message template.'
94 );
95
96 sub _export {
97   my( $self, $action, $svc ) = (shift, shift, shift);
98   my $conf = new FS::Conf;
99
100   my $msgnum = $self->option($action.'_template');
101   return if !$msgnum;
102
103   my $msg_template = FS::msg_template->by_key($msgnum);
104   return "Template $msgnum not found\n" if !$msg_template;
105
106   my $cust_pkg = $svc->cust_svc->cust_pkg;
107   my $cust_main = $svc->cust_svc->cust_pkg->cust_main if $cust_pkg;
108   my $custnum = $cust_main->custnum if $cust_main;
109   my $svcnum = $svc->svcnum if $action ne 'delete';
110
111   my @to = split(',', $self->option('to_address') || '');
112   push @to, $cust_main->invoicing_list_emailonly 
113     if $self->option('to_customer') and $cust_main;
114   if ( !@to ) {
115     warn 'No destination address for send_email export: custnum '.$cust_main->custnum;
116     # warn, don't die, but also avoid sending the template with _no_ 'to'=> 
117     # param, which would send to the customer by default.
118     return;
119   }
120
121   if ( $action eq 'replace' ) {
122     my $old = shift;
123     return $msg_template->send(
124       'cust_main' => $cust_main,
125       'object'    => [ $svc, $old ],
126       'to'        => join(',', @to),
127     );
128   }
129   else {
130     return $msg_template->send(
131       'cust_main' => $cust_main,
132       'object'    => $svc,
133       'to'        => join(',', @to),
134     );
135   }
136 }
137
138 sub _export_insert {
139   my($self, $svc) = (shift, shift);
140   $self->_export('insert', $svc);
141 }
142
143 sub _export_replace {
144   my($self, $new, $old) = (shift, shift, shift);
145   $self->_export('replace', $new, $old);
146 }
147
148 sub _export_delete {
149   my($self, $svc) = (shift, shift);
150   $self->_export('delete', $svc);
151 }
152
153 sub _export_suspend {
154   my($self, $svc) = (shift, shift);
155   $self->_export('suspend', $svc);
156 }
157
158 sub _export_unsuspend {
159   my($self, $svc) = (shift, shift);
160   $self->_export('unsuspend', $svc);
161 }
162
163 1;