+ # have a CC surcharge portion --> one-time charge
+ if ( $options{'cc_surcharge'} > 0 ) {
+ # XXX: this whole block needs to be in a transaction?
+
+ my $invnum;
+ $invnum = $options{'invnum'} if $options{'invnum'};
+ 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 'CC SURCHARGE AND NO INVOICES PICKED TO APPLY IT!';
+ return '';
+ }
+
+ my $cust_pkg;
+ my $charge_error = $self->charge({
+ 'amount' => $options{'cc_surcharge'},
+ 'pkg' => 'Credit Card Surcharge',
+ 'setuptax' => 'Y',
+ 'cust_pkg_ref' => \$cust_pkg,
+ });
+ if($charge_error) {
+ warn 'Unable to add CC surcharge cust_pkg';
+ return '';
+ }
+
+ $cust_pkg->setup(time);
+ my $cp_error = $cust_pkg->replace;
+ if($cp_error) {
+ warn 'Unable to set setup time on cust_pkg for cc surcharge';
+ # but keep going...
+ }
+
+ my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum });
+ unless ( $cust_bill ) {
+ warn "race condition + invoice deletion just happened";
+ return '';
+ }
+
+ my $grand_error =
+ $cust_bill->add_cc_surcharge($cust_pkg->pkgnum,$options{'cc_surcharge'});
+
+ warn "cannot add CC surcharge to invoice #$invnum: $grand_error"
+ if $grand_error;
+ }
+