1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
package FS::ClientAPI::Agent;
#some false laziness w/MyAccount
use strict;
use vars qw($cache);
use subs qw(_cache);
use Digest::MD5 qw(md5_hex);
use FS::Record qw(qsearchs); # qsearch dbdef dbh);
use FS::ClientAPI_SessionCache;
use FS::agent;
use FS::cust_main qw(smart_search);
sub _cache {
$cache ||= new FS::ClientAPI_SessionCache( {
'namespace' => 'FS::ClientAPI::Agent',
} );
}
sub agent_login {
my $p = shift;
#don't allow a blank login to first unconfigured agent with no user/pass
return { error => 'Must specify your reseller username and password.' }
unless length($p->{'username'}) && length($p->{'password'});
my $agent = qsearchs( 'agent', {
'username' => $p->{'username'},
'_password' => $p->{'password'},
} );
unless ( $agent ) { return { error => 'Incorrect password.' } }
my $session = {
'agentnum' => $agent->agentnum,
'agent' => $agent->agent,
};
my $session_id;
do {
$session_id = md5_hex(md5_hex(time(). {}. rand(). $$))
} until ( ! defined _cache->get($session_id) ); #just in case
_cache->set( $session_id, $session, '1 hour' );
{ 'error' => '',
'session_id' => $session_id,
};
}
sub agent_logout {
my $p = shift;
if ( $p->{'session_id'} ) {
_cache->remove($p->{'session_id'});
return { 'error' => '' };
} else {
return { 'error' => "Can't resume session" }; #better error message
}
}
sub agent_info {
my $p = shift;
my $session = _cache->get($p->{'session_id'})
or return { 'error' => "Can't resume session" }; #better error message
#my %return;
my $agentnum = $session->{'agentnum'};
my $agent = qsearchs( 'agent', { 'agentnum' => $agentnum } )
or return { 'error' => "unknown agentnum $agentnum" };
{ 'error' => '',
'agentnum' => $agentnum,
'agent' => $agent->agent,
'num_prospect' => $agent->num_prospect_cust_main,
'num_active' => $agent->num_active_cust_main,
'num_susp' => $agent->num_susp_cust_main,
'num_cancel' => $agent->num_cancel_cust_main,
#%return,
};
}
sub agent_list_customers {
my $p = shift;
my $session = _cache->get($p->{'session_id'})
or return { 'error' => "Can't resume session" }; #better error message
#my %return;
my $agentnum = $session->{'agentnum'};
my $agent = qsearchs( 'agent', { 'agentnum' => $agentnum } )
or return { 'error' => "unknown agentnum $agentnum" };
my @cust_main = smart_search( 'search' => $p->{'search'},
'agentnum' => $agentnum,
);
#aggregate searches
push @cust_main,
map $agent->$_(), map $_.'_cust_main',
grep $p->{$_}, qw( prospect active susp cancel );
#eliminate dups?
my %saw = ();
@cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
{ customers => [ map {
my $cust_main = $_;
my $hashref = $cust_main->hashref;
$hashref->{$_} = $cust_main->$_()
foreach qw(name status statuscolor);
delete $hashref->{$_} foreach qw( payinfo paycvv );
$hashref;
} @cust_main
],
}
}
1;
|