diff --git a/src/Cart.php b/src/Cart.php index ba5a51f..5fb6910 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -525,13 +525,8 @@ class Cart /** * Set the discount rate for the cart item with the given rowId. - * - * @param string $rowId - * @param int|float $taxRate - * - * @return void */ - public function setDiscount(string $rowId, $discount): void + public function setDiscount(string $rowId, float|Money $discount): void { $cartItem = $this->get($rowId); diff --git a/src/CartItem.php b/src/CartItem.php index 7e4b5fa..c897f8b 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -180,9 +180,11 @@ class CartItem implements Arrayable, Jsonable */ public function discount(): Money { + $price = $this->price(); if ($this->discount instanceof Money) { return $this->price()->subtract($this->discount); } else { + $result = $this->price()->multiply($this->discount, Config::get('cart.rounding', Money::ROUND_UP)); return $this->price()->multiply($this->discount, Config::get('cart.rounding', Money::ROUND_UP)); } } @@ -193,7 +195,8 @@ class CartItem implements Arrayable, Jsonable */ public function subtotal(): Money { - return Money::max(new Money(0, $this->price()->getCurrency()), $this->price()->add($this->discount())); + $subtotal = $this->price()->add($this->discount()); + return Money::max(new Money(0, $this->price->getCurrency()), $this->price()->subtract($this->discount())); } /** @@ -201,7 +204,8 @@ class CartItem implements Arrayable, Jsonable */ public function tax(): Money { - return $this->subtotal()->multiply($this->taxRate + 1, Config::get('cart.rounding', Money::ROUND_UP)); + $tax = $this->subtotal()->multiply($this->taxRate, Config::get('cart.rounding', Money::ROUND_UP)); + return $this->subtotal()->multiply($this->taxRate, Config::get('cart.rounding', Money::ROUND_UP)); } /** diff --git a/src/Config/cart.php b/src/Config/cart.php index b87faed..f3f2bc2 100644 --- a/src/Config/cart.php +++ b/src/Config/cart.php @@ -26,7 +26,7 @@ return [ | */ - 'tax' => 21, + 'tax' => 0.21, /* |-------------------------------------------------------------------------- diff --git a/tests/CartItemTest.php b/tests/CartItemTest.php index 51d99ee..88487d2 100644 --- a/tests/CartItemTest.php +++ b/tests/CartItemTest.php @@ -53,7 +53,7 @@ class CartItemTest extends TestCase $this->assertJson($cartItem->toJson()); - $json = '{"rowId":"07d5da5550494c62daf9993cf954303f","id":1,"name":"Some item","price":"10.00","qty":2,"weight":550,"options":{"size":"XL","color":"red"},"discount":"0.00","subtotal":"20.00","tax":"20.00","total":"40.00"}'; + $json = '{"rowId":"07d5da5550494c62daf9993cf954303f","id":1,"name":"Some item","price":"10.00","qty":2,"weight":550,"options":{"size":"XL","color":"red"},"discount":"0.00","subtotal":"20.00","tax":"0.00","total":"20.00"}'; $this->assertEquals($json, $cartItem->toJson()); } diff --git a/tests/CartTest.php b/tests/CartTest.php index 0bde298..ed841d0 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -5,7 +5,6 @@ namespace Gloudemans\Tests\Shoppingcart; use Carbon\Carbon; use Money\Money; use Money\Currency; -use Gloudemans\Shoppingcart\Calculation\GrossPrice; use Gloudemans\Shoppingcart\Cart; use Gloudemans\Shoppingcart\CartItem; use Gloudemans\Shoppingcart\CartItemOptions; @@ -535,7 +534,7 @@ class CartTest extends TestCase 'price' => '10.00', 'subtotal' => '10.00', 'tax' => '2.10', - 'total' => '7.90', + 'total' => '12.10', 'options' => [], 'discount' => '0.00', 'weight' => 0, @@ -549,7 +548,7 @@ class CartTest extends TestCase 'price' => '10.00', 'subtotal' => '10.00', 'tax' => '2.10', - 'total' => '7.90', + 'total' => '12.10', 'options' => [], 'discount' => '0.00', 'weight' => 0, @@ -582,7 +581,7 @@ class CartTest extends TestCase $cart->add(new BuyableProduct([ 'id' => 2, 'name' => 'Second item', - 'price' => '25.00', + 'price' => 2500, ]), 2); $this->assertItemsInCart(3, $cart); @@ -753,7 +752,7 @@ class CartTest extends TestCase 'name' => 'Some title', ]), 1); - $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); + $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 0.19); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); @@ -771,7 +770,7 @@ class CartTest extends TestCase $cart->add(new BuyableProduct([ 'id' => 2, 'name' => 'Some title', - 'price' => '20.00', + 'price' => 2000, ]), 2); $this->assertEquals(new Money(1050, new Currency('USD')), $cart->tax()); @@ -786,11 +785,11 @@ class CartTest extends TestCase 'name' => 'Some title', ]), 1); - $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); + $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 0.19); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals(19.0, $cartItem->taxRate); + $this->assertEquals(0.19, $cartItem->taxRate); } /** @test */ @@ -801,7 +800,7 @@ class CartTest extends TestCase $cart->add(new BuyableProduct(), 1); $cart->add(new BuyableProduct([ 'id' => 2, - 'price' => '20.00', + 'price' => 2000, ]), 2); $this->assertEquals(new Money(5000, new Currency('USD')), $cart->subtotal()); @@ -943,7 +942,7 @@ class CartTest extends TestCase /** @test */ public function it_can_calculate_all_values() { - $cart = $this->getCartDiscount(50); + $cart = $this->getCartDiscount(0.5); $cart->add(new BuyableProduct([ 'name' => 'First item', @@ -951,11 +950,11 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); + $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 0.19); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->price); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discount()); - $this->assertEquals(new Money(500, new Currency('USD')), $cartItem->subtotal()); + $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->subtotal()); $this->assertEquals(new Money(190, new Currency('USD')), $cartItem->tax()); $this->assertEquals(new Money(1190, new Currency('USD')), $cartItem->total()); } @@ -963,7 +962,7 @@ class CartTest extends TestCase /** @test */ public function it_can_calculate_all_values_after_updating_from_array() { - $cart = $this->getCartDiscount(50); + $cart = $this->getCartDiscount(0.5); $cart->add(new BuyableProduct([ 'name' => 'First item', ]), 1); @@ -972,7 +971,7 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); + $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 0.19); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->price); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discount()); @@ -984,7 +983,7 @@ class CartTest extends TestCase /** @test */ public function it_can_calculate_all_values_after_updating_from_buyable() { - $cart = $this->getCartDiscount(50); + $cart = $this->getCartDiscount(0.5); $cart->add(new BuyableProduct([ 'name' => 'First item', 'price' => '5.00', @@ -996,7 +995,7 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); + $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 0.19); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->price); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discount()); @@ -1031,7 +1030,7 @@ class CartTest extends TestCase ]), 2); $cart->setGlobalTax(0); - $cart->setGlobalDiscount(50); + $cart->setGlobalDiscount(0.5); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); @@ -1047,7 +1046,7 @@ class CartTest extends TestCase Event::fake(); - $cart = $this->getCartDiscount(50); + $cart = $this->getCartDiscount(0.5); $cart->add(new BuyableProduct([ 'name' => 'Item', ]), 1); @@ -1086,7 +1085,7 @@ class CartTest extends TestCase '--database' => 'testing', ]); Event::fake(); - $cart = $this->getCartDiscount(50); + $cart = $this->getCartDiscount(0.5); $cart->add(new BuyableProduct([ 'name' => 'Item', ]), 1); @@ -1101,12 +1100,11 @@ class CartTest extends TestCase /** @test */ public function cart_can_calculate_all_values() { - $cart = $this->getCartDiscount(50); + $cart = $this->getCartDiscount(0.5); $cart->add(new BuyableProduct([ 'name' => 'First item', ]), 1); - $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); + $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 0.19); $this->assertEquals(new Money(1000, new Currency('USD')), $cart->price()); $this->assertEquals(new Money(500, new Currency('USD')), $cart->discount()); $this->assertEquals(new Money(500, new Currency('USD')), $cart->subtotal()); @@ -1114,17 +1112,6 @@ class CartTest extends TestCase $this->assertEquals(new Money(595, new Currency('USD')), $cart->total()); } - /** @test */ - public function can_access_cart_item_propertys() - { - $cart = $this->getCartDiscount(50); - $cart->add(new BuyableProduct([ - 'name' => 'First item', - ]), 1); - $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals(50, $cartItem->discount); - } - /** @test */ public function can_set_cart_item_discount() { @@ -1133,8 +1120,10 @@ class CartTest extends TestCase 'name' => 'First item', ]), 1); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $cart->setDiscount('027c91341fd5cf4d2579b49c4b6a90da', 50); - $this->assertEquals(50, $cartItem->discount); + + $cart->setDiscount('027c91341fd5cf4d2579b49c4b6a90da', 0.5); + + $this->assertEquals(0.5, $cartItem->discount); } /** @test */ @@ -1182,7 +1171,7 @@ class CartTest extends TestCase /** @test */ public function cart_can_create_items_from_models_using_the_canbebought_trait() { - $cart = $this->getCartDiscount(50); + $cart = $this->getCartDiscount(0.5); $cart->add(new BuyableProductTrait([ 'name' => 'First item', @@ -1190,7 +1179,7 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); + $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 0.19); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->price); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discount()); @@ -1220,7 +1209,7 @@ class CartTest extends TestCase '--database' => 'testing', ]); - $cart = $this->getCartDiscount(50); + $cart = $this->getCartDiscount(0.5); $cart->add(new BuyableProduct([ 'name' => 'Item', ]), 1); @@ -1255,7 +1244,7 @@ class CartTest extends TestCase '--database' => 'testing', ]); - $cart = $this->getCartDiscount(50); + $cart = $this->getCartDiscount(0.5); $cart->add(new BuyableProduct([ 'name' => 'Item', ]), 1); @@ -1282,48 +1271,6 @@ class CartTest extends TestCase }); } - /** - * Get an instance of the cart. - * - * @return \Gloudemans\Shoppingcart\Cart - */ - private function getCart() - { - $session = $this->app->make('session'); - $events = $this->app->make('events'); - - return new Cart($session, $events); - } - - /** - * Get an instance of the cart with discount. - * - * @param int $discount - * - * @return \Gloudemans\Shoppingcart\Cart - */ - private function getCartDiscount($discount = 50) - { - $cart = $this->getCart(); - $cart->setGlobalDiscount($discount); - - return $cart; - } - - /** - * Set the config number format. - * - * @param int $decimals - * @param string $decimalPoint - * @param string $thousandSeperator - */ - private function setConfigFormat($decimals, $decimalPoint, $thousandSeperator) - { - $this->app['config']->set('cart.format.decimals', $decimals); - $this->app['config']->set('cart.format.decimal_point', $decimalPoint); - $this->app['config']->set('cart.format.thousand_separator', $thousandSeperator); - } - /** @test */ public function it_can_store__mutiple_instances_of_the_cart_in_a_database() { @@ -1391,4 +1338,46 @@ class CartTest extends TestCase Event::assertDispatched('cart.erased'); $this->assertDatabaseMissing('shoppingcart', ['identifier' => $identifier, 'instance' => Cart::DEFAULT_INSTANCE]); } + + /** + * Get an instance of the cart. + * + * @return \Gloudemans\Shoppingcart\Cart + */ + private function getCart() + { + $session = $this->app->make('session'); + $events = $this->app->make('events'); + + return new Cart($session, $events); + } + + /** + * Get an instance of the cart with discount. + * + * @param int $discount + * + * @return \Gloudemans\Shoppingcart\Cart + */ + private function getCartDiscount(float $discount = 0.5) + { + $cart = $this->getCart(); + $cart->setGlobalDiscount($discount); + + return $cart; + } + + /** + * Set the config number format. + * + * @param int $decimals + * @param string $decimalPoint + * @param string $thousandSeperator + */ + private function setConfigFormat($decimals, $decimalPoint, $thousandSeperator) + { + $this->app['config']->set('cart.format.decimals', $decimals); + $this->app['config']->set('cart.format.decimal_point', $decimalPoint); + $this->app['config']->set('cart.format.thousand_separator', $thousandSeperator); + } }