X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_credit.pm;h=bd92bdc754170a10e80d26e0ccbd4da7a0957126;hb=b5d22578f60ba105934c92c031799671bf6c0f29;hp=c716602a0101f9e540224bcd7c8e4e3e11526a33;hpb=d7e62d5ea2b5d3f3c7b8ba3da39cce1a606dc3d3;p=freeside.git diff --git a/FS/FS/cust_credit.pm b/FS/FS/cust_credit.pm index c716602a0..bd92bdc75 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; @@ -19,6 +19,8 @@ use FS::part_pkg; use FS::reason_type; use FS::reason; use FS::cust_event; +use FS::agent; +use FS::sales; $me = '[ FS::cust_credit ]'; $DEBUG = 0; @@ -311,6 +313,9 @@ sub check { || $self->ut_enum('closed', [ '', 'Y' ]) || $self->ut_foreign_keyn('pkgnum', 'cust_pkg', 'pkgnum') || $self->ut_foreign_keyn('eventnum', 'cust_event', 'eventnum') + || $self->ut_foreign_keyn('commission_agentnum', 'agent', 'agentnum') + || $self->ut_foreign_keyn('commission_salesnum', 'sales', 'salesnum') + || $self->ut_foreign_keyn('commission_pkgnum', 'cust_pkg', 'pkgnum') ; return $error if $error; @@ -717,8 +722,7 @@ sub credit_lineitems { my %cust_bill_pkg = (); my %cust_credit_bill_pkg = (); my %taxlisthash = (); - # except here they're billpaynums - my %unapplied_payments; + my %unapplied_payments = (); #invoice numbers, and then billpaynums foreach my $billpkgnum ( @{$arg{billpkgnums}} ) { my $setuprecur = shift @{$arg{setuprecurs}}; my $amount = shift @{$arg{amounts}}; @@ -753,7 +757,7 @@ 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; } @@ -893,27 +897,37 @@ 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; } } #foreach $taxline # if we unapplied any payments from line items, also unapply that # amount from the invoice - foreach my $billpaynum (keys %unapplied_payments) { + 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{$billpaynum}), + $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;