2 use base qw( FS::Commission_Mixin FS::m2m_Common FS::m2name_Common FS::Record );
6 use Business::CreditCard 0.35;
7 use FS::Record qw( dbh qsearch qsearchs );
11 use FS::agent_payment_gateway;
12 use FS::payment_gateway;
18 FS::agent - Object methods for agent records
24 $record = new FS::agent \%hash;
25 $record = new FS::agent { 'column' => 'value' };
27 $error = $record->insert;
29 $error = $new_record->replace($old_record);
31 $error = $record->delete;
33 $error = $record->check;
35 $agent_type = $record->agent_type;
37 $hashref = $record->pkgpart_hashref;
38 #may purchase $pkgpart if $hashref->{$pkgpart};
42 An FS::agent object represents an agent. Every customer has an agent. Agents
43 can be used to track things like resellers or salespeople. FS::agent inherits
44 from FS::Record. The following fields are currently supported:
50 primary key (assigned automatically for new agents)
54 Text name of this agent
58 Agent type (see L<FS::agent_type>)
60 =item ticketing_queueid
64 =item invoice_template
70 Optional agent customer (see L<FS::cust_main>)
74 Disabled flag, empty or 'Y'
78 Deprecated (never used)
82 Deprecated (never used)
86 (Deprecated) Username for the Agent interface
90 (Deprecated) Password for the Agent interface
100 Creates a new agent. To add the agent to the database, see L<"insert">.
104 sub table { 'agent'; }
108 Adds this agent to the database. If there is an error, returns the error,
109 otherwise returns false.
113 Deletes this agent from the database. Only agents with no customers can be
114 deleted. If there is an error, returns the error, otherwise returns false.
121 return "Can't delete an agent with customers!"
122 if qsearch( 'cust_main', { 'agentnum' => $self->agentnum } );
124 $self->SUPER::delete;
127 =item replace OLD_RECORD
129 Replaces OLD_RECORD with this one in the database. If there is an error,
130 returns the error, otherwise returns false.
134 Checks all fields to make sure this is a valid agent. If there is an error,
135 returns the error, otherwise returns false. Called by the insert and replace
144 $self->ut_numbern('agentnum')
145 || $self->ut_text('agent')
146 || $self->ut_number('typenum')
147 || $self->ut_numbern('freq')
148 || $self->ut_textn('prog')
149 || $self->ut_textn('invoice_template')
150 || $self->ut_foreign_keyn('agent_custnum', 'cust_main', 'custnum' )
151 || $self->ut_numbern('ticketing_queueid')
153 return $error if $error;
155 if ( $self->dbdef_table->column('disabled') ) {
156 $error = $self->ut_enum('disabled', [ '', 'Y' ] );
157 return $error if $error;
160 if ( $self->dbdef_table->column('username') ) {
161 $error = $self->ut_alphan('username');
162 return $error if $error;
163 if ( length($self->username) ) {
164 my $conflict = qsearchs('agent', { 'username' => $self->username } );
165 return 'duplicate agent username (with '. $conflict->agent. ')'
166 if $conflict && $conflict->agentnum != $self->agentnum;
167 $error = $self->ut_text('password'); # ut_text... arbitrary choice
169 $self->_password('');
173 return "Unknown typenum!"
174 unless $self->agent_type;
181 Returns the FS::agent_type object (see L<FS::agent_type>) for this agent.
183 =item agent_cust_main
185 Returns the FS::cust_main object (see L<FS::cust_main>), if any, for this
190 sub agent_cust_main {
192 qsearchs( 'cust_main', { 'custnum' => $self->agent_custnum } );
197 Returns the FS::agent_currency objects (see L<FS::agent_currency>), if any, for
200 =item agent_currency_hashref
202 Returns a hash references of supported additional currencies for this agent.
206 sub agent_currency_hashref {
208 +{ map { $_->currency => 1 }
209 $self->agent_currency
213 =item pkgpart_hashref
215 Returns a hash reference. The keys of the hash are pkgparts. The value is
216 true if this agent may purchase the specified package definition. See
221 sub pkgpart_hashref {
223 $self->agent_type->pkgpart_hashref;
226 =item ticketing_queue
228 Returns the queue name corresponding with the id from the I<ticketing_queueid>
229 field, or the empty string.
233 sub ticketing_queue {
235 FS::TicketSystem->queue($self->ticketing_queueid);
238 =item payment_gateway [ OPTION => VALUE, ... ]
240 Returns a payment gateway object (see L<FS::payment_gateway>) for this agent.
242 Currently available options are I<nofatal>, I<method>, I<thirdparty>,
243 <conf> and I<load_gatewaynum>.
245 If I<nofatal> is set, and no gateway is available, then the empty string
246 will be returned instead of throwing a fatal exception.
248 The I<method> option can be used to influence the choice
249 as well. Presently only CHEK/ECHECK and PAYPAL methods are meaningful.
251 If I<method> is CHEK/ECHECK and the default gateway is being returned,
252 the business-onlinepayment-ach gateway will be returned if available.
254 If I<thirdparty> is set and the I<method> is PAYPAL, the defined paypal
255 gateway will be returned.
257 Exisisting I<$conf> may be passed for efficiency.
261 # opts invnum/payinfo for cardtype/taxclass overrides no longer supported
262 # any future overrides added here need to be reconciled with the tokenization process
264 sub payment_gateway {
265 my ( $self, %options ) = @_;
267 $options{'conf'} ||= new FS::Conf;
268 my $conf = $options{'conf'};
270 if ( $options{thirdparty} ) {
272 # allows PayPal to coexist with credit card gateways
273 my $is_paypal = { op => '!=', value => 'PayPal' };
274 if ( uc($options{method}) eq 'PAYPAL' ) {
275 $is_paypal = 'PayPal';
278 my $gateway = qsearchs({
279 table => 'payment_gateway',
280 addl_from => ' JOIN agent_payment_gateway USING (gatewaynum) ',
282 gateway_namespace => 'Business::OnlineThirdPartyPayment',
283 gateway_module => $is_paypal,
286 extra_sql => ' AND agentnum = '.$self->agentnum,
291 } elsif ( $options{'nofatal'} ) {
294 die "no third-party gateway configured\n";
298 my $override = qsearchs('agent_payment_gateway', { agentnum => $self->agentnum } );
300 my $payment_gateway = FS::payment_gateway->by_key_or_default(
301 gatewaynum => $override ? $override->gatewaynum : '',
310 Returns all L<FS::invoice_mode> objects that are valid for this agent (i.e.
311 those with this agentnum or null agentnum).
318 table => 'invoice_mode',
319 hashref => { agentnum => $self->agentnum },
320 extra_sql => ' OR agentnum IS NULL',
321 order_by => ' ORDER BY modename',
325 =item num_prospect_cust_main
327 Returns the number of prospects (customers with no packages ever ordered) for
332 sub num_prospect_cust_main {
333 shift->num_sql(FS::cust_main->prospect_sql);
337 my( $self, $sql ) = @_;
338 my $statement = "SELECT COUNT(*) FROM cust_main WHERE agentnum = ? AND $sql";
339 my $sth = dbh->prepare($statement) or die dbh->errstr." preparing $statement";
340 $sth->execute($self->agentnum) or die $sth->errstr. " executing $statement";
341 $sth->fetchrow_arrayref->[0];
344 =item prospect_cust_main
346 Returns the prospects (customers with no packages ever ordered) for this agent,
347 as cust_main objects.
351 sub prospect_cust_main {
352 shift->cust_main_sql(FS::cust_main->prospect_sql);
356 my( $self, $sql ) = @_;
357 qsearch( 'cust_main',
358 { 'agentnum' => $self->agentnum },
364 =item num_ordered_cust_main
366 Returns the number of ordered customers for this agent (customers with packages
367 ordered, but not yet billed).
371 sub num_ordered_cust_main {
372 shift->num_sql(FS::cust_main->ordered_sql);
375 =item ordered_cust_main
377 Returns the ordered customers for this agent (customers with packages ordered,
378 but not yet billed), as cust_main objects.
382 sub ordered_cust_main {
383 shift->cust_main_sql(FS::cust_main->ordered_sql);
387 =item num_active_cust_main
389 Returns the number of active customers for this agent (customers with active
394 sub num_active_cust_main {
395 shift->num_sql(FS::cust_main->active_sql);
398 =item active_cust_main
400 Returns the active customers for this agent, as cust_main objects.
404 sub active_cust_main {
405 shift->cust_main_sql(FS::cust_main->active_sql);
408 =item num_inactive_cust_main
410 Returns the number of inactive customers for this agent (customers with no
411 active recurring packages, but otherwise unsuspended/uncancelled).
415 sub num_inactive_cust_main {
416 shift->num_sql(FS::cust_main->inactive_sql);
419 =item inactive_cust_main
421 Returns the inactive customers for this agent, as cust_main objects.
425 sub inactive_cust_main {
426 shift->cust_main_sql(FS::cust_main->inactive_sql);
430 =item num_susp_cust_main
432 Returns the number of suspended customers for this agent.
436 sub num_susp_cust_main {
437 shift->num_sql(FS::cust_main->susp_sql);
442 Returns the suspended customers for this agent, as cust_main objects.
447 shift->cust_main_sql(FS::cust_main->susp_sql);
450 =item num_cancel_cust_main
452 Returns the number of cancelled customer for this agent.
456 sub num_cancel_cust_main {
457 shift->num_sql(FS::cust_main->cancel_sql);
460 =item cancel_cust_main
462 Returns the cancelled customers for this agent, as cust_main objects.
466 sub cancel_cust_main {
467 shift->cust_main_sql(FS::cust_main->cancel_sql);
470 =item num_active_cust_pkg
472 Returns the number of active customer packages for this agent.
476 sub num_active_cust_pkg {
477 shift->num_pkg_sql(FS::cust_pkg->active_sql);
481 my( $self, $sql ) = @_;
483 "SELECT COUNT(*) FROM cust_pkg LEFT JOIN cust_main USING ( custnum )".
484 " WHERE agentnum = ? AND $sql";
485 my $sth = dbh->prepare($statement) or die dbh->errstr." preparing $statement";
486 $sth->execute($self->agentnum) or die $sth->errstr. "executing $statement";
487 $sth->fetchrow_arrayref->[0];
490 =item num_inactive_cust_pkg
492 Returns the number of inactive customer packages (one-time packages otherwise
493 unsuspended/uncancelled) for this agent.
497 sub num_inactive_cust_pkg {
498 shift->num_pkg_sql(FS::cust_pkg->inactive_sql);
501 =item num_susp_cust_pkg
503 Returns the number of suspended customer packages for this agent.
507 sub num_susp_cust_pkg {
508 shift->num_pkg_sql(FS::cust_pkg->susp_sql);
511 =item num_cancel_cust_pkg
513 Returns the number of cancelled customer packages for this agent.
517 sub num_cancel_cust_pkg {
518 shift->num_pkg_sql(FS::cust_pkg->cancel_sql);
521 =item num_on_hold_cust_pkg
523 Returns the number of inactive customer packages (one-time packages otherwise
524 unsuspended/uncancelled) for this agent.
528 sub num_on_hold_cust_pkg {
529 shift->num_pkg_sql(FS::cust_pkg->on_hold_sql);
532 =item num_not_yet_billed_cust_pkg
534 Returns the number of inactive customer packages (one-time packages otherwise
535 unsuspended/uncancelled) for this agent.
539 sub num_not_yet_billed_cust_pkg {
540 shift->num_pkg_sql(FS::cust_pkg->not_yet_billed_sql);
543 =item generate_reg_codes NUM PKGPART_ARRAYREF
545 Generates the specified number of registration codes, allowing purchase of the
546 specified package definitions. Returns an array reference of the newly
547 generated codes, or a scalar error message.
551 #false laziness w/prepay_credit::generate
552 sub generate_reg_codes {
553 my( $self, $num, $pkgparts ) = @_;
555 my @codeset = ( 'A'..'Z' );
557 local $SIG{HUP} = 'IGNORE';
558 local $SIG{INT} = 'IGNORE';
559 local $SIG{QUIT} = 'IGNORE';
560 local $SIG{TERM} = 'IGNORE';
561 local $SIG{TSTP} = 'IGNORE';
562 local $SIG{PIPE} = 'IGNORE';
564 my $oldAutoCommit = $FS::UID::AutoCommit;
565 local $FS::UID::AutoCommit = 0;
570 my $reg_code = new FS::reg_code {
571 'agentnum' => $self->agentnum,
572 'code' => join('', map($codeset[int(rand $#codeset)], (0..7) ) ),
574 my $error = $reg_code->insert($pkgparts);
576 $dbh->rollback if $oldAutoCommit;
579 push @codes, $reg_code->code;
582 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
590 Returns the number of unused registration codes for this agent.
596 my $sth = dbh->prepare(
597 "SELECT COUNT(*) FROM reg_code WHERE agentnum = ?"
598 ) or die dbh->errstr;
599 $sth->execute($self->agentnum) or die $sth->errstr;
600 $sth->fetchrow_arrayref->[0];
603 =item num_prepay_credit
605 Returns the number of unused prepaid cards for this agent.
609 sub num_prepay_credit {
611 my $sth = dbh->prepare(
612 "SELECT COUNT(*) FROM prepay_credit WHERE agentnum = ?"
613 ) or die dbh->errstr;
614 $sth->execute($self->agentnum) or die $sth->errstr;
615 $sth->fetchrow_arrayref->[0];
620 Returns the number of non-disabled sales people for this agent.
626 my $sth = dbh->prepare(
627 "SELECT COUNT(*) FROM sales WHERE agentnum = ?
628 AND ( disabled = '' OR disabled IS NULL )"
629 ) or die dbh->errstr;
630 $sth->execute($self->agentnum) or die $sth->errstr;
631 $sth->fetchrow_arrayref->[0];
634 sub commission_where {
636 'cust_credit.commission_agentnum = ' . $self->agentnum;
641 'cust_main.agentnum = ' . $self->agentnum;
650 L<FS::Record>, L<FS::agent_type>, L<FS::cust_main>, L<FS::part_pkg>,
651 schema.html from the base documentation.