_db = Qs_Db::getInstance(); $this->_errorMap[self::ERROR_INTERNAL] = 'Internal Error'; $this->_errorMap[self::ERROR_BAD_QUANTITY] = 'Please set correct quantity'; 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([ 'errorCode' => self::ERROR_INTERNAL, 'errorText' => $this->_errorMap[self::ERROR_INTERNAL], ]); } } else { $this->_response->setIsSuccess(false)->setData([ 'errorCode' => self::ERROR_VALIDATE_REQUIRED_PARAMS, 'errorText' => implode('
', $errors), ]); } } return $this->_response; } protected function _getDbShipping() { try { $select = $this->_db->select()->reset(); $select->from(Qs_Db::getPair($this->getConfig()->shippingTableAlias)); $select->where('startQuantity <= ?', $this->getOption('quantity'), Qs_Db::INT_TYPE); if ($this->getOption('quantity') > $this->_getMaxDbQuantity()) { $select->where('endQuantity >= ?', $this->_getMaxDbQuantity(), Qs_Db::INT_TYPE); } else { $select->where('endQuantity >= ?', $this->getOption('quantity'), 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 _getMaxDbQuantity() { if (empty($this->_maxDbQuantity)) { $select = $this->_db->select()->reset(); $select->from(Qs_Db::getPair($this->getConfig()->shippingTableAlias), new Zend_Db_Expr('MAX(endQuantity)')); $this->_maxDbQuantity = $this->_db->fetchOne($select); } return $this->_maxDbQuantity; } protected function _validateRateFields() { $errors = []; $quantityMinRange = $this->getConfig()->quantityMinRange; if (!($quantity = $this->getOption('quantity')) || !filter_var($quantity, FILTER_VALIDATE_INT, ['options' => ['min_range' => $quantityMinRange]]) ) { $errors[] = $this->_errorMap[self::ERROR_BAD_QUANTITY]; } return empty($errors) ? true : $errors; } }