delete fees, RT#81713
[freeside.git] / FS / bin / freeside-cdr-telapi-import
1 #!/usr/bin/perl
2
3 use strict;
4
5 use Date::Format 'time2str';
6 use Date::Parse 'str2time';
7 use Getopt::Long;
8 use Cpanel::JSON::XS;
9 use Net::HTTPS::Any qw(https_post https_get);
10 use Time::Local;
11
12 use FS::Record qw(qsearchs dbh);
13 use FS::UID qw(adminsuidsetup);
14 use FS::cdr;
15 use FS::cdr_batch;
16
17 my $host = "cdr.teleapi.net";
18
19 my @now = localtime();
20 my $now = timelocal($now[0],$now[1],$now[2],$now[3],$now[4],$now[5]); #most recent midnight
21
22 sub usage {
23   "Usage:
24 freeside-cdr-telapi-import -t type -p token -s startdate [-e enddate] freesideuser
25
26 Downloads any existing CDR voip files or CDR SMS files (type) from the start date untill the enddate and 
27 imports those records.";
28 }
29
30 my ($type,$token,$startdate,$enddate);
31 GetOptions(
32   "type=s"      => \$type,
33   "token=s"     => \$token,
34   "startdate=s" => \$startdate,
35   "enddate=s"   => \$enddate,
36 );
37
38 $startdate = str2time($startdate) or die "can't parse start date $startdate\n";
39   $startdate = time2str('%m-%d-%Y', $startdate);
40 $enddate = str2time($enddate) or die "can't parse start date $enddate\n";
41   $enddate = time2str('%m-%d-%Y', $enddate);
42
43 my $fsuser = $ARGV[-1];
44
45 die usage() unless $fsuser;
46
47 adminsuidsetup($fsuser);
48
49 my ( $page, $response, %reply_headers )= https_get(
50       'host' => $host,
51       'port' => '443',
52       'path' => '/'.$type.'/'.$startdate.'/'.$enddate.'?token='.$token,
53     );
54
55 die "Bad response from telapi server: $response"
56   unless $response =~ /^200/;
57
58 my $cdrbatch = "Telapi-import-" . $type . "-" . time2str('%Y/%m/%d-%T',$now);  
59
60 my $dir = $FS::UID::cache_dir. "/cache.". $FS::UID::datasrc; 
61
62 my $cfh = new File::Temp( TEMPLATE => 'telapi.XXXXXXXX',
63                           SUFFIX   => '.csv',
64                           DIR      => $dir,
65                          )
66     or die "can't open temporary file to save data: $!\n";
67
68 #print returned data to file handle for temp file.
69 print $cfh $page;
70
71 seek($cfh,0,0);
72
73   warn "Importing batch $cdrbatch\n";
74   my $error = FS::cdr::batch_import({
75     'batch_namevalue' => $cdrbatch,
76     'file'            => $cfh->filename,
77     'format'          => 'telapi_'.$type
78   });
79
80   warn "Error importing CDR's\n".$error if $error;
81
82 exit;