diff --git a/app/Helpers/Invoice/InvoiceItemCalc.php b/app/Helpers/Invoice/InvoiceItemCalc.php index a6db15615c97..b03df9fca1a0 100644 --- a/app/Helpers/Invoice/InvoiceItemCalc.php +++ b/app/Helpers/Invoice/InvoiceItemCalc.php @@ -52,14 +52,21 @@ class InvoiceItemCalc if($tax_rate1 != 0) { - $item_tax += $this->formatValue(($this->getLineTotal() * $tax_rate1/100) , $this->precision); + if($this->inclusive_tax) + $item_tax += $this->formatValue(($this->getLineTotal() - ($this->getLineTotal() / (1+$tax_rate1/100))) , $this->precision); + else + $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); + if($this->inclusive_tax) + $item_tax += $this->formatValue(($this->getLineTotal() - ($this->getLineTotal() / (1+$tax_rate2/100))) , $this->precision); + else + $item_tax += $this->formatValue(($this->getLineTotal() * $tax_rate2/100), $this->precision); + } $this->setTotalTaxes($item_tax); diff --git a/tests/Unit/InvoiceItemTest.php b/tests/Unit/InvoiceItemTest.php index b4c9e6552ad5..17d08a341846 100644 --- a/tests/Unit/InvoiceItemTest.php +++ b/tests/Unit/InvoiceItemTest.php @@ -26,7 +26,7 @@ class InvoiceItemTest extends TestCase $item->cost =10; $item->is_amount_discount = true; - $inclusive_tax = true + $inclusive_tax = true; $item_calc = new InvoiceItemCalc($item, 2, $inclusive_tax); $item_calc->process(); @@ -42,7 +42,7 @@ class InvoiceItemTest extends TestCase $item->is_amount_discount = true; $item->discount = 2; - $inclusive_tax = true + $inclusive_tax = true; $item_calc = new InvoiceItemCalc($item, 2, $inclusive_tax); $item_calc->process(); @@ -58,7 +58,7 @@ class InvoiceItemTest extends TestCase $item->is_amount_discount = true; $item->discount = 2.521254522145214511; - $inclusive_tax = true + $inclusive_tax = true; $item_calc = new InvoiceItemCalc($item, 2, $inclusive_tax); $item_calc->process(); @@ -66,36 +66,76 @@ class InvoiceItemTest extends TestCase $this->assertEquals($item_calc->getLineTotal(), 7.48); } - public function testInvoiceItemTotalSimpleWithDiscountWithPrecision() + public function testInvoiceItemTotalSimpleWithDiscountWithPrecisionWithSingleInclusiveTax() { $item = InvoiceItemFactory::create(); $item->qty = 1; $item->cost =10; $item->is_amount_discount = true; $item->discount = 2.521254522145214511; + $item->tax_rate1 = 10; - $inclusive_tax = true + $inclusive_tax = true; $item_calc = new InvoiceItemCalc($item, 2, $inclusive_tax); $item_calc->process(); - $this->assertEquals($item_calc->getLineTotal(), 7.48); + $this->assertEquals($item_calc->getTotalTaxes(), 0.68); } - public function testInvoiceItemTotalSimpleWithDiscountWithPrecisionWithSingleTax() + public function testInvoiceItemTotalSimpleWithDiscountWithPrecisionWithSingleExclusiveTax() { $item = InvoiceItemFactory::create(); $item->qty = 1; $item->cost =10; $item->is_amount_discount = true; $item->discount = 2.521254522145214511; + $item->tax_rate1 = 10; - $inclusive_tax = true + $inclusive_tax = false; $item_calc = new InvoiceItemCalc($item, 2, $inclusive_tax); $item_calc->process(); - $this->assertEquals($item_calc->getLineTotal(), 7.48); + $this->assertEquals($item_calc->getTotalTaxes(), 0.75); + } + + public function testInvoiceItemTotalSimpleWithDiscountWithPrecisionWithDoubleInclusiveTax() + { + $item = InvoiceItemFactory::create(); + $item->qty = 1; + $item->cost =10; + $item->is_amount_discount = true; + $item->discount = 2.521254522145214511; + $item->tax_rate1 = 10; + $item->tax_rate2 = 17.5; + + $inclusive_tax = true; + + $item_calc = new InvoiceItemCalc($item, 2, $inclusive_tax); + $item_calc->process(); + + $this->assertEquals($item_calc->getTotalTaxes(), 1.79); + } + + public function testInvoiceItemTotalSimpleWithDiscountWithPrecisionWithDoubleExclusiveTax() + { + $item = InvoiceItemFactory::create(); + $item->qty = 1; + $item->cost =10; + $item->is_amount_discount = true; + $item->discount = 2.521254522145214511; + $item->tax_rate1 = 10; + $item->tax_rate2 = 17.5; + + $inclusive_tax = false; + + $item_calc = new InvoiceItemCalc($item, 2, $inclusive_tax); + $item_calc->process(); + + $this->assertEquals($item_calc->getTotalTaxes(), 2.06); } } + +