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;
17 FS::agent - Object methods for agent records
23 $record = new FS::agent \%hash;
24 $record = new FS::agent { 'column' => 'value' };
26 $error = $record->insert;
28 $error = $new_record->replace($old_record);
30 $error = $record->delete;
32 $error = $record->check;
34 $agent_type = $record->agent_type;
36 $hashref = $record->pkgpart_hashref;
37 #may purchase $pkgpart if $hashref->{$pkgpart};
41 An FS::agent object represents an agent. Every customer has an agent. Agents
42 can be used to track things like resellers or salespeople. FS::agent inherits
43 from FS::Record. The following fields are currently supported:
49 primary key (assigned automatically for new agents)
53 Text name of this agent
57 Agent type (see L<FS::agent_type>)
59 =item ticketing_queueid
63 =item invoice_template
69 Optional agent customer (see L<FS::cust_main>)
73 Disabled flag, empty or 'Y'
77 Deprecated (never used)
81 Deprecated (never used)
85 (Deprecated) Username for the Agent interface
89 (Deprecated) Password for the Agent interface
99 Creates a new agent. To add the agent to the database, see L<"insert">.
103 sub table { 'agent'; }
107 Adds this agent to the database. If there is an error, returns the error,
108 otherwise returns false.
112 Deletes this agent from the database. Only agents with no customers can be
113 deleted. If there is an error, returns the error, otherwise returns false.
120 return "Can't delete an agent with customers!"
121 if qsearch( 'cust_main', { 'agentnum' => $self->agentnum } );
123 $self->SUPER::delete;
126 =item replace OLD_RECORD
128 Replaces OLD_RECORD with this one in the database. If there is an error,
129 returns the error, otherwise returns false.
133 Checks all fields to make sure this is a valid agent. If there is an error,
134 returns the error, otherwise returns false. Called by the insert and replace
143 $self->ut_numbern('agentnum')
144 || $self->ut_text('agent')
145 || $self->ut_number('typenum')
146 || $self->ut_numbern('freq')
147 || $self->ut_textn('prog')
148 || $self->ut_textn('invoice_template')
149 || $self->ut_foreign_keyn('agent_custnum', 'cust_main', 'custnum' )
150 || $self->ut_numbern('ticketing_queueid')
152 return $error if $error;
154 if ( $self->dbdef_table->column('disabled') ) {
155 $error = $self->ut_enum('disabled', [ '', 'Y' ] );
156 return $error if $error;
159 if ( $self->dbdef_table->column('username') ) {
160 $error = $self->ut_alphan('username');
161 return $error if $error;
162 if ( length($self->username) ) {
163 my $conflict = qsearchs('agent', { 'username' => $self->username } );
164 return 'duplicate agent username (with '. $conflict->agent. ')'
165 if $conflict && $conflict->agentnum != $self->agentnum;
166 $error = $self->ut_text('password'); # ut_text... arbitrary choice
168 $self->_password('');
172 return "Unknown typenum!"
173 unless $self->agent_type;
180 Returns the FS::agent_type object (see L<FS::agent_type>) for this agent.
182 =item agent_cust_main
184 Returns the FS::cust_main object (see L<FS::cust_main>), if any, for this
189 sub agent_cust_main {
191 qsearchs( 'cust_main', { 'custnum' => $self->agent_custnum } );
196 Returns the FS::agent_currency objects (see L<FS::agent_currency>), if any, for
199 =item agent_currency_hashref
201 Returns a hash references of supported additional currencies for this agent.
205 sub agent_currency_hashref {
207 +{ map { $_->currency => 1 }
208 $self->agent_currency
212 =item pkgpart_hashref
214 Returns a hash reference. The keys of the hash are pkgparts. The value is
215 true if this agent may purchase the specified package definition. See
220 sub pkgpart_hashref {
222 $self->agent_type->pkgpart_hashref;
225 =item ticketing_queue
227 Returns the queue name corresponding with the id from the I<ticketing_queueid>
228 field, or the empty string.
232 sub ticketing_queue {
234 FS::TicketSystem->queue($self->ticketing_queueid);
237 =item payment_gateway [ OPTION => VALUE, ... ]
239 Returns a payment gateway object (see L<FS::payment_gateway>) for this agent.
241 Currently available options are I<nofatal>, I<invnum>, I<method>,
242 I<payinfo>, and I<thirdparty>.
244 If I<nofatal> is set, and no gateway is available, then the empty string
245 will be returned instead of throwing a fatal exception.
247 If I<invnum> is set to the number of an invoice (see L<FS::cust_bill>) then
248 an attempt will be made to select a gateway suited for the taxes paid on
251 The I<method> and I<payinfo> options can be used to influence the choice
252 as well. Presently only 'CC', 'ECHECK', and 'PAYPAL' methods are meaningful.
254 When the I<method> is 'CC' then the card number in I<payinfo> can direct
255 this routine to route to a gateway suited for that type of card.
257 If I<thirdparty> is set, the defined self-service payment gateway will
262 sub payment_gateway {
263 my ( $self, %options ) = @_;
265 my $conf = new FS::Conf;
267 if ( $options{thirdparty} ) {
269 # allows PayPal to coexist with credit card gateways
270 my $is_paypal = { op => '!=', value => 'PayPal' };
271 if ( uc($options{method}) eq 'PAYPAL' ) {
272 $is_paypal = 'PayPal';
275 my $gateway = qsearchs({
276 table => 'payment_gateway',
277 addl_from => ' JOIN agent_payment_gateway USING (gatewaynum) ',
279 gateway_namespace => 'Business::OnlineThirdPartyPayment',
280 gateway_module => $is_paypal,
283 extra_sql => ' AND agentnum = '.$self->agentnum,
288 } elsif ( $options{'nofatal'} ) {
291 die "no third-party gateway configured\n";
296 if ( $options{invnum} ) {
298 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $options{invnum} } );
299 die "invnum ". $options{'invnum'}. " not found" unless $cust_bill;
305 $cust_bill->cust_bill_pkg;
307 my @taxclasses = map $_->taxclass, @part_pkg;
309 $taxclass = $taxclasses[0]
310 unless grep { $taxclasses[0] ne $_ } @taxclasses; #unless there are
311 #different taxclasses
314 #look for an agent gateway override first
316 if ( $options{method} ) {
317 if ( $options{method} eq 'CC' && $options{payinfo} ) {
318 $cardtype = cardtype($options{payinfo});
319 } elsif ( $options{method} eq 'ECHECK' ) {
322 $cardtype = $options{method}
327 qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
328 cardtype => $cardtype,
329 taxclass => $taxclass, } )
330 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
332 taxclass => $taxclass, } )
333 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
334 cardtype => $cardtype,
336 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
341 if ( $override ) { #use a payment gateway override
343 $payment_gateway = $override->payment_gateway;
345 $payment_gateway->gateway_namespace('Business::OnlinePayment')
346 unless $payment_gateway->gateway_namespace;
348 } else { #use the standard settings from the config
350 # the standard settings from the config could be moved to a null agent
351 # agent_payment_gateway referenced payment_gateway
353 unless ( $conf->exists('business-onlinepayment') ) {
354 if ( $options{'nofatal'} ) {
357 die "Real-time processing not enabled\n";
362 my $bop_config = 'business-onlinepayment';
363 $bop_config .= '-ach'
364 if ( $options{method}
365 && $options{method} =~ /^(ECHECK|CHEK)$/
366 && $conf->exists($bop_config. '-ach')
368 my ( $processor, $login, $password, $action, @bop_options ) =
369 $conf->config($bop_config);
370 $action ||= 'normal authorization';
371 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
372 die "No real-time processor is enabled - ".
373 "did you set the business-onlinepayment configuration value?\n"
376 $payment_gateway = new FS::payment_gateway;
378 $payment_gateway->gateway_namespace( $conf->config('business-onlinepayment-namespace') ||
379 'Business::OnlinePayment');
380 $payment_gateway->gateway_module($processor);
381 $payment_gateway->gateway_username($login);
382 $payment_gateway->gateway_password($password);
383 $payment_gateway->gateway_action($action);
384 $payment_gateway->set('options', [ @bop_options ]);
388 unless ( $payment_gateway->gateway_namespace ) {
389 $payment_gateway->gateway_namespace(
390 scalar($conf->config('business-onlinepayment-namespace'))
391 || 'Business::OnlinePayment'
400 Returns all L<FS::invoice_mode> objects that are valid for this agent (i.e.
401 those with this agentnum or null agentnum).
408 table => 'invoice_mode',
409 hashref => { agentnum => $self->agentnum },
410 extra_sql => ' OR agentnum IS NULL',
411 order_by => ' ORDER BY modename',
415 =item num_prospect_cust_main
417 Returns the number of prospects (customers with no packages ever ordered) for
422 sub num_prospect_cust_main {
423 shift->num_sql(FS::cust_main->prospect_sql);
427 my( $self, $sql ) = @_;
428 my $statement = "SELECT COUNT(*) FROM cust_main WHERE agentnum = ? AND $sql";
429 my $sth = dbh->prepare($statement) or die dbh->errstr." preparing $statement";
430 $sth->execute($self->agentnum) or die $sth->errstr. " executing $statement";
431 $sth->fetchrow_arrayref->[0];
434 =item prospect_cust_main
436 Returns the prospects (customers with no packages ever ordered) for this agent,
437 as cust_main objects.
441 sub prospect_cust_main {
442 shift->cust_main_sql(FS::cust_main->prospect_sql);
446 my( $self, $sql ) = @_;
447 qsearch( 'cust_main',
448 { 'agentnum' => $self->agentnum },
454 =item num_ordered_cust_main
456 Returns the number of ordered customers for this agent (customers with packages
457 ordered, but not yet billed).
461 sub num_ordered_cust_main {
462 shift->num_sql(FS::cust_main->ordered_sql);
465 =item ordered_cust_main
467 Returns the ordered customers for this agent (customers with packages ordered,
468 but not yet billed), as cust_main objects.
472 sub ordered_cust_main {
473 shift->cust_main_sql(FS::cust_main->ordered_sql);
477 =item num_active_cust_main
479 Returns the number of active customers for this agent (customers with active
484 sub num_active_cust_main {
485 shift->num_sql(FS::cust_main->active_sql);
488 =item active_cust_main
490 Returns the active customers for this agent, as cust_main objects.
494 sub active_cust_main {
495 shift->cust_main_sql(FS::cust_main->active_sql);
498 =item num_inactive_cust_main
500 Returns the number of inactive customers for this agent (customers with no
501 active recurring packages, but otherwise unsuspended/uncancelled).
505 sub num_inactive_cust_main {
506 shift->num_sql(FS::cust_main->inactive_sql);
509 =item inactive_cust_main
511 Returns the inactive customers for this agent, as cust_main objects.
515 sub inactive_cust_main {
516 shift->cust_main_sql(FS::cust_main->inactive_sql);
520 =item num_susp_cust_main
522 Returns the number of suspended customers for this agent.
526 sub num_susp_cust_main {
527 shift->num_sql(FS::cust_main->susp_sql);
532 Returns the suspended customers for this agent, as cust_main objects.
537 shift->cust_main_sql(FS::cust_main->susp_sql);
540 =item num_cancel_cust_main
542 Returns the number of cancelled customer for this agent.
546 sub num_cancel_cust_main {
547 shift->num_sql(FS::cust_main->cancel_sql);
550 =item cancel_cust_main
552 Returns the cancelled customers for this agent, as cust_main objects.
556 sub cancel_cust_main {
557 shift->cust_main_sql(FS::cust_main->cancel_sql);
560 =item num_active_cust_pkg
562 Returns the number of active customer packages for this agent.
566 sub num_active_cust_pkg {
567 shift->num_pkg_sql(FS::cust_pkg->active_sql);
571 my( $self, $sql ) = @_;
573 "SELECT COUNT(*) FROM cust_pkg LEFT JOIN cust_main USING ( custnum )".
574 " WHERE agentnum = ? AND $sql";
575 my $sth = dbh->prepare($statement) or die dbh->errstr." preparing $statement";
576 $sth->execute($self->agentnum) or die $sth->errstr. "executing $statement";
577 $sth->fetchrow_arrayref->[0];
580 =item num_inactive_cust_pkg
582 Returns the number of inactive customer packages (one-time packages otherwise
583 unsuspended/uncancelled) for this agent.
587 sub num_inactive_cust_pkg {
588 shift->num_pkg_sql(FS::cust_pkg->inactive_sql);
591 =item num_susp_cust_pkg
593 Returns the number of suspended customer packages for this agent.
597 sub num_susp_cust_pkg {
598 shift->num_pkg_sql(FS::cust_pkg->susp_sql);
601 =item num_cancel_cust_pkg
603 Returns the number of cancelled customer packages for this agent.
607 sub num_cancel_cust_pkg {
608 shift->num_pkg_sql(FS::cust_pkg->cancel_sql);
611 =item num_on_hold_cust_pkg
613 Returns the number of inactive customer packages (one-time packages otherwise
614 unsuspended/uncancelled) for this agent.
618 sub num_on_hold_cust_pkg {
619 shift->num_pkg_sql(FS::cust_pkg->on_hold_sql);
622 =item num_not_yet_billed_cust_pkg
624 Returns the number of inactive customer packages (one-time packages otherwise
625 unsuspended/uncancelled) for this agent.
629 sub num_not_yet_billed_cust_pkg {
630 shift->num_pkg_sql(FS::cust_pkg->not_yet_billed_sql);
633 =item generate_reg_codes NUM PKGPART_ARRAYREF
635 Generates the specified number of registration codes, allowing purchase of the
636 specified package definitions. Returns an array reference of the newly
637 generated codes, or a scalar error message.
641 #false laziness w/prepay_credit::generate
642 sub generate_reg_codes {
643 my( $self, $num, $pkgparts ) = @_;
645 my @codeset = ( 'A'..'Z' );
647 local $SIG{HUP} = 'IGNORE';
648 local $SIG{INT} = 'IGNORE';
649 local $SIG{QUIT} = 'IGNORE';
650 local $SIG{TERM} = 'IGNORE';
651 local $SIG{TSTP} = 'IGNORE';
652 local $SIG{PIPE} = 'IGNORE';
654 my $oldAutoCommit = $FS::UID::AutoCommit;
655 local $FS::UID::AutoCommit = 0;
660 my $reg_code = new FS::reg_code {
661 'agentnum' => $self->agentnum,
662 'code' => join('', map($codeset[int(rand $#codeset)], (0..7) ) ),
664 my $error = $reg_code->insert($pkgparts);
666 $dbh->rollback if $oldAutoCommit;
669 push @codes, $reg_code->code;
672 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
680 Returns the number of unused registration codes for this agent.
686 my $sth = dbh->prepare(
687 "SELECT COUNT(*) FROM reg_code WHERE agentnum = ?"
688 ) or die dbh->errstr;
689 $sth->execute($self->agentnum) or die $sth->errstr;
690 $sth->fetchrow_arrayref->[0];
693 =item num_prepay_credit
695 Returns the number of unused prepaid cards for this agent.
699 sub num_prepay_credit {
701 my $sth = dbh->prepare(
702 "SELECT COUNT(*) FROM prepay_credit WHERE agentnum = ?"
703 ) or die dbh->errstr;
704 $sth->execute($self->agentnum) or die $sth->errstr;
705 $sth->fetchrow_arrayref->[0];
710 Returns the number of non-disabled sales people for this agent.
716 my $sth = dbh->prepare(
717 "SELECT COUNT(*) FROM sales WHERE agentnum = ?
718 AND ( disabled = '' OR disabled IS NULL )"
719 ) or die dbh->errstr;
720 $sth->execute($self->agentnum) or die $sth->errstr;
721 $sth->fetchrow_arrayref->[0];
724 sub commission_where {
726 'cust_credit.commission_agentnum = ' . $self->agentnum;
731 'cust_main.agentnum = ' . $self->agentnum;
740 L<FS::Record>, L<FS::agent_type>, L<FS::cust_main>, L<FS::part_pkg>,
741 schema.html from the base documentation.