mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-01 00:34:35 -04:00
Working on tests for invoice items
This commit is contained in:
parent
31ce0eaca8
commit
ae57c05659
@ -12,7 +12,7 @@ class InvoiceItemFactory
|
|||||||
$item->product_key = '';
|
$item->product_key = '';
|
||||||
$item->notes = '';
|
$item->notes = '';
|
||||||
$item->discount = 0;
|
$item->discount = 0;
|
||||||
$item->is_amount_discount = false;
|
$item->is_amount_discount = true;
|
||||||
$item->tax_name1 = '';
|
$item->tax_name1 = '';
|
||||||
$item->tax_rate1 = 0;
|
$item->tax_rate1 = 0;
|
||||||
$item->tax_name2 = '';
|
$item->tax_name2 = '';
|
||||||
|
92
app/Helpers/Invoice/InvoiceItemCalc.php
Normal file
92
app/Helpers/Invoice/InvoiceItemCalc.php
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Helpers\Invoice;
|
||||||
|
|
||||||
|
use App\Models\Invoice;
|
||||||
|
use App\Utils\Traits\NumberFormatter;
|
||||||
|
|
||||||
|
class InvoiceItemCalc
|
||||||
|
{
|
||||||
|
|
||||||
|
use NumberFormatter;
|
||||||
|
|
||||||
|
protected $item;
|
||||||
|
|
||||||
|
protected $precision;
|
||||||
|
|
||||||
|
protected $inclusive_tax;
|
||||||
|
|
||||||
|
protected $total_taxes;
|
||||||
|
|
||||||
|
public function __construct(\stdClass $item, int $precision = 2, bool $inclusive_tax)
|
||||||
|
{
|
||||||
|
$this->item = $item;
|
||||||
|
$this->precision = $precision;
|
||||||
|
$this->inclusive_tax = $inclusive_tax;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function process()
|
||||||
|
{
|
||||||
|
$this->setLineTotal($this->formatValue($this->item->cost, $this->precision) * $this->formatValue($this->item->qty, $this->precision));
|
||||||
|
$this->setDiscount();
|
||||||
|
$this->calcTaxes();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function setDiscount()
|
||||||
|
{
|
||||||
|
|
||||||
|
if($this->item->is_amount_discount)
|
||||||
|
$this->setLineTotal($this->getLineTotal() - $this->formatValue($this->item->discount, $this->precision));
|
||||||
|
else
|
||||||
|
$this->setLineTotal($this->getLineTotal() - $this->formatValue(($this->getLineTotal() * $this->item->discount / 100), $this->precision));
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private function calcTaxes()
|
||||||
|
{
|
||||||
|
$item_tax = 0;
|
||||||
|
|
||||||
|
$tax_rate1 = $this->formatValue($this->item->tax_rate1, $this->precision);
|
||||||
|
|
||||||
|
if($tax_rate1 != 0)
|
||||||
|
{
|
||||||
|
$item_tax += $this->formatValue(($this->getLineTotal() * $tax_rate1/100) , $this->precision);
|
||||||
|
}
|
||||||
|
|
||||||
|
$tax_rate2 = $this->formatValue($this->item->tax_rate2, $this->precision);
|
||||||
|
|
||||||
|
if($tax_rate2 != 0)
|
||||||
|
{
|
||||||
|
$item_tax += $this->formatValue(($this->getLineTotal() * $tax_rate2/100) , $this->precision);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->setTotalTaxes($item_tax);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getLineTotal()
|
||||||
|
{
|
||||||
|
return $this->item->line_total;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setLineTotal($total)
|
||||||
|
{
|
||||||
|
$this->item->line_total = $total;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getTotalTaxes()
|
||||||
|
{
|
||||||
|
return $this->total_taxes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setTotalTaxes($total)
|
||||||
|
{
|
||||||
|
$this->total_taxes = $total;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -27,6 +27,7 @@ use App\Transformers\ClientTransformer;
|
|||||||
/**
|
/**
|
||||||
* Class ClientController
|
* Class ClientController
|
||||||
* @package App\Http\Controllers
|
* @package App\Http\Controllers
|
||||||
|
* @covers App\Http\Controllers\ClientController
|
||||||
*/
|
*/
|
||||||
class ClientController extends BaseController
|
class ClientController extends BaseController
|
||||||
{
|
{
|
||||||
|
@ -6,6 +6,12 @@ use App\Models\Invoice;
|
|||||||
use App\Utils\Traits\MakesHash;
|
use App\Utils\Traits\MakesHash;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ClientController
|
||||||
|
* @package App\Http\Controllers
|
||||||
|
* @covers App\Http\Controllers\ClientController
|
||||||
|
*/
|
||||||
|
|
||||||
class InvoiceController extends BaseController
|
class InvoiceController extends BaseController
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ class QueryLogging
|
|||||||
$timeEnd = microtime(true);
|
$timeEnd = microtime(true);
|
||||||
$time = $timeEnd - $timeStart;
|
$time = $timeEnd - $timeStart;
|
||||||
Log::info($request->method() . ' - ' . $request->url() . ": $count queries - " . $time);
|
Log::info($request->method() . ' - ' . $request->url() . ": $count queries - " . $time);
|
||||||
Log::info($queries);
|
//Log::info($queries);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -8,9 +8,9 @@ namespace App\Utils\Traits;
|
|||||||
*/
|
*/
|
||||||
trait NumberFormatter
|
trait NumberFormatter
|
||||||
{
|
{
|
||||||
private function formatValue($value) : string
|
private function formatValue($value, $precision) : string
|
||||||
{
|
{
|
||||||
return number_format($this->parseFloat($value), $this->precision, '.', '');
|
return number_format($this->parseFloat($value), $precision, '.', '');
|
||||||
}
|
}
|
||||||
|
|
||||||
private function parseFloat($value) : float
|
private function parseFloat($value) : float
|
||||||
|
@ -16,6 +16,10 @@ use Illuminate\Http\Request;
|
|||||||
use Illuminate\Support\Facades\Session;
|
use Illuminate\Support\Facades\Session;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @covers App\Http\Controllers\AccountController
|
||||||
|
*/
|
||||||
|
|
||||||
class AccountTest extends TestCase
|
class AccountTest extends TestCase
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -32,9 +36,6 @@ class AccountTest extends TestCase
|
|||||||
Model::reguard();
|
Model::reguard();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* @covers AccountController
|
|
||||||
*/
|
|
||||||
public function testAccountCreation()
|
public function testAccountCreation()
|
||||||
{
|
{
|
||||||
$data = [
|
$data = [
|
||||||
|
101
tests/Unit/InvoiceItemTest.php
Normal file
101
tests/Unit/InvoiceItemTest.php
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Unit;
|
||||||
|
|
||||||
|
use App\Factory\InvoiceItemFactory;
|
||||||
|
use App\Helpers\Invoice\InvoiceItemCalc;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @covers App\Helpers\Invoice\InvoiceItemCalc
|
||||||
|
*/
|
||||||
|
class InvoiceItemTest extends TestCase
|
||||||
|
{
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testInvoiceItemTotalSimple()
|
||||||
|
{
|
||||||
|
$item = InvoiceItemFactory::create();
|
||||||
|
$item->qty = 1;
|
||||||
|
$item->cost =10;
|
||||||
|
$item->is_amount_discount = true;
|
||||||
|
|
||||||
|
$inclusive_tax = true
|
||||||
|
|
||||||
|
$item_calc = new InvoiceItemCalc($item, 2, $inclusive_tax);
|
||||||
|
$item_calc->process();
|
||||||
|
|
||||||
|
$this->assertEquals($item_calc->getLineTotal(), 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testInvoiceItemTotalSimpleWithDiscount()
|
||||||
|
{
|
||||||
|
$item = InvoiceItemFactory::create();
|
||||||
|
$item->qty = 1;
|
||||||
|
$item->cost =10;
|
||||||
|
$item->is_amount_discount = true;
|
||||||
|
$item->discount = 2;
|
||||||
|
|
||||||
|
$inclusive_tax = true
|
||||||
|
|
||||||
|
$item_calc = new InvoiceItemCalc($item, 2, $inclusive_tax);
|
||||||
|
$item_calc->process();
|
||||||
|
|
||||||
|
$this->assertEquals($item_calc->getLineTotal(), 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testInvoiceItemTotalSimpleWithDiscountWithPrecision()
|
||||||
|
{
|
||||||
|
$item = InvoiceItemFactory::create();
|
||||||
|
$item->qty = 1;
|
||||||
|
$item->cost =10;
|
||||||
|
$item->is_amount_discount = true;
|
||||||
|
$item->discount = 2.521254522145214511;
|
||||||
|
|
||||||
|
$inclusive_tax = true
|
||||||
|
|
||||||
|
$item_calc = new InvoiceItemCalc($item, 2, $inclusive_tax);
|
||||||
|
$item_calc->process();
|
||||||
|
|
||||||
|
$this->assertEquals($item_calc->getLineTotal(), 7.48);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testInvoiceItemTotalSimpleWithDiscountWithPrecision()
|
||||||
|
{
|
||||||
|
$item = InvoiceItemFactory::create();
|
||||||
|
$item->qty = 1;
|
||||||
|
$item->cost =10;
|
||||||
|
$item->is_amount_discount = true;
|
||||||
|
$item->discount = 2.521254522145214511;
|
||||||
|
|
||||||
|
$inclusive_tax = true
|
||||||
|
|
||||||
|
$item_calc = new InvoiceItemCalc($item, 2, $inclusive_tax);
|
||||||
|
$item_calc->process();
|
||||||
|
|
||||||
|
$this->assertEquals($item_calc->getLineTotal(), 7.48);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testInvoiceItemTotalSimpleWithDiscountWithPrecisionWithSingleTax()
|
||||||
|
{
|
||||||
|
$item = InvoiceItemFactory::create();
|
||||||
|
$item->qty = 1;
|
||||||
|
$item->cost =10;
|
||||||
|
$item->is_amount_discount = true;
|
||||||
|
$item->discount = 2.521254522145214511;
|
||||||
|
|
||||||
|
$inclusive_tax = true
|
||||||
|
|
||||||
|
$item_calc = new InvoiceItemCalc($item, 2, $inclusive_tax);
|
||||||
|
$item_calc->process();
|
||||||
|
|
||||||
|
$this->assertEquals($item_calc->getLineTotal(), 7.48);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user