summaryrefslogtreecommitdiff
path: root/FS/FS/Cron/upload.pm
blob: fea3d2cc7bb89bf4342eacd8a82d47b992230b18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package FS::Cron::upload;

use strict;
use vars qw( @ISA @EXPORT_OK $me $DEBUG );
use Exporter;
use Date::Format;
use FS::UID qw(dbh);
use FS::Record qw( qsearch qsearchs );
use FS::Conf;
use FS::queue;
use FS::agent;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Request::Common;
use HTTP::Response;

@ISA = qw( Exporter );
@EXPORT_OK = qw ( upload );
$DEBUG = 0;
$me = '[FS::Cron::upload]';

#freeside-daily %opt:
#  -v: enable debugging
#  -l: debugging level
#  -m: Experimental multi-process mode uses the job queue for multi-process and/or multi-machine billing.
#  -r: Multi-process mode dry run option
#  -a: Only process customers with the specified agentnum


sub upload {
  my %opt = @_;

  my $debug = 0;
  $debug = 1 if $opt{'v'};
  $debug = $opt{'l'} if $opt{'l'};

  local $DEBUG = $debug if $debug;

  warn "$me upload called\n" if $DEBUG;

  my $conf = new FS::Conf;
  my @agent = grep { $conf->config( 'billco-username', $_->agentnum, 1 ) }
              grep { $conf->config( 'billco-password', $_->agentnum, 1 ) }
              qsearch( 'agent', {} );

  my $date =  time2str('%Y%m%d%H%M%S', $^T); # more?

  @agent = grep { $_ == $opt{'a'} } @agent if $opt{'a'};

  foreach my $agent ( @agent ) {

    my $agentnum = $agent->agentnum;

    if ( $opt{'m'} ) {

      if ( $opt{'r'} ) {
        warn "DRY RUN: would add agent $agentnum for queued upload\n";
      } else {

        my $queue = new FS::queue {
          'job'      => 'FS::Cron::upload::billco_upload',
        };
        my $error = $queue->insert(
                                    'agentnum' => $agentnum,
                                    'date'     => $date,
                                    'l'        => $opt{'l'} || '',
                                    'm'        => $opt{'m'} || '',
                                    'v'        => $opt{'v'} || '',
                                  );

      }

    } else {

      eval "&billco_upload( 'agentnum' => $agentnum, 'date' => $date );";
      warn "billco_upload failed: $@\n"
        if ( $@ );

    }

  }

}

sub billco_upload {
  my %opt = @_;

  warn "$me billco_upload called\n" if $DEBUG;
  my $conf = new FS::Conf;
  my $dir = '%%%FREESIDE_EXPORT%%%/export.'. $FS::UID::datasrc. '/cust_bill';

  my $agentnum = $opt{agentnum} or die "no agentnum provided\n";
  my $url      = $conf->config( 'billco-url', $agentnum )
    or die "no url for agent $agentnum\n";
  my $username = $conf->config( 'billco-username', $agentnum, 1 )
    or die "no username for agent $agentnum\n";
  my $password = $conf->config( 'billco-password', $agentnum, 1 )
    or die "no password for agent $agentnum\n";
  my $clicode  = $conf->config( 'billco-clicode', $agentnum )
    or die "no clicode for agent $agentnum\n";

  die "no date provided\n" unless $opt{date};
  my $zipfile  = "$dir/agentnum$agentnum-$opt{date}.zip";

  local $SIG{HUP} = 'IGNORE';
  local $SIG{INT} = 'IGNORE';
  local $SIG{QUIT} = 'IGNORE';
  local $SIG{TERM} = 'IGNORE';
  local $SIG{TSTP} = 'IGNORE';
  local $SIG{PIPE} = 'IGNORE';

  my $oldAutoCommit = $FS::UID::AutoCommit;
  local $FS::UID::AutoCommit = 0;
  my $dbh = dbh;

  my $agent = qsearchs( 'agent', { agentnum => $agentnum } )
    or die "no such agent: $agentnum";
  $agent->select_for_update; #mutex 

  unless ( -f "$dir/agentnum$agentnum-header.csv" ||
           -f "$dir/agentnum$agentnum-detail.csv" )
  {
    warn "$me neither $dir/agentnum$agentnum-header.csv nor ".
         "$dir/agentnum$agentnum-detail.csv found\n" if $DEBUG;
    $dbh->commit or die $dbh->errstr if $oldAutoCommit;
    return;
  }

  # a better way?
  if ($opt{m}) {
    my $sql = "SELECT count(*) FROM queue LEFT JOIN cust_main USING(custnum) ".
      "WHERE queue.job='FS::cust_main::queued_bill' AND cust_main.agentnum = ?";
    my $sth = $dbh->prepare($sql) or die $dbh->errstr;
    while (1) {
      $sth->execute( $agentnum )
        or die "Unexpected error executing statement $sql: ". $sth->errstr;
      last if $sth->fetchow_arrayref->[0];
      sleep 300;
    }
  }

  foreach ( qw ( header detail ) ) {
    rename "$dir/agentnum$agentnum-$_.csv",
           "$dir/agentnum$agentnum-$opt{date}-$_.csv";
  }

  my $command = "cd $dir; zip $zipfile ".
                "agentnum$agentnum-$opt{date}-header.csv ".
                "agentnum$agentnum-$opt{date}-detail.csv";

  system($command) and die "$command failed\n";

  unlink "agentnum$agentnum-$opt{date}-header.csv",
         "agentnum$agentnum-$opt{date}-detail.csv";

  my $ua = new LWP::UserAgent;
  my $res = $ua->request( POST( $url,
                                'Content_Type' => 'form-data',
                                'Content' => [ 'username' => $username,
                                               'pass'     => $password,
                                               'custid'   => $username,
                                               'clicode'  => $clicode,
                                               'file1'    => [ $zipfile ],
                                             ],
                              )
                        );

  die "upload failed: ". $res->status_line. "\n"
    unless $res->is_success;

  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
  '';

}

1;