summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorivan <ivan>2004-07-01 13:49:32 +0000
committerivan <ivan>2004-07-01 13:49:32 +0000
commit88d5d75f5297eaa96d3cd0c2a5f29c981e43e126 (patch)
treec743d4497c9d0c89cf4f40594bde3a426920fa97
parent1239c0a9987e36f2031cf56dbbe99a7ce5fd67bc (diff)
credit out self-service
-rw-r--r--FS/FS/ClientAPI/MyAccount.pm5
-rw-r--r--FS/FS/cust_main.pm16
2 files changed, 14 insertions, 7 deletions
diff --git a/FS/FS/ClientAPI/MyAccount.pm b/FS/FS/ClientAPI/MyAccount.pm
index e8ce6b432..58ab6c0b7 100644
--- a/FS/FS/ClientAPI/MyAccount.pm
+++ b/FS/FS/ClientAPI/MyAccount.pm
@@ -541,6 +541,11 @@ sub order_pkg {
if ( $cust_main->balance > $old_balance
&& $cust_main->payby !~ /^(BILL|DCRD|DCHK)$/ ) {
+ #this makes sense. credit is "un-doing" the invoice
+ $cust_main->credit( sprintf("%.2f", $cust_main->balance - $old_balance ),
+ 'self-service decline' );
+ $cust_main->apply_credits( 'order' => 'newest' );
+
$cust_pkg->cancel('quiet'=>1);
return { 'error' => '_decline' };
} else {
diff --git a/FS/FS/cust_main.pm b/FS/FS/cust_main.pm
index c3a8ad825..740b483d5 100644
--- a/FS/FS/cust_main.pm
+++ b/FS/FS/cust_main.pm
@@ -1995,28 +1995,30 @@ sub total_owed_date {
sprintf( "%.2f", $total_bill );
}
-=item apply_credits
+=item apply_credits OPTION => VALUE ...
Applies (see L<FS::cust_credit_bill>) unapplied credits (see L<FS::cust_credit>)
-to outstanding invoice balances in chronological order and returns the value
-of any remaining unapplied credits available for refund
-(see L<FS::cust_refund>).
+to outstanding invoice balances in chronological order (or reverse
+chronological order if the I<order> option is set to B<newest>) and returns the
+value of any remaining unapplied credits available for refund (see
+L<FS::cust_refund>).
=cut
sub apply_credits {
my $self = shift;
+ my %opt = @_;
return 0 unless $self->total_credited;
my @credits = sort { $b->_date <=> $a->_date} (grep { $_->credited > 0 }
qsearch('cust_credit', { 'custnum' => $self->custnum } ) );
- my @invoices = sort { $a->_date <=> $b->_date} (grep { $_->owed > 0 }
- qsearch('cust_bill', { 'custnum' => $self->custnum } ) );
+ my @invoices = $self->open_cust_bill;
+ @invoices = sort { $b->_date <=> $a->_date } @invoices
+ if defined($opt{'order'}) && $opt{'order'} eq 'newest';
my $credit;
-
foreach my $cust_bill ( @invoices ) {
my $amount;