diff --git a/src/Cart.php b/src/Cart.php index 0ead1f5..7a0333e 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -375,7 +375,7 @@ class Cart } /** - * Get the subtotal (total - tax) of the items in the cart. + * Get the discount of the items in the cart. * * @return float */ @@ -387,7 +387,7 @@ class Cart } /** - * Get the subtotal (total - tax) of the items in the cart as formatted string. + * Get the discount of the items in the cart as formatted string. * * @param int $decimals * @param string $decimalPoint @@ -401,11 +401,11 @@ class Cart } /** - * Get the subtotal (total - tax) of the items in the cart. + * Get the price of the items in the cart. * * @return float */ - public function initialFloat() + public function initialFloat() // TODO: rename and use priceTotal { return $this->getContent()->reduce(function ($initial, CartItem $cartItem) { return $initial + ($cartItem->qty * $cartItem->price); @@ -413,7 +413,7 @@ class Cart } /** - * Get the subtotal (total - tax) of the items in the cart as formatted string. + * Get the price of the items in the cart as formatted string. * * @param int $decimals * @param string $decimalPoint diff --git a/src/CartItem.php b/src/CartItem.php index 6ba3f5f..a7387cc 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -253,6 +253,20 @@ class CartItem implements Arrayable, Jsonable return $this->numberFormat($this->discountTotal, $decimals, $decimalPoint, $thousandSeperator); } + /** + * Returns the formatted total price for this cart item. + * + * @param int $decimals + * @param string $decimalPoint + * @param string $thousandSeperator + * + * @return string + */ + public function priceTotal($decimals = null, $decimalPoint = null, $thousandSeperator = null) + { + return $this->numberFormat($this->priceTotal, $decimals, $decimalPoint, $thousandSeperator); + } + /** * Set the quantity for this cart item. * @@ -354,26 +368,29 @@ class CartItem implements Arrayable, Jsonable if (property_exists($this, $attribute)) { return $this->{$attribute}; } + $decimals = config('cart.format.decimals', 2); switch ($attribute) { case 'discount': return $this->price * ($this->discountRate / 100); - case 'priceTarget': - return $this->price - $this->discount; - case 'subtotal': - return $this->priceTarget * $this->qty; case 'tax': - return $this->priceTarget * ($this->taxRate / 100); + return round($this->priceTarget * ($this->taxRate / 100), $decimals); case 'priceTax': - return $this->priceTarget + $this->tax; - case 'total': - return $this->priceTax * $this->qty; - case 'taxTotal': - return $this->tax * $this->qty; + return round($this->priceTarget + $this->tax, $decimals); case 'discountTotal': - return $this->discount * $this->qty; + return round ($this->discount * $this->qty, $decimals); case 'weightTotal': - return $this->weight * $this->qty; + return round($this->weight * $this->qty, $decimals); + case 'priceTotal': + return round($this->price * $this->qty, $decimals); + case 'subtotal': + return round($this->priceTotal - $this->discountTotal, $decimals); + case 'priceTarget': + return round(($this->priceTotal - $this->discountTotal) / $this->qty, $decimals); + case 'taxTotal': + return round($this->subtotal * ($this->taxRate / 100) , $decimals); + case 'total': + return round($this->subtotal + $this->taxTotal, $decimals); case 'model': if (isset($this->associatedModel)) { diff --git a/tests/CartTest.php b/tests/CartTest.php index e48d5b6..7d499da 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -1049,7 +1049,7 @@ class CartTest extends TestCase } /** @test */ - public function cart_hast_no_rounding_errors() + public function cart_has_no_rounding_errors() { $cart = $this->getCart(); @@ -1309,6 +1309,26 @@ class CartTest extends TestCase }); } + /** @test */ + public function it_use_correctly_rounded_values_for_totals_and_cart_summary() { + + $this->setConfigFormat(2, ',', ''); + + $cart = $this->getCartDiscount(6); + + $cartItem = $cart->add(new BuyableProduct(1, 'First item', 0.18929), 1000); + $cart->add(new BuyableProduct(2, 'Second item', 4.41632), 5); + $cart->add(new BuyableProduct(3, 'Third item', 0.37995), 25); + + $cart->setGlobalTax(22); + + // check total + $this->assertEquals('253,29', $cart->total()); + + // check that the sum of cart subvalues matches the total (in order to avoid cart summary to looks wrong) + $this->assertEquals($cart->totalFloat(), $cart->subtotalFloat() + $cart->taxFloat()); + } + /** * Get an instance of the cart. * @@ -1325,12 +1345,13 @@ class CartTest extends TestCase /** * Get an instance of the cart with discount. * + * @param int $discount * @return \Gloudemans\Shoppingcart\Cart */ - private function getCartDiscount($discount = 0) + private function getCartDiscount($discount = 50) { $cart = $this->getCart(); - $cart->setGlobalDiscount(50); + $cart->setGlobalDiscount($discount); return $cart; }