6 use File::Temp 'tempdir';
7 use Net::FTP 2.80; #3.08; 2.79 in deb 8 doesn't work, 3.08 in deb 9 does
8 use IO::Socket::SSL 2.007;
9 use FS::UID qw(adminsuidsetup datasrc dbh);
12 use FS::Record qw(qsearch qsearchs);
13 use Date::Format 'time2str';
14 use Date::Parse 'str2time';
21 use vars qw( $opt_d $opt_v $opt_c $opt_s $opt_e $opt_a );
24 my ($user, $login, $password) = @ARGV;
25 $user and $login and $password or die &usage;
27 my $dbh = adminsuidsetup $user;
28 $FS::UID::AutoCommit = 0;
30 # index already-downloaded batches
31 my @previous = qsearch({
32 'table' => 'cdr_batch',
33 'hashref' => { 'cdrbatch' => {op=>'like', value=>'voip_innovations%'} },
34 'order_by' => 'ORDER BY cdrbatch DESC',
36 my %exists = map {$_->cdrbatch => 1} @previous;
38 my $tempdir = tempdir( CLEANUP => !$opt_v );
40 my $format = 'voip_innovations';
41 my $hostname = 'customercdr.voipinnovations.com';
43 my $ftp = Net::FTP->new($hostname, Passive => 1, Debug => $opt_d)
44 or die "Can't connect to $hostname: $@\n";
47 or die "TLS initialization failed: ". $ftp->message. "\n";
49 $ftp->login($login, $password)
50 or die "Login failed: ". $ftp->message. "\n";
56 warn "Retrieving directory listing\n" if $opt_v;
59 my @dirs = $ftp->ls();
60 warn scalar(@dirs)." directories found.\n" if $opt_v;
63 my $most_recent = $previous[0];
65 if ($most_recent->cdrbatch =~ /^voip_innovations-(\d+)/) {
67 warn "limiting to dates > $date (from most recent batch)\n" if $opt_v;
68 @dirs = grep {$_ > $date} @dirs;
70 } # else download them all
74 # normalize date format
75 $opt_s = time2str('%Y%m%d', str2time($opt_s)) if $opt_s =~ /\D/;
76 warn "limiting to dates > $opt_s\n" if $opt_v;
77 @dirs = grep {$_ > $opt_s} @dirs;
81 $opt_e = time2str('%Y%m%d', str2time($opt_e)) if $opt_e =~ /\D/;
82 warn "limiting to dates < $opt_e\n" if $opt_v;
83 @dirs = grep {$_ < $opt_e} @dirs;
85 warn scalar(@dirs) ." to be downloaded\n" if $opt_v;
86 foreach my $dir (@dirs) {
88 foreach my $file ($ftp->ls) {
89 warn "downloading $file\n" if $opt_v;
90 $ftp->get($file, "$tempdir/$file");
91 warn "processing $file\n" if $opt_v;
93 # "voip_innovations-20130628/20130628_20130628.CDR"
94 my $batchname = "$format-$dir/$file";
95 if ($exists{$batchname}) {
96 warn "already imported $file\n";
99 my $import_options = {
100 'file' => "$tempdir/$file",
102 'batch_namevalue' => $batchname,
105 $import_options->{'cdrtypenum'} = $opt_c if $opt_c;
107 my $error = FS::cdr::batch_import($import_options);
110 die "error processing $dir/$file: $error\n";
123 "Usage: \n freeside-voip_innovations-cdrimport user login password\n [ options ]
126 -d: enable FTP debugging (very noisy)
127 -c num: apply a cdrtypenum to the imported CDRs
130 -a: automatically choose start date from most recently downloaded batch