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 );
15 #started as false laziness w/cust_main/Location.pm
21 # set up accessors for location fields
25 qw( address1 address2 city county state zip country district
26 latitude longitude coord_auto censustract censusyear geocode
29 foreach my $f (@location_fields) {
30 *{"FS::prospect_main::$f"} = sub {
31 carp "WARNING: tried to set cust_main.$f with accessor" if (@_ > 1);
32 my @cust_location = shift->cust_location or return '';
33 #arbitrarily picking the first because the UI only lets you add one
41 #debugging shim--probably a performance hit, so remove this at some point
45 if ( $DEBUG and grep { $_ eq $field } @location_fields ) {
46 carp "WARNING: tried to get() location field $field";
49 $self->FS::Record::get($field);
54 FS::prospect_main - Object methods for prospect_main records
58 use FS::prospect_main;
60 $record = new FS::prospect_main \%hash;
61 $record = new FS::prospect_main { 'column' => 'value' };
63 $error = $record->insert;
65 $error = $new_record->replace($old_record);
67 $error = $record->delete;
69 $error = $record->check;
73 An FS::prospect_main object represents a prospect. FS::prospect_main inherits
74 from FS::Record. The following fields are currently supported:
98 Creates a new prospect. To add the prospect to the database, see L<"insert">.
100 Note that this stores the hash reference, not a distinct copy of the hash it
101 points to. You can ask the object for a copy with the I<hash> method.
105 sub table { 'prospect_main'; }
109 Adds this record to the database. If there is an error, returns the error,
110 otherwise returns false.
117 warn "FS::prospect_main::insert called on $self with options ".
118 join(', ', map "$_=>$options{$_}", keys %options)
121 local $SIG{HUP} = 'IGNORE';
122 local $SIG{INT} = 'IGNORE';
123 local $SIG{QUIT} = 'IGNORE';
124 local $SIG{TERM} = 'IGNORE';
125 local $SIG{TSTP} = 'IGNORE';
126 local $SIG{PIPE} = 'IGNORE';
128 my $oldAutoCommit = $FS::UID::AutoCommit;
129 local $FS::UID::AutoCommit = 0;
132 warn " inserting prospect_main record" if $DEBUG;
133 my $error = $self->SUPER::insert;
135 $dbh->rollback if $oldAutoCommit;
139 if ( $options{'cust_location'} ) {
140 warn " inserting cust_location record" if $DEBUG;
141 my $cust_location = $options{'cust_location'};
142 $cust_location->prospectnum($self->prospectnum);
143 $error = $cust_location->insert;
145 $dbh->rollback if $oldAutoCommit;
150 warn " commiting transaction" if $DEBUG;
151 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
158 Delete this record from the database.
162 #delete dangling locations?
164 =item replace OLD_RECORD
166 Replaces the OLD_RECORD with this one in the database. If there is an error,
167 returns the error, otherwise returns false.
174 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
180 warn "FS::prospect_main::replace called on $new to replace $old with options".
181 " ". join(', ', map "$_ => ". $options{$_}, keys %options)
184 local $SIG{HUP} = 'IGNORE';
185 local $SIG{INT} = 'IGNORE';
186 local $SIG{QUIT} = 'IGNORE';
187 local $SIG{TERM} = 'IGNORE';
188 local $SIG{TSTP} = 'IGNORE';
189 local $SIG{PIPE} = 'IGNORE';
191 my $oldAutoCommit = $FS::UID::AutoCommit;
192 local $FS::UID::AutoCommit = 0;
195 warn " replacing prospect_main record" if $DEBUG;
196 my $error = $new->SUPER::replace($old);
198 $dbh->rollback if $oldAutoCommit;
202 if ( $options{'cust_location'} ) {
203 my $cust_location = $options{'cust_location'};
204 $cust_location->prospectnum($new->prospectnum);
205 my $method = $cust_location->locationnum ? 'replace' : 'insert';
206 warn " ${method}ing cust_location record" if $DEBUG;
207 $error = $cust_location->$method();
209 $dbh->rollback if $oldAutoCommit;
212 } elsif ( exists($options{'cust_location'}) ) {
213 foreach my $cust_location (
214 qsearch('cust_location', { 'prospectnum' => $new->prospectnum } )
216 $error = $cust_location->delete();
218 $dbh->rollback if $oldAutoCommit;
224 warn " commiting transaction" if $DEBUG;
225 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
232 Checks all fields to make sure this is a valid prospect. If there is
233 an error, returns the error, otherwise returns false. Called by the insert
242 $self->ut_numbern('prospectnum')
243 || $self->ut_foreign_key('agentnum', 'agent', 'agentnum' )
244 || $self->ut_textn('company')
246 return $error if $error;
248 my $company = $self->company;
249 $company =~ s/^\s+//;
250 $company =~ s/\s+$//;
251 $company =~ s/\s+/ /g;
252 $self->company($company);
259 Returns a name for this prospect, as a string (company name for commercial
260 prospects, contact name for residential prospects).
266 return $self->company if $self->company;
268 my $contact = ($self->contact)[0]; #first contact? good enough for now
269 return $contact->line if $contact;
271 'Prospect #'. $self->prospectnum;
276 Returns the contacts (see L<FS::contact>) associated with this prospect.
282 qsearch( 'contact', { 'prospectnum' => $self->prospectnum } );
287 Returns the locations (see L<FS::cust_location>) associated with this prospect.
293 qsearch( 'cust_location', { 'prospectnum' => $self->prospectnum,
299 Returns the qualifications (see L<FS::qual>) associated with this prospect.
305 qsearch( 'qual', { 'prospectnum' => $self->prospectnum } );
310 Returns the agent (see L<FS::agent>) for this customer.
316 qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
323 Returns a qsearch hash expression to search for the parameters specified in
324 HASHREF. Valid parameters are:
335 my( $class, $params ) = @_;
344 if ( $params->{'agentnum'} =~ /^(\d+)$/ and $1 ) {
346 "prospect_main.agentnum = $1";
350 # setup queries, subs, etc. for the search
353 $orderby ||= 'ORDER BY prospectnum';
355 # here is the agent virtualization
356 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
358 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
360 my $count_query = "SELECT COUNT(*) FROM prospect_main $extra_sql";
363 'table' => 'prospect_main',
364 #'select' => $select,
366 'extra_sql' => $extra_sql,
367 'order_by' => $orderby,
368 'count_query' => $count_query,
369 #'extra_headers' => \@extra_headers,
370 #'extra_fields' => \@extra_fields,
381 L<FS::Record>, schema.html from the base documentation.