X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_main%2FBilling_Batch.pm;h=ed853182bfc6e7699615979821c11330064892aa;hb=6f08493827f30fe18fd99c32bbb1625b064017ec;hp=f7713295dabc79e406ccb6853f74ee63580e9309;hpb=1fcb4ef3e4e3ad7eae7067d37e7675efa37680fb;p=freeside.git diff --git a/FS/FS/cust_main/Billing_Batch.pm b/FS/FS/cust_main/Billing_Batch.pm index f7713295d..ed853182b 100644 --- a/FS/FS/cust_main/Billing_Batch.pm +++ b/FS/FS/cust_main/Billing_Batch.pm @@ -83,6 +83,10 @@ sub batch_card { ); } + my $paycode= $options{paycode} || ''; + my $batch_type = "DEBIT"; + $batch_type = "CREDIT" if $paycode eq 'C'; + my $oldAutoCommit = $FS::UID::AutoCommit; local $FS::UID::AutoCommit = 0; my $dbh = dbh; @@ -95,6 +99,7 @@ sub batch_card { my %pay_batch = ( 'status' => 'O', 'payby' => FS::payby->payby2payment($payby), + 'type' => $batch_type, ); $pay_batch{agentnum} = $self->agentnum if $conf->exists('batch-spoolagent'); @@ -165,6 +170,27 @@ sub batch_card { if ($options{'processing-fee'} > 0) { my $pf_cust_pkg; my $processing_fee_text = 'Payment Processing Fee'; + + unless ( $invnum ) { # probably from a payment screen + # do we have any open invoices? pick earliest + # uses the fact that cust_main->cust_bill sorts by date ascending + my @open = $self->open_cust_bill; + $invnum = $open[0]->invnum if scalar(@open); + } + + unless ( $invnum ) { # still nothing? pick last closed invoice + # again uses fact that cust_main->cust_bill sorts by date ascending + my @closed = $self->cust_bill; + $invnum = $closed[$#closed]->invnum if scalar(@closed); + } + + unless ( $invnum ) { + # XXX: unlikely case - pre-paying before any invoices generated + # what it should do is create a new invoice and pick it + warn '\PROCESS FEE AND NO INVOICES PICKED TO APPLY IT!'; + return ''; + } + my $pf_change_error = $self->charge({ 'amount' => $options{'processing-fee'}, 'pkg' => $processing_fee_text,