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