Skip to content

Commit

Permalink
Merge pull request #58 from lukepolo/taxation_item_hash
Browse files Browse the repository at this point in the history
Taxation item hash
  • Loading branch information
lukepolo committed Jan 22, 2016
2 parents 8107c41 + f60512b commit b90a310
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 86 deletions.
10 changes: 1 addition & 9 deletions src/CartItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,10 @@ class CartItem

protected $itemHash;

public $id;
public $tax;

public $name;
public $code;
public $locale;
public $taxable;
public $lineItem;
public $discount = 0;
public $taxable;
public $subItems = [];
public $couponInfo = [];
public $internationalFormat;

/**
* CartItem constructor.
Expand Down
175 changes: 103 additions & 72 deletions src/Contracts/LaraCartContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace LukePOLO\LaraCart\Contracts;

use LukePOLO\LaraCart\CartItem;
use LukePOLO\LaraCart\LaraCart;
use LukePOLO\LaraCart\Exceptions\InvalidPrice;
use LukePOLO\LaraCart\Exceptions\InvalidQuantity;

/**
* Interface LaraCartContract
Expand All @@ -16,10 +19,11 @@ interface LaraCartContract
*
* @param string $instance
*
* @return mixed
* @return LaraCart
*/
public function setInstance($instance = 'default');


/**
* Gets the instance in the session
*
Expand All @@ -29,23 +33,6 @@ public function setInstance($instance = 'default');
*/
public function get($instance = 'default');

/**
* Updates cart session
*/
public function update();

/**
*
* Formats the number into a money format based on the locale and international formats
*
* @param $number
* @param $locale
* @param $internationalFormat
* @param $format
*
* @return string
*/
public function formatMoney($number, $locale = null, $internationalFormat = null, $format = true);

/**
* Gets an an attribute from the cart
Expand Down Expand Up @@ -73,79 +60,100 @@ public function getAttributes();
public function setAttribute($attribute, $value);

/**
* Removes an attribute from the cart
*
* @param $attribute
*/
public function removeAttribute($attribute);

/**
* Finds a cartItem based on the itemHash
*
* @param $itemHash
*
* @return CartItem | null
* Updates cart session
*/
public function getItem($itemHash);
public function update();

/**
* Gets all the items within the cart
* Removes an attribute from the cart
*
* @return array
* @param $attribute
*/
public function getItems();
public function removeAttribute($attribute);

/**
* Creates a CartItem and then adds it to cart
*
* @param $itemID
* @param string|int $itemID
* @param null $name
* @param int $qty
* @param string $price
* @param array $options
* @param bool|false $lineItem
* @param bool|true $taxable
*
* @return CartItem
*/
public function add($itemID, $name = null, $qty = 1, $price = '0.00', $options = [], $lineItem = false);
public function addLine($itemID, $name = null, $qty = 1, $price = '0.00', $options = [], $taxable = true);

/**
* Creates a CartItem and then adds it to cart
*
* @param string|int $itemID
* @param $itemID
* @param null $name
* @param int $qty
* @param string $price
* @param array $options
* @param bool|false $taxable
* @param bool|false $lineItem
*
* @return string CartItem
* @return CartItem
*/
public function addLine($itemID, $name = null, $qty = 1, $price = '0.00', $options = []);
public function add(
$itemID,
$name = null,
$qty = 1,
$price = '0.00',
$options = [],
$taxable = true,
$lineItem = false
);

/**
* Adds the cartItem into the cart session
*
* @param $cartItem
* @param CartItem $cartItem
*
* @return CartItem
*/
public function addItem(CartItem $cartItem);

/**
* Removes a CartItem based on the itemHash
* Finds a cartItem based on the itemHash
*
* @param $itemHash
*
* @return CartItem | null
*/
public function removeItem($itemHash);
public function getItem($itemHash);

/**
* Get the count based on qty, or number of unique items
* Gets all the items within the cart
*
* @param bool $withItemQty
* @return array
*/
public function getItems();


/**
* Updates an items attributes
*
* @return int
* @param $itemHash
* @param $key
* @param $value
*
* @return CartItem
*
* @throws InvalidPrice
* @throws InvalidQuantity
*/
public function count($withItemQty = true);
public function updateItem($itemHash, $key, $value);

/**
* Removes a CartItem based on the itemHash
*
* @param $itemHash
*/
public function removeItem($itemHash);

/**
* Empties the carts items
Expand Down Expand Up @@ -195,13 +203,6 @@ public function removeCoupon($code);
*/
public function getFee($name);

