示例#1
0
	/**
	* Moves the specified quantity of this item from it's current address to another address.
	*
	* @param ISC_QUOTE_ADDRESS $address
	* @param mixed $quantity
	* @return ISC_QUOTE_ITEM returns the instance of the item at it's new location (meaning, if the item is cloned the NEW instance is returned)
	*/
	public function moveToAddress(ISC_QUOTE_ADDRESS $address, $quantity = null)
	{
		// note: no stock levels should be checked here since we're just re-allocating quantity within the same order

		/** @var ISC_LOG */
		$log = $GLOBALS['ISC_CLASS_LOG'];

		// does the item already exists at the destination?
		$item = $address->getItemByHash($this->getHash());

		$log->LogSystemDebug('general', 'request to move ' . $quantity . ' x item ' . $this->getName() . ' (' . $this->getId() . ') to address ' . $address->getId());

		if ($address->getId() === $this->getAddressId(false)) {
			// item is already at destination: ignore
			return $this;
		}

		if ($quantity !== null) {
			$quantity = (int)$quantity;
		}

		if ($quantity === null || $quantity >= $this->getQuantity()) {
			// moving all of this item
			if ($item) {
				// already exists at destination so increment qty and remove current item
				$log->LogSystemDebug('general', 'adding all of item to existing item at destination');
				$item->setQuantity($this->getQuantity() + $item->getQuantity(), false);
				$this->quote->removeItem($this->getId());
				return $item;
			}

			// does not exist at destination so just move the current item there
			$log->LogSystemDebug('general', 'moving item to destination');
			$this->setAddressId($address);
			return $this;
		}

		if ($quantity < 1) {
			// nothing to do
			return $this;
		}

		// moving partial qty: remove qty from current item now
		$this->setQuantity($this->getQuantity() - $quantity, false);

		// is the quantity of the the product now less than what was originally ordered? move the excess to the new item
		$originalOrderQuantityToMove = 0;
		if ($this->getQuantity() < $this->getOriginalOrderQuantity()) {
			$originalOrderQuantityToMove = $this->getOriginalOrderQuantity() - $this->getQuantity();
			$this->setOriginalOrderQuantity($this->getQuantity());
		}

		if ($item) {
			// already exists at destination so just increment that qty
			$log->LogSystemDebug('general', 'moving partial qty to existing item at destination');
			$item->setQuantity($item->getQuantity() + $quantity, false);
			$item->setOriginalOrderQuantity($originalOrderQuantityToMove);

			return $item;
		}

		$log->LogSystemDebug('general', 'moving partial qty to new item at destination');
		// does not exist at destination so create a new instance and put it there with the right qty
		/** @var ISC_QUOTE_ITEM */
		$item = clone $this;
		$this->getQuote()->addItem($item, false);

		$item
			->setAddressId($address)
			->setQuantity($quantity, false)
			->setOriginalOrderQuantity($originalOrderQuantityToMove);

		return $item;
	}