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;
44 my $date = time2str('%Y%m%d%H%M%S', $^T); # more?
46 my $conf = new FS::Conf;
48 my @agents = $opt{'a'} ? FS::agent->by_key($opt{'a'}) : qsearch('agent', {});
50 if ( $conf->exists('cust_bill-ftp_spool') ) {
51 my $url = $conf->config('cust_bill-ftpdir');
52 $url = "/$url" unless $url =~ m[^/];
53 $url = 'ftp://' . $conf->config('cust_bill-ftpserver') . $url;
55 my $format = $conf->config('cust_bill-ftpformat');
56 my $username = $conf->config('cust_bill-ftpusername');
57 my $password = $conf->config('cust_bill-ftppassword');
64 'username' => $username,
65 'password' => $password,
70 if ( $conf->exists('cust_bill-spoolagent') ) {
71 # then push each agent's spool separately
73 push @tasks, { %task, 'agentnum' => $_->agentnum };
77 warn "Per-agent processing, but cust_bill-spoolagent is not enabled.\nSkipped invoice upload.\n";
84 else { #check each agent for billco upload settings
94 my $agentnum = $_->agentnum;
96 if ( $conf->config( 'billco-username', $agentnum, 1 ) ) {
97 my $username = $conf->config('billco-username', $agentnum, 1);
98 my $password = $conf->config('billco-password', $agentnum, 1);
99 my $clicode = $conf->config('billco-clicode', $agentnum, 1);
100 my $url = $conf->config('billco-url', $agentnum);
103 'agentnum' => $agentnum,
104 'username' => $username,
105 'password' => $password,
107 'clicode' => $clicode,
108 'format' => 'billco',
113 } #!if cust_bill-ftp_spool
117 my $agentnum = $_->{agentnum};
122 warn "DRY RUN: would add agent $agentnum for queued upload\n";
124 my $queue = new FS::queue {
125 'job' => 'FS::Cron::upload::spool_upload',
127 my $error = $queue->insert( %$_ );
132 eval { spool_upload(%$_) };
133 warn "spool_upload failed: $@\n"
145 warn "$me spool_upload called\n" if $DEBUG;
146 my $conf = new FS::Conf;
147 my $dir = '%%%FREESIDE_EXPORT%%%/export.'. $FS::UID::datasrc. '/cust_bill';
149 my $agentnum = $opt{agentnum} or die "no agentnum provided\n";
150 my $url = $opt{url} or die "no url for agent $agentnum\n";
151 $url =~ s/^\s+//; $url =~ s/\s+$//;
153 my $username = $opt{username} or die "no username for agent $agentnum\n";
154 my $password = $opt{password} or die "no password for agent $agentnum\n";
156 die "no date provided\n" unless $opt{date};
158 local $SIG{HUP} = 'IGNORE';
159 local $SIG{INT} = 'IGNORE';
160 local $SIG{QUIT} = 'IGNORE';
161 local $SIG{TERM} = 'IGNORE';
162 local $SIG{TSTP} = 'IGNORE';
163 local $SIG{PIPE} = 'IGNORE';
165 my $oldAutoCommit = $FS::UID::AutoCommit;
166 local $FS::UID::AutoCommit = 0;
169 my $agent = qsearchs( 'agent', { agentnum => $agentnum } )
170 or die "no such agent: $agentnum";
171 $agent->select_for_update; #mutex
173 if ( $opt{'format'} eq 'billco' ) {
175 my $zipfile = "$dir/agentnum$agentnum-$opt{date}.zip";
177 unless ( -f "$dir/agentnum$agentnum-header.csv" ||
178 -f "$dir/agentnum$agentnum-detail.csv" )
180 warn "$me neither $dir/agentnum$agentnum-header.csv nor ".
181 "$dir/agentnum$agentnum-detail.csv found\n" if $DEBUG;
182 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
188 my $sql = "SELECT count(*) FROM queue LEFT JOIN cust_main USING(custnum) ".
189 "WHERE queue.job='FS::cust_main::queued_bill' AND cust_main.agentnum = ?";
190 my $sth = $dbh->prepare($sql) or die $dbh->errstr;
192 $sth->execute( $agentnum )
193 or die "Unexpected error executing statement $sql: ". $sth->errstr;
194 last if $sth->fetchrow_arrayref->[0];
199 foreach ( qw ( header detail ) ) {
200 rename "$dir/agentnum$agentnum-$_.csv",
201 "$dir/agentnum$agentnum-$opt{date}-$_.csv";
204 my $command = "cd $dir; zip $zipfile ".
205 "agentnum$agentnum-$opt{date}-header.csv ".
206 "agentnum$agentnum-$opt{date}-detail.csv";
208 system($command) and die "$command failed\n";
210 unlink "agentnum$agentnum-$opt{date}-header.csv",
211 "agentnum$agentnum-$opt{date}-detail.csv";
213 if ( $url =~ /^http/i ) {
215 my $ua = new LWP::UserAgent;
216 my $res = $ua->request( POST( $url,
217 'Content_Type' => 'form-data',
218 'Content' => [ 'username' => $username,
220 'custid' => $username,
221 'clicode' => $opt{clicode},
222 'file1' => [ $zipfile ],
227 die "upload failed: ". $res->status_line. "\n"
228 unless $res->is_success;
230 } elsif ( $url =~ /^ftp:\/\/([\w\.]+)(\/.*)$/i ) {
232 my($hostname, $path) = ($1, $2);
234 my $ftp = new Net::FTP($hostname, Passive=>1)
235 or die "can't connect to $hostname: $@\n";
236 $ftp->login($username, $password)
237 or die "can't login to $hostname: ". $ftp->message."\n";
238 unless ( $ftp->cwd($path) ) {
239 my $msg = "can't cd $path on $hostname: ". $ftp->message. "\n";
240 ( $path eq '/' ) ? warn $msg : die $msg;
243 or die "can't set binary mode on $hostname\n";
246 or die "can't put $zipfile: ". $ftp->message. "\n";
251 die "unknown scheme in URL $url\n";
254 } else { #$opt{format} ne 'billco'
256 my $date = $opt{date};
257 my $file = $opt{agentnum} ? "agentnum$opt{agentnum}" : 'spool'; #.csv
258 unless ( -f "$dir/$file.csv" ) {
259 warn "$me $dir/$file.csv not found\n" if $DEBUG;
260 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
263 rename "$dir/$file.csv", "$dir/$file-$date.csv";
266 if ( $url =~ m{^ftp://([\w\.]+)(/.*)$}i ) {
268 my ($hostname, $path) = ($1, $2);
269 my $ftp = new Net::FTP ($hostname)
270 or die "can't connect to $hostname: $@\n";
271 $ftp->login($username, $password)
272 or die "can't login to $hostname: ".$ftp->message."\n";
273 unless ( $ftp->cwd($path) ) {
274 my $msg = "can't cd $path on $hostname: ".$ftp->message."\n";
275 ( $path eq '/' ) ? warn $msg : die $msg;
278 $ftp->put("$file-$date.csv")
279 or die "can't put $file-$date.csv: ".$ftp->message."\n";
283 die "malformed FTP URL $url\n";
287 $dbh->commit or die $dbh->errstr if $oldAutoCommit;