6 use Business::CreditCard 0.28;
7 use FS::Record qw( dbh qsearch qsearchs );
15 @ISA = qw( FS::m2m_Common FS::Record );
19 FS::agent - Object methods for agent records
25 $record = new FS::agent \%hash;
26 $record = new FS::agent { 'column' => 'value' };
28 $error = $record->insert;
30 $error = $new_record->replace($old_record);
32 $error = $record->delete;
34 $error = $record->check;
36 $agent_type = $record->agent_type;
38 $hashref = $record->pkgpart_hashref;
39 #may purchase $pkgpart if $hashref->{$pkgpart};
43 An FS::agent object represents an agent. Every customer has an agent. Agents
44 can be used to track things like resellers or salespeople. FS::agent inherits
45 from FS::Record. The following fields are currently supported:
51 primary key (assigned automatically for new agents)
55 Text name of this agent
59 Agent type (see L<FS::agent_type>)
61 =item ticketing_queueid
65 =item invoice_template
71 Optional agent customer (see L<FS::cust_main>)
75 Disabled flag, empty or 'Y'
79 Deprecated (never used)
83 Deprecated (never used)
87 (Deprecated) Username for the Agent interface
91 (Deprecated) Password for the Agent interface
101 Creates a new agent. To add the agent to the database, see L<"insert">.
105 sub table { 'agent'; }
109 Adds this agent to the database. If there is an error, returns the error,
110 otherwise returns false.
114 Deletes this agent from the database. Only agents with no customers can be
115 deleted. If there is an error, returns the error, otherwise returns false.
122 return "Can't delete an agent with customers!"
123 if qsearch( 'cust_main', { 'agentnum' => $self->agentnum } );
125 $self->SUPER::delete;
128 =item replace OLD_RECORD
130 Replaces OLD_RECORD with this one in the database. If there is an error,
131 returns the error, otherwise returns false.
135 Checks all fields to make sure this is a valid agent. If there is an error,
136 returns the error, otherwise returns false. Called by the insert and replace
145 $self->ut_numbern('agentnum')
146 || $self->ut_text('agent')
147 || $self->ut_number('typenum')
148 || $self->ut_numbern('freq')
149 || $self->ut_textn('prog')
150 || $self->ut_textn('invoice_template')
151 || $self->ut_foreign_keyn('agent_custnum', 'cust_main', 'custnum' )
152 || $self->ut_numbern('ticketing_queueid')
154 return $error if $error;
156 if ( $self->dbdef_table->column('disabled') ) {
157 $error = $self->ut_enum('disabled', [ '', 'Y' ] );
158 return $error if $error;
161 if ( $self->dbdef_table->column('username') ) {
162 $error = $self->ut_alphan('username');
163 return $error if $error;
164 if ( length($self->username) ) {
165 my $conflict = qsearchs('agent', { 'username' => $self->username } );
166 return 'duplicate agent username (with '. $conflict->agent. ')'
167 if $conflict && $conflict->agentnum != $self->agentnum;
168 $error = $self->ut_text('password'); # ut_text... arbitrary choice
170 $self->_password('');
174 return "Unknown typenum!"
175 unless $self->agent_type;
182 Returns the FS::agent_type object (see L<FS::agent_type>) for this agent.
188 qsearchs( 'agent_type', { 'typenum' => $self->typenum } );
191 =item agent_cust_main
193 Returns the FS::cust_main object (see L<FS::cust_main>), if any, for this
198 sub agent_cust_main {
200 qsearchs( 'cust_main', { 'custnum' => $self->agent_custnum } );
203 =item pkgpart_hashref
205 Returns a hash reference. The keys of the hash are pkgparts. The value is
206 true if this agent may purchase the specified package definition. See
211 sub pkgpart_hashref {
213 $self->agent_type->pkgpart_hashref;
216 =item ticketing_queue
218 Returns the queue name corresponding with the id from the I<ticketing_queueid>
219 field, or the empty string.
223 sub ticketing_queue {
225 FS::TicketSystem->queue($self->ticketing_queueid);
228 =item payment_gateway [ OPTION => VALUE, ... ]
230 Returns a payment gateway object (see L<FS::payment_gateway>) for this agent.
232 Currently available options are I<nofatal>, I<invnum>, I<method>,
233 I<payinfo>, and I<thirdparty>.
235 If I<nofatal> is set, and no gateway is available, then the empty string
236 will be returned instead of throwing a fatal exception.
238 If I<invnum> is set to the number of an invoice (see L<FS::cust_bill>) then
239 an attempt will be made to select a gateway suited for the taxes paid on
242 The I<method> and I<payinfo> options can be used to influence the choice
243 as well. Presently only 'CC', 'ECHECK', and 'PAYPAL' methods are meaningful.
245 When the I<method> is 'CC' then the card number in I<payinfo> can direct
246 this routine to route to a gateway suited for that type of card.
248 If I<thirdparty> is set, the defined self-service payment gateway will
253 sub payment_gateway {
254 my ( $self, %options ) = @_;
256 my $conf = new FS::Conf;
258 if ( $options{thirdparty} ) {
259 # still a kludge, but it gets the job done
260 # and the 'cardtype' semantics don't really apply to thirdparty
261 # gateways because we have to choose a gateway without ever
262 # seeing the card number
264 $conf->config('selfservice-payment_gateway', $self->agentnum);
265 my $gateway = FS::payment_gateway->by_key($gatewaynum)
270 } elsif ( $options{'nofatal'} ) {
273 die "no third-party gateway configured\n";
278 if ( $options{invnum} ) {
280 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $options{invnum} } );
281 die "invnum ". $options{'invnum'}. " not found" unless $cust_bill;
287 $cust_bill->cust_bill_pkg;
289 my @taxclasses = map $_->taxclass, @part_pkg;
291 $taxclass = $taxclasses[0]
292 unless grep { $taxclasses[0] ne $_ } @taxclasses; #unless there are
293 #different taxclasses
296 #look for an agent gateway override first
298 if ( $options{method} ) {
299 if ( $options{method} eq 'CC' && $options{payinfo} ) {
300 $cardtype = cardtype($options{payinfo});
301 } elsif ( $options{method} eq 'ECHECK' ) {
304 $cardtype = $options{method}
309 qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
310 cardtype => $cardtype,
311 taxclass => $taxclass, } )
312 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
314 taxclass => $taxclass, } )
315 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
316 cardtype => $cardtype,
318 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
323 if ( $override ) { #use a payment gateway override
325 $payment_gateway = $override->payment_gateway;
327 $payment_gateway->gateway_namespace('Business::OnlinePayment')
328 unless $payment_gateway->gateway_namespace;
330 } else { #use the standard settings from the config
332 # the standard settings from the config could be moved to a null agent
333 # agent_payment_gateway referenced payment_gateway
335 unless ( $conf->exists('business-onlinepayment') ) {
336 if ( $options{'nofatal'} ) {
339 die "Real-time processing not enabled\n";
344 my $bop_config = 'business-onlinepayment';
345 $bop_config .= '-ach'
346 if ( $options{method}
347 && $options{method} =~ /^(ECHECK|CHEK)$/
348 && $conf->exists($bop_config. '-ach')
350 my ( $processor, $login, $password, $action, @bop_options ) =
351 $conf->config($bop_config);
352 $action ||= 'normal authorization';
353 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
354 die "No real-time processor is enabled - ".
355 "did you set the business-onlinepayment configuration value?\n"
358 $payment_gateway = new FS::payment_gateway;
360 $payment_gateway->gateway_namespace( $conf->config('business-onlinepayment-namespace') ||
361 'Business::OnlinePayment');
362 $payment_gateway->gateway_module($processor);
363 $payment_gateway->gateway_username($login);
364 $payment_gateway->gateway_password($password);
365 $payment_gateway->gateway_action($action);
366 $payment_gateway->set('options', [ @bop_options ]);
370 unless ( $payment_gateway->gateway_namespace ) {
371 $payment_gateway->gateway_namespace(
372 scalar($conf->config('business-onlinepayment-namespace'))
373 || 'Business::OnlinePayment'
382 Returns all L<FS::invoice_mode> objects that are valid for this agent (i.e.
383 those with this agentnum or null agentnum).
390 table => 'invoice_mode',
391 hashref => { agentnum => $self->agentnum },
392 extra_sql => ' OR agentnum IS NULL',
393 order_by => ' ORDER BY modename',
397 =item num_prospect_cust_main
399 Returns the number of prospects (customers with no packages ever ordered) for
404 sub num_prospect_cust_main {
405 shift->num_sql(FS::cust_main->prospect_sql);
409 my( $self, $sql ) = @_;
410 my $statement = "SELECT COUNT(*) FROM cust_main WHERE agentnum = ? AND $sql";
411 my $sth = dbh->prepare($statement) or die dbh->errstr." preparing $statement";
412 $sth->execute($self->agentnum) or die $sth->errstr. " executing $statement";
413 $sth->fetchrow_arrayref->[0];
416 =item prospect_cust_main
418 Returns the prospects (customers with no packages ever ordered) for this agent,
419 as cust_main objects.
423 sub prospect_cust_main {
424 shift->cust_main_sql(FS::cust_main->prospect_sql);
428 my( $self, $sql ) = @_;
429 qsearch( 'cust_main',
430 { 'agentnum' => $self->agentnum },
436 =item num_ordered_cust_main
438 Returns the number of ordered customers for this agent (customers with packages
439 ordered, but not yet billed).
443 sub num_ordered_cust_main {
444 shift->num_sql(FS::cust_main->ordered_sql);
447 =item ordered_cust_main
449 Returns the ordered customers for this agent (customers with packages ordered,
450 but not yet billed), as cust_main objects.
454 sub ordered_cust_main {
455 shift->cust_main_sql(FS::cust_main->ordered_sql);
459 =item num_active_cust_main
461 Returns the number of active customers for this agent (customers with active
466 sub num_active_cust_main {
467 shift->num_sql(FS::cust_main->active_sql);
470 =item active_cust_main
472 Returns the active customers for this agent, as cust_main objects.
476 sub active_cust_main {
477 shift->cust_main_sql(FS::cust_main->active_sql);
480 =item num_inactive_cust_main
482 Returns the number of inactive customers for this agent (customers with no
483 active recurring packages, but otherwise unsuspended/uncancelled).
487 sub num_inactive_cust_main {
488 shift->num_sql(FS::cust_main->inactive_sql);
491 =item inactive_cust_main
493 Returns the inactive customers for this agent, as cust_main objects.
497 sub inactive_cust_main {
498 shift->cust_main_sql(FS::cust_main->inactive_sql);
502 =item num_susp_cust_main
504 Returns the number of suspended customers for this agent.
508 sub num_susp_cust_main {
509 shift->num_sql(FS::cust_main->susp_sql);
514 Returns the suspended customers for this agent, as cust_main objects.
519 shift->cust_main_sql(FS::cust_main->susp_sql);
522 =item num_cancel_cust_main
524 Returns the number of cancelled customer for this agent.
528 sub num_cancel_cust_main {
529 shift->num_sql(FS::cust_main->cancel_sql);
532 =item cancel_cust_main
534 Returns the cancelled customers for this agent, as cust_main objects.
538 sub cancel_cust_main {
539 shift->cust_main_sql(FS::cust_main->cancel_sql);
542 =item num_active_cust_pkg
544 Returns the number of active customer packages for this agent.
548 sub num_active_cust_pkg {
549 shift->num_pkg_sql(FS::cust_pkg->active_sql);
553 my( $self, $sql ) = @_;
555 "SELECT COUNT(*) FROM cust_pkg LEFT JOIN cust_main USING ( custnum )".
556 " WHERE agentnum = ? AND $sql";
557 my $sth = dbh->prepare($statement) or die dbh->errstr." preparing $statement";
558 $sth->execute($self->agentnum) or die $sth->errstr. "executing $statement";
559 $sth->fetchrow_arrayref->[0];
562 =item num_inactive_cust_pkg
564 Returns the number of inactive customer packages (one-time packages otherwise
565 unsuspended/uncancelled) for this agent.
569 sub num_inactive_cust_pkg {
570 shift->num_pkg_sql(FS::cust_pkg->inactive_sql);
573 =item num_susp_cust_pkg
575 Returns the number of suspended customer packages for this agent.
579 sub num_susp_cust_pkg {
580 shift->num_pkg_sql(FS::cust_pkg->susp_sql);
583 =item num_cancel_cust_pkg
585 Returns the number of cancelled customer packages for this agent.
589 sub num_cancel_cust_pkg {
590 shift->num_pkg_sql(FS::cust_pkg->cancel_sql);
593 =item generate_reg_codes NUM PKGPART_ARRAYREF
595 Generates the specified number of registration codes, allowing purchase of the
596 specified package definitions. Returns an array reference of the newly
597 generated codes, or a scalar error message.
601 #false laziness w/prepay_credit::generate
602 sub generate_reg_codes {
603 my( $self, $num, $pkgparts ) = @_;
605 my @codeset = ( 'A'..'Z' );
607 local $SIG{HUP} = 'IGNORE';
608 local $SIG{INT} = 'IGNORE';
609 local $SIG{QUIT} = 'IGNORE';
610 local $SIG{TERM} = 'IGNORE';
611 local $SIG{TSTP} = 'IGNORE';
612 local $SIG{PIPE} = 'IGNORE';
614 my $oldAutoCommit = $FS::UID::AutoCommit;
615 local $FS::UID::AutoCommit = 0;
620 my $reg_code = new FS::reg_code {
621 'agentnum' => $self->agentnum,
622 'code' => join('', map($codeset[int(rand $#codeset)], (0..7) ) ),
624 my $error = $reg_code->insert($pkgparts);
626 $dbh->rollback if $oldAutoCommit;
629 push @codes, $reg_code->code;
632 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
640 Returns the number of unused registration codes for this agent.
646 my $sth = dbh->prepare(
647 "SELECT COUNT(*) FROM reg_code WHERE agentnum = ?"
648 ) or die dbh->errstr;
649 $sth->execute($self->agentnum) or die $sth->errstr;
650 $sth->fetchrow_arrayref->[0];
653 =item num_prepay_credit
655 Returns the number of unused prepaid cards for this agent.
659 sub num_prepay_credit {
661 my $sth = dbh->prepare(
662 "SELECT COUNT(*) FROM prepay_credit WHERE agentnum = ?"
663 ) or die dbh->errstr;
664 $sth->execute($self->agentnum) or die $sth->errstr;
665 $sth->fetchrow_arrayref->[0];
670 Returns the number of non-disabled sales people for this agent.
676 my $sth = dbh->prepare(
677 "SELECT COUNT(*) FROM sales WHERE agentnum = ?
678 AND ( disabled = '' OR disabled IS NULL )"
679 ) or die dbh->errstr;
680 $sth->execute($self->agentnum) or die $sth->errstr;
681 $sth->fetchrow_arrayref->[0];
690 L<FS::Record>, L<FS::agent_type>, L<FS::cust_main>, L<FS::part_pkg>,
691 schema.html from the base documentation.