1 package FS::prospect_main;
2 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 );
14 #started as false laziness w/cust_main/Location.pm
20 # set up accessors for location fields
24 qw( address1 address2 city county state zip country district
25 latitude longitude coord_auto censustract censusyear geocode
28 foreach my $f (@location_fields) {
29 *{"FS::prospect_main::$f"} = sub {
30 carp "WARNING: tried to set cust_main.$f with accessor" if (@_ > 1);
31 my @cust_location = shift->cust_location or return '';
32 #arbitrarily picking the first because the UI only lets you add one
40 #debugging shim--probably a performance hit, so remove this at some point
44 if ( $DEBUG and grep { $_ eq $field } @location_fields ) {
45 carp "WARNING: tried to get() location field $field";
48 $self->FS::Record::get($field);
53 FS::prospect_main - Object methods for prospect_main records
57 use FS::prospect_main;
59 $record = new FS::prospect_main \%hash;
60 $record = new FS::prospect_main { 'column' => 'value' };
62 $error = $record->insert;
64 $error = $new_record->replace($old_record);
66 $error = $record->delete;
68 $error = $record->check;
72 An FS::prospect_main object represents a prospect. FS::prospect_main inherits
73 from FS::Record. The following fields are currently supported:
83 Agent (see L<FS::agent>)
87 Referral (see L<FS::part_referral>)
101 Creates a new prospect. To add the prospect to the database, see L<"insert">.
103 Note that this stores the hash reference, not a distinct copy of the hash it
104 points to. You can ask the object for a copy with the I<hash> method.
108 sub table { 'prospect_main'; }
112 Adds this record to the database. If there is an error, returns the error,
113 otherwise returns false.
120 warn "FS::prospect_main::insert called on $self with options ".
121 join(', ', map "$_=>$options{$_}", keys %options)
124 local $SIG{HUP} = 'IGNORE';
125 local $SIG{INT} = 'IGNORE';
126 local $SIG{QUIT} = 'IGNORE';
127 local $SIG{TERM} = 'IGNORE';
128 local $SIG{TSTP} = 'IGNORE';
129 local $SIG{PIPE} = 'IGNORE';
131 my $oldAutoCommit = $FS::UID::AutoCommit;
132 local $FS::UID::AutoCommit = 0;
135 warn " inserting prospect_main record" if $DEBUG;
136 my $error = $self->SUPER::insert;
138 $dbh->rollback if $oldAutoCommit;
142 if ( $options{'cust_location'} ) {
143 warn " inserting cust_location record" if $DEBUG;
144 my $cust_location = $options{'cust_location'};
145 $cust_location->prospectnum($self->prospectnum);
146 $error = $cust_location->insert;
148 $dbh->rollback if $oldAutoCommit;
153 warn " commiting transaction" if $DEBUG;
154 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
161 Delete this record from the database.
165 #delete dangling locations?
167 =item replace OLD_RECORD
169 Replaces the OLD_RECORD with this one in the database. If there is an error,
170 returns the error, otherwise returns false.
177 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
183 warn "FS::prospect_main::replace called on $new to replace $old with options".
184 " ". join(', ', map "$_ => ". $options{$_}, keys %options)
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 warn " replacing prospect_main record" if $DEBUG;
199 my $error = $new->SUPER::replace($old);
201 $dbh->rollback if $oldAutoCommit;
205 if ( $options{'cust_location'} ) {
206 my $cust_location = $options{'cust_location'};
207 $cust_location->prospectnum($new->prospectnum);
208 my $method = $cust_location->locationnum ? 'replace' : 'insert';
209 warn " ${method}ing cust_location record" if $DEBUG;
210 $error = $cust_location->$method();
212 $dbh->rollback if $oldAutoCommit;
215 } elsif ( exists($options{'cust_location'}) ) {
216 foreach my $cust_location (
217 qsearch('cust_location', { 'prospectnum' => $new->prospectnum } )
219 $error = $cust_location->delete();
221 $dbh->rollback if $oldAutoCommit;
227 warn " commiting transaction" if $DEBUG;
228 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
235 Checks all fields to make sure this is a valid prospect. If there is
236 an error, returns the error, otherwise returns false. Called by the insert
245 $self->ut_numbern('prospectnum')
246 || $self->ut_foreign_key( 'agentnum', 'agent', 'agentnum' )
247 || $self->ut_foreign_keyn( 'refnum', 'part_referral', 'refnum' )
248 || $self->ut_textn('company')
249 || $self->ut_foreign_keyn( 'taxstatusnum', 'tax_status', 'taxstatusnum' )
251 return $error if $error;
253 my $company = $self->company;
254 $company =~ s/^\s+//;
255 $company =~ s/\s+$//;
256 $company =~ s/\s+/ /g;
257 $self->company($company);
264 Returns a name for this prospect, as a string (company name for commercial
265 prospects, contact name for residential prospects).
271 return $self->company if $self->company;
273 my $contact = ($self->prospect_contact)[0]->contact; #first contact? good enough for now
274 return $contact->line if $contact;
276 'Prospect #'. $self->prospectnum;
281 Returns the contacts (see L<FS::contact>) associated with this prospect.
285 Returns the locations (see L<FS::cust_location>) associated with this prospect.
292 'table' => 'cust_location',
293 'hashref' => { 'prospectnum' => $self->prospectnum,
296 'order_by' => 'ORDER BY country, LOWER(state), LOWER(city), LOWER(county), LOWER(address1), LOWER(address2)',
302 Returns the qualifications (see L<FS::qual>) associated with this prospect.
306 Returns the agent (see L<FS::agent>) for this customer.
310 Returns the external tax status, as an FS::tax_status object, or the empty
311 string if there is no tax status.
317 if ( $self->taxstatusnum ) {
318 qsearchs('tax_status', { 'taxstatusnum' => $self->taxstatusnum } );
326 Returns the tax status code if there is one.
332 my $tax_status = $self->tax_status;
334 ? $tax_status->taxstatus
338 =item convert_cust_main
340 Converts this prospect to a customer.
342 If there is an error, returns an error message, otherwise, returns the
343 newly-created FS::cust_main object.
347 sub convert_cust_main {
350 my @cust_location = $self->cust_location;
351 #the interface only allows one, so we're just gonna go with that for now
353 my @contact = map $_->contact, $self->prospect_contact;
355 #XXX define one contact type as "billing", then we could pick just that one
356 my @invoicing_list = map $_->emailaddress, map $_->contact_email, @contact;
358 #XXX i'm not compatible with cust_main-require_phone (which is kind of a
359 # pre-contact thing anyway)
361 my $cust_main = new FS::cust_main {
362 'bill_location' => $cust_location[0],
363 'ship_location' => $cust_location[0],
364 ( map { $_ => $self->$_ } qw( agentnum refnum company taxstatusnum ) ),
367 $cust_main->refnum( FS::Conf->new->config('referraldefault') || 1 )
368 unless $cust_main->refnum;
370 #XXX again, arbitrary, if one contact was "billing", that would be better
372 $cust_main->set($_, $contact[0]->get($_)) foreach qw( first last );
374 $cust_main->set('first', 'Unknown');
375 $cust_main->set('last', 'Unknown');
378 #v3 payby no longer allowed
379 #$cust_main->payby('BILL');
380 #$cust_main->paydate('12/2037');
382 $cust_main->insert( {}, \@invoicing_list,
383 'prospectnum' => $self->prospectnum,
392 Returns a qsearch hash expression to search for the parameters specified in
393 HASHREF. Valid parameters are:
404 my( $class, $params ) = @_;
413 if ( $params->{'agentnum'} =~ /^(\d+)$/ and $1 ) {
415 "prospect_main.agentnum = $1";
419 # setup queries, subs, etc. for the search
422 $orderby ||= 'ORDER BY prospectnum';
424 # here is the agent virtualization
425 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
427 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
429 my $count_query = "SELECT COUNT(*) FROM prospect_main $extra_sql";
432 'table' => 'prospect_main',
433 #'select' => $select,
435 'extra_sql' => $extra_sql,
436 'order_by' => $orderby,
437 'count_query' => $count_query,
438 #'extra_headers' => \@extra_headers,
439 #'extra_fields' => \@extra_fields,
444 # stub this so that calling ->cust_bill doesn't return an empty string
449 # XXX should have real localization here eventually
451 FS::Conf->new->config('locale');
460 L<FS::Record>, schema.html from the base documentation.