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 );
7 use FS::Record qw( dbh qsearch ); # qsearchs );
13 #started as false laziness w/cust_main/Location.pm
19 # set up accessors for location fields
23 qw( address1 address2 city county state zip country district
24 latitude longitude coord_auto censustract censusyear geocode
27 foreach my $f (@location_fields) {
28 *{"FS::prospect_main::$f"} = sub {
29 carp "WARNING: tried to set cust_main.$f with accessor" if (@_ > 1);
30 my @cust_location = shift->cust_location or return '';
31 #arbitrarily picking the first because the UI only lets you add one
39 #debugging shim--probably a performance hit, so remove this at some point
43 if ( $DEBUG and grep { $_ eq $field } @location_fields ) {
44 carp "WARNING: tried to get() location field $field";
47 $self->FS::Record::get($field);
52 FS::prospect_main - Object methods for prospect_main records
56 use FS::prospect_main;
58 $record = new FS::prospect_main \%hash;
59 $record = new FS::prospect_main { 'column' => 'value' };
61 $error = $record->insert;
63 $error = $new_record->replace($old_record);
65 $error = $record->delete;
67 $error = $record->check;
71 An FS::prospect_main object represents a prospect. FS::prospect_main inherits
72 from FS::Record. The following fields are currently supported:
82 Agent (see L<FS::agent>)
86 Referral (see L<FS::part_referral>)
100 Creates a new prospect. To add the prospect to the database, see L<"insert">.
102 Note that this stores the hash reference, not a distinct copy of the hash it
103 points to. You can ask the object for a copy with the I<hash> method.
107 sub table { 'prospect_main'; }
111 Adds this record to the database. If there is an error, returns the error,
112 otherwise returns false.
119 warn "FS::prospect_main::insert called on $self with options ".
120 join(', ', map "$_=>$options{$_}", keys %options)
123 local $SIG{HUP} = 'IGNORE';
124 local $SIG{INT} = 'IGNORE';
125 local $SIG{QUIT} = 'IGNORE';
126 local $SIG{TERM} = 'IGNORE';
127 local $SIG{TSTP} = 'IGNORE';
128 local $SIG{PIPE} = 'IGNORE';
130 my $oldAutoCommit = $FS::UID::AutoCommit;
131 local $FS::UID::AutoCommit = 0;
134 warn " inserting prospect_main record" if $DEBUG;
135 my $error = $self->SUPER::insert;
137 $dbh->rollback if $oldAutoCommit;
141 if ( $options{'cust_location'} ) {
142 warn " inserting cust_location record" if $DEBUG;
143 my $cust_location = $options{'cust_location'};
144 $cust_location->prospectnum($self->prospectnum);
145 $error = $cust_location->insert;
147 $dbh->rollback if $oldAutoCommit;
152 warn " commiting transaction" if $DEBUG;
153 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
160 Delete this record from the database.
164 #delete dangling locations?
166 =item replace OLD_RECORD
168 Replaces the OLD_RECORD with this one in the database. If there is an error,
169 returns the error, otherwise returns false.
176 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
182 warn "FS::prospect_main::replace called on $new to replace $old with options".
183 " ". join(', ', map "$_ => ". $options{$_}, keys %options)
186 local $SIG{HUP} = 'IGNORE';
187 local $SIG{INT} = 'IGNORE';
188 local $SIG{QUIT} = 'IGNORE';
189 local $SIG{TERM} = 'IGNORE';
190 local $SIG{TSTP} = 'IGNORE';
191 local $SIG{PIPE} = 'IGNORE';
193 my $oldAutoCommit = $FS::UID::AutoCommit;
194 local $FS::UID::AutoCommit = 0;
197 warn " replacing prospect_main record" if $DEBUG;
198 my $error = $new->SUPER::replace($old);
200 $dbh->rollback if $oldAutoCommit;
204 if ( $options{'cust_location'} ) {
205 my $cust_location = $options{'cust_location'};
206 $cust_location->prospectnum($new->prospectnum);
207 my $method = $cust_location->locationnum ? 'replace' : 'insert';
208 warn " ${method}ing cust_location record" if $DEBUG;
209 $error = $cust_location->$method();
211 $dbh->rollback if $oldAutoCommit;
214 } elsif ( exists($options{'cust_location'}) ) {
215 foreach my $cust_location (
216 qsearch('cust_location', { 'prospectnum' => $new->prospectnum } )
218 $error = $cust_location->delete();
220 $dbh->rollback if $oldAutoCommit;
226 warn " commiting transaction" if $DEBUG;
227 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
234 Checks all fields to make sure this is a valid prospect. If there is
235 an error, returns the error, otherwise returns false. Called by the insert
244 $self->ut_numbern('prospectnum')
245 || $self->ut_foreign_key( 'agentnum', 'agent', 'agentnum' )
246 || $self->ut_foreign_key( 'refnum', 'part_referral', 'refnum' )
247 || $self->ut_textn('company')
249 return $error if $error;
251 my $company = $self->company;
252 $company =~ s/^\s+//;
253 $company =~ s/\s+$//;
254 $company =~ s/\s+/ /g;
255 $self->company($company);
262 Returns a name for this prospect, as a string (company name for commercial
263 prospects, contact name for residential prospects).
269 return $self->company if $self->company;
271 my $contact = ($self->contact)[0]; #first contact? good enough for now
272 return $contact->line if $contact;
274 'Prospect #'. $self->prospectnum;
279 Returns the contacts (see L<FS::contact>) associated with this prospect.
283 Returns the locations (see L<FS::cust_location>) associated with this prospect.
289 qsearch( 'cust_location', { 'prospectnum' => $self->prospectnum,
295 Returns the qualifications (see L<FS::qual>) associated with this prospect.
299 Returns the agent (see L<FS::agent>) for this customer.
301 =item convert_cust_main
303 Converts this prospect to a customer.
305 If there is an error, returns an error message, otherwise, returns the
306 newly-created FS::cust_main object.
310 sub convert_cust_main {
313 my @cust_location = $self->cust_location;
314 #the interface only allows one, so we're just gonna go with that for now
316 my @contact = $self->contact;
318 #XXX define one contact type as "billing", then we could pick just that one
319 my @invoicing_list = map $_->emailaddress, map $_->contact_email, @contact;
321 #XXX i'm not compatible with cust_main-require_phone (which is kind of a
322 # pre-contact thing anyway)
324 my $cust_main = new FS::cust_main {
325 'bill_location' => $cust_location[0],
326 'ship_location' => $cust_location[0],
327 ( map { $_ => $self->$_ } qw( agentnum refnum company ) ),
330 #XXX again, arbitrary, if one contact was "billing", that would be better
332 $cust_main->set($_, $contact[0]->get($_)) foreach qw( first last );
334 $cust_main->set('first', 'Unknown');
335 $cust_main->set('last', 'Unknown');
338 $cust_main->insert( {}, \@invoicing_list,
339 'prospectnum' => $self->prospectnum,
348 Returns a qsearch hash expression to search for the parameters specified in
349 HASHREF. Valid parameters are:
360 my( $class, $params ) = @_;
369 if ( $params->{'agentnum'} =~ /^(\d+)$/ and $1 ) {
371 "prospect_main.agentnum = $1";
375 # setup queries, subs, etc. for the search
378 $orderby ||= 'ORDER BY prospectnum';
380 # here is the agent virtualization
381 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
383 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
385 my $count_query = "SELECT COUNT(*) FROM prospect_main $extra_sql";
388 'table' => 'prospect_main',
389 #'select' => $select,
391 'extra_sql' => $extra_sql,
392 'order_by' => $orderby,
393 'count_query' => $count_query,
394 #'extra_headers' => \@extra_headers,
395 #'extra_fields' => \@extra_fields,
406 L<FS::Record>, schema.html from the base documentation.