4 # cdr.sftp_and_import [ -e extension ] [ -d donefolder ] [ -v ] user format [sftpuser@]servername
6 # -e: file extension, defaults to .csv
7 # -d: if specified, moves files to the specified folder when done
11 use Net::SFTP::Foreign;
12 use FS::UID qw(adminsuidsetup datasrc);
19 use vars qw( $opt_e $opt_d $opt_v );
23 #$opt_e = ".$opt_e" unless $opt_e =~ /^\./;
26 my $user = shift or die &usage;
29 # %%%FREESIDE_CACHE%%%
30 my $cachedir = '/usr/local/etc/freeside/cache.'. datasrc. '/cdrs';
31 mkdir $cachedir unless -d $cachedir;
33 my $format = shift or die &usage;
35 use vars qw( $servername );
36 $servername = shift or die &usage;
42 warn "Retreiving directory listing\n" if $opt_v;
46 my $ls = $ls_sftp->ls('.', wanted => qr/\.*$opt_e$/i );
52 foreach my $file ( @$ls ) {
54 my $filename = $file->{filename};
55 warn "Downloading $filename\n" if $opt_v;
58 my $get_sftp = sftp();
59 $get_sftp->get($filename, "$cachedir/$filename")
60 or die "Can't get $filename: ". $get_sftp->error;
62 warn "Processing $filename\n" if $opt_v;
64 my $error = FS::cdr::batch_import( {
65 'file' => "$cachedir/$filename"
67 'params' => { 'cdrbatch' => $filename, },
76 $mv_sftp->rename($filename, "$opt_d/$filename")
77 or die "can't move $filename to $opt_d: ". $mv_sftp->error;
80 unlink "$cachedir/$filename";
91 "Usage: \n cdr.import user format servername\n";
99 return $sftp if $sftp && $sftp->cwd;
101 my %sftp = ( host => $servername );
107 $sftp = Net::SFTP::Foreign->new(%sftp);
108 $sftp->error and die "SFTP connection failed: ". $sftp->error;