add to ACH batch feature from customer view page
[freeside.git] / FS / FS / cust_bill.pm
index d39e6dd..877d364 100644 (file)
@@ -1434,107 +1434,9 @@ sub batch_card {
   my ($self, %options) = @_;
   my $cust_main = $self->cust_main;
 
-  my $amount = sprintf("%.2f", $cust_main->balance - $cust_main->in_transit_payments);
-  return '' unless $amount > 0;
+  $options{invnum} = $self->invnum;
   
-  if ($options{'realtime'}) {
-    return $cust_main->realtime_bop( FS::payby->payby2bop($cust_main->payby),
-                                     $amount,
-                                     %options,
-                                   );
-  }
-
-  my $oldAutoCommit = $FS::UID::AutoCommit;
-  local $FS::UID::AutoCommit = 0;
-  my $dbh = dbh;
-
-  $dbh->do("LOCK TABLE pay_batch IN SHARE ROW EXCLUSIVE MODE")
-    or return "Cannot lock pay_batch: " . $dbh->errstr;
-
-  my %pay_batch = (
-    'status' => 'O',
-    'payby'  => FS::payby->payby2payment($cust_main->payby),
-  );
-
-  my $pay_batch = qsearchs( 'pay_batch', \%pay_batch );
-
-  unless ( $pay_batch ) {
-    $pay_batch = new FS::pay_batch \%pay_batch;
-    my $error = $pay_batch->insert;
-    if ( $error ) {
-      $dbh->rollback if $oldAutoCommit;
-      die "error creating new batch: $error\n";
-    }
-  }
-
-  my $old_cust_pay_batch = qsearchs('cust_pay_batch', {
-      'batchnum' => $pay_batch->batchnum,
-      'custnum'  => $cust_main->custnum,
-  } );
-
-  my $cust_pay_batch = new FS::cust_pay_batch ( {
-    'batchnum' => $pay_batch->batchnum,
-    'invnum'   => $self->getfield('invnum'),       # is there a better value?
-                                                   # this field should be
-                                                  # removed...
-                                                  # cust_bill_pay_batch now
-    'custnum'  => $cust_main->custnum,
-    'last'     => $cust_main->getfield('last'),
-    'first'    => $cust_main->getfield('first'),
-    'address1' => $cust_main->address1,
-    'address2' => $cust_main->address2,
-    'city'     => $cust_main->city,
-    'state'    => $cust_main->state,
-    'zip'      => $cust_main->zip,
-    'country'  => $cust_main->country,
-    'payby'    => $cust_main->payby,
-    'payinfo'  => $cust_main->payinfo,
-    'exp'      => $cust_main->paydate,
-    'payname'  => $cust_main->payname,
-    'amount'   => $amount,                          # consolidating
-  } );
-  
-  $cust_pay_batch->paybatchnum($old_cust_pay_batch->paybatchnum)
-    if $old_cust_pay_batch;
-
-  my $error;
-  if ($old_cust_pay_batch) {
-    $error = $cust_pay_batch->replace($old_cust_pay_batch)
-  } else {
-    $error = $cust_pay_batch->insert;
-  }
-
-  if ( $error ) {
-    $dbh->rollback if $oldAutoCommit;
-    die $error;
-  }
-
-  my $unapplied = $cust_main->total_credited + $cust_main->total_unapplied_payments + $cust_main->in_transit_payments;
-  foreach my $cust_bill ($cust_main->open_cust_bill) {
-    #$dbh->commit or die $dbh->errstr if $oldAutoCommit;
-    my $cust_bill_pay_batch = new FS::cust_bill_pay_batch {
-      'invnum' => $cust_bill->invnum,
-      'paybatchnum' => $cust_pay_batch->paybatchnum,
-      'amount' => $cust_bill->owed,
-      '_date' => time,
-    };
-    if ($unapplied >= $cust_bill_pay_batch->amount){
-      $unapplied -= $cust_bill_pay_batch->amount;
-      next;
-    }else{
-      $cust_bill_pay_batch->amount(sprintf ( "%.2f", 
-                                   $cust_bill_pay_batch->amount - $unapplied ));
-      $unapplied = 0;
-    }
-    $error = $cust_bill_pay_batch->insert;
-    if ( $error ) {
-      $dbh->rollback if $oldAutoCommit;
-      die $error;
-    }
-  }
-
-  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
-  '';
+  $cust_main->batch_card(%options);
 }
 
 sub _agent_template {