Mock tests for invoice calculations

This commit is contained in:
David Bomba 2019-04-10 19:09:57 +10:00
parent 1ce11fcb36
commit f7c31bd5a1
4 changed files with 68 additions and 5 deletions

View File

@ -0,0 +1,27 @@
<?php
namespace App\Factory;
class InvoiceFactory
{
public static function create() :\stdClass
{
$item = new \stdClass;
$item->qty = 0;
$item->cost = 0;
$item->product_key = '';
$item->notes = '';
$item->discount = 0;
$item->is_amount_discount = true;
$item->tax_name1 = '';
$item->tax_rate1 = 0;
$item->tax_name2 = '';
$item->tax_rate2 = 0;
$item->sort_id = 0;
$item->line_total = 0;
$item->invoice_item_type_id = 0;
return $item;
}
}

View File

@ -56,12 +56,20 @@ class InvoiceCalc
{
$this->calcLineItems()
->calcDiscount()
->sumCustomValues()
->calcBalance();
->calcCustomValues()
->calcBalance()
->calcPartial();
return $this;
}
private function calcPartial()
{
if ( !$this->invoice->id && isset($this->invoice->partial) ) {
$this->invoice->partial = max(0, min($this->formatValue($this->invoice->partial, 2), $this->invoice->balance));
}
}
private function calcDiscount()
{
if ($this->invoice->discount != 0) {
@ -81,7 +89,15 @@ class InvoiceCalc
return $this;
}
private function sumBalance()
/**
* Calculates the balance.
*
* //todo need to understand this better
*
* @return self The balance.
*/
private function calcBalance()
{
if(isset($this->invoice->id) && $this->invoice->id >= 1)
@ -95,7 +111,7 @@ class InvoiceCalc
}
private function sumCustomValues()
private function calcCustomValues()
{
$this->total += $this->getSubTotal();

View File

@ -369,7 +369,6 @@ class CreateUsersTable extends Migration
$t->string('custom_value3')->nullable();
$t->string('custom_value4')->nullable();
$t->decimal('total', 13,2);
$t->decimal('amount', 13, 2);
$t->decimal('balance', 13, 2);
$t->decimal('partial', 13, 2)->nullable();

View File

@ -0,0 +1,21 @@
<?php
namespace Tests\Unit;
use App\Factory\InvoiceItemFactory;
use Tests\TestCase;
/**
* @test
* @covers App\Helpers\Invoice\InvoiceCalc
*/
class InvoiceTest extends TestCase
{
public function setUp()
{
parent::setUp();
}
}