agent-virt batches and batchconfig-eftcanada config (argh!), RT#14859
authorivan <ivan>
Fri, 21 Oct 2011 23:09:03 +0000 (23:09 +0000)
committerivan <ivan>
Fri, 21 Oct 2011 23:09:03 +0000 (23:09 +0000)
FS/FS/Conf.pm
FS/FS/Schema.pm
FS/FS/cust_main.pm
FS/FS/pay_batch.pm
FS/bin/freeside-eftca-upload
httemplate/search/pay_batch.cgi

index bee1770..590a5c3 100644 (file)
@@ -3253,6 +3253,14 @@ and customer address. Include units.',
     'section'     => 'billing',
     'description' => 'Configuration for EFT Canada batching, four lines: 1. SFTP username, 2. SFTP password, 3. Transaction code, 4. Number of days to delay process date.',
     'type'        => 'textarea',
+    'per_agent'   => 1,
+  },
+
+  {
+    'key'         => 'batch-spoolagent',
+    'section'     => 'billing',
+    'description' => 'Store payment batches per-agent.',
+    'type'        => 'checkbox',
   },
 
   {
index f76fa61..316db6f 100644 (file)
@@ -1382,19 +1382,19 @@ sub tables_hashref {
 
     'pay_batch' => { #batches of payments to an external processor
       'columns' => [
-        'batchnum',   'serial',    '',   '', '', '', 
-       'payby',      'char',      '',    4, '', '', # CARD/CHEK
-        'status',     'char', 'NULL',     1, '', '', 
-        'download',   @date_type, '', '', 
-        'upload',     @date_type, '', '', 
+        'batchnum', 'serial',     '', '', '', '', 
+        'agentnum',    'int', 'NULL', '', '', '', 
+       'payby',      'char',     '',  4, '', '', # CARD/CHEK
+        'status',     'char', 'NULL',  1, '', '', 
+        'download',       @date_type,     '', '', 
+        'upload',         @date_type,     '', '', 
       ],
       'primary_key' => 'batchnum',
       'unique' => [],
       'index' => [],
     },
 
-    'cust_pay_batch' => { #what's this used for again?  list of customers
-                          #in current CARD batch? (necessarily CARD?)
+    'cust_pay_batch' => { #list of customers in current CARD/CHEK batch
       'columns' => [
         'paybatchnum',   'serial',    '',   '', '', '', 
         'batchnum',   'int',    '',   '', '', '', 
index 49702f6..2a8f47b 100644 (file)
@@ -2481,6 +2481,7 @@ sub batch_card {
     'status' => 'O',
     'payby'  => FS::payby->payby2payment($payby),
   );
+  $pay_batch{agentnum} = $self->agentnum if $conf->exists('batch-spoolagent');
 
   my $pay_batch = qsearchs( 'pay_batch', \%pay_batch );
 
index 90b4eb8..bde8fdb 100644 (file)
@@ -5,8 +5,9 @@ use vars qw( @ISA $DEBUG %import_info %export_info $conf );
 use Time::Local;
 use Text::CSV_XS;
 use FS::Record qw( dbh qsearch qsearchs );
-use FS::cust_pay;
 use FS::Conf;
+use FS::cust_pay;
+use FS::agent;
 use Date::Parse qw(str2time);
 use Business::CreditCard qw(cardtype);
 
@@ -40,6 +41,8 @@ from FS::Record.  The following fields are currently supported:
 
 =item batchnum - primary key
 
+=item agentnum - optional agent number for agent batches
+
 =item payby - CARD or CHEK
 
 =item status - O (Open), I (In-transit), or R (Resolved)
@@ -112,6 +115,7 @@ sub check {
     $self->ut_numbern('batchnum')
     || $self->ut_enum('payby', [ 'CARD', 'CHEK' ])
     || $self->ut_enum('status', [ 'O', 'I', 'R' ])
+    || $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum')
   ;
   return $error if $error;
 
index b501153..45a358b 100755 (executable)
@@ -38,15 +38,11 @@ else {
 }
 
 my $conf = new FS::Conf;
-my @batchconf = $conf->config('batchconfig-eft_canada');
-my $username = $batchconf[0] or die "no EFT Canada batch username configured\n";
-my $password = $batchconf[1] or die "no EFT Canada batch password configured\n";
 
 my $tmpdir = tempdir( CLEANUP => 1 ); #DIR=>somewhere?
 
-my @filenames;
-
 foreach my $pay_batch (@batches) {
+
   my $batchnum = $pay_batch->batchnum;
   my $filename = time2str('%Y%m%d', time) . '-' . sprintf('%06d.csv',$batchnum);
   print STDERR "Exporting batch $batchnum to $filename...\n" if $opt_v;
@@ -54,27 +50,27 @@ foreach my $pay_batch (@batches) {
   open OUT, ">$tmpdir/$filename";
   print OUT $text;
   close OUT;
-  push @filenames, $filename;
-}
 
-my $host = 'ftp.eftcanada.com';
-print STDERR "Connecting to $username\@$host...\n" if $opt_v;
+  my @batchconf = $conf->config('batchconfig-eft_canada', $pay_batch->agentnum);
+  my $user = $batchconf[0] or die "no EFT Canada batch username configured\n";
+  my $pass = $batchconf[1] or die "no EFT Canada batch password configured\n";
+
+  my $host = 'ftp.eftcanada.com';
+  print STDERR "Connecting to $user\@$host...\n" if $opt_v;
 
-my $sftp = Net::SFTP::Foreign->new( host => $host,
-                                    user => $username,
-                                    password => $password,
-                                    timeout => 30,
+  my $sftp = Net::SFTP::Foreign->new( host     => $host,
+                                      user     => $user,
+                                      password => $pass,
+                                      timeout  => 30,
                                     );
-die "failed to connect to '$username\@$host'\n(".$sftp->error.")\n" 
-    if $sftp->error;
+  die "failed to connect to '$user\@$host'\n(".$sftp->error.")\n" 
+      if $sftp->error;
 
-foreach my $filename (@filenames) {
   $sftp->put("$tmpdir/$filename", "$filename")
     or die "failed to upload file (".$sftp->error.")\n";
-}
 
-$FS::UID::AutoCommit = 0;
-foreach my $pay_batch (@batches) {
+  undef $sftp; #$sftp->disconnect;
+
   # Auto-approve and close the batch.  Some false laziness with manual_approve.
   my $batchnum = $pay_batch->batchnum;
   my $error;
@@ -86,7 +82,6 @@ foreach my $pay_batch (@batches) {
   $error ||= $pay_batch->set_status('R');
   die "error closing batch $batchnum: $error\n\n" if $error;
 }
-dbh->commit;
 
 print STDERR "Finished!\n" if $opt_v;
 
index 34297a5..b2a15ef 100755 (executable)
@@ -7,6 +7,9 @@
                                       'order_by'  => 'ORDER BY batchnum DESC',
                                    },
                 'count_query'   => "$count_query $extra_sql",
+                 'agent_virt'    => 1,
+                 'agent_null_right' => 'Process batches', #'Process global batches',
+                 'agent_pos'     => 1,
                 'header'        => [ 'Batch',
                                      'Type',
                                      'First Download',