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