2 use base qw( FS::Record );
5 use FS::Record qw( qsearchs dbh ); # qw( qsearch qsearchs dbh );
9 FS::contact - Object methods for contact records
15 $record = new FS::contact \%hash;
16 $record = new FS::contact { 'column' => 'value' };
18 $error = $record->insert;
20 $error = $new_record->replace($old_record);
22 $error = $record->delete;
24 $error = $record->check;
28 An FS::contact object represents an example. FS::contact inherits from
29 FS::Record. The following fields are currently supported:
65 =item selfservice_access
71 =item _password_encoding
88 Creates a new example. To add the example to the database, see L<"insert">.
90 Note that this stores the hash reference, not a distinct copy of the hash it
91 points to. You can ask the object for a copy with the I<hash> method.
95 # the new method can be inherited from FS::Record, if a table method is defined
97 sub table { 'contact'; }
101 Adds this record to the database. If there is an error, returns the error,
102 otherwise returns false.
109 local $SIG{INT} = 'IGNORE';
110 local $SIG{QUIT} = 'IGNORE';
111 local $SIG{TERM} = 'IGNORE';
112 local $SIG{TSTP} = 'IGNORE';
113 local $SIG{PIPE} = 'IGNORE';
115 my $oldAutoCommit = $FS::UID::AutoCommit;
116 local $FS::UID::AutoCommit = 0;
119 my $error = $self->SUPER::insert;
121 $dbh->rollback if $oldAutoCommit;
125 foreach my $pf ( grep { /^phonetypenum(\d+)$/ && $self->get($_) =~ /\S/ }
126 keys %{ $self->hashref } ) {
127 $pf =~ /^phonetypenum(\d+)$/ or die "wtf (daily, the)";
128 my $phonetypenum = $1;
130 my $contact_phone = new FS::contact_phone {
131 'contactnum' => $self->contactnum,
132 'phonetypenum' => $phonetypenum,
133 _parse_phonestring( $self->get($pf) ),
135 $error = $contact_phone->insert;
137 $dbh->rollback if $oldAutoCommit;
142 if ( $self->get('emailaddress') =~ /\S/ ) {
144 foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) {
146 my $contact_email = new FS::contact_email {
147 'contactnum' => $self->contactnum,
148 'emailaddress' => $email,
150 $error = $contact_email->insert;
152 $dbh->rollback if $oldAutoCommit;
160 #unless ( $import || $skip_fuzzyfiles ) {
161 #warn " queueing fuzzyfiles update\n"
163 $error = $self->queue_fuzzyfiles_update;
165 $dbh->rollback if $oldAutoCommit;
166 return "updating fuzzy search cache: $error";
170 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
178 Delete this record from the database.
182 # the delete method can be inherited from FS::Record
187 local $SIG{HUP} = 'IGNORE';
188 local $SIG{INT} = 'IGNORE';
189 local $SIG{QUIT} = 'IGNORE';
190 local $SIG{TERM} = 'IGNORE';
191 local $SIG{TSTP} = 'IGNORE';
192 local $SIG{PIPE} = 'IGNORE';
194 my $oldAutoCommit = $FS::UID::AutoCommit;
195 local $FS::UID::AutoCommit = 0;
198 foreach my $object ( $self->contact_phone, $self->contact_email ) {
199 my $error = $object->delete;
201 $dbh->rollback if $oldAutoCommit;
206 my $error = $self->SUPER::delete;
208 $dbh->rollback if $oldAutoCommit;
212 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
217 =item replace OLD_RECORD
219 Replaces the OLD_RECORD with this one in the database. If there is an error,
220 returns the error, otherwise returns false.
227 local $SIG{INT} = 'IGNORE';
228 local $SIG{QUIT} = 'IGNORE';
229 local $SIG{TERM} = 'IGNORE';
230 local $SIG{TSTP} = 'IGNORE';
231 local $SIG{PIPE} = 'IGNORE';
233 my $oldAutoCommit = $FS::UID::AutoCommit;
234 local $FS::UID::AutoCommit = 0;
237 my $error = $self->SUPER::replace(@_);
239 $dbh->rollback if $oldAutoCommit;
243 foreach my $pf ( grep { /^phonetypenum(\d+)$/ && $self->get($_) }
244 keys %{ $self->hashref } ) {
245 $pf =~ /^phonetypenum(\d+)$/ or die "wtf (daily, the)";
246 my $phonetypenum = $1;
248 my %cp = ( 'contactnum' => $self->contactnum,
249 'phonetypenum' => $phonetypenum,
251 my $contact_phone = qsearchs('contact_phone', \%cp)
252 || new FS::contact_phone \%cp;
254 my %cpd = _parse_phonestring( $self->get($pf) );
255 $contact_phone->set( $_ => $cpd{$_} ) foreach keys %cpd;
257 my $method = $contact_phone->contactphonenum ? 'replace' : 'insert';
259 $error = $contact_phone->$method;
261 $dbh->rollback if $oldAutoCommit;
266 if ( defined($self->get('emailaddress')) ) {
268 #ineffecient but whatever, how many email addresses can there be?
270 foreach my $contact_email ( $self->contact_email ) {
271 my $error = $contact_email->delete;
273 $dbh->rollback if $oldAutoCommit;
278 foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) {
280 my $contact_email = new FS::contact_email {
281 'contactnum' => $self->contactnum,
282 'emailaddress' => $email,
284 $error = $contact_email->insert;
286 $dbh->rollback if $oldAutoCommit;
294 #unless ( $import || $skip_fuzzyfiles ) {
295 #warn " queueing fuzzyfiles update\n"
297 $error = $self->queue_fuzzyfiles_update;
299 $dbh->rollback if $oldAutoCommit;
300 return "updating fuzzy search cache: $error";
304 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
310 #i probably belong in contact_phone.pm
311 sub _parse_phonestring {
314 my($countrycode, $extension) = ('1', '');
317 if ( $value =~ s/^\s*\+\s*(\d+)// ) {
323 if ( $value =~ s/\s*(ext|x)\s*(\d+)\s*$//i ) {
327 ( 'countrycode' => $countrycode,
328 'phonenum' => $value,
329 'extension' => $extension,
333 =item queue_fuzzyfiles_update
335 Used by insert & replace to update the fuzzy search cache
339 use FS::cust_main::Search;
340 sub queue_fuzzyfiles_update {
343 local $SIG{HUP} = 'IGNORE';
344 local $SIG{INT} = 'IGNORE';
345 local $SIG{QUIT} = 'IGNORE';
346 local $SIG{TERM} = 'IGNORE';
347 local $SIG{TSTP} = 'IGNORE';
348 local $SIG{PIPE} = 'IGNORE';
350 my $oldAutoCommit = $FS::UID::AutoCommit;
351 local $FS::UID::AutoCommit = 0;
354 foreach my $field ( 'first', 'last' ) {
355 my $queue = new FS::queue {
356 'job' => 'FS::cust_main::Search::append_fuzzyfiles_fuzzyfield'
358 my @args = "contact.$field", $self->get($field);
359 my $error = $queue->insert( @args );
361 $dbh->rollback if $oldAutoCommit;
362 return "queueing job (transaction rolled back): $error";
366 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
373 Checks all fields to make sure this is a valid example. If there is
374 an error, returns the error, otherwise returns false. Called by the insert
379 # the check method should currently be supplied - FS::Record contains some
380 # data checking routines
386 $self->ut_numbern('contactnum')
387 || $self->ut_foreign_keyn('prospectnum', 'prospect_main', 'prospectnum')
388 || $self->ut_foreign_keyn('custnum', 'cust_main', 'custnum')
389 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
390 || $self->ut_foreign_keyn('classnum', 'contact_class', 'classnum')
391 || $self->ut_namen('last')
392 || $self->ut_namen('first')
393 || $self->ut_textn('title')
394 || $self->ut_textn('comment')
395 || $self->ut_enum('selfservice_access', [ '', 'Y' ])
396 || $self->ut_textn('_password')
397 || $self->ut_enum('_password_encoding', [ '', 'bcrypt'])
398 || $self->ut_enum('disabled', [ '', 'Y' ])
400 return $error if $error;
402 return "No prospect or customer!" unless $self->prospectnum || $self->custnum;
403 return "Prospect and customer!" if $self->prospectnum && $self->custnum;
405 return "One of first name, last name, or title must have a value"
406 if ! grep $self->$_(), qw( first last title);
413 my $data = $self->first. ' '. $self->last;
414 $data .= ', '. $self->title
416 $data .= ' ('. $self->comment. ')'
421 sub contact_classname {
423 my $contact_class = $self->contact_class or return '';
424 $contact_class->classname;
433 L<FS::Record>, schema.html from the base documentation.