5 use Date::Format 'time2str';
6 use Date::Parse 'str2time';
9 use Net::HTTPS::Any qw(https_post);
11 use FS::Record qw(qsearchs dbh);
12 use FS::UID qw(adminsuidsetup);
16 freeside-cdr-portaone-import -h 'your.domain.com:443' -u switchusername -p switchpass
17 [-s startdate] [-e enddate] [-v] freesideuser
21 my ($host,$username,$password,$startdate,$enddate,$verbose);
23 "enddate=s" => \$enddate,
25 "password=s" => \$password,
26 "startdate=s" => \$startdate,
27 "username=s" => \$username,
28 "verbose" => \$verbose,
31 my $fsuser = $ARGV[-1];
33 die usage() unless $host && $password && $username && $fsuser;
35 adminsuidsetup($fsuser);
38 if ($host =~ /^(.*)\:(.*)$/) {
44 $startdate = str2time($startdate) or die "Can't parse startdate $startdate";
45 $startdate = time2str("%Y-%m-%d %H:%M:%S",$startdate);
48 my $lastbatch = qsearchs({
49 'table' => 'cdr_batch',
50 'hashref' => { 'cdrbatch' => {op=>'like', value=>'portaone-import%'}},
51 'order_by' => 'ORDER BY _date DESC LIMIT 1',
53 $startdate = time2str("%Y-%m-%d %H:%M:%S", $lastbatch->_date + 1) if $lastbatch;
55 $startdate ||= '2010-01-01 00:00:00'; #seems decently in the past
59 $enddate = str2time($enddate) or die "Can't parse enddate $enddate";
61 $enddate = time2str("%Y-%m-%d %H:%M:%S",$enddate);
63 $enddate ||= time2str("%Y-%m-%d %H:%M:%S",$now);
66 $FS::UID::AutoCommit = 0;
68 my $cdr_batch = new FS::cdr_batch({
69 'cdrbatch' => 'portaone-import-'. time2str('%Y/%m/%d-%T',$now),
72 my $error = $cdr_batch->insert;
75 die "Error creating batch: $error";
78 print "Downloading records from $startdate to $enddate\n" if $verbose;
80 my $auth_info; # needs to be declared undef for call_api
81 $auth_info = call_api('Session','login',{
83 'password' => $password,
87 my $custlist = call_api('Customer','get_customer_list');
88 my @custnum = map { $_->{'i_customer'} } @{$custlist->{'customer_list'}};
89 foreach my $custnum (@custnum) {
90 print "Retrieving for customer $custnum\n" if $verbose;
91 my $step = 500; # too many records was crashing server, so we request in chunks
92 my $lastcount = $step; # to get the while loop rolling
93 my $totalcount = 0; # for verbose display only
95 while ($lastcount == $step) {
96 my $xdrs = call_api('Customer','get_customer_xdrs',{
97 'i_customer' => $custnum,
98 'from_date' => $startdate,
99 'to_date' => $enddate,
104 my @xdrs = @{$xdrs->{'xdr_list'}};
105 print "Inserting ".@xdrs." records\n" if $verbose && @xdrs;
106 foreach my $xdr (@xdrs) {
107 my $desc = $xdr->{'country'};
108 if ($xdr->{'subdivision'}) {
109 $desc = ', ' . $desc if $desc;
110 $desc = $xdr->{'subdivision'} . $desc;
112 if ($xdr->{'description'}) {
113 $desc = ' (' . $desc . ')' if $desc;
114 $desc = $xdr->{'description'} . $desc;
116 my $cdr = FS::cdr->new ({
117 'cdrbatchnum' => $cdr_batch->cdrbatchnum,
118 'uniqueid' => $xdr->{'i_xdr'},
119 'src' => $xdr->{'CLI'},
120 'dst' => $xdr->{'CLD'},
121 'upstream_price' => $xdr->{'charged_amount'},
122 'startdate' => $xdr->{'unix_connect_time'},
123 'enddate' => $xdr->{'unix_disconnect_time'},
124 'accountcode' => $xdr->{'account_id'},
125 'billsec' => $xdr->{'charged_quantity'},
126 'upstream_dst_regionname' => $desc,
128 $error = $cdr->insert;
131 die "Error inserting cdr: $error";
134 $totalcount += @xdrs;
137 } #while $lastcount == $step
138 print scalar($totalcount) . " xdrs retrieved\n" if $verbose;
141 call_api('Session','logout',$auth_info);
143 ### Full list of fields returned by API:
144 #i_xdr int The unique ID of the xdr record
145 #account_id int The unique ID of the account database record
146 #CLI string Calling Line Identification
147 #CLD string Called Line Identification
148 #charged_amount float Amount charged
149 #charged_quantity int Units charged
150 #country string Country
151 #subdivision string Country subdivision
152 #description string Destination description
153 #disconnect_cause string The code of disconnect cause
154 #bill_status string Call bill status
155 #disconnect_reason string Call disconnect reason
156 #connect_time dateTime Call connect time
157 #unix_connect_time int Call connect time (expressed in Unix time format - seconds since epoch)
158 #disconnect_time dateTime Call disconnect time
159 #unix_disconnect_time int Call disconnect time (expressed in Unix time format - seconds since epoch)
160 #bill_time dateTime Call bill time
161 #bit_flags int Extended information how the service was used; the integer field that should be treated as a bit-map. Each currently used bit is listed in the Transaction_Flag_Types table (bit_offset indicates position)
162 #call_recording_url string Path to recorded .wav files
163 #call_recording_server_url string URL to the recording server
170 my ($service,$method,$params) = @_;
171 my %auth_info = $auth_info ? ('auth_info' => encode_json($auth_info)) : ();
173 print "Calling $service/$method\n" if $verbose;
174 my ( $page, $response, %reply_headers ) = https_post(
177 'path' => '/rest/'.$service.'/'.$method.'/',
178 'args' => [ %auth_info, 'params' => encode_json($params) ],
180 return decode_json($page) if $response eq '200 OK';
182 if ($response =~ /^500/) {
183 my $error = decode_json($page);
184 die "Server returned error during $service/$method: ".$error->{'faultstring'}
185 if $error->{'faultcode'};
187 die "Bad response from server during $service/$method: $response";