Skip to content

Commit

Permalink
Merge pull request #8 from jarjar123/pr47_refactory_a
Browse files Browse the repository at this point in the history
Código refatorado
  • Loading branch information
rafaelpatro committed Mar 30, 2015
2 parents 10779de + e2915a5 commit a383848
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,8 @@ public function collectRates(Mage_Shipping_Model_Rate_Request $request)
}

// Fix weight
$weightCompare = $this->getConfigData('maxweight');
if ($this->getConfigData('weight_type') == PedroTeixeira_Correios_Model_Source_WeightType::WEIGHT_GR) {
$this->_packageWeight = number_format($this->_packageWeight / 1000, 2, '.', '');
$weightCompare = number_format($weightCompare / 1000, 2, '.', '');
}

// Check weght
if ($this->_packageWeight > $weightCompare) {
$this->_throwError('maxweighterror', 'Weight exceeded limit', __LINE__);
return $this->_result;
}

// Check weight zero
Expand All @@ -115,7 +107,7 @@ public function collectRates(Mage_Shipping_Model_Rate_Request $request)
return $this->_result;
}

$this->_filterByItem();
$this->_filterMethodByItemRestriction();
if ($this->_getQuotes()->getError()) {
return $this->_result;
}
Expand All @@ -135,10 +127,10 @@ protected function _getQuotes()
{
$softErrors = explode(',', $this->getConfigData('soft_errors'));
$correiosReturn = $this->_getCorreiosReturn();
$correiosReturn = $this->_addPostMethods($correiosReturn);

if ($correiosReturn !== false) {

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

foreach ($correiosReturn as $servicos) {
Expand Down Expand Up @@ -683,10 +675,8 @@ protected function _loadMidSize()
$volumeFactor = $this->getConfigData('coeficiente_volume');
$volumeTotal = $this->_volumeWeight * $volumeFactor;
$pow = round(pow((int) $volumeTotal, (1/3)));
$x1 = $this->getConfigData('altura_padrao');
$x2 = $this->getConfigData('largura_padrao');
$x3 = $this->getConfigData('comprimento_padrao');
$this->_midSize = max($pow, $x1, $x2, $x3);
$min = $this->getConfigData('midsize_min');
$this->_midSize = max($pow, $min);
return $this;
}

Expand All @@ -709,7 +699,9 @@ protected function _removeInvalidServices()
}
}

if (count($tmpMethods) == 0) {
$isDivisible = (count($tmpMethods) == 0);
$isLoopBreakable = (count($this->_postMethodsExplode) > 0);
if ($isDivisible && $isLoopBreakable) {
return $this->_splitPack();
}

Expand Down Expand Up @@ -795,7 +787,7 @@ protected function _addPostMethods($cServico)
*
* @return PedroTeixeira_Correios_Model_Carrier_CorreiosMethod
*/
protected function _filterByItem()
protected function _filterMethodByItemRestriction()
{
if ( $this->getConfigFlag('filter_by_item') ) {
$items = Mage::getSingleton('checkout/cart')->getQuote()->getAllVisibleItems();
Expand All @@ -804,18 +796,16 @@ protected function _filterByItem()
$items = Mage::getSingleton('adminhtml/session_quote')->getQuote()->getAllVisibleItems();
}

/* @var $item Mage_Eav_Model_Entity_Abstract */
$intersection = $this->_postMethodsExplode;
foreach ($items as $item) {
/* @var $_product Mage_Catalog_Model_Product */
$product = Mage::getModel('catalog/product')->load($item->getProductId());
$postMethodsList = explode(',', $this->_postMethods);
$prodPostMethods = explode(',', $product->getData('postmethods'));
$intersection = array_intersect($prodPostMethods, $postMethodsList);
$this->_postMethods = implode(',', $intersection);
$intersection = array_intersect($prodPostMethods, $intersection);
}

$this->_postMethodsExplode = $intersection;
$this->_postMethods = implode(',', $intersection);
$this->_postMethodsFixed = $this->_postMethods;
$this->_postMethodsExplode = trim($this->_postMethods) ? explode(",", $this->_postMethods) : array();
}

return $this;
Expand Down
17 changes: 1 addition & 16 deletions app/code/community/PedroTeixeira/Correios/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@
<check_dimensions>1</check_dimensions>
<min_order_value>0</min_order_value>
<max_order_value>10000</max_order_value>
<maxweight>30</maxweight>
<handling_fee>0</handling_fee>
<ws_timeout>20</ws_timeout>

Expand All @@ -101,21 +100,8 @@
<altura_padrao>2</altura_padrao>
<comprimento_padrao>16</comprimento_padrao>
<largura_padrao>11</largura_padrao>
<altura_sent>2</altura_sent>
<comprimento_sent>16</comprimento_sent>
<largura_sent>11</largura_sent>
<midsize_min>16</midsize_min>

<!-- http://www2.correios.com.br/sistemas/precosprazos/Formato.cfm -->
<volume_validation>
<altura_max>105</altura_max>
<altura_min>2</altura_min>
<comprimento_max>105</comprimento_max>
<comprimento_min>16</comprimento_min>
<largura_max>105</largura_max>
<largura_min>11</largura_min>
<sum_max>200</sum_max>
<sum_min>29</sum_min>
</volume_validation>
<validate>
<serv_40010>
<max>
Expand Down Expand Up @@ -227,7 +213,6 @@
<serv_41300>PAC GF,4</serv_41300>
<serv_10065>Carta Comercial,3</serv_10065>
<serv_10138>Carta Comercial Registrada,3</serv_10138>
<volume_weight_min>5</volume_weight_min>
<coeficiente_volume>6000</coeficiente_volume>
<acobrar_code>40045</acobrar_code>
<contrato_codes>40096,81019,41068,41300</contrato_codes>
Expand Down

0 comments on commit a383848

Please sign in to comment.