1 package FS::Cron::upload;
4 use vars qw( @ISA @EXPORT_OK $me $DEBUG );
8 use FS::Record qw( qsearch qsearchs );
14 use HTTP::Request::Common;
18 @ISA = qw( Exporter );
19 @EXPORT_OK = qw ( upload );
21 $me = '[FS::Cron::upload]';
24 # -v: enable debugging
26 # -m: Experimental multi-process mode uses the job queue for multi-process and/or multi-machine billing.
27 # -r: Multi-process mode dry run option
28 # -a: Only process customers with the specified agentnum
35 $debug = 1 if $opt{'v'};
36 $debug = $opt{'l'} if $opt{'l'};
38 local $DEBUG = $debug if $debug;
40 warn "$me upload called\n" if $DEBUG;
42 my $conf = new FS::Conf;
43 my @agent = grep { $conf->config( 'billco-username', $_->agentnum, 1 ) }
44 grep { $conf->config( 'billco-password', $_->agentnum, 1 ) }
45 qsearch( 'agent', {} );
47 my $date = time2str('%Y%m%d%H%M%S', $^T); # more?
49 @agent = grep { $_ == $opt{'a'} } @agent if $opt{'a'};
51 foreach my $agent ( @agent ) {
53 my $agentnum = $agent->agentnum;
58 warn "DRY RUN: would add agent $agentnum for queued upload\n";
61 my $queue = new FS::queue {
62 'job' => 'FS::Cron::upload::billco_upload',
64 my $error = $queue->insert(
65 'agentnum' => $agentnum,
67 'l' => $opt{'l'} || '',
68 'm' => $opt{'m'} || '',
69 'v' => $opt{'v'} || '',
76 eval "&billco_upload( 'agentnum' => $agentnum, 'date' => $date );";
77 warn "billco_upload failed: $@\n"
89 warn "$me billco_upload called\n" if $DEBUG;
90 my $conf = new FS::Conf;
91 my $dir = '%%%FREESIDE_EXPORT%%%/export.'. $FS::UID::datasrc. '/cust_bill';
93 my $agentnum = $opt{agentnum} or die "no agentnum provided\n";
94 my $url = $conf->config( 'billco-url', $agentnum )
95 or die "no url for agent $agentnum\n";
96 my $username = $conf->config( 'billco-username', $agentnum, 1 )
97 or die "no username for agent $agentnum\n";
98 my $password = $conf->config( 'billco-password', $agentnum, 1 )
99 or die "no password for agent $agentnum\n";
100 my $clicode = $conf->config( 'billco-clicode', $agentnum, 1 );
101 #or die "no clicode for agent $agentnum\n";
103 die "no date provided\n" unless $opt{date};
104 my $zipfile = "$dir/agentnum$agentnum-$opt{date}.zip";
106 local $SIG{HUP} = 'IGNORE';
107 local $SIG{INT} = 'IGNORE';
108 local $SIG{QUIT} = 'IGNORE';
109 local $SIG{TERM} = 'IGNORE';
110 local $SIG{TSTP} = 'IGNORE';
111 local $SIG{PIPE} = 'IGNORE';
113 my $oldAutoCommit = $FS::UID::AutoCommit;
114 local $FS::UID::AutoCommit = 0;
117 my $agent = qsearchs( 'agent', { agentnum => $agentnum } )
118 or die "no such agent: $agentnum";
119 $agent->select_for_update; #mutex
121 unless ( -f "$dir/agentnum$agentnum-header.csv" ||
122 -f "$dir/agentnum$agentnum-detail.csv" )
124 warn "$me neither $dir/agentnum$agentnum-header.csv nor ".
125 "$dir/agentnum$agentnum-detail.csv found\n" if $DEBUG;
126 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
132 my $sql = "SELECT count(*) FROM queue LEFT JOIN cust_main USING(custnum) ".
133 "WHERE queue.job='FS::cust_main::queued_bill' AND cust_main.agentnum = ?";
134 my $sth = $dbh->prepare($sql) or die $dbh->errstr;
136 $sth->execute( $agentnum )
137 or die "Unexpected error executing statement $sql: ". $sth->errstr;
138 last if $sth->fetchow_arrayref->[0];
143 foreach ( qw ( header detail ) ) {
144 rename "$dir/agentnum$agentnum-$_.csv",
145 "$dir/agentnum$agentnum-$opt{date}-$_.csv";
148 my $command = "cd $dir; zip $zipfile ".
149 "agentnum$agentnum-$opt{date}-header.csv ".
150 "agentnum$agentnum-$opt{date}-detail.csv";
152 system($command) and die "$command failed\n";
154 unlink "agentnum$agentnum-$opt{date}-header.csv",
155 "agentnum$agentnum-$opt{date}-detail.csv";
157 if ( $url =~ /^http/i ) {
159 my $ua = new LWP::UserAgent;
160 my $res = $ua->request( POST( $url,
161 'Content_Type' => 'form-data',
162 'Content' => [ 'username' => $username,
164 'custid' => $username,
165 'clicode' => $clicode,
166 'file1' => [ $zipfile ],
171 die "upload failed: ". $res->status_line. "\n"
172 unless $res->is_success;
174 } elsif ( $url =~ /^ftp:\/\/([\w\.]+)(\/.*)$/i ) {
176 my($hostname, $path) = ($1, $2);
178 my $ftp = new Net::FTP($hostname)
179 or die "can't connect to $hostname: $@\n";
180 $ftp->login($username, $password)
181 or die "can't login to $hostname: ". $ftp->message."\n";
182 unless ( $ftp->cwd($path) ) {
183 my $msg = "can't cd $path on $hostname: ". $ftp->message. "\n";
184 ( $path eq '/' ) ? warn $msg : die $msg;
187 or die "can't set binary mode on $hostname\n";
190 or die "can't put $zipfile: ". $ftp->message. "\n";
195 die "unknown scheme in URL $url\n";
198 $dbh->commit or die $dbh->errstr if $oldAutoCommit;