diff --git a/app/Factory/InvoiceItemFactory.php b/app/Factory/InvoiceItemFactory.php index d0fb921441e7..a853bd6ec698 100644 --- a/app/Factory/InvoiceItemFactory.php +++ b/app/Factory/InvoiceItemFactory.php @@ -12,7 +12,7 @@ class InvoiceItemFactory $item->product_key = ''; $item->notes = ''; $item->discount = 0; - $item->is_amount_discount = false; + $item->is_amount_discount = true; $item->tax_name1 = ''; $item->tax_rate1 = 0; $item->tax_name2 = ''; diff --git a/app/Helpers/Invoice/InvoiceItemCalc.php b/app/Helpers/Invoice/InvoiceItemCalc.php new file mode 100644 index 000000000000..a6db15615c97 --- /dev/null +++ b/app/Helpers/Invoice/InvoiceItemCalc.php @@ -0,0 +1,92 @@ +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; + } + +} \ No newline at end of file diff --git a/app/Http/Controllers/ClientController.php b/app/Http/Controllers/ClientController.php index c73240202e1f..2dfa0144ac8b 100644 --- a/app/Http/Controllers/ClientController.php +++ b/app/Http/Controllers/ClientController.php @@ -27,6 +27,7 @@ use App\Transformers\ClientTransformer; /** * Class ClientController * @package App\Http\Controllers + * @covers App\Http\Controllers\ClientController */ class ClientController extends BaseController { diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index c87fa582194e..6fbbed63d761 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -6,6 +6,12 @@ use App\Models\Invoice; use App\Utils\Traits\MakesHash; use Illuminate\Http\Request; +/** + * Class ClientController + * @package App\Http\Controllers + * @covers App\Http\Controllers\ClientController + */ + class InvoiceController extends BaseController { diff --git a/app/Http/Middleware/QueryLogging.php b/app/Http/Middleware/QueryLogging.php index 603c04384924..73fc3f1af3ae 100644 --- a/app/Http/Middleware/QueryLogging.php +++ b/app/Http/Middleware/QueryLogging.php @@ -39,7 +39,7 @@ class QueryLogging $timeEnd = microtime(true); $time = $timeEnd - $timeStart; Log::info($request->method() . ' - ' . $request->url() . ": $count queries - " . $time); - Log::info($queries); + //Log::info($queries); } } diff --git a/app/Utils/Traits/NumberFormatter.php b/app/Utils/Traits/NumberFormatter.php index 7eda13ffabdf..4e68ec41f21b 100644 --- a/app/Utils/Traits/NumberFormatter.php +++ b/app/Utils/Traits/NumberFormatter.php @@ -8,9 +8,9 @@ namespace App\Utils\Traits; */ 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 diff --git a/tests/Feature/AccountTest.php b/tests/Feature/AccountTest.php index e8c9a152857b..f1454303714e 100644 --- a/tests/Feature/AccountTest.php +++ b/tests/Feature/AccountTest.php @@ -16,6 +16,10 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Session; use Tests\TestCase; +/* + * @covers App\Http\Controllers\AccountController + */ + class AccountTest extends TestCase { @@ -32,9 +36,6 @@ class AccountTest extends TestCase Model::reguard(); } - /* - * @covers AccountController - */ public function testAccountCreation() { $data = [ diff --git a/tests/Unit/InvoiceItemTest.php b/tests/Unit/InvoiceItemTest.php new file mode 100644 index 000000000000..b4c9e6552ad5 --- /dev/null +++ b/tests/Unit/InvoiceItemTest.php @@ -0,0 +1,101 @@ +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); + } + +}