Skip to content

Commit

Permalink
#96 Adicionada nova variável de configuração 'hard_errors'
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelpatro committed Jul 20, 2015
2 parents 60d607d + c38f378 commit 3e16026
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 42 deletions.
Expand Up @@ -155,40 +155,22 @@ protected function _getQuotes()
$correiosReturn = $this->_getCorreiosReturn();

if ($correiosReturn !== false) {

$errorList = array();
$correiosReturn = $this->_addPostMethods($correiosReturn);
$existReturn = false;

foreach ($correiosReturn as $servicos) {

$errorId = (string) $servicos->Erro;
$errorList[$errorId] = $servicos->MsgErro;

if ($errorId != '0' && !in_array($errorId, $softErrors)) {
continue;
}

$stringPrice = (string) $servicos->Valor;
$stringPrice = str_replace('.', '', $stringPrice);
$stringPrice = str_replace(',', '.', $stringPrice);
$shippingPrice = floatval($stringPrice);
$shippingPrice *= pow(2, $this->_splitUp);
$shippingDelivery = (int) $servicos->PrazoEntrega;

if ($shippingPrice <= 0) {
continue;
}

$this->_appendShippingReturn((string) $servicos->Codigo, $shippingPrice, $shippingDelivery);
if ($this->getConfigFlag('show_soft_errors') && !isset($isWarnAppended)) {
$isWarnAppended = $this->_appendShippingWarning($servicos);
}
$existReturn = true;
}

if ($existReturn === false) {
$this->_throwError('urlerror', 'URL Error, all services return with error', __LINE__);
return $this->_result;
$servicos->Valor = $this->_getFormatPrice((string) $servicos->Valor);
$this->_appendShippingReturn($servicos);
}
$this->_appendShippingErrors($errorList);
} else {
return $this->_result;
}
Expand Down Expand Up @@ -342,18 +324,23 @@ protected function _getCorreiosReturn()
/**
* Apend shipping value to return
*
* @param string $shippingMethod Method of shipping
* @param int $shippingPrice Price
* @param int $correiosDelivery Delivery date
* @param SimpleXMLElement $servico Service Data
*
* @return void
*/
protected function _appendShippingReturn($shippingMethod, $shippingPrice = 0, $correiosDelivery = 0)
protected function _appendShippingReturn(SimpleXMLElement $servico)
{
$correiosDelivery = (int) $servico->PrazoEntrega;
$shippingMethod = (string) $servico->Codigo;
$shippingPrice = (float) $servico->Valor;
if ($shippingPrice <= 0) {
return;
}

$errorMsg = $this->_getSoftErrorMsg((string) $servico->Erro);
$method = Mage::getModel('shipping/rate_result_method');
$method->setCarrier($this->_code);
$method->setCarrierTitle($this->getConfigData('title'));
$method->setCarrierTitle($this->getConfigData('title') . $this->_getSplitUpMsg() . $errorMsg);
$method->setMethod($shippingMethod);

$shippingCost = $shippingPrice;
Expand Down Expand Up @@ -443,6 +430,7 @@ protected function _generateVolumeWeight()
$items = Mage::getSingleton('adminhtml/session_quote')->getQuote()->getAllVisibleItems();
}

$items = $this->_loadBundleChildren($items);
foreach ($items as $item) {
$_product = $item->getProduct();

Expand Down Expand Up @@ -486,7 +474,7 @@ protected function _generateVolumeWeight()

$itemAltura = $this->_getFitHeight($item);
$pesoCubicoTotal += (($itemAltura * $itemLargura * $itemComprimento) *
$item->getQty()) / $this->getConfigData('coeficiente_volume');
$item->getTotalQty()) / $this->getConfigData('coeficiente_volume');

$this->_postingDays = max($this->_postingDays, (int) $_product->getData('posting_days'));
}
Expand Down Expand Up @@ -819,6 +807,7 @@ protected function _filterMethodByItemRestriction()
$items = Mage::getSingleton('adminhtml/session_quote')->getQuote()->getAllVisibleItems();
}

$items = $this->_loadBundleChildren($items);
$intersection = $this->_postMethodsExplode;
foreach ($items as $item) {
$product = Mage::getModel('catalog/product')->load($item->getProductId());
Expand Down Expand Up @@ -934,23 +923,116 @@ protected function _validateZipRestriction($method)
}

