1 package FS::prospect_main;
4 use base qw( FS::Quotable_Mixin FS::o2m_Common FS::Record );
6 use Scalar::Util qw( blessed );
7 use FS::Record qw( dbh qsearch qsearchs );
17 FS::prospect_main - Object methods for prospect_main records
21 use FS::prospect_main;
23 $record = new FS::prospect_main \%hash;
24 $record = new FS::prospect_main { 'column' => 'value' };
26 $error = $record->insert;
28 $error = $new_record->replace($old_record);
30 $error = $record->delete;
32 $error = $record->check;
36 An FS::prospect_main object represents a prospect. FS::prospect_main inherits
37 from FS::Record. The following fields are currently supported:
61 Creates a new prospect. To add the prospect to the database, see L<"insert">.
63 Note that this stores the hash reference, not a distinct copy of the hash it
64 points to. You can ask the object for a copy with the I<hash> method.
68 sub table { 'prospect_main'; }
72 Adds this record to the database. If there is an error, returns the error,
73 otherwise returns false.
80 warn "FS::prospect_main::insert called on $self with options ".
81 join(', ', map "$_=>$options{$_}", keys %options)
84 local $SIG{HUP} = 'IGNORE';
85 local $SIG{INT} = 'IGNORE';
86 local $SIG{QUIT} = 'IGNORE';
87 local $SIG{TERM} = 'IGNORE';
88 local $SIG{TSTP} = 'IGNORE';
89 local $SIG{PIPE} = 'IGNORE';
91 my $oldAutoCommit = $FS::UID::AutoCommit;
92 local $FS::UID::AutoCommit = 0;
95 warn " inserting prospect_main record" if $DEBUG;
96 my $error = $self->SUPER::insert;
98 $dbh->rollback if $oldAutoCommit;
102 if ( $options{'cust_location'} ) {
103 warn " inserting cust_location record" if $DEBUG;
104 my $cust_location = $options{'cust_location'};
105 $cust_location->prospectnum($self->prospectnum);
106 $error = $cust_location->insert;
108 $dbh->rollback if $oldAutoCommit;
113 warn " commiting transaction" if $DEBUG;
114 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
121 Delete this record from the database.
125 #delete dangling locations?
127 =item replace OLD_RECORD
129 Replaces the OLD_RECORD with this one in the database. If there is an error,
130 returns the error, otherwise returns false.
137 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
143 warn "FS::prospect_main::replace called on $new to replace $old with options".
144 " ". join(', ', map "$_ => ". $options{$_}, keys %options)
147 local $SIG{HUP} = 'IGNORE';
148 local $SIG{INT} = 'IGNORE';
149 local $SIG{QUIT} = 'IGNORE';
150 local $SIG{TERM} = 'IGNORE';
151 local $SIG{TSTP} = 'IGNORE';
152 local $SIG{PIPE} = 'IGNORE';
154 my $oldAutoCommit = $FS::UID::AutoCommit;
155 local $FS::UID::AutoCommit = 0;
158 warn " replacing prospect_main record" if $DEBUG;
159 my $error = $new->SUPER::replace($old);
161 $dbh->rollback if $oldAutoCommit;
165 if ( $options{'cust_location'} ) {
166 my $cust_location = $options{'cust_location'};
167 $cust_location->prospectnum($new->prospectnum);
168 my $method = $cust_location->locationnum ? 'replace' : 'insert';
169 warn " ${method}ing cust_location record" if $DEBUG;
170 $error = $cust_location->$method();
172 $dbh->rollback if $oldAutoCommit;
175 } elsif ( exists($options{'cust_location'}) ) {
176 foreach my $cust_location (
177 qsearch('cust_location', { 'prospectnum' => $new->prospectnum } )
179 $error = $cust_location->delete();
181 $dbh->rollback if $oldAutoCommit;
187 warn " commiting transaction" if $DEBUG;
188 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
195 Checks all fields to make sure this is a valid prospect. If there is
196 an error, returns the error, otherwise returns false. Called by the insert
205 $self->ut_numbern('prospectnum')
206 || $self->ut_foreign_key('agentnum', 'agent', 'agentnum' )
207 || $self->ut_textn('company')
209 return $error if $error;
216 Returns a name for this prospect, as a string (company name for commercial
217 prospects, contact name for residential prospects).
223 return $self->company if $self->company;
225 my $contact = ($self->contact)[0]; #first contact? good enough for now
226 return $contact->line if $contact;
228 'Prospect #'. $self->prospectnum;
233 Returns the contacts (see L<FS::contact>) associated with this prospect.
239 qsearch( 'contact', { 'prospectnum' => $self->prospectnum } );
244 Returns the locations (see L<FS::cust_location>) associated with this prospect.
250 qsearch( 'cust_location', { 'prospectnum' => $self->prospectnum,
256 Returns the qualifications (see L<FS::qual>) associated with this prospect.
262 qsearch( 'qual', { 'prospectnum' => $self->prospectnum } );
267 Returns the agent (see L<FS::agent>) for this customer.
273 qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
280 Returns a qsearch hash expression to search for the parameters specified in
281 HASHREF. Valid parameters are:
292 my( $class, $params ) = @_;
301 if ( $params->{'agentnum'} =~ /^(\d+)$/ and $1 ) {
303 "prospect_main.agentnum = $1";
307 # setup queries, subs, etc. for the search
310 $orderby ||= 'ORDER BY prospectnum';
312 # here is the agent virtualization
313 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
315 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
317 my $count_query = "SELECT COUNT(*) FROM prospect_main $extra_sql";
320 'table' => 'prospect_main',
321 #'select' => $select,
323 'extra_sql' => $extra_sql,
324 'order_by' => $orderby,
325 'count_query' => $count_query,
326 #'extra_headers' => \@extra_headers,
327 #'extra_fields' => \@extra_fields,
338 L<FS::Record>, schema.html from the base documentation.