/**
* Getes all the fees on the cart object
*
* @return mixed
*/
public function getFees();

/**
* Allows to charge for additional fees that may or may not be taxable
* ex - service fee , delivery fee, tips
Expand All @@ -221,49 +222,79 @@ public function addFee($name, $amount, $taxable = false, Array $options = []);
public function removeFee($name);

/**
* Gets all the fee totals
* Gets the total tax for the cart
*
* @param bool|true $format
*
* @return string
*/
public function feeTotals($format = true);
public function taxTotal($format = true);

/**
* Gets the total amount discounted
* Gets the total of the cart with or without tax
*
* @param bool|true $format
* @param boolean $format
* @param boolean $withDiscount
*
* @return int|string
* @return string
*/
public function totalDiscount($format = true);
public function total($format = true, $withDiscount = true);

/**
* Gets the total tax for the cart
* Gets the subtotal of the cart with or without tax
*
* @param bool|true $format
* @param bool|false $tax
* @param boolean $format
* @param boolean $withDiscount
*
* @return string
*/
public function taxTotal($format = true);
public function subTotal($tax = false, $format = true, $withDiscount = true);

/**
* Gets the subtotal of the cart with or without tax
* Get the count based on qty, or number of unique items
*
* @param bool|false $tax
* @param bool|true $format
* @param bool|true $withDiscount
* @param bool $withItemQty
*
* @return int
*/
public function count($withItemQty = true);

/**
*
* Formats the number into a money format based on the locale and international formats
*
* @param $number
* @param $locale
* @param $internationalFormat
* @param $format
*
* @return string
*/
public function subTotal($tax = false, $format = true, $withDiscount = true);
public function formatMoney($number, $locale = null, $internationalFormat = null, $format = true);

/**
* Gets the total of the cart with or without tax
* Gets all the fee totals
*
* @param boolean $format
*
* @param bool|true $format
* @param bool|true $withDiscount
* @return string
*/
public function total($format = true, $withDiscount = true);
public function feeTotals($format = true);

/**
* Getes all the fees on the cart object
*
* @return mixed
*/
public function getFees();

/**
* Gets the total amount discounted
*
* @param bool|true $format
*
* @return int|string
*/
public function totalDiscount($format = true);
}
8 changes: 4 additions & 4 deletions src/Traits/CartOptionsMagicMethodsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ public function __get($option)
*/
public function __set($option, $value)
{
if(is_callable(array($this, 'generateHash'))) {
$this->generateHash();
}

switch ($option) {
case LaraCart::QTY:
if (!is_numeric($value) || $value < 0) {
Expand All @@ -59,6 +55,10 @@ public function __set($option, $value)
break;
}
array_set($this->options, $option, $value);

if(is_callable(array($this, 'generateHash'))) {
$this->generateHash();
}
}

/**
Expand Down
19 changes: 19 additions & 0 deletions tests/ItemsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,23 @@ public function testSetQty()
$this->assertEquals('The quantity must be a valid number', $e->getMessage());
}
}

/**
* Tests the different taxes on items
*/
public function testDifferentTaxes() {

$item = $this->addItem();

$prevHash = $item->getHash();

$item->tax = .05;

$this->assertNotEquals($prevHash, $item->getHash());

$item = $this->addItem();
$item->tax = .3;

$this->assertEquals('2.35', $this->laracart->total(false));
}
}
21 changes: 21 additions & 0 deletions tests/LaraCartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public function testFormatMoney()
$this->assertEquals('$25.54', $this->laracart->formatMoney('25.544'));
}

/**
* Test getting the attributes from the cart
*/
public function testGetAttributes()
{
$this->laracart->setAttribute('test1', 1);
Expand All @@ -47,6 +50,9 @@ public function testGetAttributes()
$this->assertEquals(2, $attributes['test2']);
}

/**
* Test removing attributes from the cart
*/
public function testRemoveAttribute()
{
$this->laracart->setAttribute('test1', 1);
Expand Down Expand Up @@ -124,4 +130,19 @@ public function testDestroyOtherCart()

$this->assertEquals(1, $cart->count());
}

/**
* Tests if generating a new hash when we change an option
*/
public function testGeneratingHashes() {

$item = $this->addItem();

$prevHash = $item->getHash();

$item->name = 'NEW NAME';

$this->assertNotEquals($prevHash, $item->getHash());
}

}
Loading

0 comments on commit b90a310

Please sign in to comment.