_db = Qs_Db::getInstance(); $this->_errorMap[self::ERROR_INTERNAL] = 'Internal Error'; $this->_errorMap[self::ERROR_BAD_PRICE] = 'Please set correct price'; parent::__construct($options); } public function getRate() { return $this->_getDbResponse(); } protected function _getDbResponse() { if (null == $this->_response->getData()) { if (true === ($errors = $this->_validateRateFields())) { if (!($dbResponse = $this->_getDbShipping()) instanceof Qs_Exception) { $this->_response->prepareServiceResponse($dbResponse); } else { $this->_response->setIsSuccess(false)->setData(array( 'errorCode' => self::ERROR_INTERNAL, 'errorText' => $this->_errorMap[self::ERROR_INTERNAL] )); } } else { $this->_response->setIsSuccess(false)->setData(array( 'errorCode' => self::ERROR_VALIDATE_REQUIRED_PARAMS, 'errorText' => implode('
', $errors) )); } } return $this->_response; } protected function _getDbShipping() { $response = array( 'price' => 0 ); if (count($this->_getShippingList()) && $this->_getMaxDbPrice()) { try { $select = $this->_db->select()->reset(); $select->from(Qs_Db::getPair($this->getConfig()->shippingTableAlias)); $select->where('startPrice <= ?', $this->getPrice(), Qs_Db::INT_TYPE); if ($this->getPrice() > $this->_getMaxDbPrice()) { $select->where('endPrice >= ?', $this->_getMaxDbPrice(), Qs_Db::INT_TYPE); } else { $select->where('endPrice >= ?', $this->getPrice(), Qs_Db::INT_TYPE); } $response = $this->_db->fetchRow($select); } catch (Qs_Db_Exception $e) { return new App_Service_Shipping_Manual_Exception($e->getMessage(), $e->getCode()); } } return $response; } protected function _getMaxDbPrice() { if (empty($this->_maxDbPrice)) { $select = $this->_db->select()->reset(); $select->from(Qs_Db::getPair($this->getConfig()->shippingTableAlias), new Zend_Db_Expr('MAX(endPrice)')); $this->_maxDbPrice = $this->_db->fetchOne($select); } return $this->_maxDbPrice; } protected function getPrice() { if (null === $this->_price) { $cart = new App_ECommerce_Cart_Obj(); $cartId = reset($this->getOption('items'))['cartId']; $cart->setPrimaryKey($cartId); $cart->setFilter(array('doNotApplyShipping' => 'n')); $this->_price = (float) $cart->getTotal(); } return $this->_price; } protected function _getShippingList() { if ($this->getOption('items')) { $cart = new App_ECommerce_Cart_Obj(); $cartId = reset($this->getOption('items'))['cartId']; $cart->setPrimaryKey($cartId); $cart->setFilter(array('doNotApplyShipping' => 'n')); return $cart->getList(); } return null; } protected function _validateRateFields() { $errors = array(); if (count($this->_getShippingList())) { $priceMinRange = $this->getConfig()->priceMinRange; if (!($price = $this->getPrice()) || !filter_var($price, FILTER_VALIDATE_FLOAT, array('options' => array('min_range' => $priceMinRange))) ) { $errors[] = $this->_errorMap[self::ERROR_BAD_PRICE]; } } return empty($errors) ? true : $errors; } }