X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=FS%2FFS%2Fcust_credit.pm;h=0376f1dc4fb3433408441521dc2aac5ca52ad7a3;hp=7741bbee299e5cd09ef5c91eb42fe0f2aa1a9a92;hb=9d35792778885932c09102bd011b518eb47c5131;hpb=95cef2cea4c98d8fde7f58bacce3cf1da955c1a0 diff --git a/FS/FS/cust_credit.pm b/FS/FS/cust_credit.pm index 7741bbee2..0376f1dc4 100644 --- a/FS/FS/cust_credit.pm +++ b/FS/FS/cust_credit.pm @@ -7,7 +7,7 @@ use vars qw( $conf $unsuspendauto $me $DEBUG ); use List::Util qw( min ); use Date::Format; -use FS::UID qw( dbh getotaker ); +use FS::UID qw( dbh ); use FS::Misc qw(send_email); use FS::Record qw( qsearch qsearchs dbdef ); use FS::CurrentUser; @@ -717,6 +717,7 @@ sub credit_lineitems { my %cust_bill_pkg = (); my %cust_credit_bill_pkg = (); my %taxlisthash = (); + my %unapplied_payments = (); #invoice numbers, and then billpaynums foreach my $billpkgnum ( @{$arg{billpkgnums}} ) { my $setuprecur = shift @{$arg{setuprecurs}}; my $amount = shift @{$arg{amounts}}; @@ -744,7 +745,6 @@ sub credit_lineitems { push @{$cust_bill_pkg{$invnum}}, $cust_bill_pkg; - my %unapplied_payments; # billpaynum => amount #unapply any payments applied to this line item (other credits too?) foreach my $cust_bill_pay_pkg ( $cust_bill_pkg->cust_bill_pay_pkg($setuprecur) ) { $error = $cust_bill_pay_pkg->delete; @@ -752,31 +752,9 @@ sub credit_lineitems { $dbh->rollback if $oldAutoCommit; return "Error unapplying payment: $error"; } - $unapplied_payments{$cust_bill_pay_pkg->billpaynum} + $unapplied_payments{$invnum}{$cust_bill_pay_pkg->billpaynum} += $cust_bill_pay_pkg->amount; } - # also unapply that amount from the invoice so it doesn't screw up - # application of the credit - foreach my $billpaynum (keys %unapplied_payments) { - my $cust_bill_pay = FS::cust_bill_pay->by_key($billpaynum) - or die "broken payment application $billpaynum"; - $error = $cust_bill_pay->delete; # can't replace - - my $new_cust_bill_pay = FS::cust_bill_pay->new({ - $cust_bill_pay->hash, - billpaynum => '', - amount => sprintf('%.2f', - $cust_bill_pay->amount - $unapplied_payments{$billpaynum}), - }); - - if ( $new_cust_bill_pay->amount > 0 ) { - $error ||= $new_cust_bill_pay->insert; - } - if ( $error ) { - $dbh->rollback if $oldAutoCommit; - return "Error unapplying payment: $error"; - } - } #$subtotal += $amount; $cust_credit_bill{$invnum} += $amount; @@ -904,8 +882,53 @@ sub credit_lineitems { }; } # if $amount > 0 + + #unapply any payments applied to the tax + foreach my $cust_bill_pay_pkg + ( $tax_item->cust_bill_pay_pkg('setup') ) + { + $error = $cust_bill_pay_pkg->delete; + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return "Error unapplying payment: $error"; + } + $unapplied_payments{$invnum}{$cust_bill_pay_pkg->billpaynum} + += $cust_bill_pay_pkg->amount; + } } #foreach $taxline + # if we unapplied any payments from line items, also unapply that + # amount from the invoice + foreach my $billpaynum (keys %{$unapplied_payments{$invnum}}) { + my $cust_bill_pay = FS::cust_bill_pay->by_key($billpaynum) + or die "broken payment application $billpaynum"; + my @subapps = $cust_bill_pay->lineitem_applications; + $error = $cust_bill_pay->delete; # can't replace + + my $new_cust_bill_pay = FS::cust_bill_pay->new({ + $cust_bill_pay->hash, + billpaynum => '', + amount => sprintf('%.2f', + $cust_bill_pay->amount + - $unapplied_payments{$invnum}{$billpaynum}), + }); + + if ( $new_cust_bill_pay->amount > 0 ) { + $error ||= $new_cust_bill_pay->insert; + # Also reapply it to everything it was applied to before. + # Note that we've already deleted cust_bill_pay_pkg records for the + # items we're crediting, so they aren't on this list. + foreach my $cust_bill_pay_pkg (@subapps) { + $cust_bill_pay_pkg->billpaypkgnum(''); + $cust_bill_pay_pkg->billpaynum($new_cust_bill_pay->billpaynum); + $error ||= $cust_bill_pay_pkg->insert; + } + } + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return "Error unapplying payment: $error"; + } + } #insert cust_credit_bill my $cust_credit_bill = new FS::cust_credit_bill {