per-agent payment receipt addresses, #10797
[freeside.git] / FS / FS / msg_template.pm
1 package FS::msg_template;
2
3 use strict;
4 use base qw( FS::Record );
5 use Text::Template;
6 use FS::Misc qw( generate_email send_email );
7 use FS::Conf;
8 use FS::Record qw( qsearch qsearchs );
9
10 use Date::Format qw( time2str );
11 use HTML::Entities qw( decode_entities encode_entities ) ;
12 use HTML::FormatText;
13 use HTML::TreeBuilder;
14 use vars '$DEBUG';
15
16 $DEBUG=0;
17
18 =head1 NAME
19
20 FS::msg_template - Object methods for msg_template records
21
22 =head1 SYNOPSIS
23
24   use FS::msg_template;
25
26   $record = new FS::msg_template \%hash;
27   $record = new FS::msg_template { 'column' => 'value' };
28
29   $error = $record->insert;
30
31   $error = $new_record->replace($old_record);
32
33   $error = $record->delete;
34
35   $error = $record->check;
36
37 =head1 DESCRIPTION
38
39 An FS::msg_template object represents a customer message template.
40 FS::msg_template inherits from FS::Record.  The following fields are currently
41 supported:
42
43 =over 4
44
45 =item msgnum
46
47 primary key
48
49 =item msgname
50
51 Template name.
52
53 =item agentnum
54
55 Agent associated with this template.  Can be NULL for a global template.
56
57 =item mime_type
58
59 MIME type.  Defaults to text/html.
60
61 =item from_addr
62
63 Source email address.
64
65 =item subject
66
67 The message subject line, in L<Text::Template> format.
68
69 =item body
70
71 The message body, as plain text or HTML, in L<Text::Template> format.
72
73 =item disabled
74
75 disabled
76
77 =back
78
79 =head1 METHODS
80
81 =over 4
82
83 =item new HASHREF
84
85 Creates a new template.  To add the template to the database, see L<"insert">.
86
87 Note that this stores the hash reference, not a distinct copy of the hash it
88 points to.  You can ask the object for a copy with the I<hash> method.
89
90 =cut
91
92 # the new method can be inherited from FS::Record, if a table method is defined
93
94 sub table { 'msg_template'; }
95
96 =item insert
97
98 Adds this record to the database.  If there is an error, returns the error,
99 otherwise returns false.
100
101 =cut
102
103 # the insert method can be inherited from FS::Record
104
105 =item delete
106
107 Delete this record from the database.
108
109 =cut
110
111 # the delete method can be inherited from FS::Record
112
113 =item replace OLD_RECORD
114
115 Replaces the OLD_RECORD with this one in the database.  If there is an error,
116 returns the error, otherwise returns false.
117
118 =cut
119
120 # the replace method can be inherited from FS::Record
121
122 =item check
123
124 Checks all fields to make sure this is a valid template.  If there is
125 an error, returns the error, otherwise returns false.  Called by the insert
126 and replace methods.
127
128 =cut
129
130 # the check method should currently be supplied - FS::Record contains some
131 # data checking routines
132
133 sub check {
134   my $self = shift;
135
136   my $error = 
137     $self->ut_numbern('msgnum')
138     || $self->ut_text('msgname')
139     || $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum')
140     || $self->ut_textn('mime_type')
141     || $self->ut_anything('subject')
142     || $self->ut_anything('body')
143     || $self->ut_enum('disabled', [ '', 'Y' ] )
144     || $self->ut_textn('from_addr')
145   ;
146   return $error if $error;
147
148   $self->mime_type('text/html') unless $self->mime_type;
149
150   $self->SUPER::check;
151 }
152
153 =item prepare OPTION => VALUE
154
155 Fills in the template and returns a hash of the 'from' address, 'to' 
156 addresses, subject line, and body.
157
158 Options are passed as a list of name/value pairs:
159
160 =over 4
161
162 =item cust_main
163
164 Customer object (required).
165
166 =item object
167
168 Additional context object (currently, can be a cust_main, cust_pkg, 
169 cust_bill, cust_pay, cust_pay_pending, or svc_(acct, phone, broadband, 
170 domain) ).  If the object is a svc_*, its cust_pkg will be fetched and 
171 used for substitution.
172
173 As a special case, this may be an arrayref of two objects.  Both 
174 objects will be available for substitution, with their field names 
175 prefixed with 'new_' and 'old_' respectively.  This is used in the 
176 rt_ticket export when exporting "replace" events.
177
178 =item from_config
179
180 Configuration option to use as the source address, based on the customer's 
181 agentnum.  If unspecified (or the named option is empty), 'invoice_from' 
182 will be used.
183
184 The I<from_addr> field in the template takes precedence over this.
185
186 =item to
187
188 Destination address.  The default is to use the customer's 
189 invoicing_list addresses.  Multiple addresses may be comma-separated.
190
191 =back
192
193 =cut
194
195 sub prepare {
196   my( $self, %opt ) = @_;
197
198   my $cust_main = $opt{'cust_main'};
199   my $object = $opt{'object'};
200   warn "preparing template '".$self->msgname."' to cust#".$cust_main->custnum."\n"
201     if($DEBUG);
202
203   my $subs = $self->substitutions;
204
205   ###
206   # create substitution table
207   ###  
208   my %hash;
209   my @objects = ($cust_main);
210   my @prefixes = ('');
211   my $svc;
212   if( ref $object ) {
213     if( ref($object) eq 'ARRAY' ) {
214       # [new, old], for provisioning tickets
215       push @objects, $object->[0], $object->[1];
216       push @prefixes, 'new_', 'old_';
217       $svc = $object->[0] if $object->[0]->isa('FS::svc_Common');
218     }
219     else {
220       push @objects, $object;
221       push @prefixes, '';
222       $svc = $object if $object->isa('FS::svc_Common');
223     }
224   }
225   if( $svc ) {
226     push @objects, $svc->cust_svc->cust_pkg;
227     push @prefixes, '';
228   }
229
230   foreach my $obj (@objects) {
231     my $prefix = shift @prefixes;
232     foreach my $name (@{ $subs->{$obj->table} }) {
233       if(!ref($name)) {
234         # simple case
235         $hash{$prefix.$name} = $obj->$name();
236       }
237       elsif( ref($name) eq 'ARRAY' ) {
238         # [ foo => sub { ... } ]
239         $hash{$prefix.($name->[0])} = $name->[1]->($obj);
240       }
241       else {
242         warn "bad msg_template substitution: '$name'\n";
243         #skip it?
244       } 
245     } 
246   } 
247   $_ = encode_entities($_) foreach values(%hash);
248
249
250   ###
251   # clean up template
252   ###
253   my $subject_tmpl = new Text::Template (
254     TYPE   => 'STRING',
255     SOURCE => $self->subject,
256   );
257   my $subject = $subject_tmpl->fill_in( HASH => \%hash );
258
259   my $body = $self->body;
260   my ($skin, $guts) = eviscerate($body);
261   @$guts = map { 
262     $_ = decode_entities($_); # turn all punctuation back into itself
263     s/\r//gs;           # remove \r's
264     s/<br[^>]*>/\n/gsi; # and <br /> tags
265     s/<p>/\n/gsi;       # and <p>
266     s/<\/p>//gsi;       # and </p>
267     s/\240/ /gs;        # and &nbsp;
268     $_
269   } @$guts;
270   
271   $body = '{ use Date::Format qw(time2str); "" }';
272   while(@$skin || @$guts) {
273     $body .= shift(@$skin) || '';
274     $body .= shift(@$guts) || '';
275   }
276
277   ###
278   # fill-in
279   ###
280
281   my $body_tmpl = new Text::Template (
282     TYPE          => 'STRING',
283     SOURCE        => $body,
284   );
285
286   $body = $body_tmpl->fill_in( HASH => \%hash );
287
288   ###
289   # and email
290   ###
291
292   my @to;
293   if ( exists($opt{'to'}) ) {
294     @to = split(/\s*,\s*/, $opt{'to'});
295   }
296   else {
297     @to = $cust_main->invoicing_list_emailonly;
298   }
299   # no warning when preparing with no destination
300
301   my $conf = new FS::Conf;
302   my $from_addr = $self->from_addr;
303
304   if ( !$from_addr ) {
305     if ( $opt{'from_config'} ) {
306       $from_addr = scalar( $conf->config($opt{'from_config'}, 
307                                          $cust_main->agentnum) );
308     }
309     $from_addr ||= scalar( $conf->config('invoice_from',
310                                          $cust_main->agentnum) );
311   }
312
313   (
314     'from' => $from_addr,
315     'to'   => \@to,
316     'bcc'  => $self->bcc_addr || undef,
317     'subject'   => $subject,
318     'html_body' => $body,
319     'text_body' => HTML::FormatText->new(leftmargin => 0, rightmargin => 70
320                     )->format( HTML::TreeBuilder->new_from_content($body) ),
321   );
322
323 }
324
325 =item send OPTION => VALUE
326
327 Fills in the template and sends it to the customer.  Options are as for 
328 'prepare'.
329
330 =cut
331
332 # broken out from prepare() in case we want to queue the sending,
333 # preview it, etc.
334 sub send {
335   my $self = shift;
336   send_email(generate_email($self->prepare(@_)));
337 }
338
339 # helper sub for package dates
340 my $ymd = sub { $_[0] ? time2str('%Y-%m-%d', $_[0]) : '' };
341
342 # needed for some things
343 my $conf = new FS::Conf;
344
345 #return contexts and fill-in values
346 # If you add anything, be sure to add a description in 
347 # httemplate/edit/msg_template.html.
348 sub substitutions {
349   { 'cust_main' => [qw(
350       display_custnum agentnum agent_name
351
352       last first company
353       name name_short contact contact_firstlast
354       address1 address2 city county state zip
355       country
356       daytime night fax
357
358       has_ship_address
359       ship_last ship_first ship_company
360       ship_name ship_name_short ship_contact ship_contact_firstlast
361       ship_address1 ship_address2 ship_city ship_county ship_state ship_zip
362       ship_country
363       ship_daytime ship_night ship_fax
364
365       paymask payname paytype payip
366       num_cancelled_pkgs num_ncancelled_pkgs num_pkgs
367       classname categoryname
368       balance
369       credit_limit
370       invoicing_list_emailonly
371       cust_status ucfirst_cust_status cust_statuscolor
372
373       signupdate dundate
374       expdate
375       packages recurdates
376       ),
377       # expdate is a special case
378       [ signupdate_ymd    => sub { time2str('%Y-%m-%d', shift->signupdate) } ],
379       [ dundate_ymd       => sub { time2str('%Y-%m-%d', shift->dundate) } ],
380       [ paydate_my        => sub { sprintf('%02d/%04d', shift->paydate_monthyear) } ],
381       [ otaker_first      => sub { shift->access_user->first } ],
382       [ otaker_last       => sub { shift->access_user->last } ],
383       [ payby             => sub { FS::payby->shortname(shift->payby) } ],
384       [ company_name      => sub { 
385           $conf->config('company_name', shift->agentnum) 
386         } ],
387       [ company_address   => sub {
388           $conf->config('company_address', shift->agentnum)
389         } ],
390     ],
391     # next_bill_date
392     'cust_pkg'  => [qw( 
393       pkgnum pkg_label pkg_label_long
394       location_label
395       status statuscolor
396     
397       start_date setup bill last_bill 
398       adjourn susp expire 
399       labels_short
400       ),
401       [ pkg               => sub { shift->part_pkg->pkg } ],
402       [ cancel            => sub { shift->getfield('cancel') } ], # grrr...
403       [ start_ymd         => sub { $ymd->(shift->getfield('start_date')) } ],
404       [ setup_ymd         => sub { $ymd->(shift->getfield('setup')) } ],
405       [ next_bill_ymd     => sub { $ymd->(shift->getfield('bill')) } ],
406       [ last_bill_ymd     => sub { $ymd->(shift->getfield('last_bill')) } ],
407       [ adjourn_ymd       => sub { $ymd->(shift->getfield('adjourn')) } ],
408       [ susp_ymd          => sub { $ymd->(shift->getfield('susp')) } ],
409       [ expire_ymd        => sub { $ymd->(shift->getfield('expire')) } ],
410       [ cancel_ymd        => sub { $ymd->(shift->getfield('cancel')) } ],
411     ],
412     'cust_bill' => [qw(
413       invnum
414       _date
415     )],
416     #XXX not really thinking about cust_bill substitutions quite yet
417     
418     # for welcome and limit warning messages
419     'svc_acct' => [qw(
420       svcnum
421       username
422       domain
423       ),
424       [ password          => sub { shift->getfield('_password') } ],
425     ],
426     'svc_domain' => [qw(
427       svcnum
428       domain
429       ),
430       [ registrar         => sub {
431           my $registrar = qsearchs('registrar', 
432             { registrarnum => shift->registrarnum} );
433           $registrar ? $registrar->registrarname : ''
434         }
435       ],
436       [ catchall          => sub { 
437           my $svc_acct = qsearchs('svc_acct', { svcnum => shift->catchall });
438           $svc_acct ? $svc_acct->email : ''
439         }
440       ],
441     ],
442     'svc_phone' => [qw(
443       svcnum
444       phonenum
445       countrycode
446       domain
447       )
448     ],
449     'svc_broadband' => [qw(
450       svcnum
451       speed_up
452       speed_down
453       ip_addr
454       mac_addr
455       )
456     ],
457     # for payment receipts
458     'cust_pay' => [qw(
459       paynum
460       _date
461       ),
462       [ paid              => sub { sprintf("%.2f", shift->paid) } ],
463       # overrides the one in cust_main in cases where a cust_pay is passed
464       [ payby             => sub { FS::payby->shortname(shift->payby) } ],
465       [ date              => sub { time2str("%a %B %o, %Y", shift->_date) } ],
466       [ payinfo           => sub { 
467           my $cust_pay = shift;
468           ($cust_pay->payby eq 'CARD' || $cust_pay->payby eq 'CHEK') ?
469             $cust_pay->paymask : $cust_pay->decrypt($cust_pay->payinfo)
470         } ],
471     ],
472     # for payment decline messages
473     # try to support all cust_pay fields
474     # 'error' is a special case, it contains the raw error from the gateway
475     'cust_pay_pending' => [qw(
476       _date
477       error
478       ),
479       [ paid              => sub { sprintf("%.2f", shift->paid) } ],
480       [ payby             => sub { FS::payby->shortname(shift->payby) } ],
481       [ date              => sub { time2str("%a %B %o, %Y", shift->_date) } ],
482       [ payinfo           => sub {
483           my $pending = shift;
484           ($pending->payby eq 'CARD' || $pending->payby eq 'CHEK') ?
485             $pending->paymask : $pending->decrypt($pending->payinfo)
486         } ],
487     ],
488   };
489 }
490
491 sub _upgrade_data {
492   my ($self, %opts) = @_;
493
494   my @fixes = (
495     [ 'alerter_msgnum',  'alerter_template',   '',               '', '' ],
496     [ 'cancel_msgnum',   'cancelmessage',      'cancelsubject',  '', '' ],
497     [ 'decline_msgnum',  'declinetemplate',    '',               '', '' ],
498     [ 'impending_recur_msgnum', 'impending_recur_template', '',  '', 'impending_recur_bcc' ],
499     [ 'payment_receipt_msgnum', 'payment_receipt_email', '',     '', '' ],
500     [ 'welcome_msgnum',  'welcome_email',      'welcome_email-subject', 'welcome_email-from', '' ],
501     [ 'warning_msgnum',  'warning_email',      'warning_email-subject', 'warning_email-from', '' ],
502   );
503  
504   my $conf = new FS::Conf;
505   my @agentnums = ('', map {$_->agentnum} qsearch('agent', {}));
506   foreach my $agentnum (@agentnums) {
507     foreach (@fixes) {
508       my ($newname, $oldname, $subject, $from, $bcc) = @$_;
509       if ($conf->exists($oldname, $agentnum)) {
510         my $new = new FS::msg_template({
511            'msgname'   => $oldname,
512            'agentnum'  => $agentnum,
513            'from_addr' => ($from && $conf->config($from, $agentnum)) || 
514                           $conf->config('invoice_from', $agentnum),
515            'bcc_addr'  => ($bcc && $conf->config($from, $agentnum)) || '',
516            'subject'   => ($subject && $conf->config($subject, $agentnum)) || '',
517            'mime_type' => 'text/html',
518            'body'      => join('<BR>',$conf->config($oldname, $agentnum)),
519         });
520         my $error = $new->insert;
521         die $error if $error;
522         $conf->set($newname, $new->msgnum, $agentnum);
523         $conf->delete($oldname, $agentnum);
524         $conf->delete($from, $agentnum) if $from;
525         $conf->delete($subject, $agentnum) if $subject;
526       }
527     }
528   }
529 }
530
531 sub eviscerate {
532   # Every bit as pleasant as it sounds.
533   #
534   # We do this because Text::Template::Preprocess doesn't
535   # actually work.  It runs the entire template through 
536   # the preprocessor, instead of the code segments.  Which 
537   # is a shame, because Text::Template already contains
538   # the code to do this operation.
539   my $body = shift;
540   my (@outside, @inside);
541   my $depth = 0;
542   my $chunk = '';
543   while($body || $chunk) {
544     my ($first, $delim, $rest);
545     # put all leading non-delimiters into $first
546     ($first, $rest) =
547         ($body =~ /^((?:\\[{}]|[^{}])*)(.*)$/s);
548     $chunk .= $first;
549     # put a leading delimiter into $delim if there is one
550     ($delim, $rest) =
551       ($rest =~ /^([{}]?)(.*)$/s);
552
553     if( $delim eq '{' ) {
554       $chunk .= '{';
555       if( $depth == 0 ) {
556         push @outside, $chunk;
557         $chunk = '';
558       }
559       $depth++;
560     }
561     elsif( $delim eq '}' ) {
562       $depth--;
563       if( $depth == 0 ) {
564         push @inside, $chunk;
565         $chunk = '';
566       }
567       $chunk .= '}';
568     }
569     else {
570       # no more delimiters
571       if( $depth == 0 ) {
572         push @outside, $chunk . $rest;
573       } # else ? something wrong
574       last;
575     }
576     $body = $rest;
577   }
578   (\@outside, \@inside);
579 }
580
581 =back
582
583 =head1 BUGS
584
585 =head1 SEE ALSO
586
587 L<FS::Record>, schema.html from the base documentation.
588
589 =cut
590
591 1;
592