summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--FS/FS/cdr/ani_networks.pm81
-rwxr-xr-xFS/bin/freeside-cdr-aninetworks-import162
2 files changed, 243 insertions, 0 deletions
diff --git a/FS/FS/cdr/ani_networks.pm b/FS/FS/cdr/ani_networks.pm
new file mode 100644
index 0000000..cac30c4
--- /dev/null
+++ b/FS/FS/cdr/ani_networks.pm
@@ -0,0 +1,81 @@
+package FS::cdr::ani_networks;
+use base qw( FS::cdr );
+
+use strict;
+use vars qw( %info );
+use Time::Local;
+
+%info = (
+ 'name' => 'ANI NETWORKS',
+ 'weight' => 60,
+ 'type' => 'fixedlength',
+ 'fixedlength_format' => [qw(
+ call_date_time:14:1:14
+ bill_to_number:15:15:29
+ translate_number:10:30:39
+ originating_number:10:40:49
+ originating_lata:3:50:52
+ originating_city:30:53:82
+ originating_state:2:83:84
+ originating_country:4:85:88
+ terminating_number:15:89:103
+ terminating_lata:3:104:106
+ terminating_city:30:107:136
+ terminating_state:2:137:138
+ terminating_citycode:3:139:141
+ terminating_country:4:142:145
+ call_type:2:146:147
+ call_transport:1:148:148
+ account_code:12:149:160
+ info_digits:2:161:162
+ duration:8:163:170
+ wholesale_amount:9:171:179
+ cic:4:180:183
+ originating_lrn:10:184:193
+ terminating_lrn:10:194:203
+ originating_ocn:4:204:207
+ terminating_ocn:4:208:211
+ )],
+ 'import_fields' => [
+
+ sub { #call_date and time
+ my($cdr, $data, $conf, $param) = @_;
+ $data =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/ or die "unparsable record_date: $data";
+ $cdr->set('calldate', "$2/$3/$1 $4:$5:$6");
+ },
+
+ 'charged_party', #bill to number
+ '', #translate number
+
+ 'src', #originating number
+
+ '', #originating lata
+ '', #originating city
+ '', #originating state
+ '', #originating country
+
+ 'dst', #terminating number
+
+ '', #terminating lata
+ '', #terminating city
+ '', #terminating state
+ '', #terminating city code
+ '', #terminating country
+
+ '', #call type
+ '', #call transport
+ 'accountcode', #account code
+ '', #info digits
+ 'duration', #duration
+ '', #wholesale amount
+ '', #cic
+ 'src_lrn', #originating lrn
+ 'dst_lrn', #terminating lrn
+ '', #originating ocn
+ '', #terminating ocn
+
+ ],
+
+);
+
+1; \ No newline at end of file
diff --git a/FS/bin/freeside-cdr-aninetworks-import b/FS/bin/freeside-cdr-aninetworks-import
new file mode 100755
index 0000000..0fd0659
--- /dev/null
+++ b/FS/bin/freeside-cdr-aninetworks-import
@@ -0,0 +1,162 @@
+#!/usr/bin/perl
+
+use strict;
+use Getopt::Std;
+use Date::Format;
+use File::Temp 'tempdir';
+use Net::SFTP::Foreign::Compat;
+use FS::UID qw(adminsuidsetup datasrc dbh);
+use FS::cdr;
+use FS::cdr_batch;
+use FS::Record qw(qsearch qsearchs);
+use Date::Format 'time2str';
+use Date::Parse 'str2time';
+
+
+###
+# parse command line
+###
+
+use vars qw( $opt_d $opt_v $opt_c $opt_s $opt_e $opt_a );
+getopts('dvc:s:e:a');
+
+my ($user, $login, $password) = @ARGV;
+($user and $login and $password) or die &usage;
+
+my $dbh = adminsuidsetup $user;
+$FS::UID::AutoCommit = 0;
+
+# index already-downloaded batches
+my @previous = qsearch({
+ 'table' => 'cdr_batch',
+ 'hashref' => { 'cdrbatch' => {op=>'like', value=>'ani_networks%'} },
+ 'order_by' => 'ORDER BY cdrbatch DESC',
+});
+my %exists = map {$_->cdrbatch => 1} @previous;
+
+my $format = 'ani_networks';
+my $host = 'arkftp.aninetworks.com';
+
+###
+# get the file list
+###
+
+warn "Retrieving directory listing\n" if $opt_v;
+
+my $sftp = sftp();
+
+## get the current working dir
+my $cwd = $sftp->cwd;
+
+## switch to CDR dir
+$sftp->setcwd($cwd . '/CDR') or die "can't chdir to $cwd/CDR\n";
+
+my $ls = $sftp->ls('.', wanted => qr/^UYM.*.zip$/i, names_only =>1 );
+my @files = @$ls;
+
+warn scalar(@files)." CDR files found.\n" if $opt_v;
+# apply date range
+if ( $opt_a ) {
+ my $most_recent = $previous[0];
+ if ($most_recent) {
+ if ($most_recent->cdrbatch =~ /^ani_networks-(\d+)/) {
+ my $date = $1;
+ warn "limiting to dates > $date (from most recent batch)\n" if $opt_v;
+ @files = grep { /^*Daily_(\d+)_/ && $1 > $date } @files;
+ }
+ } # else download them all
+}
+if ( $opt_s ) {
+ # start date
+ # normalize date format
+ $opt_s = time2str('%Y%m%d', str2time($opt_s)) if $opt_s =~ /\D/;
+ warn "limiting to dates > $opt_s\n" if $opt_v;
+ @files= grep { /^*Daily_(\d+)_/ && $1 >= $opt_s } @files;
+}
+if ( $opt_e ) {
+ # end date
+ $opt_e = time2str('%Y%m%d', str2time($opt_e)) if $opt_e =~ /\D/;
+ warn "limiting to dates < $opt_e\n" if $opt_v;
+ @files= grep { /^*Daily_(\d+)_/ && $1 < $opt_e } @files;
+}
+warn scalar(@files) ." files to be downloaded\n" if $opt_v;
+foreach my $file (@files) {
+
+ my $tmpdir = tempdir( CLEANUP => $opt_v );
+
+ warn "downloading $file to $tmpdir\n" if $opt_v;
+ $sftp = sftp();
+ $sftp->get($file, "$tmpdir/$file");
+
+ ## extract zip file
+ if(system ("unzip $tmpdir/$file -d $tmpdir") != 0) {
+ unlink "$tmpdir/$file";
+ my $error = "unzip of '$tmpdir/$file' failed\n";
+ if ( $opt_s ) {
+ warn $error;
+ next;
+ } else {
+ die $error;
+ }
+ }
+
+ warn "processing $file\n" if $opt_v;
+
+ my $batchname = "$format-$file";
+ if ($exists{$batchname}) {
+ warn "already imported $file\n";
+ next;
+ }
+
+ my $unzipped_file = $file;
+ $unzipped_file =~ s/.zip/.txt/i;
+
+ warn "going to import file $unzipped_file" if $opt_v;
+
+ my $import_options = {
+ 'file' => "$tmpdir/$unzipped_file",
+ 'format' => $format,
+ 'batch_namevalue' => $batchname,
+ 'empty_ok' => 1,
+ };
+ $import_options->{'cdrtypenum'} = $opt_c if $opt_c;
+
+ my $error = FS::cdr::batch_import($import_options);
+
+ if ( $error ) {
+ die "error processing $unzipped_file: $error\n";
+ }
+}
+warn "finished\n" if $opt_v;
+$dbh->commit;
+
+###
+# subs
+###
+
+sub usage {
+ "Usage: \n freeside-cdr-aninetworks-import [ options ] user login password
+ Options:
+ -v: be verbose
+ -d: enable FTP debugging (very noisy)
+ -c num: apply a cdrtypenum to the imported CDRs
+ -s date: start date
+ -e date: end date
+ -a: automatically choose start date from most recently downloaded batch
+
+";
+}
+
+sub sftp {
+
+ #reuse connections
+ return $sftp if $sftp && $sftp->cwd;
+
+ my %sftp = ( host => $host, user => $login, password => $password );
+
+ $sftp = Net::SFTP::Foreign->new(%sftp);
+ $sftp->error and die "SFTP connection failed: ". $sftp->error;
+
+ $sftp;
+}
+