/**
* Add a warning message at the top of the shipping method list.
* Some special errors must be sent to users.
* If not applicable, the default error will be sent.
*
* @param array $errorList Error List
*
* @return boolean
*/
protected function _appendShippingErrors($errorList)
{
$output = false;
$successCode = '0';
$hasValidQuote = array_key_exists($successCode, $errorList);
if (!$hasValidQuote) {
$displayErrorList = explode(',', $this->getConfigData('hard_errors'));
if ($this->getConfigFlag('show_soft_errors')) {
$softErrorList = explode(',', $this->getConfigData('soft_errors'));
$displayErrorList = array_merge($displayErrorList, $softErrorList);
}
foreach ($errorList as $errorCode => $errorMsg) {
$isDisplayError = in_array($errorCode, $displayErrorList);
if ($isDisplayError) {
$error = Mage::getModel('shipping/rate_result_error');
$error->setCarrier($this->_code);
$error->setErrorMessage($errorMsg);
$this->_result->append($error);
$output = true;
}
}
if (!$output) {
$logMsg = implode(',', $errorList);
Mage::log("{$this->_code}: Warning! There is no valid quotes, and no one error was throwed: {$logMsg}");
}
}
return $output;
}

/**
* Returns a short message showing the number of the packs that will be needed.
*
* @param SimpleXMLElement $servico Post Method
* @return string
*/
protected function _getSplitUpMsg()
{
$msg = "";
if ($this->_splitUp > 0) {
$qty = pow(2, $this->_splitUp);
$msg.= " / {$qty} volumes";
}
return $msg;
}

/**
* Returns a short warning message.
*
* @param string $error Error Id
*
* @return boolean
* @return string
*/
protected function _appendShippingWarning(SimpleXMLElement $servico)
protected function _getSoftErrorMsg($error)
{
$id = (string) $servico->Erro;
$ids = explode(',', $this->getConfigData('soft_errors'));
if (in_array($id, $ids)) {
$error = Mage::getModel('shipping/rate_result_error');
$error->setCarrier($this->_code);
$error->setErrorMessage($servico->MsgErro);
$this->_result->append($error);
return true;
$msg = "";
if ($this->getConfigFlag('show_soft_errors')) {
$softErrorList = explode(',', $this->getConfigData('soft_errors'));
$isSoftError = in_array($error, $softErrorList);
if ($isSoftError) {
$msg.= " / Área de Risco";
}
}
return false;
return $msg;
}

/**
* Returns the price as float, and fixed by pack division.
*
* @param string $price Price String
*
* @return float
*/
protected function _getFormatPrice($price)
{
$stringPrice = str_replace('.', '', $price);
$stringPrice = str_replace(',', '.', $stringPrice);
$shippingPrice = floatval($stringPrice);
$shippingPrice *= pow(2, $this->_splitUp);
return $shippingPrice;
}

/**
* Filter visible and bundle children products.
*
* @param array $items Product Items
*
* @return array
*/
protected function _loadBundleChildren($items)
{
$visibleAndBundleChildren = array();
/* @var $item Mage_Sales_Model_Quote_Item */
foreach ($items as $item) {
$product = $item->getProduct();
$isBundle = ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE);
if ($isBundle) {
/* @var $child Mage_Sales_Model_Quote_Item */
foreach ($item->getChildren() as $child) {
$visibleAndBundleChildren[] = $child;
}
} else {
$visibleAndBundleChildren[] = $item;
}
}
return $visibleAndBundleChildren;
}
}
1 change: 1 addition & 0 deletions app/code/community/PedroTeixeira/Correios/etc/config.xml
Expand Up @@ -84,6 +84,7 @@
<model>PedroTeixeira_Correios_Model_Carrier_CorreiosMethod</model>
<title>Correios</title>
<postmethods>40010</postmethods>
<hard_errors>-3,-6,-10,-33,-888,7,99</hard_errors>
<soft_errors>009,010,011</soft_errors>
<show_soft_errors>0</show_soft_errors>
<prazo_entrega>0</prazo_entrega>
Expand Down

0 comments on commit 3e16026

Please sign in to comment.