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 );
7 use FS::Record qw( dbh qsearch qsearchs );
16 #started as false laziness w/cust_main/Location.pm
22 # set up accessors for location fields
26 qw( address1 address2 city county state zip country district
27 latitude longitude coord_auto censustract censusyear geocode
30 foreach my $f (@location_fields) {
31 *{"FS::prospect_main::$f"} = sub {
32 carp "WARNING: tried to set cust_main.$f with accessor" if (@_ > 1);
33 my @cust_location = shift->cust_location or return '';
34 #arbitrarily picking the first because the UI only lets you add one
42 #debugging shim--probably a performance hit, so remove this at some point
46 if ( $DEBUG and grep { $_ eq $field } @location_fields ) {
47 carp "WARNING: tried to get() location field $field";
50 $self->FS::Record::get($field);
55 FS::prospect_main - Object methods for prospect_main records
59 use FS::prospect_main;
61 $record = new FS::prospect_main \%hash;
62 $record = new FS::prospect_main { 'column' => 'value' };
64 $error = $record->insert;
66 $error = $new_record->replace($old_record);
68 $error = $record->delete;
70 $error = $record->check;
74 An FS::prospect_main object represents a prospect. FS::prospect_main inherits
75 from FS::Record. The following fields are currently supported:
85 Agent (see L<FS::agent>)
89 Referral (see L<FS::part_referral>)
103 Creates a new prospect. To add the prospect to the database, see L<"insert">.
105 Note that this stores the hash reference, not a distinct copy of the hash it
106 points to. You can ask the object for a copy with the I<hash> method.
110 sub table { 'prospect_main'; }
114 Adds this record to the database. If there is an error, returns the error,
115 otherwise returns false.
122 warn "FS::prospect_main::insert called on $self with options ".
123 join(', ', map "$_=>$options{$_}", keys %options)
126 local $SIG{HUP} = 'IGNORE';
127 local $SIG{INT} = 'IGNORE';
128 local $SIG{QUIT} = 'IGNORE';
129 local $SIG{TERM} = 'IGNORE';
130 local $SIG{TSTP} = 'IGNORE';
131 local $SIG{PIPE} = 'IGNORE';
133 my $oldAutoCommit = $FS::UID::AutoCommit;
134 local $FS::UID::AutoCommit = 0;
137 warn " inserting prospect_main record" if $DEBUG;
138 my $error = $self->SUPER::insert;
140 $dbh->rollback if $oldAutoCommit;
144 if ( $options{'cust_location'} ) {
145 warn " inserting cust_location record" if $DEBUG;
146 my $cust_location = $options{'cust_location'};
147 $cust_location->prospectnum($self->prospectnum);
148 $error = $cust_location->insert;
150 $dbh->rollback if $oldAutoCommit;
155 warn " commiting transaction" if $DEBUG;
156 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
163 Delete this record from the database.
167 #delete dangling locations?
169 =item replace OLD_RECORD
171 Replaces the OLD_RECORD with this one in the database. If there is an error,
172 returns the error, otherwise returns false.
179 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
185 warn "FS::prospect_main::replace called on $new to replace $old with options".
186 " ". join(', ', map "$_ => ". $options{$_}, keys %options)
189 local $SIG{HUP} = 'IGNORE';
190 local $SIG{INT} = 'IGNORE';
191 local $SIG{QUIT} = 'IGNORE';
192 local $SIG{TERM} = 'IGNORE';
193 local $SIG{TSTP} = 'IGNORE';
194 local $SIG{PIPE} = 'IGNORE';
196 my $oldAutoCommit = $FS::UID::AutoCommit;
197 local $FS::UID::AutoCommit = 0;
200 warn " replacing prospect_main record" if $DEBUG;
201 my $error = $new->SUPER::replace($old);
203 $dbh->rollback if $oldAutoCommit;
207 if ( $options{'cust_location'} ) {
208 my $cust_location = $options{'cust_location'};
209 $cust_location->prospectnum($new->prospectnum);
210 my $method = $cust_location->locationnum ? 'replace' : 'insert';
211 warn " ${method}ing cust_location record" if $DEBUG;
212 $error = $cust_location->$method();
214 $dbh->rollback if $oldAutoCommit;
217 } elsif ( exists($options{'cust_location'}) ) {
218 foreach my $cust_location (
219 qsearch('cust_location', { 'prospectnum' => $new->prospectnum } )
221 $error = $cust_location->delete();
223 $dbh->rollback if $oldAutoCommit;
229 warn " commiting transaction" if $DEBUG;
230 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
237 Checks all fields to make sure this is a valid prospect. If there is
238 an error, returns the error, otherwise returns false. Called by the insert
247 $self->ut_numbern('prospectnum')
248 || $self->ut_foreign_key( 'agentnum', 'agent', 'agentnum' )
249 || $self->ut_foreign_key( 'refnum', 'part_referral', 'refnum' )
250 || $self->ut_textn('company')
252 return $error if $error;
254 my $company = $self->company;
255 $company =~ s/^\s+//;
256 $company =~ s/\s+$//;
257 $company =~ s/\s+/ /g;
258 $self->company($company);
265 Returns a name for this prospect, as a string (company name for commercial
266 prospects, contact name for residential prospects).
272 return $self->company if $self->company;
274 my $contact = ($self->contact)[0]; #first contact? good enough for now
275 return $contact->line if $contact;
277 'Prospect #'. $self->prospectnum;
282 Returns the contacts (see L<FS::contact>) associated with this prospect.
288 qsearch( 'contact', { 'prospectnum' => $self->prospectnum } );
293 Returns the locations (see L<FS::cust_location>) associated with this prospect.
299 qsearch( 'cust_location', { 'prospectnum' => $self->prospectnum,
305 Returns the qualifications (see L<FS::qual>) associated with this prospect.
311 qsearch( 'qual', { 'prospectnum' => $self->prospectnum } );
316 Returns the agent (see L<FS::agent>) for this customer.
322 qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
325 =item convert_cust_main
327 Converts this prospect to a customer.
329 If there is an error, returns an error message, otherwise, returns the
330 newly-created FS::cust_main object.
334 sub convert_cust_main {
337 my @cust_location = $self->cust_location;
338 #the interface only allows one, so we're just gonna go with that for now
340 my @contact = $self->contact;
342 #XXX define one contact type as "billing", then we could pick just that one
343 my @invoicing_list = map $_->emailaddress, map $_->contact_email, @contact;
345 #XXX i'm not compatible with cust_main-require_phone (which is kind of a
346 # pre-contact thing anyway)
348 my $cust_main = new FS::cust_main {
349 'bill_location' => $cust_location[0],
350 'ship_location' => $cust_location[0],
351 ( map { $_ => $self->$_ } qw( agentnum refnum company ) ),
354 #XXX again, arbitrary, if one contact was "billing", that would be better
356 $cust_main->set($_, $contact[0]->get($_)) foreach qw( first last );
358 $cust_main->set('first', 'Unknown');
359 $cust_main->set('last', 'Unknown');
362 $cust_main->insert( {}, \@invoicing_list,
363 'prospectnum' => $self->prospectnum,
372 Returns a qsearch hash expression to search for the parameters specified in
373 HASHREF. Valid parameters are:
384 my( $class, $params ) = @_;
393 if ( $params->{'agentnum'} =~ /^(\d+)$/ and $1 ) {
395 "prospect_main.agentnum = $1";
399 # setup queries, subs, etc. for the search
402 $orderby ||= 'ORDER BY prospectnum';
404 # here is the agent virtualization
405 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
407 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
409 my $count_query = "SELECT COUNT(*) FROM prospect_main $extra_sql";
412 'table' => 'prospect_main',
413 #'select' => $select,
415 'extra_sql' => $extra_sql,
416 'order_by' => $orderby,
417 'count_query' => $count_query,
418 #'extra_headers' => \@extra_headers,
419 #'extra_fields' => \@extra_fields,
430 L<FS::Record>, schema.html from the base documentation.