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:
78 Creates a new example. To add the example to the database, see L<"insert">.
80 Note that this stores the hash reference, not a distinct copy of the hash it
81 points to. You can ask the object for a copy with the I<hash> method.
85 # the new method can be inherited from FS::Record, if a table method is defined
87 sub table { 'contact'; }
91 Adds this record to the database. If there is an error, returns the error,
92 otherwise returns false.
99 local $SIG{INT} = 'IGNORE';
100 local $SIG{QUIT} = 'IGNORE';
101 local $SIG{TERM} = 'IGNORE';
102 local $SIG{TSTP} = 'IGNORE';
103 local $SIG{PIPE} = 'IGNORE';
105 my $oldAutoCommit = $FS::UID::AutoCommit;
106 local $FS::UID::AutoCommit = 0;
109 my $error = $self->SUPER::insert;
111 $dbh->rollback if $oldAutoCommit;
115 foreach my $pf ( grep { /^phonetypenum(\d+)$/ && $self->get($_) =~ /\S/ }
116 keys %{ $self->hashref } ) {
117 $pf =~ /^phonetypenum(\d+)$/ or die "wtf (daily, the)";
118 my $phonetypenum = $1;
120 my $contact_phone = new FS::contact_phone {
121 'contactnum' => $self->contactnum,
122 'phonetypenum' => $phonetypenum,
123 _parse_phonestring( $self->get($pf) ),
125 $error = $contact_phone->insert;
127 $dbh->rollback if $oldAutoCommit;
132 if ( $self->get('emailaddress') =~ /\S/ ) {
134 foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) {
136 my $contact_email = new FS::contact_email {
137 'contactnum' => $self->contactnum,
138 'emailaddress' => $email,
140 $error = $contact_email->insert;
142 $dbh->rollback if $oldAutoCommit;
150 #unless ( $import || $skip_fuzzyfiles ) {
151 #warn " queueing fuzzyfiles update\n"
153 $error = $self->queue_fuzzyfiles_update;
155 $dbh->rollback if $oldAutoCommit;
156 return "updating fuzzy search cache: $error";
160 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
168 Delete this record from the database.
172 # the delete method can be inherited from FS::Record
177 local $SIG{HUP} = 'IGNORE';
178 local $SIG{INT} = 'IGNORE';
179 local $SIG{QUIT} = 'IGNORE';
180 local $SIG{TERM} = 'IGNORE';
181 local $SIG{TSTP} = 'IGNORE';
182 local $SIG{PIPE} = 'IGNORE';
184 my $oldAutoCommit = $FS::UID::AutoCommit;
185 local $FS::UID::AutoCommit = 0;
188 foreach my $object ( $self->contact_phone, $self->contact_email ) {
189 my $error = $object->delete;
191 $dbh->rollback if $oldAutoCommit;
196 my $error = $self->SUPER::delete;
198 $dbh->rollback if $oldAutoCommit;
202 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
207 =item replace OLD_RECORD
209 Replaces the OLD_RECORD with this one in the database. If there is an error,
210 returns the error, otherwise returns false.
217 local $SIG{INT} = 'IGNORE';
218 local $SIG{QUIT} = 'IGNORE';
219 local $SIG{TERM} = 'IGNORE';
220 local $SIG{TSTP} = 'IGNORE';
221 local $SIG{PIPE} = 'IGNORE';
223 my $oldAutoCommit = $FS::UID::AutoCommit;
224 local $FS::UID::AutoCommit = 0;
227 my $error = $self->SUPER::replace(@_);
229 $dbh->rollback if $oldAutoCommit;
233 foreach my $pf ( grep { /^phonetypenum(\d+)$/ && $self->get($_) }
234 keys %{ $self->hashref } ) {
235 $pf =~ /^phonetypenum(\d+)$/ or die "wtf (daily, the)";
236 my $phonetypenum = $1;
238 my %cp = ( 'contactnum' => $self->contactnum,
239 'phonetypenum' => $phonetypenum,
241 my $contact_phone = qsearchs('contact_phone', \%cp)
242 || new FS::contact_phone \%cp;
244 my %cpd = _parse_phonestring( $self->get($pf) );
245 $contact_phone->set( $_ => $cpd{$_} ) foreach keys %cpd;
247 my $method = $contact_phone->contactphonenum ? 'replace' : 'insert';
249 $error = $contact_phone->$method;
251 $dbh->rollback if $oldAutoCommit;
256 if ( defined($self->get('emailaddress')) ) {
258 #ineffecient but whatever, how many email addresses can there be?
260 foreach my $contact_email ( $self->contact_email ) {
261 my $error = $contact_email->delete;
263 $dbh->rollback if $oldAutoCommit;
268 foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) {
270 my $contact_email = new FS::contact_email {
271 'contactnum' => $self->contactnum,
272 'emailaddress' => $email,
274 $error = $contact_email->insert;
276 $dbh->rollback if $oldAutoCommit;
284 #unless ( $import || $skip_fuzzyfiles ) {
285 #warn " queueing fuzzyfiles update\n"
287 $error = $self->queue_fuzzyfiles_update;
289 $dbh->rollback if $oldAutoCommit;
290 return "updating fuzzy search cache: $error";
294 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
300 #i probably belong in contact_phone.pm
301 sub _parse_phonestring {
304 my($countrycode, $extension) = ('1', '');
307 if ( $value =~ s/^\s*\+\s*(\d+)// ) {
313 if ( $value =~ s/\s*(ext|x)\s*(\d+)\s*$//i ) {
317 ( 'countrycode' => $countrycode,
318 'phonenum' => $value,
319 'extension' => $extension,
323 =item queue_fuzzyfiles_update
325 Used by insert & replace to update the fuzzy search cache
329 use FS::cust_main::Search;
330 sub queue_fuzzyfiles_update {
333 local $SIG{HUP} = 'IGNORE';
334 local $SIG{INT} = 'IGNORE';
335 local $SIG{QUIT} = 'IGNORE';
336 local $SIG{TERM} = 'IGNORE';
337 local $SIG{TSTP} = 'IGNORE';
338 local $SIG{PIPE} = 'IGNORE';
340 my $oldAutoCommit = $FS::UID::AutoCommit;
341 local $FS::UID::AutoCommit = 0;
344 foreach my $field ( 'first', 'last' ) {
345 my $queue = new FS::queue {
346 'job' => 'FS::cust_main::Search::append_fuzzyfiles_fuzzyfield'
348 my @args = "contact.$field", $self->get($field);
349 my $error = $queue->insert( @args );
351 $dbh->rollback if $oldAutoCommit;
352 return "queueing job (transaction rolled back): $error";
356 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
363 Checks all fields to make sure this is a valid example. If there is
364 an error, returns the error, otherwise returns false. Called by the insert
369 # the check method should currently be supplied - FS::Record contains some
370 # data checking routines
376 $self->ut_numbern('contactnum')
377 || $self->ut_foreign_keyn('prospectnum', 'prospect_main', 'prospectnum')
378 || $self->ut_foreign_keyn('custnum', 'cust_main', 'custnum')
379 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
380 || $self->ut_foreign_keyn('classnum', 'contact_class', 'classnum')
381 || $self->ut_namen('last')
382 || $self->ut_namen('first')
383 || $self->ut_textn('title')
384 || $self->ut_textn('comment')
385 || $self->ut_enum('disabled', [ '', 'Y' ])
387 return $error if $error;
389 return "No prospect or customer!" unless $self->prospectnum || $self->custnum;
390 return "Prospect and customer!" if $self->prospectnum && $self->custnum;
392 return "One of first name, last name, or title must have a value"
393 if ! grep $self->$_(), qw( first last title);
400 my $data = $self->first. ' '. $self->last;
401 $data .= ', '. $self->title
403 $data .= ' ('. $self->comment. ')'
408 sub contact_classname {
410 my $contact_class = $self->contact_class or return '';
411 $contact_class->classname;
420 L<FS::Record>, schema.html from the base documentation.