logging of template-generated mail, #12809
[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 vars qw( @statuses );
7
8 =head1 NAME
9
10 FS::cust_msg - Object methods for cust_msg records
11
12 =head1 SYNOPSIS
13
14   use FS::cust_msg;
15
16   $record = new FS::cust_msg \%hash;
17   $record = new FS::cust_msg { 'column' => 'value' };
18
19   $error = $record->insert;
20
21   $error = $record->check;
22
23 =head1 DESCRIPTION
24
25 An FS::cust_msg object represents a template-generated message sent to 
26 a customer (see L<FS::msg_template>).  FS::cust_msg inherits from
27 FS::Record.  The following fields are currently supported:
28
29 =over 4
30
31 =item custmsgnum - primary key
32
33 =item custnum - customer number
34
35 =item msgnum - template number
36
37 =item _date - the time the message was sent
38
39 =item env_from - envelope From address
40
41 =item env_to - envelope To addresses, including Bcc, separated by newlines
42
43 =item header - message header
44
45 =item body - message body
46
47 =item error - Email::Sender error message (or null for success)
48
49 =back
50
51 =head1 METHODS
52
53 =over 4
54
55 =item new HASHREF
56
57 Creates a new 
58
59 =cut
60
61 # the new method can be inherited from FS::Record, if a table method is defined
62
63 sub table { 'cust_msg'; }
64
65 sub nohistory_fields { ('header', 'body'); } 
66 # history is kind of pointless on this table
67
68 @statuses = qw( prepared sent failed );
69
70 =item insert
71
72 Adds this record to the database.  If there is an error, returns the error 
73 and emits a warning; otherwise returns false.
74
75 =cut
76
77 sub insert {
78   # warn of all errors here; failing to insert/update one of these should 
79   # cause a warning at worst
80   my $self = shift;
81   my $error = $self->SUPER::insert;
82   warn "[cust_msg] error logging message status: $error\n" if $error;
83   return $error;
84 }
85
86 =item delete
87
88 Delete this record from the database.  There's no reason to do this.
89
90 =cut
91
92 sub delete {
93   my $self = shift;
94   warn "[cust_msg] log entry deleted\n";
95   return $self->SUPER::delete;
96 }
97
98 =item replace OLD_RECORD
99
100 Replaces the OLD_RECORD with this one in the database.  If there is an error,
101 returns the error and emits a warning, otherwise returns false.
102
103 =cut
104
105 sub replace {
106   my $self = shift;
107   my $error = $self->SUPER::replace(@_);
108   warn "[cust_msg] error logging message status: $error\n" if $error;
109   return $error;
110 }
111
112 =item check
113
114 Checks all fields to make sure this is a valid example.  If there is
115 an error, returns the error, otherwise returns false.  Called by the insert
116 and replace methods.
117
118 =cut
119
120 # the check method should currently be supplied - FS::Record contains some
121 # data checking routines
122
123 sub check {
124   my $self = shift;
125
126   my $error = 
127     $self->ut_numbern('custmsgnum')
128     || $self->ut_number('custnum')
129     || $self->ut_foreign_key('custnum', 'cust_main', 'custnum')
130     || $self->ut_numbern('msgnum')
131     || $self->ut_foreign_keyn('msgnum', 'msg_template', 'msgnum')
132     || $self->ut_numbern('_date')
133     || $self->ut_textn('env_from')
134     || $self->ut_textn('env_to')
135     || $self->ut_anything('header')
136     || $self->ut_anything('body')
137     || $self->ut_enum('status', \@statuses)
138     || $self->ut_textn('error')
139   ;
140   return $error if $error;
141
142   $self->SUPER::check;
143 }
144
145 =back
146
147 =head1 SEE ALSO
148
149 L<FS::msg_template>, L<FS::cust_main>, L<FS::Record>.
150
151 =cut
152
153 1;
154