RT# 83450 - fixed rateplan export
[freeside.git] / FS / FS / part_export / pbxware.pm
index e647dce..e535e6e 100644 (file)
@@ -5,7 +5,7 @@ use strict;
 
 use Tie::IxHash;
 use LWP::UserAgent;
-use JSON;
+use Cpanel::JSON::XS;
 use HTTP::Request::Common;
 use Digest::MD5 qw(md5_hex);
 use FS::Record qw(dbh);
@@ -17,9 +17,12 @@ our $DEBUG = 0;
 # our $DEBUG = 2; # log requests and content of replies
 
 tie my %options, 'Tie::IxHash',
-  'apikey'  => { label => 'API key' },
-  'debug'   => { label => 'Enable debugging', type => 'checkbox', value => 1 },
-; # best. API. ever.
+  'apikey'   => { label => 'API key' },
+  'debug'    => { label => 'Enable debugging', type => 'checkbox', value => 1 },
+  'ext'      => { label => 'PBXware "ext" field in CDR download request', },
+  'cdrtype'  => { label => 'PBXware "cdrtype" field in CDR download request', },
+  'trunkdst' => { label => 'PBXware "trunkdst" field in CDR download request', },
+;
 
 our %info = (
   'svc'         => [qw(svc_phone)],
@@ -28,7 +31,7 @@ our %info = (
   'notes' => <<'END'
 <P>Export to <a href="www.bicomsystems.com/pbxware-3-8">Bicom PBXware</a> 
 softswitch.</P>
-<P><I>This export does not provision services.</I> Currently you will need
+<P><I>This export does not yet provision services.</I> Currently you will need
 to provision trunks and extensions through PBXware. The export only downloads 
 CDRs.</P>
 <P>Set the export machine to the name or IP address of your PBXware server,
@@ -57,7 +60,7 @@ returns an error message. If there are no new CDRs, returns nothing.
 # map their column names to cdr fields
 # (warning: API docs are not quite accurate here)
 our %column_map = (
-  'Tenant'      => 'subscriber',
+  'Tenant'      => 'accountcode',
   'From'        => 'src',
   'To'          => 'dst',
   'Date/Time'   => 'startdate',
@@ -96,6 +99,10 @@ sub import_cdrs {
     end       => $ed->strftime('%b-%d-%Y'),
     endtime   => $ed->strftime('%H:%M:%S'),
   );
+
+  $opt{$_} = $self->option($_)
+    for grep length( $self->option($_) ), qw( ext cdrtype trunkdst );
+
   # unlike Certain Other VoIP providers, this one does proper pagination if
   # the result set is too big to fit in a single chunk.
   my $page = 1;
@@ -131,7 +138,7 @@ sub import_cdrs {
       # page's IDs or something.
       my $uniqueid = md5_hex(join(',',@$row));
       if ( FS::cdr->row_exists('uniqueid = ?', $uniqueid) ) {
-        warn "skipped duplicate row in page $page\n" if $DEBUG > 1;
+        warn "skipped duplicate row in page $page\n" if $DEBUG;
         next CDR;
       }
 
@@ -140,6 +147,11 @@ sub import_cdrs {
         uniqueid    => $uniqueid,
       );
       @hash{@names} = @$row;
+      # strip non-numeric junk that sometimes gets appended to these (it 
+      # causes problems creating Freeside detail records)
+      foreach (qw(src dst)) {
+        $hash{$_} =~ s/\D*$//;
+      }
 
       my $cdr = FS::cdr->new(\%hash);
       $error = $cdr->insert;
@@ -163,6 +175,10 @@ sub api_request {
   my $self = shift;
   my ($method, $content) = @_;
   $DEBUG ||= 1 if $self->option('debug');
+
+# kludge to curb excessive paranoia in LWP 6.0+
+local $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
+
   my $url = 'https://' . $self->machine;
   my $request = POST($url,
     [ %$content,
@@ -171,7 +187,7 @@ sub api_request {
     ]
   );
   warn "$me $method\n" if $DEBUG;
-  warn $request->as_string."\n" if $DEBUG > 1;
+  warn $request->as_string."\n" if $DEBUG;
 
   my $ua = LWP::UserAgent->new;
   my $response = $ua->request($request);