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:
71 =item selfservice_access
77 =item _password_encoding
94 Creates a new example. To add the example to the database, see L<"insert">.
96 Note that this stores the hash reference, not a distinct copy of the hash it
97 points to. You can ask the object for a copy with the I<hash> method.
101 # the new method can be inherited from FS::Record, if a table method is defined
103 sub table { 'contact'; }
107 Adds this record to the database. If there is an error, returns the error,
108 otherwise returns false.
115 local $SIG{INT} = 'IGNORE';
116 local $SIG{QUIT} = 'IGNORE';
117 local $SIG{TERM} = 'IGNORE';
118 local $SIG{TSTP} = 'IGNORE';
119 local $SIG{PIPE} = 'IGNORE';
121 my $oldAutoCommit = $FS::UID::AutoCommit;
122 local $FS::UID::AutoCommit = 0;
125 my $error = $self->SUPER::insert;
127 $dbh->rollback if $oldAutoCommit;
131 foreach my $pf ( grep { /^phonetypenum(\d+)$/ && $self->get($_) =~ /\S/ }
132 keys %{ $self->hashref } ) {
133 $pf =~ /^phonetypenum(\d+)$/ or die "wtf (daily, the)";
134 my $phonetypenum = $1;
136 my $contact_phone = new FS::contact_phone {
137 'contactnum' => $self->contactnum,
138 'phonetypenum' => $phonetypenum,
139 _parse_phonestring( $self->get($pf) ),
141 $error = $contact_phone->insert;
143 $dbh->rollback if $oldAutoCommit;
148 if ( $self->get('emailaddress') =~ /\S/ ) {
150 foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) {
152 my $contact_email = new FS::contact_email {
153 'contactnum' => $self->contactnum,
154 'emailaddress' => $email,
156 $error = $contact_email->insert;
158 $dbh->rollback if $oldAutoCommit;
166 #unless ( $import || $skip_fuzzyfiles ) {
167 #warn " queueing fuzzyfiles update\n"
169 $error = $self->queue_fuzzyfiles_update;
171 $dbh->rollback if $oldAutoCommit;
172 return "updating fuzzy search cache: $error";
176 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
184 Delete this record from the database.
188 # the delete method can be inherited from FS::Record
193 local $SIG{HUP} = 'IGNORE';
194 local $SIG{INT} = 'IGNORE';
195 local $SIG{QUIT} = 'IGNORE';
196 local $SIG{TERM} = 'IGNORE';
197 local $SIG{TSTP} = 'IGNORE';
198 local $SIG{PIPE} = 'IGNORE';
200 my $oldAutoCommit = $FS::UID::AutoCommit;
201 local $FS::UID::AutoCommit = 0;
204 foreach my $object ( $self->contact_phone, $self->contact_email ) {
205 my $error = $object->delete;
207 $dbh->rollback if $oldAutoCommit;
212 my $error = $self->SUPER::delete;
214 $dbh->rollback if $oldAutoCommit;
218 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
223 =item replace OLD_RECORD
225 Replaces the OLD_RECORD with this one in the database. If there is an error,
226 returns the error, otherwise returns false.
233 local $SIG{INT} = 'IGNORE';
234 local $SIG{QUIT} = 'IGNORE';
235 local $SIG{TERM} = 'IGNORE';
236 local $SIG{TSTP} = 'IGNORE';
237 local $SIG{PIPE} = 'IGNORE';
239 my $oldAutoCommit = $FS::UID::AutoCommit;
240 local $FS::UID::AutoCommit = 0;
243 my $error = $self->SUPER::replace(@_);
245 $dbh->rollback if $oldAutoCommit;
249 foreach my $pf ( grep { /^phonetypenum(\d+)$/ && $self->get($_) }
250 keys %{ $self->hashref } ) {
251 $pf =~ /^phonetypenum(\d+)$/ or die "wtf (daily, the)";
252 my $phonetypenum = $1;
254 my %cp = ( 'contactnum' => $self->contactnum,
255 'phonetypenum' => $phonetypenum,
257 my $contact_phone = qsearchs('contact_phone', \%cp)
258 || new FS::contact_phone \%cp;
260 my %cpd = _parse_phonestring( $self->get($pf) );
261 $contact_phone->set( $_ => $cpd{$_} ) foreach keys %cpd;
263 my $method = $contact_phone->contactphonenum ? 'replace' : 'insert';
265 $error = $contact_phone->$method;
267 $dbh->rollback if $oldAutoCommit;
272 if ( defined($self->get('emailaddress')) ) {
274 #ineffecient but whatever, how many email addresses can there be?
276 foreach my $contact_email ( $self->contact_email ) {
277 my $error = $contact_email->delete;
279 $dbh->rollback if $oldAutoCommit;
284 foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) {
286 my $contact_email = new FS::contact_email {
287 'contactnum' => $self->contactnum,
288 'emailaddress' => $email,
290 $error = $contact_email->insert;
292 $dbh->rollback if $oldAutoCommit;
300 #unless ( $import || $skip_fuzzyfiles ) {
301 #warn " queueing fuzzyfiles update\n"
303 $error = $self->queue_fuzzyfiles_update;
305 $dbh->rollback if $oldAutoCommit;
306 return "updating fuzzy search cache: $error";
310 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
316 #i probably belong in contact_phone.pm
317 sub _parse_phonestring {
320 my($countrycode, $extension) = ('1', '');
323 if ( $value =~ s/^\s*\+\s*(\d+)// ) {
329 if ( $value =~ s/\s*(ext|x)\s*(\d+)\s*$//i ) {
333 ( 'countrycode' => $countrycode,
334 'phonenum' => $value,
335 'extension' => $extension,
339 =item queue_fuzzyfiles_update
341 Used by insert & replace to update the fuzzy search cache
345 use FS::cust_main::Search;
346 sub queue_fuzzyfiles_update {
349 local $SIG{HUP} = 'IGNORE';
350 local $SIG{INT} = 'IGNORE';
351 local $SIG{QUIT} = 'IGNORE';
352 local $SIG{TERM} = 'IGNORE';
353 local $SIG{TSTP} = 'IGNORE';
354 local $SIG{PIPE} = 'IGNORE';
356 my $oldAutoCommit = $FS::UID::AutoCommit;
357 local $FS::UID::AutoCommit = 0;
360 foreach my $field ( 'first', 'last' ) {
361 my $queue = new FS::queue {
362 'job' => 'FS::cust_main::Search::append_fuzzyfiles_fuzzyfield'
364 my @args = "contact.$field", $self->get($field);
365 my $error = $queue->insert( @args );
367 $dbh->rollback if $oldAutoCommit;
368 return "queueing job (transaction rolled back): $error";
372 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
379 Checks all fields to make sure this is a valid example. If there is
380 an error, returns the error, otherwise returns false. Called by the insert
385 # the check method should currently be supplied - FS::Record contains some
386 # data checking routines
392 $self->ut_numbern('contactnum')
393 || $self->ut_foreign_keyn('prospectnum', 'prospect_main', 'prospectnum')
394 || $self->ut_foreign_keyn('custnum', 'cust_main', 'custnum')
395 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
396 || $self->ut_foreign_keyn('classnum', 'contact_class', 'classnum')
397 || $self->ut_namen('last')
398 || $self->ut_namen('first')
399 || $self->ut_textn('title')
400 || $self->ut_textn('comment')
401 || $self->ut_enum('selfservice_access', [ '', 'Y' ])
402 || $self->ut_textn('_password')
403 || $self->ut_enum('_password_encoding', [ '', 'bcrypt'])
404 || $self->ut_enum('disabled', [ '', 'Y' ])
406 return $error if $error;
408 return "No prospect or customer!" unless $self->prospectnum || $self->custnum;
409 return "Prospect and customer!" if $self->prospectnum && $self->custnum;
411 return "One of first name, last name, or title must have a value"
412 if ! grep $self->$_(), qw( first last title);
419 my $data = $self->first. ' '. $self->last;
420 $data .= ', '. $self->title
422 $data .= ' ('. $self->comment. ')'
429 return '' unless $self->locationnum;
430 qsearchs('cust_location', { 'locationnum' => $self->locationnum } );
435 return '' unless $self->classnum;
436 qsearchs('contact_class', { 'classnum' => $self->classnum } );
439 sub contact_classname {
441 my $contact_class = $self->contact_class or return '';
442 $contact_class->classname;
447 qsearch('contact_phone', { 'contactnum' => $self->contactnum } );
452 qsearch('contact_email', { 'contactnum' => $self->contactnum } );
457 qsearchs('cust_main', { 'custnum' => $self->custnum } );
466 L<FS::Record>, schema.html from the base documentation.