Skip to content

Commit

Permalink
Fix receipts (#1388)
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints committed Jun 30, 2022
1 parent 3a3d9c7 commit 3c9b2ed
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 18 deletions.
24 changes: 17 additions & 7 deletions resources/views/receipt.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,23 +289,33 @@
@endforeach
@endunless

<!-- Starting Balance -->
@if ($invoice->hasStartingBalance())
<!-- Display The Final Total -->
<tr>
<td colspan="{{ $invoice->hasTax() ? 3 : 2 }}" style="text-align: right;">
Total
</td>
<td>
{{ $invoice->realTotal() }}
</td>
</tr>

<!-- Applied Balance -->
@if ($invoice->hasEndingBalance())
<tr>
<td colspan="{{ $invoice->hasTax() ? 3 : 2 }}" style="text-align: right;">
Customer Balance
Applied balance
</td>
<td>{{ $invoice->startingBalance() }}</td>
<td>{{ $invoice->appliedBalance() }}</td>
</tr>
@endif

<!-- Display The Final Total -->
<!-- Display The Amount Due -->
<tr>
<td colspan="{{ $invoice->hasTax() ? 3 : 2 }}" style="text-align: right;">
<strong>Total</strong>
<strong>Amount due</strong>
</td>
<td>
<strong>{{ $invoice->total() }}</strong>
<strong>{{ $invoice->amountDue() }}</strong>
</td>
</tr>
</table>
Expand Down
98 changes: 96 additions & 2 deletions src/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function dueDate($timezone = null)
}

/**
* Get the total amount that was paid (or will be paid).
* Get the total amount minus the starting balance that was paid (or will be paid).
*
* @return string
*/
Expand All @@ -126,7 +126,7 @@ public function total()
}

/**
* Get the raw total amount that was paid (or will be paid).
* Get the raw total amount minus the starting balance that was paid (or will be paid).
*
* @return int
*/
Expand All @@ -135,6 +135,26 @@ public function rawTotal()
return $this->invoice->total + $this->rawStartingBalance();
}

/**
* Get the total amount that was paid (or will be paid).
*
* @return string
*/
public function realTotal()
{
return $this->formatAmount($this->rawRealTotal());
}

/**
* Get the raw total amount that was paid (or will be paid).
*
* @return int
*/
public function rawRealTotal()
{
return $this->invoice->total;
}

/**
* Get the total of the invoice (before discounts).
*
Expand All @@ -145,6 +165,26 @@ public function subtotal()
return $this->formatAmount($this->invoice->subtotal);
}

/**
* Get the amount due for the invoice.
*
* @return string
*/
public function amountDue()
{
return $this->formatAmount($this->rawAmountDue());
}

/**
* Get the raw amount due for the invoice.
*
* @return int
*/
public function rawAmountDue()
{
return $this->invoice->amount_due ?? 0;
}

/**
* Determine if the account had a starting balance.
*
Expand Down Expand Up @@ -175,6 +215,60 @@ public function rawStartingBalance()
return $this->invoice->starting_balance ?? 0;
}

/**
* Determine if the account had an ending balance.
*
* @return bool
*/
public function hasEndingBalance()
{
return ! is_null($this->invoice->ending_balance);
}

/**
* Get the ending balance for the invoice.
*
* @return string
*/
public function endingBalance()
{
return $this->formatAmount($this->rawEndingBalance());
}

/**
* Get the raw ending balance for the invoice.
*
* @return int
*/
public function rawEndingBalance()
{
return $this->invoice->ending_balance ?? 0;
}

/**
* Get the applied balance for the invoice.
*
* @return string
*/
public function appliedBalance()
{
return $this->formatAmount($this->rawAppliedBalance());
}

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

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

/**
* Determine if the invoice has one or more discounts applied.
*
Expand Down
34 changes: 25 additions & 9 deletions tests/Unit/InvoiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function test_it_can_return_its_total()

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

$total = $invoice->total();
$total = $invoice->realTotal();

$this->assertEquals('$10.00', $total);
}
Expand All @@ -86,7 +86,7 @@ public function test_it_can_return_its_raw_total()

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

$total = $invoice->rawTotal();
$total = $invoice->rawRealTotal();

$this->assertEquals(1000, $total);
}
Expand Down Expand Up @@ -166,25 +166,41 @@ public function test_it_can_return_its_starting_balance()

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

$startingBalance = $invoice->startingBalance();

$this->assertEquals('-$4.50', $startingBalance);
$this->assertEquals('-$4.50', $invoice->startingBalance());
$this->assertEquals(-450, $invoice->rawStartingBalance());
}

public function test_it_can_return_its_raw_starting_balance()
public function test_it_can_return_its_ending_balance()
{
$stripeInvoice = new StripeInvoice();
$stripeInvoice->customer = 'foo';
$stripeInvoice->starting_balance = -450;
$stripeInvoice->ending_balance = -450;
$stripeInvoice->currency = 'USD';

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

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

$startingBalance = $invoice->rawStartingBalance();
$this->assertEquals('-$4.50', $invoice->endingBalance());
$this->assertEquals(-450, $invoice->rawEndingBalance());
}

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

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

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

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

public function test_it_can_determine_if_it_has_a_discount_applied()
Expand Down

0 comments on commit 3c9b2ed

Please sign in to comment.