packages and recurdates for impending_recur templates
[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, svc_acct, cust_pay, or cust_pay_pending object).
170
171 =back
172
173 =cut
174
175 sub prepare {
176   my( $self, %opt ) = @_;
177
178   my $cust_main = $opt{'cust_main'};
179   my $object = $opt{'object'};
180   warn "preparing template '".$self->msgname."' to cust#".$cust_main->custnum."\n"
181     if($DEBUG);
182
183   my $subs = $self->substitutions;
184
185   ###
186   # create substitution table
187   ###  
188   my %hash;
189   foreach my $obj ($cust_main, $object || ()) {
190     foreach my $name (@{ $subs->{$obj->table} }) {
191       if(!ref($name)) {
192         # simple case
193         $hash{$name} = $obj->$name();
194       }
195       elsif( ref($name) eq 'ARRAY' ) {
196         # [ foo => sub { ... } ]
197         $hash{$name->[0]} = $name->[1]->($obj);
198       }
199       else {
200         warn "bad msg_template substitution: '$name'\n";
201         #skip it?
202       } 
203     } 
204   } 
205   $_ = encode_entities($_) foreach values(%hash);
206
207
208   ###
209   # clean up template
210   ###
211   my $subject_tmpl = new Text::Template (
212     TYPE   => 'STRING',
213     SOURCE => $self->subject,
214   );
215   my $subject = $subject_tmpl->fill_in( HASH => \%hash );
216
217   my $body = $self->body;
218   my ($skin, $guts) = eviscerate($body);
219   @$guts = map { 
220     $_ = decode_entities($_); # turn all punctuation back into itself
221     s/\r//gs;           # remove \r's
222     s/<br[^>]*>/\n/gsi; # and <br /> tags
223     s/<p>/\n/gsi;       # and <p>
224     s/<\/p>//gsi;       # and </p>
225     s/\240/ /gs;        # and &nbsp;
226     $_
227   } @$guts;
228   
229   $body = '{ use Date::Format qw(time2str); "" }';
230   while(@$skin || @$guts) {
231     $body .= shift(@$skin) || '';
232     $body .= shift(@$guts) || '';
233   }
234
235   ###
236   # fill-in
237   ###
238
239   my $body_tmpl = new Text::Template (
240     TYPE          => 'STRING',
241     SOURCE        => $body,
242   );
243
244   $body = $body_tmpl->fill_in( HASH => \%hash );
245
246   ###
247   # and email
248   ###
249
250   my @to = $cust_main->invoicing_list_emailonly;
251   warn "prepared msg_template with no email destination (custnum ".
252     $cust_main->custnum.")\n"
253     if !@to;
254
255   my $conf = new FS::Conf;
256
257   (
258     'from' => $self->from_addr || 
259               scalar( $conf->config('invoice_from', $cust_main->agentnum) ),
260     'to'   => \@to,
261     'bcc'  => $self->bcc_addr || undef,
262     'subject'   => $subject,
263     'html_body' => $body,
264     'text_body' => HTML::FormatText->new(leftmargin => 0, rightmargin => 70
265                     )->format( HTML::TreeBuilder->new_from_content($body) ),
266   );
267
268 }
269
270 =item send OPTION => VALUE
271
272 Fills in the template and sends it to the customer.  Options are as for 
273 'prepare'.
274
275 =cut
276
277 # broken out from prepare() in case we want to queue the sending,
278 # preview it, etc.
279 sub send {
280   my $self = shift;
281   send_email(generate_email($self->prepare(@_)));
282 }
283
284 # helper sub for package dates
285 my $ymd = sub { $_[0] ? time2str('%Y-%m-%d', $_[0]) : '' };
286
287 # needed for some things
288 my $conf = new FS::Conf;
289
290 #return contexts and fill-in values
291 # If you add anything, be sure to add a description in 
292 # httemplate/edit/msg_template.html.
293 sub substitutions {
294   { 'cust_main' => [qw(
295       display_custnum agentnum agent_name
296
297       last first company
298       name name_short contact contact_firstlast
299       address1 address2 city county state zip
300       country
301       daytime night fax
302
303       has_ship_address
304       ship_last ship_first ship_company
305       ship_name ship_name_short ship_contact ship_contact_firstlast
306       ship_address1 ship_address2 ship_city ship_county ship_state ship_zip
307       ship_country
308       ship_daytime ship_night ship_fax
309
310       paymask payname paytype payip
311       num_cancelled_pkgs num_ncancelled_pkgs num_pkgs
312       classname categoryname
313       balance
314       invoicing_list_emailonly
315       cust_status ucfirst_cust_status cust_statuscolor
316
317       signupdate dundate
318       expdate
319       packages recurdates
320       ),
321       # expdate is a special case
322       [ signupdate_ymd    => sub { time2str('%Y-%m-%d', shift->signupdate) } ],
323       [ dundate_ymd       => sub { time2str('%Y-%m-%d', shift->dundate) } ],
324       [ paydate_my        => sub { sprintf('%02d/%04d', shift->paydate_monthyear) } ],
325       [ otaker_first      => sub { shift->access_user->first } ],
326       [ otaker_last       => sub { shift->access_user->last } ],
327       [ payby             => sub { FS::payby->shortname(shift->payby) } ],
328       [ company_name      => sub { 
329           $conf->config('company_name', shift->agentnum) 
330         } ],
331       [ company_address   => sub {
332           $conf->config('company_address', shift->agentnum)
333         } ],
334     ],
335     # next_bill_date
336     'cust_pkg'  => [qw( 
337       pkgnum pkg_label pkg_label_long
338       location_label
339       status statuscolor
340     
341       start_date setup bill last_bill 
342       adjourn susp expire 
343       labels_short
344       ),
345       [ cancel            => sub { shift->getfield('cancel') } ], # grrr...
346       [ start_ymd         => sub { $ymd->(shift->getfield('start_date')) } ],
347       [ setup_ymd         => sub { $ymd->(shift->getfield('setup')) } ],
348       [ next_bill_ymd     => sub { $ymd->(shift->getfield('bill')) } ],
349       [ last_bill_ymd     => sub { $ymd->(shift->getfield('last_bill')) } ],
350       [ adjourn_ymd       => sub { $ymd->(shift->getfield('adjourn')) } ],
351       [ susp_ymd          => sub { $ymd->(shift->getfield('susp')) } ],
352       [ expire_ymd        => sub { $ymd->(shift->getfield('expire')) } ],
353       [ cancel_ymd        => sub { $ymd->(shift->getfield('cancel')) } ],
354     ],
355     'cust_bill' => [qw(
356       invnum
357       _date
358     )],
359     #XXX not really thinking about cust_bill substitutions quite yet
360     
361     # for welcome and limit warning messages
362     'svc_acct' => [qw(
363       username
364       ),
365       [ password          => sub { shift->getfield('_password') } ],
366     ],
367     # for payment receipts
368     'cust_pay' => [qw(
369       paynum
370       _date
371       ),
372       [ paid              => sub { sprintf("%.2f", shift->paid) } ],
373       # overrides the one in cust_main in cases where a cust_pay is passed
374       [ payby             => sub { FS::payby->shortname(shift->payby) } ],
375       [ date              => sub { time2str("%a %B %o, %Y", shift->_date) } ],
376       [ payinfo           => sub { 
377           my $cust_pay = shift;
378           ($cust_pay->payby eq 'CARD' || $cust_pay->payby eq 'CHEK') ?
379             $cust_pay->paymask : $cust_pay->decrypt($cust_pay->payinfo)
380         } ],
381     ],
382     # for payment decline messages
383     # try to support all cust_pay fields
384     # 'error' is a special case, it contains the raw error from the gateway
385     'cust_pay_pending' => [qw(
386       _date
387       error
388       ),
389       [ paid              => sub { sprintf("%.2f", shift->paid) } ],
390       [ payby             => sub { FS::payby->shortname(shift->payby) } ],
391       [ date              => sub { time2str("%a %B %o, %Y", shift->_date) } ],
392       [ payinfo           => sub {
393           my $pending = shift;
394           ($pending->payby eq 'CARD' || $pending->payby eq 'CHEK') ?
395             $pending->paymask : $pending->decrypt($pending->payinfo)
396         } ],
397     ],
398   };
399 }
400
401 sub _upgrade_data {
402   my ($self, %opts) = @_;
403
404   my @fixes = (
405     [ 'alerter_msgnum',  'alerter_template',   '',               '', '' ],
406     [ 'cancel_msgnum',   'cancelmessage',      'cancelsubject',  '', '' ],
407     [ 'decline_msgnum',  'declinetemplate',    '',               '', '' ],
408     [ 'impending_recur_msgnum', 'impending_recur_template', '',  '', 'impending_recur_bcc' ],
409     [ 'payment_receipt_msgnum', 'payment_receipt_email', '',     '', '' ],
410     [ 'welcome_msgnum',  'welcome_email',      'welcome_email-subject', 'welcome_email-from', '' ],
411     [ 'warning_msgnum',  'warning_email',      'warning_email-subject', 'warning_email-from', '' ],
412   );
413  
414   my $conf = new FS::Conf;
415   my @agentnums = ('', map {$_->agentnum} qsearch('agent', {}));
416   foreach my $agentnum (@agentnums) {
417     foreach (@fixes) {
418       my ($newname, $oldname, $subject, $from, $bcc) = @$_;
419       if ($conf->exists($oldname, $agentnum)) {
420         my $new = new FS::msg_template({
421            'msgname'   => $oldname,
422            'agentnum'  => $agentnum,
423            'from_addr' => ($from && $conf->config($from, $agentnum)) || 
424                           $conf->config('invoice_from', $agentnum),
425            'bcc_addr'  => ($bcc && $conf->config($from, $agentnum)) || '',
426            'subject'   => ($subject && $conf->config($subject, $agentnum)) || '',
427            'mime_type' => 'text/html',
428            'body'      => join('<BR>',$conf->config($oldname, $agentnum)),
429         });
430         my $error = $new->insert;
431         die $error if $error;
432         $conf->set($newname, $new->msgnum, $agentnum);
433         $conf->delete($oldname, $agentnum);
434         $conf->delete($from, $agentnum) if $from;
435         $conf->delete($subject, $agentnum) if $subject;
436       }
437     }
438   }
439 }
440
441 sub eviscerate {
442   # Every bit as pleasant as it sounds.
443   #
444   # We do this because Text::Template::Preprocess doesn't
445   # actually work.  It runs the entire template through 
446   # the preprocessor, instead of the code segments.  Which 
447   # is a shame, because Text::Template already contains
448   # the code to do this operation.
449   my $body = shift;
450   my (@outside, @inside);
451   my $depth = 0;
452   my $chunk = '';
453   while($body || $chunk) {
454     my ($first, $delim, $rest);
455     # put all leading non-delimiters into $first
456     ($first, $rest) =
457         ($body =~ /^((?:\\[{}]|[^{}])*)(.*)$/s);
458     $chunk .= $first;
459     # put a leading delimiter into $delim if there is one
460     ($delim, $rest) =
461       ($rest =~ /^([{}]?)(.*)$/s);
462
463     if( $delim eq '{' ) {
464       $chunk .= '{';
465       if( $depth == 0 ) {
466         push @outside, $chunk;
467         $chunk = '';
468       }
469       $depth++;
470     }
471     elsif( $delim eq '}' ) {
472       $depth--;
473       if( $depth == 0 ) {
474         push @inside, $chunk;
475         $chunk = '';
476       }
477       $chunk .= '}';
478     }
479     else {
480       # no more delimiters
481       if( $depth == 0 ) {
482         push @outside, $chunk . $rest;
483       } # else ? something wrong
484       last;
485     }
486     $body = $rest;
487   }
488   (\@outside, \@inside);
489 }
490
491 =back
492
493 =head1 BUGS
494
495 =head1 SEE ALSO
496
497 L<FS::Record>, schema.html from the base documentation.
498
499 =cut
500
501 1;
502