Skip to content

Commit

Permalink
Fix applied balance on invoices (#1445)
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints authored Sep 26, 2022
1 parent 713d7d3 commit 0c6467f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion resources/views/receipt.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@
</tr>

<!-- Applied Balance -->
@if ($invoice->rawAppliedBalance() > 0)
@if ($invoice->hasAppliedBalance())
<tr>
<td colspan="{{ $invoice->hasTax() ? 3 : 2 }}" style="text-align: right;">
Applied balance
Expand Down
16 changes: 11 additions & 5 deletions src/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,16 @@ public function rawEndingBalance()
return $this->invoice->ending_balance ?? 0;
}

/**
* Determine if the invoice has balance applied.
*
* @return bool
*/
public function hasAppliedBalance()
{
return $this->rawAppliedBalance() < 0;
}

/**
* Get the applied balance for the invoice.
*
Expand All @@ -257,16 +267,12 @@ public function appliedBalance()
}

/**
* Get the raw ending balance for the invoice.
* Get the raw applied balance for the invoice.
*
* @return int
*/
public function rawAppliedBalance()
{
if (! $this->hasEndingBalance()) {
return 0;
}

return $this->rawStartingBalance() - $this->rawEndingBalance();
}

Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/InvoiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,29 @@ public function test_it_can_return_its_applied_balance()

$invoice = new Invoice($user, $stripeInvoice);

$this->assertTrue($invoice->hasAppliedBalance());
$this->assertEquals('-$1.50', $invoice->appliedBalance());
$this->assertEquals(-150, $invoice->rawAppliedBalance());
}

public function test_it_can_return_its_applied_balance_when_depleted()
{
$stripeInvoice = new StripeInvoice();
$stripeInvoice->customer = 'foo';
$stripeInvoice->ending_balance = 0;
$stripeInvoice->starting_balance = -500;
$stripeInvoice->currency = 'USD';

$user = new User();
$user->stripe_id = 'foo';

$invoice = new Invoice($user, $stripeInvoice);

$this->assertTrue($invoice->hasAppliedBalance());
$this->assertEquals('-$5.00', $invoice->appliedBalance());
$this->assertEquals(-500, $invoice->rawAppliedBalance());
}

public function test_it_can_determine_if_it_has_a_discount_applied()
{
$discountAmount = new stdClass();
Expand Down

0 comments on commit 0c6467f

Please sign in to comment.