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