5 use FS::Record qw( dbh qsearch qsearchs );
11 @ISA = qw( FS::Record );
15 FS::agent - Object methods for agent records
21 $record = new FS::agent \%hash;
22 $record = new FS::agent { 'column' => 'value' };
24 $error = $record->insert;
26 $error = $new_record->replace($old_record);
28 $error = $record->delete;
30 $error = $record->check;
32 $agent_type = $record->agent_type;
34 $hashref = $record->pkgpart_hashref;
35 #may purchase $pkgpart if $hashref->{$pkgpart};
39 An FS::agent object represents an agent. Every customer has an agent. Agents
40 can be used to track things like resellers or salespeople. FS::agent inherits
41 from FS::Record. The following fields are currently supported:
45 =item agentnum - primary key (assigned automatically for new agents)
47 =item agent - Text name of this agent
49 =item typenum - Agent type. See L<FS::agent_type>
51 =item prog - For future use.
53 =item freq - For future use.
55 =item disabled - Disabled flag, empty or 'Y'
57 =item username - Username for the Agent interface
59 =item _password - Password for the Agent interface
69 Creates a new agent. To add the agent to the database, see L<"insert">.
73 sub table { 'agent'; }
77 Adds this agent to the database. If there is an error, returns the error,
78 otherwise returns false.
82 Deletes this agent from the database. Only agents with no customers can be
83 deleted. If there is an error, returns the error, otherwise returns false.
90 return "Can't delete an agent with customers!"
91 if qsearch( 'cust_main', { 'agentnum' => $self->agentnum } );
96 =item replace OLD_RECORD
98 Replaces OLD_RECORD with this one in the database. If there is an error,
99 returns the error, otherwise returns false.
103 Checks all fields to make sure this is a valid agent. If there is an error,
104 returns the error, otherwise returns false. Called by the insert and replace
113 $self->ut_numbern('agentnum')
114 || $self->ut_text('agent')
115 || $self->ut_number('typenum')
116 || $self->ut_numbern('freq')
117 || $self->ut_textn('prog')
119 return $error if $error;
121 if ( $self->dbdef_table->column('disabled') ) {
122 $error = $self->ut_enum('disabled', [ '', 'Y' ] );
123 return $error if $error;
126 if ( $self->dbdef_table->column('username') ) {
127 $error = $self->ut_alphan('username');
128 return $error if $error;
129 if ( length($self->username) ) {
130 my $conflict = qsearchs('agent', { 'username' => $self->username } );
131 return 'duplicate agent username (with '. $conflict->agent. ')'
132 if $conflict && $conflict->agentnum != $self->agentnum;
133 $error = $self->ut_text('password'); # ut_text... arbitrary choice
135 $self->_password('');
139 return "Unknown typenum!"
140 unless $self->agent_type;
147 Returns the FS::agent_type object (see L<FS::agent_type>) for this agent.
153 qsearchs( 'agent_type', { 'typenum' => $self->typenum } );
156 =item pkgpart_hashref
158 Returns a hash reference. The keys of the hash are pkgparts. The value is
159 true if this agent may purchase the specified package definition. See
164 sub pkgpart_hashref {
166 $self->agent_type->pkgpart_hashref;
169 =item num_prospect_cust_main
171 Returns the number of prospects (customers with no packages ever ordered) for
176 sub num_prospect_cust_main {
177 shift->num_sql(FS::cust_main->prospect_sql);
181 my( $self, $sql ) = @_;
182 my $sth = dbh->prepare(
183 "SELECT COUNT(*) FROM cust_main WHERE agentnum = ? AND $sql"
184 ) or die dbh->errstr;
185 $sth->execute($self->agentnum) or die $sth->errstr;
186 $sth->fetchrow_arrayref->[0];
189 =item prospect_cust_main
191 Returns the prospects (customers with no packages ever ordered) for this agent,
192 as cust_main objects.
196 sub prospect_cust_main {
197 shift->cust_main_sql(FS::cust_main->prospect_sql);
201 my( $self, $sql ) = @_;
202 qsearch( 'cust_main',
203 { 'agentnum' => $self->agentnum },
209 =item num_active_cust_main
211 Returns the number of active customers for this agent.
215 sub num_active_cust_main {
216 shift->num_sql(FS::cust_main->active_sql);
219 =item active_cust_main
221 Returns the active customers for this agent, as cust_main objects.
225 sub active_cust_main {
226 shift->cust_main_sql(FS::cust_main->active_sql);
229 =item num_susp_cust_main
231 Returns the number of suspended customers for this agent.
235 sub num_susp_cust_main {
236 shift->num_sql(FS::cust_main->susp_sql);
241 Returns the suspended customers for this agent, as cust_main objects.
246 shift->cust_main_sql(FS::cust_main->susp_sql);
249 =item num_cancel_cust_main
251 Returns the number of cancelled customer for this agent.
255 sub num_cancel_cust_main {
256 shift->num_sql(FS::cust_main->cancel_sql);
259 =item cancel_cust_main
261 Returns the cancelled customers for this agent, as cust_main objects.
265 sub cancel_cust_main {
266 shift->cust_main_sql(FS::cust_main->cancel_sql);
269 =item generate_reg_codes NUM PKGPART_ARRAYREF
271 Generates the specified number of registration codes, allowing purchase of the
272 specified package definitions. Returns an array reference of the newly
273 generated codes, or a scalar error message.
277 #false laziness w/prepay_credit::generate
278 sub generate_reg_codes {
279 my( $self, $num, $pkgparts ) = @_;
281 my @codeset = ( 'A'..'Z' );
283 local $SIG{HUP} = 'IGNORE';
284 local $SIG{INT} = 'IGNORE';
285 local $SIG{QUIT} = 'IGNORE';
286 local $SIG{TERM} = 'IGNORE';
287 local $SIG{TSTP} = 'IGNORE';
288 local $SIG{PIPE} = 'IGNORE';
290 my $oldAutoCommit = $FS::UID::AutoCommit;
291 local $FS::UID::AutoCommit = 0;
296 my $reg_code = new FS::reg_code {
297 'agentnum' => $self->agentnum,
298 'code' => join('', map($codeset[int(rand $#codeset)], (0..7) ) ),
300 my $error = $reg_code->insert($pkgparts);
302 $dbh->rollback if $oldAutoCommit;
305 push @codes, $reg_code->code;
308 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
316 Returns the number of unused registration codes for this agent.
322 my $sth = dbh->prepare(
323 "SELECT COUNT(*) FROM reg_code WHERE agentnum = ?"
324 ) or die dbh->errstr;
325 $sth->execute($self->agentnum) or die $sth->errstr;
326 $sth->fetchrow_arrayref->[0];
329 =item num_prepay_credit
331 Returns the number of unused prepaid cards for this agent.
335 sub num_prepay_credit {
337 my $sth = dbh->prepare(
338 "SELECT COUNT(*) FROM prepay_credit WHERE agentnum = ?"
339 ) or die dbh->errstr;
340 $sth->execute($self->agentnum) or die $sth->errstr;
341 $sth->fetchrow_arrayref->[0];
351 L<FS::Record>, L<FS::agent_type>, L<FS::cust_main>, L<FS::part_pkg>,
352 schema.html from the base documentation.