1 package FS::prospect_main;
4 use base qw( FS::Quotable_Mixin FS::o2m_Common FS::Record );
5 use vars qw( $DEBUG @location_fields );
6 use Scalar::Util qw( blessed );
8 use FS::Record qw( dbh qsearch qsearchs );
10 use FS::cust_location;
17 #started as false laziness w/cust_main/Location.pm
23 # set up accessors for location fields
27 qw( address1 address2 city county state zip country district
28 latitude longitude coord_auto censustract censusyear geocode
31 foreach my $f (@location_fields) {
32 *{"FS::prospect_main::$f"} = sub {
33 carp "WARNING: tried to set cust_main.$f with accessor" if (@_ > 1);
34 my @cust_location = shift->cust_location or return '';
35 #arbitrarily picking the first because the UI only lets you add one
43 #debugging shim--probably a performance hit, so remove this at some point
47 if ( $DEBUG and grep { $_ eq $field } @location_fields ) {
48 carp "WARNING: tried to get() location field $field";
51 $self->FS::Record::get($field);
56 FS::prospect_main - Object methods for prospect_main records
60 use FS::prospect_main;
62 $record = new FS::prospect_main \%hash;
63 $record = new FS::prospect_main { 'column' => 'value' };
65 $error = $record->insert;
67 $error = $new_record->replace($old_record);
69 $error = $record->delete;
71 $error = $record->check;
75 An FS::prospect_main object represents a prospect. FS::prospect_main inherits
76 from FS::Record. The following fields are currently supported:
86 Agent (see L<FS::agent>)
90 Referral (see L<FS::part_referral>)
104 Creates a new prospect. To add the prospect to the database, see L<"insert">.
106 Note that this stores the hash reference, not a distinct copy of the hash it
107 points to. You can ask the object for a copy with the I<hash> method.
111 sub table { 'prospect_main'; }
115 Adds this record to the database. If there is an error, returns the error,
116 otherwise returns false.
123 warn "FS::prospect_main::insert called on $self with options ".
124 join(', ', map "$_=>$options{$_}", keys %options)
127 local $SIG{HUP} = 'IGNORE';
128 local $SIG{INT} = 'IGNORE';
129 local $SIG{QUIT} = 'IGNORE';
130 local $SIG{TERM} = 'IGNORE';
131 local $SIG{TSTP} = 'IGNORE';
132 local $SIG{PIPE} = 'IGNORE';
134 my $oldAutoCommit = $FS::UID::AutoCommit;
135 local $FS::UID::AutoCommit = 0;
138 warn " inserting prospect_main record" if $DEBUG;
139 my $error = $self->SUPER::insert;
141 $dbh->rollback if $oldAutoCommit;
145 if ( $options{'cust_location'} ) {
146 warn " inserting cust_location record" if $DEBUG;
147 my $cust_location = $options{'cust_location'};
148 $cust_location->prospectnum($self->prospectnum);
149 $error = $cust_location->insert;
151 $dbh->rollback if $oldAutoCommit;
156 warn " commiting transaction" if $DEBUG;
157 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
164 Delete this record from the database.
168 #delete dangling locations?
170 =item replace OLD_RECORD
172 Replaces the OLD_RECORD with this one in the database. If there is an error,
173 returns the error, otherwise returns false.
180 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
186 warn "FS::prospect_main::replace called on $new to replace $old with options".
187 " ". join(', ', map "$_ => ". $options{$_}, keys %options)
190 local $SIG{HUP} = 'IGNORE';
191 local $SIG{INT} = 'IGNORE';
192 local $SIG{QUIT} = 'IGNORE';
193 local $SIG{TERM} = 'IGNORE';
194 local $SIG{TSTP} = 'IGNORE';
195 local $SIG{PIPE} = 'IGNORE';
197 my $oldAutoCommit = $FS::UID::AutoCommit;
198 local $FS::UID::AutoCommit = 0;
201 warn " replacing prospect_main record" if $DEBUG;
202 my $error = $new->SUPER::replace($old);
204 $dbh->rollback if $oldAutoCommit;
208 if ( $options{'cust_location'} ) {
209 my $cust_location = $options{'cust_location'};
210 $cust_location->prospectnum($new->prospectnum);
211 my $method = $cust_location->locationnum ? 'replace' : 'insert';
212 warn " ${method}ing cust_location record" if $DEBUG;
213 $error = $cust_location->$method();
215 $dbh->rollback if $oldAutoCommit;
218 } elsif ( exists($options{'cust_location'}) ) {
219 foreach my $cust_location (
220 qsearch('cust_location', { 'prospectnum' => $new->prospectnum } )
222 $error = $cust_location->delete();
224 $dbh->rollback if $oldAutoCommit;
230 warn " commiting transaction" if $DEBUG;
231 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
238 Checks all fields to make sure this is a valid prospect. If there is
239 an error, returns the error, otherwise returns false. Called by the insert
248 $self->ut_numbern('prospectnum')
249 || $self->ut_foreign_key( 'agentnum', 'agent', 'agentnum' )
250 || $self->ut_foreign_key( 'refnum', 'part_referral', 'refnum' )
251 || $self->ut_textn('company')
253 return $error if $error;
255 my $company = $self->company;
256 $company =~ s/^\s+//;
257 $company =~ s/\s+$//;
258 $company =~ s/\s+/ /g;
259 $self->company($company);
266 Returns a name for this prospect, as a string (company name for commercial
267 prospects, contact name for residential prospects).
273 return $self->company if $self->company;
275 my $contact = ($self->contact)[0]; #first contact? good enough for now
276 return $contact->line if $contact;
278 'Prospect #'. $self->prospectnum;
283 Returns the contacts (see L<FS::contact>) associated with this prospect.
289 qsearch( 'contact', { 'prospectnum' => $self->prospectnum } );
294 Returns the locations (see L<FS::cust_location>) associated with this prospect.
300 qsearch( 'cust_location', { 'prospectnum' => $self->prospectnum,
306 Returns the qualifications (see L<FS::qual>) associated with this prospect.
312 qsearch( 'qual', { 'prospectnum' => $self->prospectnum } );
317 Returns the agent (see L<FS::agent>) for this customer.
323 qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
326 =item convert_cust_main
328 Converts this prospect to a customer.
330 If there is an error, returns an error message, otherwise, returns the
331 newly-created FS::cust_main object.
335 sub convert_cust_main {
338 my @cust_location = $self->cust_location;
339 #the interface only allows one, so we're just gonna go with that for now
341 my @contact = $self->contact;
343 #XXX define one contact type as "billing", then we could pick just that one
344 my @invoicing_list = map $_->emailaddress, map $_->contact_email, @contact;
346 #XXX i'm not compatible with cust_main-require_phone (which is kind of a
347 # pre-contact thing anyway)
349 my $cust_main = new FS::cust_main {
350 'bill_location' => $cust_location[0],
351 'ship_location' => $cust_location[0],
352 ( map { $_ => $self->$_ } qw( agentnum refnum company ) ),
355 $cust_main->refnum( FS::Conf->new->config('referraldefault') || 1 )
356 unless $cust_main->refnum;
358 #XXX again, arbitrary, if one contact was "billing", that would be better
360 $cust_main->set($_, $contact[0]->get($_)) foreach qw( first last );
362 $cust_main->set('first', 'Unknown');
363 $cust_main->set('last', 'Unknown');
367 $cust_main->payby('BILL');
368 $cust_main->paydate('12/2037');
370 $cust_main->insert( {}, \@invoicing_list,
371 'prospectnum' => $self->prospectnum,
380 Returns a qsearch hash expression to search for the parameters specified in
381 HASHREF. Valid parameters are:
392 my( $class, $params ) = @_;
401 if ( $params->{'agentnum'} =~ /^(\d+)$/ and $1 ) {
403 "prospect_main.agentnum = $1";
407 # setup queries, subs, etc. for the search
410 $orderby ||= 'ORDER BY prospectnum';
412 # here is the agent virtualization
413 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
415 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
417 my $count_query = "SELECT COUNT(*) FROM prospect_main $extra_sql";
420 'table' => 'prospect_main',
421 #'select' => $select,
423 'extra_sql' => $extra_sql,
424 'order_by' => $orderby,
425 'count_query' => $count_query,
426 #'extra_headers' => \@extra_headers,
427 #'extra_fields' => \@extra_fields,
438 L<FS::Record>, schema.html from the base documentation.