diff --git a/build/logs/clover.xml b/build/logs/clover.xml index be49475..bd330ee 100644 --- a/build/logs/clover.xml +++ b/build/logs/clover.xml @@ -1,6 +1,6 @@ - - + + @@ -30,85 +30,83 @@ - + - - - - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + - - - + + + - - + + - - - - - - - - + + + + + + + + - - - + + + - - + - + + - - - - - + + + + + - - - - + + + + - + + @@ -138,7 +136,7 @@ - + @@ -187,129 +185,130 @@ - - + + - - - - - + + + + + - - - - - + + + + + - - - + + + - + - + - - - + + + - - - - - - - - - + + + + + + + + + - - + + - - - + + + - - - - - - - + + + + + + + - - - + + + - - - - - - - + + + + + + + - - - + + + - - - + + + - - - - - + + + + + - - - - - + + + + + - - - - + + + + - - - - - - - + + + + + + - - - - - + + + + + + - - - + + + + @@ -469,6 +468,6 @@ - + diff --git a/src/CartItem.php b/src/CartItem.php index 994ff6f..f50c294 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -24,25 +24,30 @@ class CartItem public $code; public $price; public $locale; + public $taxable; public $discount; public $lineItem; public $subItems = []; public $internationalFormat; /** - * @param string $id - * @param string $name - * @param int $qty - * @param float $price + * CartItem constructor. + * + * @param $id + * @param $name + * @param $qty + * @param $price * @param array $options - * @param bool $lineItem + * @param bool|true $taxable + * @param bool|false $lineItem */ - public function __construct($id, $name, $qty, $price, $options = [], $lineItem = false) + public function __construct($id, $name, $qty, $price, $options = [], $taxable = true, $lineItem = false) { $this->id = $id; $this->qty = $qty; $this->name = $name; $this->options = $options; + $this->taxable = $taxable; $this->lineItem = $lineItem; $this->price = floatval($price); $this->tax = config('laracart.tax'); @@ -124,13 +129,9 @@ public function addSubItem(array $subItem) */ public function getPrice($tax = false, $format = true) { - $price = $this->price; - - foreach ($this->subItems as $subItem) { - $price += $subItem->getPrice(false); - } + $price = $this->price * $this->qty; - if ($tax) { + if ($tax && $this->taxable) { $price += $price * $this->tax; } @@ -199,13 +200,14 @@ public function subTotal($tax = false, $format = true, $withDiscount = true) public function subItemsTotal($tax = false, $format = true) { $total = 0; + foreach ($this->subItems as $item) { if (isset($item->price)) { - $total += $item->price; + $total += $item->getPrice($tax); } } - if ($tax) { + if ($tax && $this->taxable) { $total = $total + ($total * $this->tax); } diff --git a/src/LaraCart.php b/src/LaraCart.php index 43f4056..0eee7b8 100644 --- a/src/LaraCart.php +++ b/src/LaraCart.php @@ -183,11 +183,12 @@ public function getItems() * @param int $qty * @param string $price * @param array $options + * @param bool|false $taxable * @param bool|false $lineItem * * @return CartItem */ - public function add($itemID, $name = null, $qty = 1, $price = '0.00', $options = [], $lineItem = false) + public function add($itemID, $name = null, $qty = 1, $price = '0.00', $options = [], $taxable = true, $lineItem = false) { return $this->addItem( new CartItem( @@ -196,6 +197,7 @@ public function add($itemID, $name = null, $qty = 1, $price = '0.00', $options = $qty, $price, $options, + $taxable, $lineItem ) ); @@ -212,9 +214,9 @@ public function add($itemID, $name = null, $qty = 1, $price = '0.00', $options = * * @return CartItem */ - public function addLine($itemID, $name = null, $qty = 1, $price = '0.00', $options = []) + public function addLine($itemID, $name = null, $qty = 1, $price = '0.00', $options = [], $taxable = true) { - return $this->add($itemID, $name, $qty, $price, $options, true); + return $this->add($itemID, $name, $qty, $price, $options, $taxable, true); } /** diff --git a/tests/LaraCartTest.php b/tests/LaraCartTest.php index 1b0da99..cd08c64 100644 --- a/tests/LaraCartTest.php +++ b/tests/LaraCartTest.php @@ -218,8 +218,6 @@ public function testSubItem() $subItem = $item->addSubItem($subItemData); - $item->findSubItem($subItem->getHash()); - $this->assertEquals(new \LukePOLO\LaraCart\CartSubItem($subItemData), $subItem); $this->assertEquals('2', $subItem->price); $this->assertEquals('$2.00', $subItem->getPrice()); @@ -232,15 +230,16 @@ public function testSubItem() $this->assertFalse(isset($subItem->testz)); - $this->assertEquals("$3.00", $item->getPrice()); + $this->assertEquals("$1.00", $item->getPrice()); + $this->assertEquals('$3.00', $item->subTotal()); $subItem->items[] = new \LukePOLO\LaraCart\CartItem( - '123', 'subItem', 1, 3, [ + '123', 'subItem', 10, 20, [ 'testing' => 1, ] ); - $this->assertEquals('$8.00', $item->subTotal()); + $this->assertEquals('$203.00', $item->subTotal()); } public function testRemoveItem() @@ -488,7 +487,25 @@ public function testTotal() ] ); + $this->assertEquals('$1.07', $this->laracart->total()); $this->assertEquals('1.07', $this->laracart->total(false)); + + + // TODO - Test taxable fees + $item = $this->laracart->add( + '1', + 'Testing Item', + 1, + '1.00', + [ + 'b_test' => 'option_1', + 'a_test' => 'option_2', + ], + false + ); + + $this->assertEquals('$2.07', $this->laracart->total()); + $this->assertEquals('2.07', $this->laracart->total(false)); } }