4 use base qw( FS::Record );
5 use FS::Record qw( qsearch qsearchs dbh );
10 use FS::contact_email;
14 FS::contact - Object methods for contact records
20 $record = new FS::contact \%hash;
21 $record = new FS::contact { 'column' => 'value' };
23 $error = $record->insert;
25 $error = $new_record->replace($old_record);
27 $error = $record->delete;
29 $error = $record->check;
33 An FS::contact object represents an example. FS::contact inherits from
34 FS::Record. The following fields are currently supported:
83 Creates a new example. To add the example to the database, see L<"insert">.
85 Note that this stores the hash reference, not a distinct copy of the hash it
86 points to. You can ask the object for a copy with the I<hash> method.
90 # the new method can be inherited from FS::Record, if a table method is defined
92 sub table { 'contact'; }
96 Adds this record to the database. If there is an error, returns the error,
97 otherwise returns false.
104 local $SIG{INT} = 'IGNORE';
105 local $SIG{QUIT} = 'IGNORE';
106 local $SIG{TERM} = 'IGNORE';
107 local $SIG{TSTP} = 'IGNORE';
108 local $SIG{PIPE} = 'IGNORE';
110 my $oldAutoCommit = $FS::UID::AutoCommit;
111 local $FS::UID::AutoCommit = 0;
114 my $error = $self->SUPER::insert;
116 $dbh->rollback if $oldAutoCommit;
120 foreach my $pf ( grep { /^phonetypenum(\d+)$/ && $self->get($_) =~ /\S/ }
121 keys %{ $self->hashref } ) {
122 $pf =~ /^phonetypenum(\d+)$/ or die "wtf (daily, the)";
123 my $phonetypenum = $1;
125 my $contact_phone = new FS::contact_phone {
126 'contactnum' => $self->contactnum,
127 'phonetypenum' => $phonetypenum,
128 _parse_phonestring( $self->get($pf) ),
130 $error = $contact_phone->insert;
132 $dbh->rollback if $oldAutoCommit;
137 if ( $self->get('emailaddress') =~ /\S/ ) {
138 my $contact_email = new FS::contact_email {
139 'contactnum' => $self->contactnum,
140 'emailaddress' => $self->get('emailaddress'),
142 $error = $contact_email->insert;
144 $dbh->rollback if $oldAutoCommit;
149 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
157 Delete this record from the database.
161 # the delete method can be inherited from FS::Record
163 # XXX delete contact_phone, contact_email
165 =item replace OLD_RECORD
167 Replaces the OLD_RECORD with this one in the database. If there is an error,
168 returns the error, otherwise returns false.
175 local $SIG{INT} = 'IGNORE';
176 local $SIG{QUIT} = 'IGNORE';
177 local $SIG{TERM} = 'IGNORE';
178 local $SIG{TSTP} = 'IGNORE';
179 local $SIG{PIPE} = 'IGNORE';
181 my $oldAutoCommit = $FS::UID::AutoCommit;
182 local $FS::UID::AutoCommit = 0;
185 my $error = $self->SUPER::replace(@_);
187 $dbh->rollback if $oldAutoCommit;
191 foreach my $pf ( grep { /^phonetypenum(\d+)$/ && $self->get($_) }
192 keys %{ $self->hashref } ) {
193 $pf =~ /^phonetypenum(\d+)$/ or die "wtf (daily, the)";
194 my $phonetypenum = $1;
196 my %cp = ( 'contactnum' => $self->contactnum,
197 'phonetypenum' => $phonetypenum,
199 my $contact_phone = qsearchs('contact_phone', \%cp)
200 || new FS::contact_phone \%cp;
202 my %cpd = _parse_phonestring( $self->get($pf) );
203 $contact_phone->set( $_ => $cpd{$_} ) foreach keys %cpd;
205 my $method = $contact_phone->contactphonenum ? 'replace' : 'insert';
207 $error = $contact_phone->$method;
209 $dbh->rollback if $oldAutoCommit;
214 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
220 #i probably belong in contact_phone.pm
221 sub _parse_phonestring {
224 my($countrycode, $extension) = ('1', '');
227 if ( $value =~ s/^\s*\+\s*(\d+)// ) {
233 if ( $value =~ s/\s*(ext|x)\s*(\d+)\s*$//i ) {
237 ( 'countrycode' => $countrycode,
238 'phonenum' => $value,
239 'extension' => $extension,
245 Checks all fields to make sure this is a valid example. If there is
246 an error, returns the error, otherwise returns false. Called by the insert
251 # the check method should currently be supplied - FS::Record contains some
252 # data checking routines
258 $self->ut_numbern('contactnum')
259 || $self->ut_foreign_keyn('prospectnum', 'prospect_main', 'prospectnum')
260 || $self->ut_foreign_keyn('custnum', 'cust_main', 'custnum')
261 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
262 || $self->ut_textn('last')
263 || $self->ut_textn('first')
264 || $self->ut_textn('title')
265 || $self->ut_textn('comment')
266 || $self->ut_enum('disabled', [ '', 'Y' ])
268 return $error if $error;
270 return "No prospect or customer!" unless $self->prospectnum || $self->custnum;
271 return "Prospect and customer!" if $self->prospectnum && $self->custnum;
273 return "One of first name, last name, or title must have a value"
274 if ! grep $self->$_(), qw( first last title);
281 my $data = $self->first. ' '. $self->last;
282 $data .= ', '. $self->title
284 $data .= ' ('. $self->comment. ')'
295 L<FS::Record>, schema.html from the base documentation.