1 package FS::ClientAPI::Agent;
3 #some false laziness w/MyAccount
8 use Digest::MD5 qw(md5_hex);
9 use FS::Record qw(qsearchs); # qsearch dbdef dbh);
10 use FS::ClientAPI_SessionCache;
12 use FS::cust_main::Search qw(smart_search);
17 $cache ||= new FS::ClientAPI_SessionCache( {
18 'namespace' => 'FS::ClientAPI::Agent',
25 my $conf = new FS::Conf;
26 return { error=>'Disabled' } unless $conf->exists('selfservice-agent_signup');
28 #add a customer record and set agent_custnum?
30 my $agent = new FS::agent {
31 'typenum' => $conf->config('selfservice-agent_signup-agent_type'),
32 'agent' => $p->{'agent'},
33 'username' => $p->{'username'},
34 '_password' => $p->{'password'},
38 my $error = $agent->insert;
40 return { 'error' => $error } if $error;
42 agent_login({ 'username' => $p->{'username'},
43 'password' => $p->{'password'},
50 #don't allow a blank login to first unconfigured agent with no user/pass
51 return { error => 'Must specify your reseller username and password.' }
52 unless length($p->{'username'}) && length($p->{'password'});
54 my $agent = qsearchs( 'agent', {
55 'username' => $p->{'username'},
56 '_password' => $p->{'password'},
59 unless ( $agent ) { return { error => 'Incorrect password.' } }
62 'agentnum' => $agent->agentnum,
63 'agent' => $agent->agent,
68 $session_id = md5_hex(md5_hex(time(). {}. rand(). $$))
69 } until ( ! defined _cache->get($session_id) ); #just in case
71 _cache->set( $session_id, $session, '1 hour' );
74 'session_id' => $session_id,
80 if ( $p->{'session_id'} ) {
81 _cache->remove($p->{'session_id'});
82 return { 'error' => '' };
84 return { 'error' => "Can't resume session" }; #better error message
91 my $session = _cache->get($p->{'session_id'})
92 or return { 'error' => "Can't resume session" }; #better error message
96 my $agentnum = $session->{'agentnum'};
98 my $agent = qsearchs( 'agent', { 'agentnum' => $agentnum } )
99 or return { 'error' => "unknown agentnum $agentnum" };
102 'agentnum' => $agentnum,
103 'agent' => $agent->agent,
104 'num_prospect' => $agent->num_prospect_cust_main,
105 'num_active' => $agent->num_active_cust_main,
106 'num_susp' => $agent->num_susp_cust_main,
107 'num_cancel' => $agent->num_cancel_cust_main,
113 sub agent_list_customers {
116 my $session = _cache->get($p->{'session_id'})
117 or return { 'error' => "Can't resume session" }; #better error message
121 my $agentnum = $session->{'agentnum'};
123 my $agent = qsearchs( 'agent', { 'agentnum' => $agentnum } )
124 or return { 'error' => "unknown agentnum $agentnum" };
126 my @cust_main = smart_search( 'search' => $p->{'search'},
127 'agentnum' => $agentnum,
132 map $agent->$_(), map $_.'_cust_main',
133 grep $p->{$_}, qw( prospect active susp cancel );
137 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
139 { customers => [ map {
141 my $hashref = $cust_main->hashref;
142 $hashref->{$_} = $cust_main->$_()
143 foreach qw(name status statuscolor status_label);
144 delete $hashref->{$_} foreach qw( payinfo paycvv );
154 my($session, $agentnum, $svc_acct) = _session_agentnum_svc_acct($p);
155 return { 'error' => $session } unless ref($session);
158 #'username' => $username,
159 #'domain' => $domain,
160 'available' => $svc_acct ? 0 : 1,
165 sub _session_agentnum_svc_acct {
168 my $session = _cache->get($p->{'session_id'})
169 or return "Can't resume session"; #better error message
171 my $username = $p->{'username'};
173 #XXX some way to default this per agent (by default product's service def?)
174 my $domain = $p->{'domain'};
176 my $svc_domain = qsearchs('svc_domain', { 'domain' => $domain } )
177 or return { 'error' => 'Unknown domain' };
179 my $svc_acct = qsearchs('svc_acct', { 'username' => $username,
180 'domsvc' => $svc_domain->svcnum, } );
182 ( $session, $session->{'agentnum'}, $svc_acct );
186 sub _session_agentnum_cust_pkg {
188 my($session, $agentnum, $svc_acct) = _session_agentnum_svc_acct($p);
189 return $session unless ref($session);
190 return 'Account not found' unless $svc_acct;
191 my $cust_svc = $svc_acct->cust_svc;
192 return 'Unlinked account' unless $cust_svc->pkgnum;
193 my $cust_pkg = $cust_svc->cust_pkg;
194 return 'Not your account' unless $cust_pkg->cust_main->agentnum == $agentnum;
195 ($session, $agentnum, $cust_pkg);
198 sub suspend_username {
200 my($session, $agentnum, $cust_pkg) = _session_agentnum_cust_pkg($p);
201 return { 'error' => $session } unless ref($session);
203 return { 'error' => $cust_pkg->suspend };
206 sub unsuspend_username {
208 my($session, $agentnum, $cust_pkg) = _session_agentnum_cust_pkg($p);
209 return { 'error' => $session } unless ref($session);
211 return { 'error' => $cust_pkg->unsuspend };