Merge branch 'master' of https://github.com/jgoodman/Freeside
[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 an email message generated by Freeside 
26 and sent to a customer (see L<FS::msg_template>).  FS::cust_msg inherits 
27 from 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 msgtype - the message type
38
39 =item _date - the time the message was sent
40
41 =item env_from - envelope From address
42
43 =item env_to - envelope To addresses, including Bcc, separated by newlines
44
45 =item header - message header
46
47 =item body - message body
48
49 =item error - Email::Sender error message (or null for success)
50
51 =back
52
53 =head1 METHODS
54
55 =over 4
56
57 =item new HASHREF
58
59 Creates a new 
60
61 =cut
62
63 # the new method can be inherited from FS::Record, if a table method is defined
64
65 sub table { 'cust_msg'; }
66
67 sub nohistory_fields { ('header', 'body'); } 
68 # history is kind of pointless on this table
69
70 @statuses = qw( prepared sent failed );
71
72 =item insert
73
74 Adds this record to the database.  If there is an error, returns the error 
75 and emits a warning; otherwise returns false.
76
77 =cut
78
79 sub insert {
80   # warn of all errors here; failing to insert/update one of these should 
81   # cause a warning at worst
82   my $self = shift;
83   my $error = $self->SUPER::insert;
84   warn "[cust_msg] error logging message status: $error\n" if $error;
85   return $error;
86 }
87
88 =item delete
89
90 Delete this record from the database.  There's no reason to do this.
91
92 =cut
93
94 sub delete {
95   my $self = shift;
96   warn "[cust_msg] log entry deleted\n";
97   return $self->SUPER::delete;
98 }
99
100 =item replace OLD_RECORD
101
102 Replaces the OLD_RECORD with this one in the database.  If there is an error,
103 returns the error and emits a warning, otherwise returns false.
104
105 =cut
106
107 sub replace {
108   my $self = shift;
109   my $error = $self->SUPER::replace(@_);
110   warn "[cust_msg] error logging message status: $error\n" if $error;
111   return $error;
112 }
113
114 =item check
115
116 Checks all fields to make sure this is a valid example.  If there is
117 an error, returns the error, otherwise returns false.  Called by the insert
118 and replace methods.
119
120 =cut
121
122 # the check method should currently be supplied - FS::Record contains some
123 # data checking routines
124
125 sub check {
126   my $self = shift;
127
128   my $error = 
129     $self->ut_numbern('custmsgnum')
130     || $self->ut_numbern('custnum')
131     || $self->ut_foreign_keyn('custnum', 'cust_main', 'custnum')
132     || $self->ut_numbern('msgnum')
133     || $self->ut_foreign_keyn('msgnum', 'msg_template', 'msgnum')
134     || $self->ut_numbern('_date')
135     || $self->ut_textn('env_from')
136     || $self->ut_textn('env_to')
137     || $self->ut_anything('header')
138     || $self->ut_anything('body')
139     || $self->ut_enum('status', \@statuses)
140     || $self->ut_textn('error')
141     || $self->ut_enum('msgtype', [  '',
142                                     'invoice',
143                                     'receipt',
144                                     'admin',
145                                  ])
146   ;
147   return $error if $error;
148
149   $self->SUPER::check;
150 }
151
152 =back
153
154 =head1 SEE ALSO
155
156 L<FS::msg_template>, L<FS::cust_main>, L<FS::Record>.
157
158 =cut
159
160 1;
161