From 96318ad08284a84ea591a66cfdef6a4cea60790b Mon Sep 17 00:00:00 2001 From: levinse Date: Sun, 23 Jan 2011 04:56:10 +0000 Subject: [PATCH] add possibly finished credit card surcharge, RT11237 --- FS/FS/cust_bill.pm | 31 ++++++++++++++++++++++++++++++- FS/FS/cust_main/Billing_Realtime.pm | 34 +++++++++++++++++++--------------- 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/FS/FS/cust_bill.pm b/FS/FS/cust_bill.pm index 62ab87f0f..49d9128bd 100644 --- a/FS/FS/cust_bill.pm +++ b/FS/FS/cust_bill.pm @@ -286,11 +286,40 @@ sub replace_check { #return "Can't change _date!" unless $old->_date eq $new->_date; return "Can't change _date" unless $old->_date == $new->_date; return "Can't change charged" unless $old->charged == $new->charged - || $old->charged == 0; + || $old->charged == 0 + || $new->{'Hash'}{'cc_surcharge_replace_hack'}; ''; } + +=item add_cc_surcharge + +Giant hack + +=cut + +sub add_cc_surcharge { + my ($self, $pkgnum, $amount) = (shift, shift, shift); + + my $error; + my $cust_bill_pkg = new FS::cust_bill_pkg({ + 'invnum' => $self->invnum, + 'pkgnum' => $pkgnum, + 'setup' => $amount, + }); + $error = $cust_bill_pkg->insert; + return $error if $error; + + $self->{'Hash'}{'cc_surcharge_replace_hack'} = 1; + $self->charged($self->charged+$amount); + $error = $self->replace; + return $error if $error; + + $self->apply_payments_and_credits; +} + + =item check Checks all fields to make sure this is a valid invoice. If there is an error, diff --git a/FS/FS/cust_main/Billing_Realtime.pm b/FS/FS/cust_main/Billing_Realtime.pm index e94f78e3a..dc896fa64 100644 --- a/FS/FS/cust_main/Billing_Realtime.pm +++ b/FS/FS/cust_main/Billing_Realtime.pm @@ -912,6 +912,8 @@ sub _realtime_bop_result { # 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 @@ -942,26 +944,28 @@ sub _realtime_bop_result { 'cust_pkg_ref' => \$cust_pkg, }); if($charge_error) { - warn 'Unable to add CC surcharge'; + 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_pkg = new FS::cust_bill_pkg({ - 'invnum' => $invnum, - 'pkgnum' => $cust_pkg->pkgnum, - 'setup' => $options{'cc_surcharge'}, - }); - my $cbp_error = $cust_bill_pkg->insert; - - if ( $cbp_error) { - warn 'Cannot add CC surcharge line item to invoice #'.$invnum; - return ''; - } else { - my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum }); - warn 'invoice for cc surcharge: ' . Dumper($cust_bill) if $DEBUG; - $cust_bill->apply_payments_and_credits; + 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; } return ''; #no error -- 2.11.0