1 package FS::prospect_main;
4 use base qw( FS::o2m_Common FS::Record );
6 use Scalar::Util qw( blessed );
7 use FS::Record qw( dbh qsearch ); #qsearchs );
16 FS::prospect_main - Object methods for prospect_main records
20 use FS::prospect_main;
22 $record = new FS::prospect_main \%hash;
23 $record = new FS::prospect_main { 'column' => 'value' };
25 $error = $record->insert;
27 $error = $new_record->replace($old_record);
29 $error = $record->delete;
31 $error = $record->check;
35 An FS::prospect_main object represents a prospect. FS::prospect_main inherits
36 from FS::Record. The following fields are currently supported:
60 Creates a new prospect. To add the prospect to the database, see L<"insert">.
62 Note that this stores the hash reference, not a distinct copy of the hash it
63 points to. You can ask the object for a copy with the I<hash> method.
67 sub table { 'prospect_main'; }
71 Adds this record to the database. If there is an error, returns the error,
72 otherwise returns false.
79 warn "FS::prospect_main::insert called on $self with options ".
80 join(', ', map "$_=>$options{$_}", keys %options)
83 local $SIG{HUP} = 'IGNORE';
84 local $SIG{INT} = 'IGNORE';
85 local $SIG{QUIT} = 'IGNORE';
86 local $SIG{TERM} = 'IGNORE';
87 local $SIG{TSTP} = 'IGNORE';
88 local $SIG{PIPE} = 'IGNORE';
90 my $oldAutoCommit = $FS::UID::AutoCommit;
91 local $FS::UID::AutoCommit = 0;
94 warn " inserting prospect_main record" if $DEBUG;
95 my $error = $self->SUPER::insert;
97 $dbh->rollback if $oldAutoCommit;
101 if ( $options{'cust_location'} ) {
102 warn " inserting cust_location record" if $DEBUG;
103 my $cust_location = $options{'cust_location'};
104 $cust_location->prospectnum($self->prospectnum);
105 $error = $cust_location->insert;
107 $dbh->rollback if $oldAutoCommit;
112 warn " commiting transaction" if $DEBUG;
113 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
120 Delete this record from the database.
124 #delete dangling locations?
126 =item replace OLD_RECORD
128 Replaces the OLD_RECORD with this one in the database. If there is an error,
129 returns the error, otherwise returns false.
136 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
142 warn "FS::prospect_main::replace called on $new to replace $old with options".
143 " ". join(', ', map "$_ => ". $options{$_}, keys %options)
146 local $SIG{HUP} = 'IGNORE';
147 local $SIG{INT} = 'IGNORE';
148 local $SIG{QUIT} = 'IGNORE';
149 local $SIG{TERM} = 'IGNORE';
150 local $SIG{TSTP} = 'IGNORE';
151 local $SIG{PIPE} = 'IGNORE';
153 my $oldAutoCommit = $FS::UID::AutoCommit;
154 local $FS::UID::AutoCommit = 0;
157 warn " replacing prospect_main record" if $DEBUG;
158 my $error = $new->SUPER::replace($old);
160 $dbh->rollback if $oldAutoCommit;
164 if ( $options{'cust_location'} ) {
165 my $cust_location = $options{'cust_location'};
166 $cust_location->prospectnum($new->prospectnum);
167 my $method = $cust_location->locationnum ? 'replace' : 'insert';
168 warn " ${method}ing cust_location record" if $DEBUG;
169 $error = $cust_location->$method();
171 $dbh->rollback if $oldAutoCommit;
174 } elsif ( exists($options{'cust_location'}) ) {
175 foreach my $cust_location (
176 qsearch('cust_location', { 'prospectnum' => $new->prospectnum } )
178 $error = $cust_location->delete();
180 $dbh->rollback if $oldAutoCommit;
186 warn " commiting transaction" if $DEBUG;
187 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
194 Checks all fields to make sure this is a valid prospect. If there is
195 an error, returns the error, otherwise returns false. Called by the insert
204 $self->ut_numbern('prospectnum')
205 || $self->ut_foreign_key('agentnum', 'agent', 'agentnum' )
206 || $self->ut_text('company')
208 return $error if $error;
219 $self->company; #at least until this is nullable
224 Returns the contacts (see L<FS::contact>) associated with this prospect.
230 qsearch( 'contact', { 'prospectnum' => $self->prospectnum } );
235 Returns the locations (see L<FS::cust_location>) associated with this prospect.
241 qsearch( 'cust_location', { 'prospectnum' => $self->prospectnum } );
248 Returns a qsearch hash expression to search for the parameters specified in
249 HASHREF. Valid parameters are:
260 my( $class, $params ) = @_;
269 if ( $params->{'agentnum'} =~ /^(\d+)$/ and $1 ) {
271 "prospect_main.agentnum = $1";
275 # setup queries, subs, etc. for the search
278 $orderby ||= 'ORDER BY prospectnum';
280 # here is the agent virtualization
281 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
283 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
285 my $count_query = "SELECT COUNT(*) FROM prospect_main $extra_sql";
288 'table' => 'prospect_main',
289 #'select' => $select,
291 'extra_sql' => $extra_sql,
292 'order_by' => $orderby,
293 'count_query' => $count_query,
294 #'extra_headers' => \@extra_headers,
295 #'extra_fields' => \@extra_fields,
306 L<FS::Record>, schema.html from the base documentation.