6 use File::Temp 'tempdir';
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=>'voip_innovations%'} },
33 'order_by' => 'ORDER BY cdrbatch DESC',
35 my %exists = map {$_->cdrbatch => 1} @previous;
37 my $tempdir = tempdir( CLEANUP => !$opt_v );
39 my $format = 'voip_innovations';
40 my $hostname = 'cdrs.globalpopsvoip.com';
42 my $ftp = Net::FTP->new($hostname, Debug => $opt_d)
43 or die "Can't connect to $hostname: $@\n";
45 $ftp->login($login, $password)
46 or die "Login failed: ".$ftp->message."\n";
52 warn "Retrieving directory listing\n" if $opt_v;
55 my @dirs = $ftp->ls();
56 warn scalar(@dirs)." directories found.\n" if $opt_v;
59 my $most_recent = $previous[0];
61 if ($most_recent->cdrbatch =~ /^voip_innovations-(\d+)/) {
63 warn "limiting to dates > $date (from most recent batch)\n" if $opt_v;
64 @dirs = grep {$_ > $date} @dirs;
66 } # else download them all
70 # normalize date format
71 $opt_s = time2str('%Y%m%d', str2time($opt_s)) if $opt_s =~ /\D/;
72 warn "limiting to dates > $opt_s\n" if $opt_v;
73 @dirs = grep {$_ > $opt_s} @dirs;
77 $opt_e = time2str('%Y%m%d', str2time($opt_e)) if $opt_e =~ /\D/;
78 warn "limiting to dates < $opt_e\n" if $opt_v;
79 @dirs = grep {$_ < $opt_e} @dirs;
81 warn scalar(@dirs) ." to be downloaded\n" if $opt_v;
82 foreach my $dir (@dirs) {
84 foreach my $file ($ftp->ls) {
85 warn "downloading $file\n" if $opt_v;
86 $ftp->get($file, "$tempdir/$file");
87 warn "processing $file\n" if $opt_v;
89 # "voip_innovations-20130628/20130628_20130628.CDR"
90 my $batchname = "$format-$dir/$file";
91 if ($exists{$batchname}) {
92 warn "already imported $file\n";
95 my $import_options = {
96 'file' => "$tempdir/$file",
98 'batch_namevalue' => $batchname,
101 $import_options->{'cdrtypenum'} = $opt_c if $opt_c;
103 my $error = FS::cdr::batch_import($import_options);
106 die "error processing $dir/$file: $error\n";
119 "Usage: \n cdr-voip_innovations.import user login password\n [ options ]
122 -d: enable FTP debugging (very noisy)
123 -c num: apply a cdrtypenum to the imported CDRs
126 -a: automatically choose start date from most recently downloaded batch