diff --git a/build/logs/clover.xml b/build/logs/clover.xml index 195536b..05f72f7 100644 --- a/build/logs/clover.xml +++ b/build/logs/clover.xml @@ -1,6 +1,6 @@ - - + + @@ -38,7 +38,7 @@ - + @@ -120,43 +120,48 @@ - - - - + + + - - - - - - - - - - + + + + + + + + + + + + - - + + + + - - - - - - + + - - + - - + + + + + + + + + @@ -379,101 +384,101 @@ - + - + + - + - - + + - + - - - + + + - - - - - - - - + + + + + + + + + - - - - + + + + - - - - + + + - - + + - - - - - + + + + + - + - - - - - - + + + + + + - - + + + - - - - - - - - + + + + + + + + - - - - - - - - + + + + + + + + - - - - + + + + - - - - + + @@ -711,6 +716,6 @@ - + diff --git a/src/CartItem.php b/src/CartItem.php index a28ff5c..cb90ee3 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -279,9 +279,10 @@ public function getDiscount($format = true) * * @param int $amountNotTaxable * + * @param int $totalDiscount * @return int|mixed */ - public function tax($amountNotTaxable = 0) + public function tax($amountNotTaxable = 0, $totalDiscount = 0) { if (!$this->taxable) { $amountNotTaxable = $this->price * $this->qty; @@ -295,10 +296,21 @@ public function tax($amountNotTaxable = 0) $itemCount++; } + if($totalDiscount !== 0) { + $totalTax = $totalTax - ($totalDiscount - ($totalDiscount/(1 + $this->tax))); + } + return $totalTax - $amountNotTaxable; } - return $this->tax * ($this->subTotal(false, !config('laracart.discountTaxable', false), true) - $amountNotTaxable); + $totalTax = $this->tax * ($this->subTotal(false, !config('laracart.discountTaxable', false), true) - $amountNotTaxable); + + + if($totalDiscount !== 0) { + $totalTax = $totalTax - ($totalDiscount - ($totalDiscount/(1 + $this->tax))); + } + + return $totalTax; } /** diff --git a/src/LaraCart.php b/src/LaraCart.php index b07fc0a..a4bd263 100644 --- a/src/LaraCart.php +++ b/src/LaraCart.php @@ -561,20 +561,19 @@ public function removeFees() * * @param bool|true $format * @param bool|true $withFees - * + * @param bool $grossTaxes * @return string */ - public function taxTotal($format = true, $withFees = true) + public function taxTotal($format = true, $withFees = true, $grossTaxes = true) { $totalTax = 0; $discounted = 0; - $totalDiscount = $this->totalDiscount(false, false); + $tempTotalDiscount = $totalDiscount = $this->totalDiscount(false, false); if(config('laracart.discountsAlreadyTaxed')) { $totalDiscount = 0; } - if ($this->count() != 0) { /** * @var @@ -582,7 +581,7 @@ public function taxTotal($format = true, $withFees = true) */ foreach ($this->getItems() as $index => $item) { if ($discounted >= $totalDiscount) { - $totalTax += $item->tax(); + $totalTax += $item->tax(null, $grossTaxes && config('laracart.discountsAlreadyTaxed') ? $tempTotalDiscount : 0); } else { $itemPrice = $item->subTotal(false, config('laracart.discountTaxable', false)); if (($discounted + $itemPrice) > $totalDiscount) { @@ -601,7 +600,6 @@ public function taxTotal($format = true, $withFees = true) } } } - return $this->formatMoney($totalTax, null, null, $format); } @@ -628,7 +626,7 @@ public function total($format = true, $withDiscount = true, $withTax = true, $wi } if ($withTax) { - $total += $this->taxTotal(false, $withFees); + $total += $this->taxTotal(false, $withFees, false); } return $this->formatMoney($total, null, null, $format); diff --git a/tests/TotalsTest.php b/tests/TotalsTest.php index f9fd469..3b11d21 100644 --- a/tests/TotalsTest.php +++ b/tests/TotalsTest.php @@ -306,7 +306,7 @@ public function testTaxationOnCoupons() } public function testTaxationAndDiscount() { - + $this->app['config']->set('laracart.tax', .19); $this->app['config']->set('laracart.tax_by_item', true); $this->app['config']->set('laracart.discountTaxable', false); $this->app['config']->set('laracart.discountsAlreadyTaxed', true); @@ -329,7 +329,7 @@ public function testTaxationAndDiscount() { $this->assertEquals(100, $this->laracart->subTotal(false)); $this->assertEquals(50, $this->laracart->totalDiscount(false)); - $this->assertEquals(19.00, $this->laracart->taxTotal(false)); + $this->assertEquals(11.02, $this->laracart->taxTotal(false)); $this->assertEquals(69.00, $this->laracart->total(false)); } }