5 use Date::Format 'time2str';
6 use Date::Parse 'str2time';
9 use Net::HTTPS::Any qw(https_post https_get);
12 use FS::Record qw(qsearchs dbh);
13 use FS::UID qw(adminsuidsetup);
17 my $host = "cdr.teleapi.net";
19 my @now = localtime();
20 my $now = timelocal($now[0],$now[1],$now[2],$now[3],$now[4],$now[5]); #most recent midnight
24 freeside-cdr-telapi-import -t type -p token -s startdate [-e enddate] freesideuser
26 Downloads any existing CDR voip files or CDR SMS files (type) from the start date untill the enddate and
27 imports those records.";
30 my ($type,$token,$startdate,$enddate);
34 "startdate=s" => \$startdate,
35 "enddate=s" => \$enddate,
38 my $fsuser = $ARGV[-1];
40 die usage() unless $fsuser;
42 adminsuidsetup($fsuser);
44 my ( $page, $response, %reply_headers )= https_get(
47 'path' => '/'.$type.'/'.$startdate.'/'.$enddate.'?token='.$token,
50 die "Bad response from telapi server: $response"
51 unless $response =~ /^200/;
53 my $cdrbatch = "Telapi-import-" . $type . "-" . time2str('%Y/%m/%d-%T',$now);
55 my $dir = $FS::UID::cache_dir. "/cache.". $FS::UID::datasrc;
57 my $cfh = new File::Temp( TEMPLATE => 'telapi.XXXXXXXX',
61 or die "can't open temporary file to save data: $!\n";
63 #print returned data to file handle for temp file.
68 print "Importing batch $cdrbatch\n";
69 my $error = FS::cdr::batch_import({
70 'batch_namevalue' => $cdrbatch,
71 'file' => $cfh->filename,
72 'format' => 'telapi_'.$type