summaryrefslogtreecommitdiff
path: root/FS/FS/part_export/send_email.pm
blob: a54fa16006bc29a33353bc6de3e9e8af27549598 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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;

@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  => { disabled => '', },
                  order_by => 'ORDER BY msgnum ASC'
                })
    );
    sort keys (%templates);
  },
);

tie my %options, 'Tie::IxHash', (
  'insert_template' => {
    before  => '
<TR><TD COLSPAN=2>
<TABLE>
  <TR><TH></TH><TH>Template</TH></TR>
  <TR><TD>New service</TD><TD>',
    %template_select,
    after   => '</TD></TR>
',
  },
  'delete_template' => {
    before  => '
  <TR><TD>Delete</TD><TD>',
    %template_select,
    after   => '</TD></TR>
',
  },
  'replace_template' => {
    before => '
  <TR><TD>Modify</TD><TD>',
    %template_select,
    after   => '</TD></TR>
',
  },
  'suspend_template' => {
    before  => '
  <TR><TD>Suspend</TD><TD>',
    %template_select,
    after   => '</TD></TR>
',
  },
  'unsuspend_template' => {
    before  => '
  <TR><TD>Unsuspend</TD><TD>',
    %template_select,
    after   => '</TD></TR>
  </TABLE>
</TD></TR>',
  },
  'to_customer' => {
    label   => 'Send to customer',
    type    => 'checkbox',
  },
  'to_address' => {
    label => 'Send to other address: ',
    type      => 'text',
  },
);

%info = (
  'svc'      => [qw( svc_acct svc_hardware svc_broadband svc_phone svc_domain
                     svc_dsl svc_circuit svc_fiber
                )],
  'desc'     =>
  'Send an email message',
  'options'  => \%options,
  'nodomain' => '',
  'no_machine' => 1,
  '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;