コード例 #1
0
ファイル: service.orderbase.php プロジェクト: hungnv0789/vhtm
	/**
	 * Handle the order response
	 *
	 * Method will handle all the order responses. Mainly it is used for recording the "orderitem" references
	 *
	 * @access public
	 * @return bool The output of the parent execResponse method on success, throw an error on failure
	 */
	public function execResponse()
	{
		$output = parent::execResponse();

		if (isc_strtolower(trim($this->spool["service"])) == "query") {
			return $output;
		}

		/**
		 * If we died at the parent then there is no point in continuing
		 */
		if (!$output) {
			return $output;
		}

		if ($this->accounting->getValue("orderoption") == "order") {
			$salesLineRetTag = "SalesOrderLineRet";
		} else {
			$salesLineRetTag = "SalesReceiptLineRet";
		}

		/**
		 * Record the TxnLineID's for all the products
		 */
		if (!isset($this->spool["response"][$salesLineRetTag]) || !is_array($this->spool["response"][$salesLineRetTag])) {
			throw new QBException("Unable to find the " . $salesLineRetTag . " records for order ID: " . $this->spool["nodeId"], array("Tag" => $salesLineRetTag, " response" => $this->spool["response"]));
		}

		/**
		 * Remove all the TxnListID records for this order first as QB will change the TxnListID records EVERYTIME
		 * you add/edit an order (fun times)
		 */
		$searchData = array(
			"OrderID" => $this->spool["nodeId"]
		);

		$orderItemRef = $this->accounting->getReference("orderitem", $searchData, '', '', false);

		while (is_array($orderItemRef)) {
			$this->accounting->unsetReference("orderitem", $orderItemRef["accountingrefid"]);
			$orderItemRef = $this->accounting->getReference("orderitem", $searchData, '', '', false);
		}

		foreach ($this->spool["response"][$salesLineRetTag] as $productData) {

			if (!array_key_exists("TxnLineID", $productData)) {
				throw new QBException("Unable to find product TxnLineID for order ID: " . $this->spool["nodeId"], array("order" => $this->spool["nodeData"], "product" => $productData));
			}

			if (!array_key_exists("ItemRef", $productData) || !is_array($productData["ItemRef"]) || !array_key_exists("ListID", $productData["ItemRef"])) {
				throw new QBException("Unable to find product ListID for order ID: " . $this->spool["nodeId"], array("order" => $this->spool["nodeData"], "product" => $productData));
			}

			/**
			 * OK, recird the TxnListID. Unfortunately we do not know if this product is a normal one, a variation, a shipping cost
			 * or a tax component, so check for all
			 */
			$checkTypes = array("product", "productvariation", "prerequisite");
			$productType = "";

			foreach ($checkTypes as $checkType) {
				if ($this->accounting->getReference($checkType, '', $productData["ItemRef"]["ListID"], '', false)) {
					$productType = $checkType;
					break;
				}
			}

			/**
			 * If no reference then something is wrong
			 */
			if (trim($productType) == '') {
				throw new QBException("Unable to find product reference for order ID: " . $this->spool["nodeId"], array("order" => $this->spool["nodeData"], "product" => $productData));
			}

			/**
			 * OK, we've got the "product" reference data, now create the orderitem reference
			 */
			$referenceData = array(
				"TxnLineID" => $productData["TxnLineID"],
				"ListID" => $productData["ItemRef"]["ListID"],
				"OrderID" => $this->spool["nodeId"],
				"Type" => $productType
			);
			/**
			 * This one is setting up the orderitem (product) in the database in the accountingref table
			 */
			$refId = $this->accounting->setReference("orderitem", $referenceData, '', $productData["TxnLineID"], $prodRef["accountingrefnodeid"]);

			if (!isId($refId)) {
				throw new QBException("Unable to create product reference for order ID: " . $this->spool["nodeId"], array("order" => $this->spool["nodeData"], "product" => $productData));
			}
		}

		return $output;
	}