fix A/R report
[freeside.git] / bin / cdr-thinktel.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=>'thinktel%'} },
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 = 'thinktel';
40 my $hostname = 'ucontrol.thinktel.ca';
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 @files = grep { $_ =~ /MetaSwitch/ } $ftp->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 =~ /^thinktel-(\d+)/) {
63       my $date = $1;
64       warn "limiting to dates > $date (from most recent batch)\n" if $opt_v;
65       @files = grep { /^(\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 { /^(\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 { /^(\d+)_/ && $1 < $opt_e } @files;
81 }
82 warn scalar(@files) ." files to be downloaded to '$tempdir'\n" if $opt_v;
83 foreach my $file (@files) {
84
85   warn "downloading $file\n" if $opt_v;
86   $ftp->get($file, "$tempdir/$file");
87   warn "processing $file\n" if $opt_v;
88
89   my $batchname = "$format-$file";
90   if ($exists{$batchname}) {
91     warn "already imported $file\n";
92     next;
93   }
94   my $import_options = {
95     'file'            => "$tempdir/$file",
96     'format'          => $format,
97     'batch_namevalue' => $batchname,
98     'empty_ok'        => 1,
99   };
100   $import_options->{'cdrtypenum'} = $opt_c if $opt_c;
101   
102   my $error = FS::cdr::batch_import($import_options);
103
104   if ( $error ) {
105     die "error processing $file: $error\n";
106   }
107 }
108 warn "finished\n" if $opt_v;
109 $dbh->commit;
110
111 ###
112 # subs
113 ###
114
115 sub usage {
116   "Usage: \n  cdr-thinktel.import [ options ] user login password
117   Options:
118     -v: be verbose
119     -d: enable FTP debugging (very noisy)
120     -c num: apply a cdrtypenum to the imported CDRs
121     -s date: start date
122     -e date: end date
123     -a: automatically choose start date from most recently downloaded batch
124
125 ";
126 }
127