RT# 79121 - Added new cdr import format for ani networks, and cron script for same
[freeside.git] / FS / bin / freeside-cdr-aninetworks-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::SFTP::Foreign::Compat;
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=>'ani_networks%'} },
33     'order_by'  => 'ORDER BY cdrbatch DESC',
34 });
35 my %exists = map {$_->cdrbatch => 1} @previous;
36
37 my $format = 'ani_networks';
38 my $host = 'arkftp.aninetworks.com';
39
40 ###
41 # get the file list
42 ###
43
44 warn "Retrieving directory listing\n" if $opt_v;
45
46 my $sftp = sftp();
47
48 ## get the current working dir
49 my $cwd = $sftp->cwd;
50
51 ## switch to CDR dir
52 $sftp->setcwd($cwd . '/CDR') or die "can't chdir to $cwd/CDR\n";
53
54 my $ls = $sftp->ls('.', wanted => qr/^UYM.*.zip$/i, names_only =>1 );
55 my @files = @$ls;
56
57 warn scalar(@files)." CDR files found.\n" if $opt_v;
58 # apply date range
59 if ( $opt_a ) {
60   my $most_recent = $previous[0];
61   if ($most_recent) {
62     if ($most_recent->cdrbatch =~ /^ani_networks-(\d+)/) {
63       my $date = $1;
64       warn "limiting to dates > $date (from most recent batch)\n" if $opt_v;
65       @files = grep { /^*Daily_(\d+)_/ && $1 > $date } @files;
66     }
67   } # else download them all
68 }
69 if ( $opt_s ) {
70   # start date
71   # normalize date format
72   $opt_s = time2str('%Y%m%d', str2time($opt_s)) if $opt_s =~ /\D/;
73   warn "limiting to dates > $opt_s\n" if $opt_v;
74   @files= grep { /^*Daily_(\d+)_/ && $1 >= $opt_s } @files;
75 }
76 if ( $opt_e ) {
77   # end date
78   $opt_e = time2str('%Y%m%d', str2time($opt_e)) if $opt_e =~ /\D/;
79   warn "limiting to dates < $opt_e\n" if $opt_v;
80   @files= grep { /^*Daily_(\d+)_/ && $1 < $opt_e } @files;
81 }
82 warn scalar(@files) ." files to be downloaded\n" if $opt_v;
83 foreach my $file (@files) {
84
85   my $tmpdir = tempdir( CLEANUP => $opt_v );
86
87   warn "downloading $file to $tmpdir\n" if $opt_v;
88   $sftp = sftp();
89   $sftp->get($file, "$tmpdir/$file");
90
91   ## extract zip file
92   if(system ("unzip $tmpdir/$file -d $tmpdir") != 0) {
93     unlink "$tmpdir/$file";
94     my $error = "unzip of '$tmpdir/$file' failed\n";
95     if ( $opt_s ) {
96       warn $error;
97       next;
98     } else {
99       die $error;
100     }
101   }
102
103   warn "processing $file\n" if $opt_v;
104
105   my $batchname = "$format-$file";
106   if ($exists{$batchname}) {
107     warn "already imported $file\n";
108     next;
109   }
110
111   my $unzipped_file = $file;
112   $unzipped_file =~ s/.zip/.txt/i;
113
114   warn "going to import file $unzipped_file" if $opt_v;
115
116   my $import_options = {
117     'file'            => "$tmpdir/$unzipped_file",
118     'format'          => $format,
119     'batch_namevalue' => $batchname,
120     'empty_ok'        => 1,
121   };
122   $import_options->{'cdrtypenum'} = $opt_c if $opt_c;
123   
124   my $error = FS::cdr::batch_import($import_options);
125
126   if ( $error ) {
127     die "error processing $unzipped_file: $error\n";
128   }
129 }
130 warn "finished\n" if $opt_v;
131 $dbh->commit;
132
133 ###
134 # subs
135 ###
136
137 sub usage {
138   "Usage: \n  freeside-cdr-aninetworks-import [ options ] user login password
139   Options:
140     -v: be verbose
141     -d: enable FTP debugging (very noisy)
142     -c num: apply a cdrtypenum to the imported CDRs
143     -s date: start date
144     -e date: end date
145     -a: automatically choose start date from most recently downloaded batch
146
147 ";
148 }
149
150 sub sftp {
151
152   #reuse connections
153   return $sftp if $sftp && $sftp->cwd;
154
155   my %sftp = ( host => $host, user => $login, password => $password );
156
157   $sftp = Net::SFTP::Foreign->new(%sftp);
158   $sftp->error and die "SFTP connection failed: ". $sftp->error;
159
160   $sftp;
161 }
162