Net::SSLglue never worked, need deb9 Net::FTP for this to work
[freeside.git] / FS / bin / freeside-voipinnovations-cdrimport
1 #!/usr/bin/perl
2
3 use strict;
4 use Getopt::Std;
5 use Date::Format;
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);
10 use FS::cdr;
11 use FS::cdr_batch;
12 use FS::Record qw(qsearch qsearchs);
13 use Date::Format 'time2str';
14 use Date::Parse 'str2time';
15
16
17 ###
18 # parse command line
19 ###
20
21 use vars qw( $opt_d $opt_v $opt_c $opt_s $opt_e $opt_a );
22 getopts('dvc:s:e:a');
23
24 my ($user, $login, $password) = @ARGV;
25 $user and $login and $password or die &usage;
26
27 my $dbh = adminsuidsetup $user;
28 $FS::UID::AutoCommit = 0;
29
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',
35 });
36 my %exists = map {$_->cdrbatch => 1} @previous;
37
38 my $tempdir = tempdir( CLEANUP => !$opt_v );
39
40 my $format = 'voip_innovations';
41 my $hostname = 'customercdr.voipinnovations.com';
42
43 my $ftp = Net::FTP->new($hostname, Passive => 1, Debug => $opt_d)
44   or die "Can't connect to $hostname: $@\n";
45
46 $ftp->starttls()
47   or die "TLS initialization failed: ". $ftp->message. "\n";
48
49 $ftp->login($login, $password)
50   or die "Login failed: ". $ftp->message. "\n";
51
52 ###
53 # get the file list
54 ###
55
56 warn "Retrieving directory listing\n" if $opt_v;
57
58 #$ftp->cwd('/');
59 my @dirs = $ftp->ls();
60 warn scalar(@dirs)." directories found.\n" if $opt_v;
61 # apply date range
62 if ( $opt_a ) {
63   my $most_recent = $previous[0];
64   if ($most_recent) {
65     if ($most_recent->cdrbatch =~ /^voip_innovations-(\d+)/) {
66       my $date = $1;
67       warn "limiting to dates > $date (from most recent batch)\n" if $opt_v;
68       @dirs = grep {$_ > $date} @dirs;
69     }
70   } # else download them all
71 }
72 if ( $opt_s ) {
73   # start date
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;
78 }
79 if ( $opt_e ) {
80   # end date
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;
84 }
85 warn scalar(@dirs) ." to be downloaded\n" if $opt_v;
86 foreach my $dir (@dirs) {
87   $ftp->cwd($dir);
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;
92
93     # "voip_innovations-20130628/20130628_20130628.CDR"
94     my $batchname = "$format-$dir/$file";
95     if ($exists{$batchname}) {
96       warn "already imported $file\n";
97       next;
98     }
99     my $import_options = {
100       'file'            => "$tempdir/$file",
101       'format'          => $format,
102       'batch_namevalue' => $batchname,
103       'empty_ok'        => 1,
104     };
105     $import_options->{'cdrtypenum'} = $opt_c if $opt_c;
106   
107     my $error = FS::cdr::batch_import($import_options);
108
109     if ( $error ) {
110       die "error processing $dir/$file: $error\n";
111     }
112   }
113   $ftp->cwd('..');
114 }
115 warn "finished\n";
116 $dbh->commit;
117
118 ###
119 # subs
120 ###
121
122 sub usage {
123   "Usage: \n  freeside-voip_innovations-cdrimport user login password\n [ options ]
124   Options:
125     -v: be verbose
126     -d: enable FTP debugging (very noisy)
127     -c num: apply a cdrtypenum to the imported CDRs
128     -s date: start date
129     -e date: end date
130     -a: automatically choose start date from most recently downloaded batch
131   ";
132 }
133