delete fees, RT#81713
[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 from last downloaded batch.
59 if ( $opt_a ) {
60   my $most_recent = $previous[0];
61   if ($most_recent) {
62     if ($most_recent->cdrbatch =~ /^*Daily_(\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   }
68 }
69
70 # apply a start date if given
71 if ( $opt_s ) {
72   # normalize date format
73   $opt_s = time2str('%Y%m%d', str2time($opt_s)) if $opt_s =~ /\D/;
74   warn "limiting to dates > $opt_s\n" if $opt_v;
75   @files= grep { /^*Daily_(\d+)_/ && $1 >= $opt_s } @files;
76 }
77
78 # apply a end date if given
79 if ( $opt_e ) {
80   # normalize date format
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   @files= grep { /^*Daily_(\d+)_/ && $1 < $opt_e } @files;
84 }
85
86 warn scalar(@files) ." files to be downloaded\n" if $opt_v;
87 foreach my $file (@files) {
88
89   my $tmpdir = tempdir( CLEANUP => $opt_v );
90
91   warn "downloading $file to $tmpdir\n" if $opt_v;
92   $sftp = sftp();
93   $sftp->get($file, "$tmpdir/$file");
94
95   ## extract zip file
96   if(system ("unzip $tmpdir/$file -d $tmpdir") != 0) {
97     unlink "$tmpdir/$file";
98     my $error = "unzip of '$tmpdir/$file' failed\n";
99     if ( $opt_s ) {
100       warn $error;
101       next;
102     } else {
103       die $error;
104     }
105   }
106
107   warn "processing $file\n" if $opt_v;
108
109   my $batchname = "$format-$file";
110   if ($exists{$batchname}) {
111     warn "already imported $file\n";
112     next;
113   }
114
115   my $unzipped_file = $file;
116   $unzipped_file =~ s/.zip/.txt/i;
117
118   warn "going to import file $unzipped_file" if $opt_v;
119
120   my $import_options = {
121     'file'            => "$tmpdir/$unzipped_file",
122     'format'          => $format,
123     'batch_namevalue' => $batchname,
124     'empty_ok'        => 1,
125   };
126   $import_options->{'cdrtypenum'} = $opt_c if $opt_c;
127   
128   my $error = FS::cdr::batch_import($import_options);
129
130   if ( $error ) {
131     die "error processing $unzipped_file: $error\n";
132   }
133 }
134 warn "finished\n" if $opt_v;
135 $dbh->commit;
136
137 ###
138 # subs
139 ###
140
141 sub usage {
142   "Usage: \n  freeside-cdr-aninetworks-import [ options ] user login password
143   Options:
144     -v: be verbose
145     -d: enable FTP debugging (very noisy)
146     -c num: apply a cdrtypenum to the imported CDRs
147     -s date: start date
148     -e date: end date
149     -a: automatically choose start date from most recently downloaded batch
150
151 ";
152 }
153
154 sub sftp {
155
156   #reuse connections
157   return $sftp if $sftp && $sftp->cwd;
158
159   my %sftp = ( host => $host,
160                user => $login,
161                password => $password,
162                more => [-o => 'StrictHostKeyChecking no'],
163              );
164
165   $sftp = Net::SFTP::Foreign->new(%sftp);
166   $sftp->error and die "SFTP connection failed: ". $sftp->error;
167
168   $sftp;
169 }
170
171 =head1 NAME
172
173 freeside-cdr-aninetworks-import - Download CDR files from a remote server via SFTP
174
175 =head1 SYNOPSIS
176
177   freeside-cdr-aninetworks-import [ -v ] [ -d ] [ -a ] [ -c cdrtypenum ]
178     [ -s startdate ] [ -e enddate ] user sftpuser sftppassword
179
180 =head1 DESCRIPTION
181
182 Command line tool to download CDR files from a remote server via SFTP
183 and then import them into the database.
184
185 -v: be verbose
186
187 -d: enable sftp debugging (very noisy)
188
189 -a: automatically choose start date from most recently downloaded batch
190
191 -c: cdrtypenum to set, defaults to none
192
193 -s: if specified, sets a startdate. startdate starts at 00:00:00
194
195 -e: if specified, sets a enddate. enddate starts at 00:00:00 so if you wish to include enddate must add one more day.
196
197 user: freeside username
198
199 sftpuser: sftp user for arkftp.aninetworks.com
200
201 sftppassword: password for sftp user
202
203 =head1 EXAMPLES
204
205 freeside-cdr-aninetworks-import -a <freeside user> <sftp login> <sftp password>
206 will get all cdr files starting from the day of the last day processed.
207
208 freeside-cdr-aninetworks-import -s 20180120 -e 20180121 <freeside user> <sftp login> <sftp password>
209 will get all cdr files from 01/20/2018
210
211 freeside-cdr-aninetworks-import -v -s $(date --date="-1 day" +\%Y\%m\%d) -e $(date +\%Y\%m\%d) <freeside user> <sftp login> <sftp password>
212 will get all cdr files from yesterday
213
214 =head1 BUGS
215
216 =head1 SEE ALSO
217
218 L<FS::cdr>
219
220 =cut
221
222 1;