autoload methods returning foreign records, RT#13971
[freeside.git] / FS / FS / contact.pm
1 package FS::contact;
2 use base qw( FS::Record );
3
4 use strict;
5 use FS::Record qw( qsearchs dbh ); # qw( qsearch qsearchs dbh );
6 use FS::contact_phone;
7
8 =head1 NAME
9
10 FS::contact - Object methods for contact records
11
12 =head1 SYNOPSIS
13
14   use FS::contact;
15
16   $record = new FS::contact \%hash;
17   $record = new FS::contact { 'column' => 'value' };
18
19   $error = $record->insert;
20
21   $error = $new_record->replace($old_record);
22
23   $error = $record->delete;
24
25   $error = $record->check;
26
27 =head1 DESCRIPTION
28
29 An FS::contact object represents an example.  FS::contact inherits from
30 FS::Record.  The following fields are currently supported:
31
32 =over 4
33
34 =item contactnum
35
36 primary key
37
38 =item prospectnum
39
40 prospectnum
41
42 =item custnum
43
44 custnum
45
46 =item locationnum
47
48 locationnum
49
50 =item last
51
52 last
53
54 =item first
55
56 first
57
58 =item title
59
60 title
61
62 =item comment
63
64 comment
65
66 =item selfservice_access
67
68 empty or Y
69
70 =item _password
71
72 =item _password_encoding
73
74 empty or bcrypt
75
76 =item disabled
77
78 disabled
79
80
81 =back
82
83 =head1 METHODS
84
85 =over 4
86
87 =item new HASHREF
88
89 Creates a new example.  To add the example to the database, see L<"insert">.
90
91 Note that this stores the hash reference, not a distinct copy of the hash it
92 points to.  You can ask the object for a copy with the I<hash> method.
93
94 =cut
95
96 # the new method can be inherited from FS::Record, if a table method is defined
97
98 sub table { 'contact'; }
99
100 =item insert
101
102 Adds this record to the database.  If there is an error, returns the error,
103 otherwise returns false.
104
105 =cut
106
107 sub insert {
108   my $self = shift;
109
110   local $SIG{INT} = 'IGNORE';
111   local $SIG{QUIT} = 'IGNORE';
112   local $SIG{TERM} = 'IGNORE';
113   local $SIG{TSTP} = 'IGNORE';
114   local $SIG{PIPE} = 'IGNORE';
115
116   my $oldAutoCommit = $FS::UID::AutoCommit;
117   local $FS::UID::AutoCommit = 0;
118   my $dbh = dbh;
119
120   my $error = $self->SUPER::insert;
121   if ( $error ) {
122     $dbh->rollback if $oldAutoCommit;
123     return $error;
124   }
125
126   foreach my $pf ( grep { /^phonetypenum(\d+)$/ && $self->get($_) =~ /\S/ }
127                         keys %{ $self->hashref } ) {
128     $pf =~ /^phonetypenum(\d+)$/ or die "wtf (daily, the)";
129     my $phonetypenum = $1;
130
131     my $contact_phone = new FS::contact_phone {
132       'contactnum' => $self->contactnum,
133       'phonetypenum' => $phonetypenum,
134       _parse_phonestring( $self->get($pf) ),
135     };
136     $error = $contact_phone->insert;
137     if ( $error ) {
138       $dbh->rollback if $oldAutoCommit;
139       return $error;
140     }
141   }
142
143   if ( $self->get('emailaddress') =~ /\S/ ) {
144
145     foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) {
146  
147       my $contact_email = new FS::contact_email {
148         'contactnum'   => $self->contactnum,
149         'emailaddress' => $email,
150       };
151       $error = $contact_email->insert;
152       if ( $error ) {
153         $dbh->rollback if $oldAutoCommit;
154         return $error;
155       }
156
157     }
158
159   }
160
161   #unless ( $import || $skip_fuzzyfiles ) {
162     #warn "  queueing fuzzyfiles update\n"
163     #  if $DEBUG > 1;
164     $error = $self->queue_fuzzyfiles_update;
165     if ( $error ) {
166       $dbh->rollback if $oldAutoCommit;
167       return "updating fuzzy search cache: $error";
168     }
169   #}
170
171   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
172
173   '';
174
175 }
176
177 =item delete
178
179 Delete this record from the database.
180
181 =cut
182
183 # the delete method can be inherited from FS::Record
184
185 sub delete {
186   my $self = shift;
187
188   local $SIG{HUP} = 'IGNORE';
189   local $SIG{INT} = 'IGNORE';
190   local $SIG{QUIT} = 'IGNORE';
191   local $SIG{TERM} = 'IGNORE';
192   local $SIG{TSTP} = 'IGNORE';
193   local $SIG{PIPE} = 'IGNORE';
194
195   my $oldAutoCommit = $FS::UID::AutoCommit;
196   local $FS::UID::AutoCommit = 0;
197   my $dbh = dbh;
198
199   foreach my $object ( $self->contact_phone, $self->contact_email ) {
200     my $error = $object->delete;
201     if ( $error ) {
202       $dbh->rollback if $oldAutoCommit;
203       return $error;
204     }
205   }
206
207   my $error = $self->SUPER::delete;
208   if ( $error ) {
209     $dbh->rollback if $oldAutoCommit;
210     return $error;
211   }
212
213   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
214   '';
215
216 }
217
218 =item replace OLD_RECORD
219
220 Replaces the OLD_RECORD with this one in the database.  If there is an error,
221 returns the error, otherwise returns false.
222
223 =cut
224
225 sub replace {
226   my $self = shift;
227
228   local $SIG{INT} = 'IGNORE';
229   local $SIG{QUIT} = 'IGNORE';
230   local $SIG{TERM} = 'IGNORE';
231   local $SIG{TSTP} = 'IGNORE';
232   local $SIG{PIPE} = 'IGNORE';
233
234   my $oldAutoCommit = $FS::UID::AutoCommit;
235   local $FS::UID::AutoCommit = 0;
236   my $dbh = dbh;
237
238   my $error = $self->SUPER::replace(@_);
239   if ( $error ) {
240     $dbh->rollback if $oldAutoCommit;
241     return $error;
242   }
243
244   foreach my $pf ( grep { /^phonetypenum(\d+)$/ && $self->get($_) }
245                         keys %{ $self->hashref } ) {
246     $pf =~ /^phonetypenum(\d+)$/ or die "wtf (daily, the)";
247     my $phonetypenum = $1;
248
249     my %cp = ( 'contactnum'   => $self->contactnum,
250                'phonetypenum' => $phonetypenum,
251              );
252     my $contact_phone = qsearchs('contact_phone', \%cp)
253                         || new FS::contact_phone   \%cp;
254
255     my %cpd = _parse_phonestring( $self->get($pf) );
256     $contact_phone->set( $_ => $cpd{$_} ) foreach keys %cpd;
257
258     my $method = $contact_phone->contactphonenum ? 'replace' : 'insert';
259
260     $error = $contact_phone->$method;
261     if ( $error ) {
262       $dbh->rollback if $oldAutoCommit;
263       return $error;
264     }
265   }
266
267   if ( defined($self->get('emailaddress')) ) {
268
269     #ineffecient but whatever, how many email addresses can there be?
270
271     foreach my $contact_email ( $self->contact_email ) {
272       my $error = $contact_email->delete;
273       if ( $error ) {
274         $dbh->rollback if $oldAutoCommit;
275         return $error;
276       }
277     }
278
279     foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) {
280  
281       my $contact_email = new FS::contact_email {
282         'contactnum'   => $self->contactnum,
283         'emailaddress' => $email,
284       };
285       $error = $contact_email->insert;
286       if ( $error ) {
287         $dbh->rollback if $oldAutoCommit;
288         return $error;
289       }
290
291     }
292
293   }
294
295   #unless ( $import || $skip_fuzzyfiles ) {
296     #warn "  queueing fuzzyfiles update\n"
297     #  if $DEBUG > 1;
298     $error = $self->queue_fuzzyfiles_update;
299     if ( $error ) {
300       $dbh->rollback if $oldAutoCommit;
301       return "updating fuzzy search cache: $error";
302     }
303   #}
304
305   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
306
307   '';
308
309 }
310
311 #i probably belong in contact_phone.pm
312 sub _parse_phonestring {
313   my $value = shift;
314
315   my($countrycode, $extension) = ('1', '');
316
317   #countrycode
318   if ( $value =~ s/^\s*\+\s*(\d+)// ) {
319     $countrycode = $1;
320   } else {
321     $value =~ s/^\s*1//;
322   }
323   #extension
324   if ( $value =~ s/\s*(ext|x)\s*(\d+)\s*$//i ) {
325      $extension = $2;
326   }
327
328   ( 'countrycode' => $countrycode,
329     'phonenum'    => $value,
330     'extension'   => $extension,
331   );
332 }
333
334 =item queue_fuzzyfiles_update
335
336 Used by insert & replace to update the fuzzy search cache
337
338 =cut
339
340 use FS::cust_main::Search;
341 sub queue_fuzzyfiles_update {
342   my $self = shift;
343
344   local $SIG{HUP} = 'IGNORE';
345   local $SIG{INT} = 'IGNORE';
346   local $SIG{QUIT} = 'IGNORE';
347   local $SIG{TERM} = 'IGNORE';
348   local $SIG{TSTP} = 'IGNORE';
349   local $SIG{PIPE} = 'IGNORE';
350
351   my $oldAutoCommit = $FS::UID::AutoCommit;
352   local $FS::UID::AutoCommit = 0;
353   my $dbh = dbh;
354
355   foreach my $field ( 'first', 'last' ) {
356     my $queue = new FS::queue { 
357       'job' => 'FS::cust_main::Search::append_fuzzyfiles_fuzzyfield'
358     };
359     my @args = "contact.$field", $self->get($field);
360     my $error = $queue->insert( @args );
361     if ( $error ) {
362       $dbh->rollback if $oldAutoCommit;
363       return "queueing job (transaction rolled back): $error";
364     }
365   }
366
367   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
368   '';
369
370 }
371
372 =item check
373
374 Checks all fields to make sure this is a valid example.  If there is
375 an error, returns the error, otherwise returns false.  Called by the insert
376 and replace methods.
377
378 =cut
379
380 # the check method should currently be supplied - FS::Record contains some
381 # data checking routines
382
383 sub check {
384   my $self = shift;
385
386   my $error = 
387     $self->ut_numbern('contactnum')
388     || $self->ut_foreign_keyn('prospectnum', 'prospect_main', 'prospectnum')
389     || $self->ut_foreign_keyn('custnum',     'cust_main',     'custnum')
390     || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
391     || $self->ut_foreign_keyn('classnum',    'contact_class', 'classnum')
392     || $self->ut_namen('last')
393     || $self->ut_namen('first')
394     || $self->ut_textn('title')
395     || $self->ut_textn('comment')
396     || $self->ut_enum('selfservice_access', [ '', 'Y' ])
397     || $self->ut_textn('_password')
398     || $self->ut_enum('_password_encoding', [ '', 'bcrypt'])
399     || $self->ut_enum('disabled', [ '', 'Y' ])
400   ;
401   return $error if $error;
402
403   return "No prospect or customer!" unless $self->prospectnum || $self->custnum;
404   return "Prospect and customer!"       if $self->prospectnum && $self->custnum;
405
406   return "One of first name, last name, or title must have a value"
407     if ! grep $self->$_(), qw( first last title);
408
409   $self->SUPER::check;
410 }
411
412 sub line {
413   my $self = shift;
414   my $data = $self->first. ' '. $self->last;
415   $data .= ', '. $self->title
416     if $self->title;
417   $data .= ' ('. $self->comment. ')'
418     if $self->comment;
419   $data;
420 }
421
422 sub contact_classname {
423   my $self = shift;
424   my $contact_class = $self->contact_class or return '';
425   $contact_class->classname;
426 }
427
428 =back
429
430 =head1 BUGS
431
432 =head1 SEE ALSO
433
434 L<FS::Record>, schema.html from the base documentation.
435
436 =cut
437
438 1;
439