72901: OFM Freeside Note Classes [v3 merge]
[freeside.git] / FS / FS / cust_main_note.pm
1 package FS::cust_main_note;
2
3 use strict;
4 use base qw( FS::otaker_Mixin FS::Record );
5 use Carp;
6 use FS::Record qw( qsearch qsearchs );
7 use FS::cust_note_class;
8 use FS::Conf;
9
10 =head1 NAME
11
12 FS::cust_main_note - Object methods for cust_main_note records
13
14 =head1 SYNOPSIS
15
16   use FS::cust_main_note;
17
18   $record = new FS::cust_main_note \%hash;
19   $record = new FS::cust_main_note { 'column' => 'value' };
20
21   $error = $record->insert;
22
23   $error = $new_record->replace($old_record);
24
25   $error = $record->delete;
26
27   $error = $record->check;
28
29 =head1 DESCRIPTION
30
31 An FS::cust_main_note object represents a note attachted to a customer.
32 FS::cust_main_note inherits from FS::Record.  The following fields are
33 currently supported:
34
35 =over 4
36
37 =item notenum
38
39 primary key
40
41 =item custnum
42
43 =item classnum
44
45 =item _date
46
47 =item usernum
48
49 =item comments
50
51 =back
52
53 =head1 METHODS
54
55 =over 4
56
57 =item new HASHREF
58
59 Creates a new customer note.  To add the note to the database, see L<"insert">.
60
61 Note that this stores the hash reference, not a distinct copy of the hash it
62 points to.  You can ask the object for a copy with the I<hash> method.
63
64 =cut
65
66 # the new method can be inherited from FS::Record, if a table method is defined
67
68 sub table { 'cust_main_note'; }
69
70 =item insert
71
72 Adds this record to the database.  If there is an error, returns the error,
73 otherwise returns false.
74
75 =cut
76
77 # the insert method can be inherited from FS::Record
78
79 =item delete
80
81 Delete this record from the database.
82
83 =cut
84
85 # the delete method can be inherited from FS::Record
86
87 =item replace OLD_RECORD
88
89 Replaces the OLD_RECORD with this one in the database.  If there is an error,
90 returns the error, otherwise returns false.
91
92 =cut
93
94 # the replace method can be inherited from FS::Record
95
96 =item check
97
98 Checks all fields to make sure this is a valid example.  If there is
99 an error, returns the error, otherwise returns false.  Called by the insert
100 and replace methods.
101
102 =cut
103
104 # the check method should currently be supplied - FS::Record contains some
105 # data checking routines
106
107 sub check {
108   my $self = shift;
109
110   my $error = 
111     $self->ut_numbern('notenum')
112     || $self->ut_number('custnum')
113     || $self->ut_foreign_keyn('classnum', 'cust_note_class', 'classnum')
114     || $self->ut_numbern('_date')
115     || $self->ut_textn('otaker')
116     || $self->ut_anything('comments')
117     || $self->ut_numbern('sticky')
118   ;
119   return $error if $error;
120
121   if (!$self->classnum) {
122     my $conf = new FS::Conf;
123     return 'Note class is required'
124       if $conf->exists('note-classes')
125         and $conf->config('note-classes') eq 'Required';
126   }
127
128   $self->SUPER::check;
129 }
130
131 =item cust_note_class
132
133 Returns the customer note class, as an FS::cust_note_class object, or the empty
134 string if there is no note class.
135
136 =cut
137
138 sub cust_note_class {
139   my $self = shift;
140   if ( $self->classnum ) {
141     qsearchs('cust_note_class', { 'classnum' => $self->classnum } );
142   } else {
143     return '';
144   } 
145 }
146
147 =item classname 
148
149 Returns the customer note class name, or the empty string if there is no 
150 customer note class.
151
152 =cut
153
154 sub classname {
155   my $self = shift;
156   my $cust_note_class = $self->cust_note_class;
157   $cust_note_class ? $cust_note_class->classname : '';
158 }
159
160
161 #false laziness w/otaker_Mixin & cust_attachment
162 sub otaker {
163   my $self = shift;
164   if ( scalar(@_) ) { #set
165     my $otaker = shift;
166     my($l,$f) = (split(', ', $otaker));
167     my $access_user =  qsearchs('access_user', { 'username'=>$otaker }     )
168                     || qsearchs('access_user', { 'first'=>$f, 'last'=>$l } )
169       or croak "can't set otaker: $otaker not found!"; #confess?
170     $self->usernum( $access_user->usernum );
171     $otaker; #not sure return is used anywhere, but just in case
172   } else { #get
173     if ( $self->usernum ) {
174       $self->access_user->username;
175     } elsif ( length($self->get('otaker')) ) {
176       $self->get('otaker');
177     } else {
178       '';
179     }
180   }
181 }
182
183 # Used by FS::Upgrade to migrate to a new database.
184 sub _upgrade_data {  # class method
185   my ($class, %opts) = @_;
186   $class->_upgrade_otaker(%opts);
187 }
188
189 =back
190
191 =head1 BUGS
192
193 Lurking in the cracks.
194
195 =head1 SEE ALSO
196
197 L<FS::Record>, schema.html from the base documentation.
198
199 =cut
200
201 1;
202