add to ACH batch feature from customer view page
[freeside.git] / FS / FS / pay_batch.pm
index 5a06ba6..c097534 100644 (file)
@@ -138,7 +138,7 @@ sub set_status {
   $self->replace();
 }
 
-=item import results OPTION => VALUE, ...
+=item import_results OPTION => VALUE, ...
 
 Import batch results.
 
@@ -162,6 +162,7 @@ sub import_results {
   my $formatre;      # for Fixed.+
   my @values;
   my $begin_condition;
+  my $pre_hook;
   my $end_condition;
   my $end_hook;
   my $hook;
@@ -340,7 +341,7 @@ sub import_results {
 
     @fields = (
       '',            # Name
-      'paybatchnum', # ID:  Invoice number of the transaction
+      'custnum'    , # ID:  Customer number of the transaction
       'aba',         # ABA Number for the transaction
       'payinfo',     # Bank Account Number for the transaction
       '',            # Transaction Type:  27 - debit
@@ -354,6 +355,20 @@ sub import_results {
       '';
     };
 
+    $pre_hook = sub {
+      my $hash = shift;
+      my @cust_pay_batch =    # this is dodgy, it works due to autoposting
+        qsearch('cust_pay_batch', { 'custnum' => $hash->{'custnum'}+0, 
+                                    'status'  => ''
+                                  } );
+      if ( scalar(@cust_pay_batch) == 1 ) {
+        $hash->{'paybatchnum'} = $cust_pay_batch[0]->paybatchnum;
+      }else{
+        return "can't find batch payment for customer number " .$hash->{custnum};
+      }
+      '';
+    };
+
     $hook = sub {
       my $hash = shift;
       $hash->{'_date'} = time;  # got a better one?
@@ -429,6 +444,14 @@ sub import_results {
       $hash{$field} = $value;
     }
 
+    if ( defined($pre_hook) ) {
+      my $error = &{$pre_hook}(\%hash);
+      if ( $error ) {
+        $dbh->rollback if $oldAutoCommit;
+        return $error;
+      }
+    }
+
     if ( &{$end_condition}(\%hash) ) {
       my $error = &{$end_hook}(\%hash, $total);
       if ( $error ) {