- print "Processing customer $custnum\n" if $verbose;
- my $xdrs = call_api('Customer','get_customer_xdrs',{
- 'i_customer' => $custnum,
- 'from_date' => $startdate,
- 'to_date' => $enddate,
- 'cdr_entity' => 'C', # can also be 'A', or blank for both
- });
- my @xdrs = @{$xdrs->{'xdr_list'}};
- print scalar(@xdrs) . " xdrs retrieved\n" if $verbose;
- $results->{$custnum} = \@xdrs;
-}
+ print "Retrieving for customer $custnum\n" if $verbose;
+ my $step = 500; # too many records was crashing server, so we request in chunks
+ my $lastcount = $step; # to get the while loop rolling
+ my $totalcount = 0; # for verbose display only
+ my $offset = 0;
+ while ($lastcount == $step) {
+ my $xdrs = call_api('Customer','get_customer_xdrs',{
+ 'i_customer' => $custnum,
+ 'from_date' => $startdate,
+ 'to_date' => $enddate,
+ 'cdr_entity' => 'A',
+ 'limit' => $step,
+ 'offset' => $offset,
+ });
+ my @xdrs = @{$xdrs->{'xdr_list'}};
+ print "Retrieved ".@xdrs." records\n" if $verbose && @xdrs;
+ my $skipped = 0; # for verbose display only
+ foreach my $xdr (@xdrs) {
+ if ( FS::cdr->row_exists('uniqueid = ?', $xdr->{'i_xdr'}) ) {
+ $skipped += 1;
+ next;
+ }
+ my $desc = $xdr->{'country'};
+ if ($xdr->{'subdivision'}) {
+ $desc = ', ' . $desc if $desc;
+ $desc = $xdr->{'subdivision'} . $desc;
+ }
+ if ($xdr->{'description'}) {
+ $desc = ' (' . $desc . ')' if $desc;
+ $desc = $xdr->{'description'} . $desc;
+ }
+ my $cdr = FS::cdr->new ({
+ 'cdrbatchnum' => $cdr_batch->cdrbatchnum,
+ 'uniqueid' => $xdr->{'i_xdr'},
+ 'src' => $xdr->{'CLI'},
+ 'dst' => $xdr->{'CLD'},
+ 'upstream_price' => $xdr->{'charged_amount'},
+ 'startdate' => $xdr->{'unix_connect_time'},
+ 'enddate' => $xdr->{'unix_disconnect_time'},
+ 'accountcode' => $xdr->{'account_id'},
+ 'billsec' => $xdr->{'charged_quantity'},
+ 'upstream_dst_regionname' => $desc,
+ });
+ $error = $cdr->insert;
+ if ($error) {
+ dbh->rollback;
+ die "Error inserting cdr: $error";
+ }
+ } #foreach $xdr
+ print "Skipped $skipped duplicate records\n" if $verbose && $skipped;
+ $totalcount += @xdrs - $skipped;
+ $lastcount = @xdrs;
+ $offset += $step;
+ } #while $lastcount == $step
+ print scalar($totalcount) . " records inserted\n" if $verbose;
+} #foreach $custnum