2 use base qw( FS::Record );
5 use FS::Record qw( qsearch qsearchs dbh );
10 use FS::contact_phone;
11 use FS::contact_email;
15 FS::contact - Object methods for contact records
21 $record = new FS::contact \%hash;
22 $record = new FS::contact { 'column' => 'value' };
24 $error = $record->insert;
26 $error = $new_record->replace($old_record);
28 $error = $record->delete;
30 $error = $record->check;
34 An FS::contact object represents an example. FS::contact inherits from
35 FS::Record. The following fields are currently supported:
84 Creates a new example. To add the example to the database, see L<"insert">.
86 Note that this stores the hash reference, not a distinct copy of the hash it
87 points to. You can ask the object for a copy with the I<hash> method.
91 # the new method can be inherited from FS::Record, if a table method is defined
93 sub table { 'contact'; }
97 Adds this record to the database. If there is an error, returns the error,
98 otherwise returns false.
105 local $SIG{INT} = 'IGNORE';
106 local $SIG{QUIT} = 'IGNORE';
107 local $SIG{TERM} = 'IGNORE';
108 local $SIG{TSTP} = 'IGNORE';
109 local $SIG{PIPE} = 'IGNORE';
111 my $oldAutoCommit = $FS::UID::AutoCommit;
112 local $FS::UID::AutoCommit = 0;
115 my $error = $self->SUPER::insert;
117 $dbh->rollback if $oldAutoCommit;
121 foreach my $pf ( grep { /^phonetypenum(\d+)$/ && $self->get($_) =~ /\S/ }
122 keys %{ $self->hashref } ) {
123 $pf =~ /^phonetypenum(\d+)$/ or die "wtf (daily, the)";
124 my $phonetypenum = $1;
126 my $contact_phone = new FS::contact_phone {
127 'contactnum' => $self->contactnum,
128 'phonetypenum' => $phonetypenum,
129 _parse_phonestring( $self->get($pf) ),
131 $error = $contact_phone->insert;
133 $dbh->rollback if $oldAutoCommit;
138 if ( $self->get('emailaddress') =~ /\S/ ) {
140 foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) {
142 my $contact_email = new FS::contact_email {
143 'contactnum' => $self->contactnum,
144 'emailaddress' => $email,
146 $error = $contact_email->insert;
148 $dbh->rollback if $oldAutoCommit;
156 #unless ( $import || $skip_fuzzyfiles ) {
157 #warn " queueing fuzzyfiles update\n"
159 $error = $self->queue_fuzzyfiles_update;
161 $dbh->rollback if $oldAutoCommit;
162 return "updating fuzzy search cache: $error";
166 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
174 Delete this record from the database.
178 # the delete method can be inherited from FS::Record
183 local $SIG{HUP} = 'IGNORE';
184 local $SIG{INT} = 'IGNORE';
185 local $SIG{QUIT} = 'IGNORE';
186 local $SIG{TERM} = 'IGNORE';
187 local $SIG{TSTP} = 'IGNORE';
188 local $SIG{PIPE} = 'IGNORE';
190 my $oldAutoCommit = $FS::UID::AutoCommit;
191 local $FS::UID::AutoCommit = 0;
194 foreach my $object ( $self->contact_phone, $self->contact_email ) {
195 my $error = $object->delete;
197 $dbh->rollback if $oldAutoCommit;
202 my $error = $self->SUPER::delete;
204 $dbh->rollback if $oldAutoCommit;
208 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
213 =item replace OLD_RECORD
215 Replaces the OLD_RECORD with this one in the database. If there is an error,
216 returns the error, otherwise returns false.
223 local $SIG{INT} = 'IGNORE';
224 local $SIG{QUIT} = 'IGNORE';
225 local $SIG{TERM} = 'IGNORE';
226 local $SIG{TSTP} = 'IGNORE';
227 local $SIG{PIPE} = 'IGNORE';
229 my $oldAutoCommit = $FS::UID::AutoCommit;
230 local $FS::UID::AutoCommit = 0;
233 my $error = $self->SUPER::replace(@_);
235 $dbh->rollback if $oldAutoCommit;
239 foreach my $pf ( grep { /^phonetypenum(\d+)$/ && $self->get($_) }
240 keys %{ $self->hashref } ) {
241 $pf =~ /^phonetypenum(\d+)$/ or die "wtf (daily, the)";
242 my $phonetypenum = $1;
244 my %cp = ( 'contactnum' => $self->contactnum,
245 'phonetypenum' => $phonetypenum,
247 my $contact_phone = qsearchs('contact_phone', \%cp)
248 || new FS::contact_phone \%cp;
250 my %cpd = _parse_phonestring( $self->get($pf) );
251 $contact_phone->set( $_ => $cpd{$_} ) foreach keys %cpd;
253 my $method = $contact_phone->contactphonenum ? 'replace' : 'insert';
255 $error = $contact_phone->$method;
257 $dbh->rollback if $oldAutoCommit;
262 if ( defined($self->get('emailaddress')) ) {
264 #ineffecient but whatever, how many email addresses can there be?
266 foreach my $contact_email ( $self->contact_email ) {
267 my $error = $contact_email->delete;
269 $dbh->rollback if $oldAutoCommit;
274 foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) {
276 my $contact_email = new FS::contact_email {
277 'contactnum' => $self->contactnum,
278 'emailaddress' => $email,
280 $error = $contact_email->insert;
282 $dbh->rollback if $oldAutoCommit;
290 #unless ( $import || $skip_fuzzyfiles ) {
291 #warn " queueing fuzzyfiles update\n"
293 $error = $self->queue_fuzzyfiles_update;
295 $dbh->rollback if $oldAutoCommit;
296 return "updating fuzzy search cache: $error";
300 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
306 #i probably belong in contact_phone.pm
307 sub _parse_phonestring {
310 my($countrycode, $extension) = ('1', '');
313 if ( $value =~ s/^\s*\+\s*(\d+)// ) {
319 if ( $value =~ s/\s*(ext|x)\s*(\d+)\s*$//i ) {
323 ( 'countrycode' => $countrycode,
324 'phonenum' => $value,
325 'extension' => $extension,
329 =item queue_fuzzyfiles_update
331 Used by insert & replace to update the fuzzy search cache
335 use FS::cust_main::Search;
336 sub queue_fuzzyfiles_update {
339 local $SIG{HUP} = 'IGNORE';
340 local $SIG{INT} = 'IGNORE';
341 local $SIG{QUIT} = 'IGNORE';
342 local $SIG{TERM} = 'IGNORE';
343 local $SIG{TSTP} = 'IGNORE';
344 local $SIG{PIPE} = 'IGNORE';
346 my $oldAutoCommit = $FS::UID::AutoCommit;
347 local $FS::UID::AutoCommit = 0;
350 foreach my $field ( 'first', 'last' ) {
351 my $queue = new FS::queue {
352 'job' => 'FS::cust_main::Search::append_fuzzyfiles_fuzzyfield'
354 my @args = "contact.$field", $self->get($field);
355 my $error = $queue->insert( @args );
357 $dbh->rollback if $oldAutoCommit;
358 return "queueing job (transaction rolled back): $error";
362 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
369 Checks all fields to make sure this is a valid example. If there is
370 an error, returns the error, otherwise returns false. Called by the insert
375 # the check method should currently be supplied - FS::Record contains some
376 # data checking routines
382 $self->ut_numbern('contactnum')
383 || $self->ut_foreign_keyn('prospectnum', 'prospect_main', 'prospectnum')
384 || $self->ut_foreign_keyn('custnum', 'cust_main', 'custnum')
385 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
386 || $self->ut_foreign_keyn('classnum', 'contact_class', 'classnum')
387 || $self->ut_namen('last')
388 || $self->ut_namen('first')
389 || $self->ut_textn('title')
390 || $self->ut_textn('comment')
391 || $self->ut_enum('disabled', [ '', 'Y' ])
393 return $error if $error;
395 return "No prospect or customer!" unless $self->prospectnum || $self->custnum;
396 return "Prospect and customer!" if $self->prospectnum && $self->custnum;
398 return "One of first name, last name, or title must have a value"
399 if ! grep $self->$_(), qw( first last title);
406 my $data = $self->first. ' '. $self->last;
407 $data .= ', '. $self->title
409 $data .= ' ('. $self->comment. ')'
416 return '' unless $self->locationnum;
417 qsearchs('cust_location', { 'locationnum' => $self->locationnum } );
422 return '' unless $self->classnum;
423 qsearchs('contact_class', { 'classnum' => $self->classnum } );
426 sub contact_classname {
428 my $contact_class = $self->contact_class or return '';
429 $contact_class->classname;
434 qsearch('contact_phone', { 'contactnum' => $self->contactnum } );
439 qsearch('contact_email', { 'contactnum' => $self->contactnum } );
444 qsearchs('cust_main', { 'custnum' => $self->custnum } );
453 L<FS::Record>, schema.html from the base documentation.