6 use File::Temp 'tempdir';
7 use Net::SFTP::Foreign::Compat;
8 use FS::UID qw(adminsuidsetup datasrc dbh);
11 use FS::Record qw(qsearch qsearchs);
12 use Date::Format 'time2str';
13 use Date::Parse 'str2time';
20 use vars qw( $opt_d $opt_v $opt_c $opt_s $opt_e $opt_a );
23 my ($user, $login, $password) = @ARGV;
24 ($user and $login and $password) or die &usage;
26 my $dbh = adminsuidsetup $user;
27 $FS::UID::AutoCommit = 0;
29 # index already-downloaded batches
30 my @previous = qsearch({
31 'table' => 'cdr_batch',
32 'hashref' => { 'cdrbatch' => {op=>'like', value=>'ani_networks%'} },
33 'order_by' => 'ORDER BY cdrbatch DESC',
35 my %exists = map {$_->cdrbatch => 1} @previous;
37 my $format = 'ani_networks';
38 my $host = 'arkftp.aninetworks.com';
44 warn "Retrieving directory listing\n" if $opt_v;
48 ## get the current working dir
52 $sftp->setcwd($cwd . '/CDR') or die "can't chdir to $cwd/CDR\n";
54 my $ls = $sftp->ls('.', wanted => qr/^UYM.*.zip$/i, names_only =>1 );
57 warn scalar(@files)." CDR files found.\n" if $opt_v;
60 my $most_recent = $previous[0];
62 if ($most_recent->cdrbatch =~ /^ani_networks-(\d+)/) {
64 warn "limiting to dates > $date (from most recent batch)\n" if $opt_v;
65 @files = grep { /^*Daily_(\d+)_/ && $1 > $date } @files;
67 } # else download them all
71 # normalize date format
72 $opt_s = time2str('%Y%m%d', str2time($opt_s)) if $opt_s =~ /\D/;
73 warn "limiting to dates > $opt_s\n" if $opt_v;
74 @files= grep { /^*Daily_(\d+)_/ && $1 >= $opt_s } @files;
78 $opt_e = time2str('%Y%m%d', str2time($opt_e)) if $opt_e =~ /\D/;
79 warn "limiting to dates < $opt_e\n" if $opt_v;
80 @files= grep { /^*Daily_(\d+)_/ && $1 < $opt_e } @files;
82 warn scalar(@files) ." files to be downloaded\n" if $opt_v;
83 foreach my $file (@files) {
85 my $tmpdir = tempdir( CLEANUP => $opt_v );
87 warn "downloading $file to $tmpdir\n" if $opt_v;
89 $sftp->get($file, "$tmpdir/$file");
92 if(system ("unzip $tmpdir/$file -d $tmpdir") != 0) {
93 unlink "$tmpdir/$file";
94 my $error = "unzip of '$tmpdir/$file' failed\n";
103 warn "processing $file\n" if $opt_v;
105 my $batchname = "$format-$file";
106 if ($exists{$batchname}) {
107 warn "already imported $file\n";
111 my $unzipped_file = $file;
112 $unzipped_file =~ s/.zip/.txt/i;
114 warn "going to import file $unzipped_file" if $opt_v;
116 my $import_options = {
117 'file' => "$tmpdir/$unzipped_file",
119 'batch_namevalue' => $batchname,
122 $import_options->{'cdrtypenum'} = $opt_c if $opt_c;
124 my $error = FS::cdr::batch_import($import_options);
127 die "error processing $unzipped_file: $error\n";
130 warn "finished\n" if $opt_v;
138 "Usage: \n freeside-cdr-aninetworks-import [ options ] user login password
141 -d: enable FTP debugging (very noisy)
142 -c num: apply a cdrtypenum to the imported CDRs
145 -a: automatically choose start date from most recently downloaded batch
153 return $sftp if $sftp && $sftp->cwd;
155 my %sftp = ( host => $host,
157 password => $password,
158 more => [-o => 'StrictHostKeyChecking no'],
161 $sftp = Net::SFTP::Foreign->new(%sftp);
162 $sftp->error and die "SFTP connection failed: ". $sftp->error;