db026808c5b6ca73044b2c2f0c466ed1b1a926d0
[freeside.git] / FS / FS / cust_msg.pm
1 package FS::cust_msg;
2
3 use strict;
4 use base qw( FS::cust_main_Mixin FS::Record );
5 use FS::Record qw( qsearch qsearchs );
6 use MIME::Parser;
7 use vars qw( @statuses );
8
9 =head1 NAME
10
11 FS::cust_msg - Object methods for cust_msg records
12
13 =head1 SYNOPSIS
14
15   use FS::cust_msg;
16
17   $record = new FS::cust_msg \%hash;
18   $record = new FS::cust_msg { 'column' => 'value' };
19
20   $error = $record->insert;
21
22   $error = $record->check;
23
24 =head1 DESCRIPTION
25
26 An FS::cust_msg object represents an email message generated by Freeside 
27 and sent to a customer (see L<FS::msg_template>).  FS::cust_msg inherits 
28 from FS::Record.  The following fields are currently supported:
29
30 =over 4
31
32 =item custmsgnum - primary key
33
34 =item custnum - customer number
35
36 =item msgnum - template number
37
38 =item msgtype - the message type
39
40 =item _date - the time the message was sent
41
42 =item env_from - envelope From address
43
44 =item env_to - envelope To addresses, including Bcc, separated by newlines
45
46 =item header - message header
47
48 =item body - message body (as a complete MIME document)
49
50 =item preview - HTML fragment to show as a preview of the message
51
52 =item error - Email::Sender error message (or null for success)
53
54 =item status - "prepared", "sent", or "failed"
55
56 =back
57
58 =head1 METHODS
59
60 =over 4
61
62 =item new HASHREF
63
64 Creates a new 
65
66 =cut
67
68 # the new method can be inherited from FS::Record, if a table method is defined
69
70 sub table { 'cust_msg'; }
71
72 sub nohistory_fields { ('header', 'body'); } 
73 # history is kind of pointless on this table
74
75 @statuses = qw( prepared sent failed );
76
77 =item insert
78
79 Adds this record to the database.  If there is an error, returns the error 
80 and emits a warning; otherwise returns false.
81
82 =cut
83
84 sub insert {
85   # warn of all errors here; failing to insert/update one of these should 
86   # cause a warning at worst
87   my $self = shift;
88   my $error = $self->SUPER::insert;
89   warn "[cust_msg] error logging message status: $error\n" if $error;
90   return $error;
91 }
92
93 =item delete
94
95 Delete this record from the database.  There's no reason to do this.
96
97 =cut
98
99 sub delete {
100   my $self = shift;
101   warn "[cust_msg] log entry deleted\n";
102   return $self->SUPER::delete;
103 }
104
105 =item replace OLD_RECORD
106
107 Replaces the OLD_RECORD with this one in the database.  If there is an error,
108 returns the error and emits a warning, otherwise returns false.
109
110 =cut
111
112 sub replace {
113   my $self = shift;
114   my $error = $self->SUPER::replace(@_);
115   warn "[cust_msg] error logging message status: $error\n" if $error;
116   return $error;
117 }
118
119 =item check
120
121 Checks all fields to make sure this is a valid example.  If there is
122 an error, returns the error, otherwise returns false.  Called by the insert
123 and replace methods.
124
125 =cut
126
127 # the check method should currently be supplied - FS::Record contains some
128 # data checking routines
129
130 sub check {
131   my $self = shift;
132
133   my $error = 
134     $self->ut_numbern('custmsgnum')
135     || $self->ut_numbern('custnum')
136     || $self->ut_foreign_keyn('custnum', 'cust_main', 'custnum')
137     || $self->ut_numbern('msgnum')
138     || $self->ut_foreign_keyn('msgnum', 'msg_template', 'msgnum')
139     || $self->ut_numbern('_date')
140     || $self->ut_textn('env_from')
141     || $self->ut_textn('env_to')
142     || $self->ut_anything('header')
143     || $self->ut_anything('body')
144     || $self->ut_anything('preview')
145     || $self->ut_enum('status', \@statuses)
146     || $self->ut_textn('error')
147     || $self->ut_enum('msgtype', [  '',
148                                     'invoice',
149                                     'receipt',
150                                     'admin',
151                                  ])
152   ;
153   return $error if $error;
154
155   $self->SUPER::check;
156 }
157
158 =item send
159
160 Sends the message through its parent L<FS::msg_template>. Returns an error
161 message on error, or an empty string.
162
163 =cut
164
165 sub send {
166   my $self = shift;
167   # it's still allowed to have cust_msgs without message templates, but only 
168   # for email.
169   my $msg_template = $self->msg_template || 'FS::msg_template::email';
170   $msg_template->send_prepared($self);
171 }
172
173 =item entity
174
175 Returns the complete message as a L<MIME::Entity>.
176
177 XXX this only works if the message in fact contains a MIME entity. Messages
178 created by external APIs may not look like that.
179
180 =item parts
181
182 Returns a list of the MIME parts contained in the message, as L<MIME::Entity>
183 objects.
184
185 =cut
186
187 sub entity {
188   my $self = shift;
189   if ( !exists($self->{entity}) ) {
190     my $parser = MIME::Parser->new;
191     my $output_dir = "$FS::UID::cache_dir/cache.$FS::UID::datasrc/mimeparts";
192     mkdir($output_dir) unless -d $output_dir;
193     $parser->output_under($output_dir);
194     $self->{entity} =
195       $parser->parse_data( $self->header . "\n" . $self->body );
196   }
197   $self->{entity};
198 }
199
200 sub parts {
201   my $self = shift;
202   # return only the parts with bodies, not the multipart containers
203   grep { $_->bodyhandle } $self->entity->parts_DFS;
204 }
205
206 =back
207
208 =head1 SUBROUTINES
209
210 =over 4
211
212 =item process_send CUSTMSGNUM
213
214 Given a C<cust_msg.custmsgnum> value, sends the message. It must already
215 have been prepared (via L<FS::msg_template/prepare>).
216
217 =cut
218
219 sub process_send {
220   my $custmsgnum = shift;
221   my $cust_msg = FS::cust_msg->by_key($custmsgnum)
222     or die "cust_msg #$custmsgnum not found";
223   my $error = $cust_msg->send;
224   die $error if $error;
225 }
226
227 =head1 SEE ALSO
228
229 L<FS::msg_template>, L<FS::cust_main>, L<FS::Record>.
230
231 =cut
232
233 1;
234