public function __construct(array $args) { if ($this->validate($args)) { $this->id = $args['id']; if (isset($args['name'])) { $this->name = $args['name']; } $this->product = $args['product']; if (isset($args['price'])) { $this->price = Helpers::normalizePrice($args['price']); } if (isset($args['points'])) { $this->points = $args['points']; } $this->quantity = $args['quantity']; if (isset($args['attributes'])) { $this->attributes = new ItemAttributeCollection($args['attributes']); } $this->conditions = new ConditionCollection('item-conditions'); if (isset($args['conditions'])) { foreach ($args['conditions'] as $condition) { $this->conditions->addCondition($condition); } } } }
public function addTotal($params = []) { if (Helpers::isMultiArray($params)) { foreach ($params as $total) { $this->addTotal($total); } } $total = new Total($params); $id = $params['id']; if ($this->has($id)) { $this->updateTotal($id, $total); } else { Event::fire($this->instance . '.adding', [$total, $this]); $this->put($id, $total); Event::fire($this->instance . '.added', [$total, $this]); } }
public function addItem($params = []) { if (Helpers::isMultiArray($params)) { foreach ($params as $item) { $this->addItem($item); } } $item = new Item($params); $id = $params['id']; if ($this->has($id)) { $this->updateItem($id, $item); } else { Event::fire($this->instance . '.adding', [$item, $this]); $this->put($id, $item); Event::fire($this->instance . '.added', [$item, $this]); } }
/** * apply condition * * @param $totalOrSubTotalOrPrice * @param $conditionValue * @return float */ protected function apply($totalOrSubTotalOrPrice, $conditionValue) { // if value has a percentage sign on it, we will get first // its percentage then we will evaluate again if the value // has a minus or plus sign so we can decide what to do with the // percentage, whether to add or subtract it to the total/subtotal/price // if we can't find any plus/minus sign, we will assume it as plus sign if ($this->valueIsPercentage($conditionValue)) { if ($this->valueIsToBeSubtracted($conditionValue)) { $value = Helpers::normalizePrice($this->cleanValue($conditionValue)); $this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100); $result = floatval($totalOrSubTotalOrPrice - $this->parsedRawValue); } else { if ($this->valueIsToBeAdded($conditionValue)) { $value = Helpers::normalizePrice($this->cleanValue($conditionValue)); $this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100); $result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue); } else { $value = Helpers::normalizePrice($conditionValue); $this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100); $result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue); } } } else { if ($this->valueIsToBeSubtracted($conditionValue)) { $this->parsedRawValue = Helpers::normalizePrice($this->cleanValue($conditionValue)); $result = floatval($totalOrSubTotalOrPrice - $this->parsedRawValue); } else { if ($this->valueIsToBeAdded($conditionValue)) { $this->parsedRawValue = Helpers::normalizePrice($this->cleanValue($conditionValue)); $result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue); } else { $this->parsedRawValue = Helpers::normalizePrice($conditionValue); $result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue); } } } return $result; }