[freeside-commits] freeside/FS/FS Conf.pm, 1.376, 1.377 Schema.pm, 1.225, 1.226 Upgrade.pm, 1.42, 1.43 cust_main.pm, 1.526, 1.527 cust_pkg.pm, 1.165, 1.166 msg_template.pm, 1.4, 1.5 svc_acct.pm, 1.297, 1.298
Mark Wells
mark at wavetail.420.am
Wed Jul 28 16:16:32 PDT 2010
Update of /home/cvs/cvsroot/freeside/FS/FS
In directory wavetail.420.am:/tmp/cvs-serv32139/FS/FS
Modified Files:
Conf.pm Schema.pm Upgrade.pm cust_main.pm cust_pkg.pm
msg_template.pm svc_acct.pm
Log Message:
msg_template improvements, RT#8324
Index: Conf.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/Conf.pm,v
retrieving revision 1.376
retrieving revision 1.377
diff -u -w -d -r1.376 -r1.377
--- Conf.pm 27 Jul 2010 01:48:23 -0000 1.376
+++ Conf.pm 28 Jul 2010 23:16:28 -0000 1.377
@@ -565,6 +565,21 @@
logo.eps
);
+my %msg_template_options = (
+ 'type' => 'select-sub',
+ 'options_sub' => sub { require FS::Record;
+ require FS::agent;
+ require FS::msg_template;
+ map { $_->msgnum, $_->msgname }
+ qsearch('msg_template', { disabled => '' });
+ },
+ 'option_sub' => sub { require FS::msg_template;
+ my $msg_template = FS::msg_template->by_key(shift);
+ $msg_template ? $msg_template->msgname : ''
+ },
+);
+
+
#Billing (81 items)
#Invoicing (50 items)
#UI (69 items)
@@ -572,7 +587,6 @@
#...
#Unclassified (77 items)
-
@config_items = map { new FS::ConfItem $_ } (
{
@@ -584,7 +598,7 @@
{
'key' => 'alert_expiration',
- 'section' => 'billing',
+ 'section' => 'notification',
'description' => 'Enable alerts about billing method expiration (i.e. expiring credit cards).',
'type' => 'checkbox',
'per_agent' => 1,
@@ -592,13 +606,20 @@
{
'key' => 'alerter_template',
- 'section' => 'billing',
- 'description' => 'Template file for billing method expiration alerts (i.e. expiring credit cards). See the <a href="http://www.freeside.biz/mediawiki/index.php/Freeside:1.7:Documentation:Administration#Credit_cards_and_Electronic_checks">billing documentation</a> for details.',
+ 'section' => 'deprecated',
+ 'description' => 'Template file for billing method expiration alerts (i.e. expiring credit cards).',
'type' => 'textarea',
'per_agent' => 1,
},
{
+ 'key' => 'alerter_msgnum',
+ 'section' => 'notification',
+ 'description' => 'Template to use for credit card expiration alerts.',
+ %msg_template_options,
+ },
+
+ {
'key' => 'apacheip',
#not actually deprecated yet
#'section' => 'deprecated',
@@ -1787,44 +1808,58 @@
},
{
+ 'key' => 'decline_msgnum',
+ 'section' => 'notification',
+ 'description' => 'Template to use for credit card and electronic check decline messages.',
+ %msg_template_options,
+ },
+
+ {
'key' => 'declinetemplate',
- 'section' => 'billing',
+ 'section' => 'deprecated',
'description' => 'Template file for credit card and electronic check decline emails.',
'type' => 'textarea',
},
{
'key' => 'emaildecline',
- 'section' => 'billing',
+ 'section' => 'notification',
'description' => 'Enable emailing of credit card and electronic check decline notices.',
'type' => 'checkbox',
},
{
'key' => 'emaildecline-exclude',
- 'section' => 'billing',
+ 'section' => 'notification',
'description' => 'List of error messages that should not trigger email decline notices, one per line.',
'type' => 'textarea',
},
{
+ 'key' => 'cancel_msgnum',
+ 'section' => 'notification',
+ 'description' => 'Template to use for cancellation emails.',
+ %msg_template_options,
+ },
+
+ {
'key' => 'cancelmessage',
- 'section' => 'billing',
+ 'section' => 'deprecated',
'description' => 'Template file for cancellation emails.',
'type' => 'textarea',
},
{
'key' => 'cancelsubject',
- 'section' => 'billing',
+ 'section' => 'deprecated',
'description' => 'Subject line for cancellation emails.',
'type' => 'text',
},
{
'key' => 'emailcancel',
- 'section' => 'billing',
- 'description' => 'Enable emailing of cancellation notices. Make sure to fill in the cancelmessage and cancelsubject configuration values as well.',
+ 'section' => 'notification',
+ 'description' => 'Enable emailing of cancellation notices. Make sure to select the template in the cancel_msgnum option.',
'type' => 'checkbox',
},
@@ -1878,16 +1913,23 @@
},
{
+ 'key' => 'welcome_msgnum',
+ 'section' => 'notification',
+ 'description' => 'Template to use for welcome messages when a svc_acct record is created.',
+ %msg_template_options,
+ },
+
+ {
'key' => 'welcome_email',
- 'section' => '',
- 'description' => 'Template file for welcome email. Welcome emails are sent to the customer email invoice destination(s) each time a svc_acct record is created. See the <a href="http://search.cpan.org/dist/Text-Template/lib/Text/Template.pm">Text::Template</a> documentation for details on the template substitution language. The following variables are available<ul><li><code>$username</code> <li><code>$password</code> <li><code>$first</code> <li><code>$last</code> <li><code>$pkg</code></ul>',
+ 'section' => 'deprecated',
+ 'description' => 'Template file for welcome email. Welcome emails are sent to the customer email invoice destination(s) each time a svc_acct record is created.',
'type' => 'textarea',
'per_agent' => 1,
},
{
'key' => 'welcome_email-from',
- 'section' => '',
+ 'section' => 'deprecated',
'description' => 'From: address header for welcome email',
'type' => 'text',
'per_agent' => 1,
@@ -1895,7 +1937,7 @@
{
'key' => 'welcome_email-subject',
- 'section' => '',
+ 'section' => 'deprecated',
'description' => 'Subject: header for welcome email',
'type' => 'text',
'per_agent' => 1,
@@ -1903,7 +1945,7 @@
{
'key' => 'welcome_email-mimetype',
- 'section' => '',
+ 'section' => 'deprecated',
'description' => 'MIME type for welcome email',
'type' => 'select',
'select_enum' => [ 'text/plain', 'text/html' ],
@@ -1917,37 +1959,44 @@
'type' => 'textarea',
},
+# {
+# 'key' => 'warning_msgnum',
+# 'section' => 'notification',
+# 'description' => 'Template to use for warning messages, sent to the customer email invoice destination(s) when a svc_acct record has its usage drop below a threshold.',
+# %msg_template_options,
+# },
+
{
'key' => 'warning_email',
- 'section' => '',
+ 'section' => 'notification',
'description' => 'Template file for warning email. Warning emails are sent to the customer email invoice destination(s) each time a svc_acct record has its usage drop below a threshold or 0. See the <a href="http://search.cpan.org/dist/Text-Template/lib/Text/Template.pm">Text::Template</a> documentation for details on the template substitution language. The following variables are available<ul><li><code>$username</code> <li><code>$password</code> <li><code>$first</code> <li><code>$last</code> <li><code>$pkg</code> <li><code>$column</code> <li><code>$amount</code> <li><code>$threshold</code></ul>',
'type' => 'textarea',
},
{
'key' => 'warning_email-from',
- 'section' => '',
+ 'section' => 'notification',
'description' => 'From: address header for warning email',
'type' => 'text',
},
{
'key' => 'warning_email-cc',
- 'section' => '',
+ 'section' => 'notification',
'description' => 'Additional recipient(s) (comma separated) for warning email when remaining usage reaches zero.',
'type' => 'text',
},
{
'key' => 'warning_email-subject',
- 'section' => '',
+ 'section' => 'notification',
'description' => 'Subject: header for warning email',
'type' => 'text',
},
{
'key' => 'warning_email-mimetype',
- 'section' => '',
+ 'section' => 'notification',
'description' => 'MIME type for warning email',
'type' => 'select',
'select_enum' => [ 'text/plain', 'text/html' ],
@@ -2880,8 +2929,15 @@
},
{
+ 'key' => 'impending_recur_msgnum',
+ 'section' => 'notification',
+ 'description' => 'Template to use for alerts about first-time recurring billing.',
+ %msg_template_options,
+ },
+
+ {
'key' => 'impending_recur_template',
- 'section' => 'billing',
+ 'section' => 'deprecated',
'description' => 'Template file for alerts about looming first time recurrant billing. See the <a href="http://search.cpan.org/dist/Text-Template/lib/Text/Template.pm">Text::Template</a> documentation for details on the template substitition language. Also see packages with a <a href="../browse/part_pkg.cgi">flat price plan</a> The following variables are available<ul><li><code>$packages</code> allowing <code>$packages->[0]</code> thru <code>$packages->[n]</code> <li><code>$package</code> the first package, same as <code>$packages->[0]</code> <li><code>$recurdates</code> allowing <code>$recurdates->[0]</code> thru <code>$recurdates->[n]</code> <li><code>$recurdate</code> the first recurdate, same as <code>$recurdate->[0]</code> <li><code>$first</code> <li><code>$last</code></ul>',
# <li><code>$payby</code> <li><code>$expdate</code> most likely only confuse
'type' => 'textarea',
Index: Upgrade.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/Upgrade.pm,v
retrieving revision 1.42
retrieving revision 1.43
diff -u -w -d -r1.42 -r1.43
--- Upgrade.pm 28 Jul 2010 02:54:45 -0000 1.42
+++ Upgrade.pm 28 Jul 2010 23:16:29 -0000 1.43
@@ -157,6 +157,9 @@
#default namespace
'payment_gateway' => [],
+ #migrate to templates
+ 'msg_template' => [],
+
;
\%hash;
Index: svc_acct.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/svc_acct.pm,v
retrieving revision 1.297
retrieving revision 1.298
diff -u -w -d -r1.297 -r1.298
--- svc_acct.pm 23 Jul 2010 10:02:39 -0000 1.297
+++ svc_acct.pm 28 Jul 2010 23:16:30 -0000 1.298
@@ -721,6 +721,13 @@
}
#welcome email
+ 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);
+ }
+ else { #!$msgnum
my ($to,$welcome_template,$welcome_from,$welcome_subject,$welcome_subject_template,$welcome_mimetype)
= ('','','','','','');
@@ -740,7 +747,7 @@
$welcome_mimetype = $conf->config('welcome_email-mimetype', $agentnum)
|| 'text/plain';
}
- if ( $welcome_template && $cust_pkg ) {
+ if ( $welcome_template ) {
my $to = join(', ', grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list );
if ( $to ) {
@@ -794,9 +801,9 @@
}
- }
-
- } # if ( $cust_pkg )
+ } # if $welcome_template
+ } # if !$msgnum
+ } # if $cust_pkg
$dbh->commit or die $dbh->errstr if $oldAutoCommit;
''; #no error
Index: cust_pkg.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/cust_pkg.pm,v
retrieving revision 1.165
retrieving revision 1.166
diff -u -w -d -r1.165 -r1.166
--- cust_pkg.pm 27 Jul 2010 06:48:39 -0000 1.165
+++ cust_pkg.pm 28 Jul 2010 23:16:30 -0000 1.166
@@ -708,12 +708,21 @@
my @invoicing_list = grep { $_ !~ /^(POST|FAX)$/ } $self->cust_main->invoicing_list;
if ( !$options{'quiet'} && $conf->exists('emailcancel') && @invoicing_list ) {
- my $error = send_email(
+ my $msgnum = $conf->config('cancel_msgnum', $self->cust_main->agentnum);
+ my $error = '';
+ if ( $msgnum ) {
+ my $msg_template = qsearchs('msg_template', { msgnum => $msgnum });
+ $error = $msg_template->send( 'cust_main' => $self->cust_main,
+ 'object' => $self );
+ }
+ else {
+ $error = send_email(
'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
'to' => \@invoicing_list,
'subject' => ( $conf->config('cancelsubject') || 'Cancellation Notice' ),
'body' => [ map "$_\n", $conf->config('cancelmessage') ],
);
+ }
#should this do something on errors?
}
@@ -1930,7 +1939,7 @@
my %labels;
#tie %labels, 'Tie::IxHash';
push @{ $labels{$_->[0]} }, $_->[1]
- foreach $self->h_labels(@_);
+ foreach $self->$method(@_);
my @labels;
foreach my $label ( keys %labels ) {
my %seen = ();
Index: msg_template.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/msg_template.pm,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -w -d -r1.4 -r1.5
--- msg_template.pm 22 Jul 2010 16:47:43 -0000 1.4
+++ msg_template.pm 28 Jul 2010 23:16:30 -0000 1.5
@@ -7,6 +7,12 @@
use FS::Conf;
use FS::Record qw( qsearch qsearchs );
+use Date::Format qw( time2str );
+use HTML::Entities qw( encode_entities) ;
+use vars '$DEBUG';
+
+$DEBUG=1;
+
=head1 NAME
FS::msg_template - Object methods for msg_template records
@@ -40,25 +46,32 @@
=item msgname
-msgname
+Template name.
=item agentnum
-agentnum
+Agent associated with this template. Can be NULL for a global template.
=item mime_type
-mime_type
+MIME type. Defaults to text/html.
+
+=item from_addr
+
+Source email address.
+
+=item subject
+
+The message subject line, in L<Text::Template> format.
=item body
-body
+The message body, as plain text or HTML, in L<Text::Template> format.
=item disabled
disabled
-
=back
=head1 METHODS
@@ -126,17 +139,23 @@
|| $self->ut_anything('subject')
|| $self->ut_anything('body')
|| $self->ut_enum('disabled', [ '', 'Y' ] )
+ || $self->ut_textn('from_addr')
;
return $error if $error;
+ my $body = $self->body;
+ $body =~ s/ / /g; # just in case these somehow get in
+ $self->body($body);
+
$self->mime_type('text/html') unless $self->mime_type;
$self->SUPER::check;
}
-=item send OPTION => VALUE, ...
+=item prepare OPTION => VALUE
-Fills in the template and emails it to the customer.
+Fills in the template and returns a hash of the 'from' address, 'to'
+addresses, subject line, and body.
Options are passed as a list of name/value pairs:
@@ -155,23 +174,41 @@
=cut
-sub send {
+sub prepare {
my( $self, %opt ) = @_;
my $cust_main = $opt{'cust_main'};
my $object = $opt{'object'};
-
- ###
- # fill-in
- ###
+ warn "preparing template '".$self->msgname."' to cust#".$cust_main->custnum."\n"
+ if($DEBUG);
my $subs = $self->substitutions;
- #XXX html escape this stuff
- my %hash = map { $_ => $cust_main->$_() } @{ $subs->{'cust_main'} };
- unless ( ! $object || $object->table eq 'cust_main' ) {
- %hash = ( %hash, map { $_ => $object->$_() } @{ $subs->{$object->table} } );
+ ###
+ # create substitution table
+ ###
+ my %hash;
+ foreach my $obj ($cust_main, $object || ()) {
+ foreach my $name (@{ $subs->{$obj->table} }) {
+ if(!ref($name)) {
+ # simple case
+ $hash{$name} = $obj->$name();
+ }
+ elsif( ref($name) eq 'ARRAY' ) {
+ # [ foo => sub { ... } ]
+ $hash{$name->[0]} = $name->[1]->($obj);
+ }
+ else {
+ warn "bad msg_template substitution: '$name'\n";
+ #skip it?
+ }
}
+ }
+ $_ = encode_entities($_) foreach values(%hash); # HTML escape
+
+ ###
+ # fill-in
+ ###
my $subject_tmpl = new Text::Template (
TYPE => 'STRING',
@@ -194,21 +231,36 @@
my $conf = new FS::Conf;
- send_email(
- generate_email(
- #XXX override from in event?
- 'from' => scalar( $conf->config('invoice_from', $cust_main->agentnum) ),
+ (
+ 'from' => $self->from ||
+ scalar( $conf->config('invoice_from', $cust_main->agentnum) ),
'to' => \@to,
'subject' => $subject,
'html_body' => $body,
#XXX auto-make a text copy w/HTML::FormatText?
# alas, us luddite mutt/pine users just aren't that big a deal
- )
);
}
+=item send OPTION => VALUE
+
+Fills in the template and sends it to the customer. Options are as for
+'prepare'.
+
+=cut
+
+sub send {
+ my $self = shift;
+ send_email(generate_email($self->prepare(@_)));
+}
+
+# helper sub for package dates
+my $ymd = sub { $_[0] ? time2str('%Y-%m-%d', $_[0]) : '' };
+
#return contexts and fill-in values
+# If you add anything, be sure to add a description in
+# httemplate/edit/msg_template.html.
sub substitutions {
{ 'cust_main' => [qw(
display_custnum agentnum agent_name
@@ -232,22 +284,86 @@
balance
invoicing_list_emailonly
cust_status ucfirst_cust_status cust_statuscolor
- )],
- #XXX make these pretty: signupdate dundate paydate_monthyear usernum
- # next_bill_date
+ signupdate dundate
+ ),
+ [ signupdate_ymd => sub { time2str('%Y-%m-%d', shift->signupdate) } ],
+ [ dundate_ymd => sub { time2str('%Y-%m-%d', shift->dundate) } ],
+ [ paydate_my => sub { sprintf('%02d/%04d', shift->paydate_monthyear) } ],
+ [ otaker_first => sub { shift->access_user->first } ],
+ [ otaker_last => sub { shift->access_user->last } ],
+ ],
+ # next_bill_date
'cust_pkg' => [qw(
- )],
- #XXX these are going to take more pretty-ing up
+ pkgnum pkg_label pkg_label_long
+ location_label
+ status statuscolor
+ start_date setup bill last_bill
+ adjourn susp expire
+ labels_short
+ ),
+ [ cancel => sub { shift->getfield('cancel') } ], # grrr...
+ [ start_ymd => sub { $ymd->(shift->getfield('start_date')) } ],
+ [ setup_ymd => sub { $ymd->(shift->getfield('setup')) } ],
+ [ next_bill_ymd => sub { $ymd->(shift->getfield('bill')) } ],
+ [ last_bill_ymd => sub { $ymd->(shift->getfield('last_bill')) } ],
+ [ adjourn_ymd => sub { $ymd->(shift->getfield('adjourn')) } ],
+ [ susp_ymd => sub { $ymd->(shift->getfield('susp')) } ],
+ [ expire_ymd => sub { $ymd->(shift->getfield('expire')) } ],
+ [ cancel_ymd => sub { $ymd->(shift->getfield('cancel')) } ],
+ ],
'cust_bill' => [qw(
invnum
)],
#XXX not really thinking about cust_bill substitutions quite yet
+ 'svc_acct' => [qw(
+ username
+ ),
+ [ password => sub { shift->getfield('_password') } ],
+ ], # for welcome messages
};
}
+sub _upgrade_data {
+ my ($self, %opts) = @_;
+
+ my @fixes = (
+ [ 'alerter_msgnum', 'alerter_template', '', '' ],
+ [ 'cancel_msgnum', 'cancelmessage', 'cancelsubject', '' ],
+ [ 'decline_msgnum', 'declinetemplate', '', '' ],
+ [ 'impending_recur_msgnum', 'impending_recur_template', '', '' ],
+ [ 'welcome_msgnum', 'welcome_email', 'welcome_email-subject', 'welcome_email-from' ],
+ [ 'warning_msgnum', 'warning_email', 'warning_email-subject', 'warning_email-from' ],
+ );
+
+ my $conf = new FS::Conf;
+ my @agentnums = ('', map {$_->agentnum} qsearch('agent', {}));
+ foreach my $agentnum (@agentnums) {
+ foreach (@fixes) {
+ my ($newname, $oldname, $subject, $from) = @$_;
+ if ($conf->exists($oldname, $agentnum)) {
+ my $new = new FS::msg_template({
+ 'msgname' => $oldname,
+ 'agentnum' => $agentnum,
+ 'from_addr' => ($from && $conf->config($from, $agentnum)) ||
+ $conf->config('invoice_from', $agentnum),
+ 'subject' => ($subject && $conf->config($subject, $agentnum)) || '',
+ 'mime_type' => 'text/html',
+ 'body' => join('<BR>',$conf->config($oldname, $agentnum)),
+ });
+ my $error = $new->insert;
+ die $error if $error;
+ $conf->set($newname, $new->msgnum, $agentnum);
+ $conf->delete($oldname, $agentnum);
+ $conf->delete($from, $agentnum) if $from;
+ $conf->delete($subject, $agentnum) if $subject;
+ }
+ }
+ }
+}
+
=back
=head1 BUGS
Index: cust_main.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/cust_main.pm,v
retrieving revision 1.526
retrieving revision 1.527
diff -u -w -d -r1.526 -r1.527
--- cust_main.pm 25 Jul 2010 07:03:22 -0000 1.526
+++ cust_main.pm 28 Jul 2010 23:16:29 -0000 1.527
@@ -5111,6 +5111,16 @@
&& ! grep { $transaction->error_message =~ /$_/ }
$conf->config('emaildecline-exclude')
) {
+
+ # Send a decline alert to the customer.
+ my $msgnum = $conf->config('decline_msgnum', $self->agentnum);
+ my $error = '';
+ if ( $msgnum ) {
+ my $msg_template = qsearchs('msg_template', { msgnum => $msgnum });
+ $error = $msg_template->send( 'cust_main' => $self );
+ }
+ else { #!$msgnum
+
my @templ = $conf->config('declinetemplate');
my $template = new Text::Template (
TYPE => 'ARRAY',
@@ -5133,6 +5143,7 @@
'subject' => 'Your payment could not be processed',
'body' => [ $template->fill_in(HASH => $templ_hash) ],
);
+ }
$perror .= " (also received error sending decline notification: $error)"
if $error;
@@ -8759,6 +8770,9 @@
=item notify CUSTOMER_OBJECT TEMPLATE_NAME OPTIONS
+Deprecated. Use event notification and message templates
+(L<FS::msg_template>) instead.
+
Sends a templated email notification to the customer (see L<Text::Template>).
OPTIONS is a hash and may include
Index: Schema.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/Schema.pm,v
retrieving revision 1.225
retrieving revision 1.226
diff -u -w -d -r1.225 -r1.226
--- Schema.pm 15 Jul 2010 21:46:30 -0000 1.225
+++ Schema.pm 28 Jul 2010 23:16:29 -0000 1.226
@@ -2939,6 +2939,7 @@
'mime_type', 'varchar', '', $char_d, '', '',
'body', 'blob', 'NULL', '', '', '',
'disabled', 'char', 'NULL', 1, '', '',
+ 'from_addr', 'varchar', 'NULL', 255, '', '',
],
'primary_key' => 'msgnum',
'unique' => [ ['msgname', 'mime_type'] ],
More information about the freeside-commits
mailing list