use Date::Format 'time2str';
use Date::Parse 'str2time';
use Getopt::Long;
-use JSON;
+use Cpanel::JSON::XS;
use Net::HTTPS::Any qw(https_post);
+use Time::Local;
use FS::Record qw(qsearchs dbh);
use FS::UID qw(adminsuidsetup);
+use FS::cdr;
+use FS::cdr_batch;
sub usage {
"Usage:
+freeside-cdr-portaone-import -x exportnum [-s startdate] [-e enddate] [-v] freesideuser
+
freeside-cdr-portaone-import -h 'your.domain.com:443' -u switchusername -p switchpass
[-s startdate] [-e enddate] [-v] freesideuser
";
}
-my ($host,$username,$password,$startdate,$enddate,$verbose);
+my ($host,$username,$password,$startdate,$enddate,$verbose,$exportnum);
GetOptions(
"enddate=s" => \$enddate,
"host=s" => \$host,
"startdate=s" => \$startdate,
"username=s" => \$username,
"verbose" => \$verbose,
+ "x=s" => \$exportnum,
);
my $fsuser = $ARGV[-1];
-die usage() unless $host && $password && $username && $fsuser;
+die usage() unless $fsuser;
adminsuidsetup($fsuser);
$port = $2;
}
+if ($exportnum) {
+ my $export = qsearchs('part_export', { 'exportnum' => $exportnum });
+ die "Export not found" unless $export;
+ $host = $export->machine;
+ $port = $export->option('port');
+ $username = $export->option('username');
+ $password = $export->option('password');
+ die "Auth info not fully specified in export"
+ unless $host && $port && $username && $password;
+}
+
+die usage() unless $host && $password && $username;
+
if ($startdate) {
$startdate = str2time($startdate) or die "Can't parse startdate $startdate";
$startdate = time2str("%Y-%m-%d %H:%M:%S",$startdate);
'hashref' => { 'cdrbatch' => {op=>'like', value=>'portaone-import%'}},
'order_by' => 'ORDER BY _date DESC LIMIT 1',
});
- $startdate = time2str("%Y-%m-%d %H:%M:%S", $lastbatch->_date + 1) if $lastbatch;
+ $startdate = time2str("%Y-%m-%d %H:%M:%S", $lastbatch->_date) if $lastbatch;
}
$startdate ||= '2010-01-01 00:00:00'; #seems decently in the past
-my $now = time;
+my @now = localtime();
+my $now = timelocal(0,0,0,$now[3],$now[4],$now[5]); #most recent midnight
if ($enddate) {
$enddate = str2time($enddate) or die "Can't parse enddate $enddate";
$now = $enddate;
$FS::UID::AutoCommit = 0;
+my $cdrbatchname = 'portaone-import-'. time2str('%Y/%m/%d-%T',$now);
+die "Batch $cdrbatchname already exists, please specify a different end date. " . usage()
+ if FS::cdr_batch->row_exists('cdrbatch = ?', $cdrbatchname);
my $cdr_batch = new FS::cdr_batch({
- 'cdrbatch' => 'portaone-import-'. time2str('%Y/%m/%d-%T',$now),
+ 'cdrbatch' => $cdrbatchname,
'_date' => $now,
});
my $error = $cdr_batch->insert;
'offset' => $offset,
});
my @xdrs = @{$xdrs->{'xdr_list'}};
- print "Inserting ".@xdrs." records\n" if $verbose && @xdrs;
+ 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;
die "Error inserting cdr: $error";
}
} #foreach $xdr
- $totalcount += @xdrs;
+ print "Skipped $skipped duplicate records\n" if $verbose && $skipped;
+ $totalcount += @xdrs - $skipped;
$lastcount = @xdrs;
$offset += $step;
} #while $lastcount == $step
- print scalar($totalcount) . " xdrs retrieved\n" if $verbose;
+ print scalar($totalcount) . " records inserted\n" if $verbose;
} #foreach $custnum
call_api('Session','logout',$auth_info);