diff --git a/app/code/community/PedroTeixeira/Correios/Model/Cache.php b/app/code/community/PedroTeixeira/Correios/Model/Cache.php index 0d0a7f1..92354b3 100644 --- a/app/code/community/PedroTeixeira/Correios/Model/Cache.php +++ b/app/code/community/PedroTeixeira/Correios/Model/Cache.php @@ -202,25 +202,44 @@ public function loadByTags() /** * Validate the response data from Correios. + * This method will choose between Request Cache or Save in Cache + * + * Step 1: + * Invalid responses must call the Cache load. + * Cache loading is requested by throwing adapter exception. + * + * Step 2: + * To save valid responses, it must contain no errors. + * Errors are detected by pattern_nocache and returns false. * * @param string $data XML Content + * + * @throws Zend_Http_Client_Adapter_Exception * * @return boolean */ protected function _isValidCache($data) { - $response = Zend_Http_Response::fromString($data); - $content = $response->getBody(); - $pattern = $this->getConfigData('pattern_nocache'); - if ($pattern != '' && preg_match($pattern, $content, $matches)) { - return false; + // Step 1 + try { + $response = Zend_Http_Response::fromString($data); + $content = $response->getBody(); + } catch (Zend_Http_Exception $e) { + throw new Zend_Http_Client_Adapter_Exception($e->getMessage()); } + if (empty($content)) { - return false; + throw new Zend_Http_Client_Adapter_Exception(); } libxml_use_internal_errors(true); $xml = simplexml_load_string($content); if (!$xml || !isset($xml->cServico)) { + throw new Zend_Http_Client_Adapter_Exception(); + } + + // Step 2 + $pattern = $this->getConfigData('pattern_nocache'); + if ($pattern != '' && preg_match($pattern, $content, $matches)) { return false; } return true; @@ -231,19 +250,16 @@ protected function _isValidCache($data) * * @param string $data XML Content * - * @throws Exception - * - * @return PedroTeixeira_Correios_Model_Cache + * @return boolean|PedroTeixeira_Correios_Model_Cache */ public function save($data) { - if (!$this->_isValidCache($data)) { - return false; // Invalid for the Cache only - } - $id = $this->_getId(); - $tags = $this->getCacheTags(); - if ($this->getCache()->save($data, $id, $tags)) { - Mage::log("{$this->_code} [cache]: mode={$this->getConfigData('cache_mode')} status=write key={$id}"); + if ($this->_isValidCache($data)) { + $id = $this->_getId(); + $tags = $this->getCacheTags(); + if ($this->getCache()->save($data, $id, $tags)) { + Mage::log("{$this->_code} [cache]: mode={$this->getConfigData('cache_mode')} status=write key={$id}"); + } } return $this; } @@ -263,4 +279,4 @@ public function getConfigData($field) $path = 'carriers/' . $this->_code . '/' . $field; return Mage::getStoreConfig($path); } -} \ No newline at end of file +} diff --git a/app/code/community/PedroTeixeira/Correios/Model/Carrier/CorreiosMethod.php b/app/code/community/PedroTeixeira/Correios/Model/Carrier/CorreiosMethod.php index 690cdeb..933cf6b 100644 --- a/app/code/community/PedroTeixeira/Correios/Model/Carrier/CorreiosMethod.php +++ b/app/code/community/PedroTeixeira/Correios/Model/Carrier/CorreiosMethod.php @@ -45,6 +45,7 @@ class PedroTeixeira_Correios_Model_Carrier_CorreiosMethod protected $_freeMethodWeight = null; protected $_midSize = null; protected $_splitUp = 0; + protected $_postingDays = 0; /** * Post methods @@ -178,6 +179,9 @@ protected function _getQuotes() } $this->_appendShippingReturn((string) $servicos->Codigo, $shippingPrice, $shippingDelivery); + if ($this->getConfigFlag('show_soft_errors') && !isset($isWarnAppended)) { + $isWarnAppended = $this->_appendShippingWarning($servicos); + } $existReturn = true; } @@ -368,7 +372,7 @@ protected function _appendShippingReturn($shippingMethod, $shippingPrice = 0, $c sprintf( $this->getConfigData('msgprazo'), $shippingData[0], - (int) ($correiosDelivery + $this->getConfigData('add_prazo')) + (int) ($correiosDelivery + $this->getConfigData('add_prazo') + $this->_postingDays) ) ); } else { @@ -376,7 +380,7 @@ protected function _appendShippingReturn($shippingMethod, $shippingPrice = 0, $c sprintf( $this->getConfigData('msgprazo'), $shippingData[0], - (int) ($shippingData[1] + $this->getConfigData('add_prazo')) + (int) ($shippingData[1] + $this->getConfigData('add_prazo') + $this->_postingDays) ) ); } @@ -483,6 +487,8 @@ protected function _generateVolumeWeight() $itemAltura = $this->_getFitHeight($item); $pesoCubicoTotal += (($itemAltura * $itemLargura * $itemComprimento) * $item->getQty()) / $this->getConfigData('coeficiente_volume'); + + $this->_postingDays = max($this->_postingDays, (int) $_product->getData('posting_days')); } $this->_volumeWeight = number_format($pesoCubicoTotal, 2, '.', ''); @@ -712,21 +718,11 @@ protected function _loadMidSize() */ protected function _removeInvalidServices() { - $this->_loadMidSize(); $tmpMethods = $this->_postMethodsExplode; - foreach ($tmpMethods as $key => $method) { - $isOverSize = ($this->_midSize > $this->getConfigData("validate/serv_{$method}/max/size")); - $isOverSize |= ($this->_midSize * 3 > $this->getConfigData("validate/serv_{$method}/max/sum")); - $isOverWeight = ($this->_packageWeight > $this->getConfigData("validate/serv_{$method}/max/weight")); - - if ($isOverSize || $isOverWeight) { - unset($tmpMethods[$key]); - } - } - - $isDivisible = (count($tmpMethods) == 0); - $isLoopBreakable = (count($this->_postMethodsExplode) > 0); - if ($isDivisible && $isLoopBreakable) { + $tmpMethods = $this->_filterMethodByConfigRestriction($tmpMethods); + $isDivisible = (count($tmpMethods) == 0); + + if ($isDivisible) { return $this->_splitPack(); } @@ -764,24 +760,28 @@ protected function _removeInvalidServices() */ protected function _addPostMethods($cServico) { - $i = 0; - while (!is_null($this->getConfigData("add_method_{$i}"))) { + $addMethods = $this->getConfigData("add_postmethods"); + foreach ($addMethods as $configData) { $isValid = true; - $isValid &= $this->_packageWeight >= $this->getConfigData("add_method_{$i}/from/weight"); - $isValid &= $this->_packageWeight <= $this->getConfigData("add_method_{$i}/to/weight"); - $isValid &= $this->_midSize >= $this->getConfigData("add_method_{$i}/from/size"); - $isValid &= $this->_midSize <= $this->getConfigData("add_method_{$i}/to/size"); - $isValid &= $this->_toZip >= $this->getConfigData("add_method_{$i}/from/zip"); - $isValid &= $this->_toZip <= $this->getConfigData("add_method_{$i}/to/zip"); + $isValid &= $this->_packageWeight >= $configData['from']['weight']; + $isValid &= $this->_packageWeight <= $configData['to']['weight']; + $isValid &= $this->_midSize >= $configData['from']['size']; + $isValid &= $this->_midSize <= $configData['to']['size']; + $isValid &= $this->_toZip >= $configData['from']['zip']; + $isValid &= $this->_toZip <= $configData['to']['zip']; if ($isValid) { - $price = $this->getConfigData("add_method_{$i}/price"); - $days = $this->getConfigData("add_method_{$i}/days"); - $method = $this->getConfigData("add_method_{$i}/code"); + $price = $configData['price']; + $days = $configData['days']; + $method = $configData['code']; foreach ($cServico as $servico) { if ($servico->Codigo == $method) { - $servico->Valor = number_format($price, 2, ',', ''); - $servico->PrazoEntrega = $days; + if (!empty($price)) { + $servico->Valor = number_format($price, 2, ',', ''); + } + if (!empty($days)) { + $servico->PrazoEntrega = $days; + } $servico->EntregaDomiciliar = 'S'; $servico->EntregaSabado = 'S'; $servico->Erro = '0'; @@ -789,8 +789,6 @@ protected function _addPostMethods($cServico) } } } - - $i++; } return $cServico; @@ -874,7 +872,9 @@ protected function _getFitHeight($item) */ protected function _splitPack() { - if ($this->getConfigFlag('split_pack')) { + $isSplitEnabled = $this->getConfigFlag('split_pack'); + $isMethodAvailable = (count($this->_postMethodsExplode) > 0); + if ($isSplitEnabled && $isMethodAvailable) { $this->_splitUp++; $this->_volumeWeight /= 2; $this->_packageWeight /= 2; @@ -883,4 +883,74 @@ protected function _splitPack() } return false; } + + /** + * Receive a list of methods, and validate one-by-one using the config settings. + * Returns a list of valid methods or empty. + * + * @param array $postmethods Services List + * + * @return array + */ + protected function _filterMethodByConfigRestriction($postmethods) + { + $validMethods = array(); + $this->_loadMidSize(); + foreach ($postmethods as $key => $method) { + $isOverSize = ($this->_midSize > $this->getConfigData("validate/serv_{$method}/max/size")); + $isOverSize |= ($this->_midSize * 3 > $this->getConfigData("validate/serv_{$method}/max/sum")); + $isOverWeight = ($this->_packageWeight > $this->getConfigData("validate/serv_{$method}/max/weight")); + $isOverCubic = ($this->_volumeWeight > $this->getConfigData("validate/serv_{$method}/max/volume_weight")); + $isZipAllowed = $this->_validateZipRestriction($method); + + if (!$isOverSize && !$isOverWeight && !$isOverCubic && $isZipAllowed) { + $validMethods[] = $method; + } + } + return $validMethods; + } + + /** + * Loads the zip range list. + * Returns TRUE only if zip target is included in the range. + * + * @param array $method Current Post Method + * + * @return boolean + */ + protected function _validateZipRestriction($method) + { + $zipConfig = $this->getConfigData("validate/serv_{$method}/zips"); + foreach ($zipConfig as $data) { + $zipRange = explode(',', $data); + $isBetweenRange = true; + $isBetweenRange &= ($this->_toZip >= $zipRange[0]); + $isBetweenRange &= ($this->_toZip <= $zipRange[1]); + if ($isBetweenRange) { + return true; + } + } + return false; + } + + /** + * Add a warning message at the top of the shipping method list. + * + * @param SimpleXMLElement $servico Post Method + * + * @return boolean + */ + protected function _appendShippingWarning(SimpleXMLElement $servico) + { + $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; + } + return false; + } } diff --git a/app/code/community/PedroTeixeira/Correios/etc/config.xml b/app/code/community/PedroTeixeira/Correios/etc/config.xml index 57a6f48..3ecb92a 100644 --- a/app/code/community/PedroTeixeira/Correios/etc/config.xml +++ b/app/code/community/PedroTeixeira/Correios/etc/config.xml @@ -15,7 +15,7 @@ - 4.4.0 + 4.5.0 @@ -31,6 +31,7 @@ + @@ -84,6 +85,7 @@ Correios 40010 009,010,011 + 0 0 1 0 @@ -108,77 +110,261 @@ 105 200 30 + 50 + + 00000000,99999999 + 105 200 30 + 50 + + 00000000,99999999 + 105 200 15 + 50 + + 00000000,99999999 + 105 200 30 + 50 + + 00000000,99999999 + 105 200 30 + 50 + + 00000000,99999999 + 105 200 10 + 50 + + 00000000,99999999 + 105 200 10 + 50 + + 00000000,99999999 + 105 200 30 + 50 + + 00000000,99999999 + 150 300 30 + 125 + + 01000000,01599999 + 02000000,05899999 + 06000000,06699999 + 07000000,07299999 + 07750000,07799999 + 08000000,08499999 + 09000000,09999999 + 11010000,11099999 + 12010010,12120999 + 12209000,12248999 + 12400000,12449999 + 12580000,12580999 + 13000000,13139999 + 13140000,13149999 + 13170000,13189999 + 13200000,13219999 + 13295000,13299999 + 13315000,13319999 + 13400000,13433050 + 13440000,13449999 + 13450000,13459999 + 13460000,13460999 + 13465000,13479999 + 13480000,13489999 + 13495000,13499999 + 13520000,13524999 + 13525000,13529999 + 13560000,13579999 + 14000000,14114999 + 14400000,14414999 + 14800000,14811999 + 15000000,15105999 + 16000000,16129999 + 17000000,17110999 + 18000000,18109999 + 19000000,19159999 + 19300000,19349999 + 20000000,20099999 + 20140000,20599999 + 20700000,20999999 + 20710000,20745312 + 20750000,20766840 + 20770000,20785510 + 20910000,20943580 + 20950003,20975210 + 21000000,21099999 + 21130000,21149999 + 21200000,21599999 + 21310000,21341790 + 21350000,21557300 + 21600000,21999999 + 21902710,21941840 + 22000000,23799999 + 24000000,24799999 + 25600000,25779999 + 27165000,27165999 + 27197000,27475999 + 27900000,27999999 + 28000000,28154999 + 29000000,29099999 + 29130000,29130999 + 30000000,30599999 + 30700000,32199999 + 32300000,32399999 + 32920000,32920001 + 34600000,34749999 + 35000001,35099999 + 35160000,35164999 + 35167000,35167999 + 35170000,35174999 + 35180000,35184999 + 36010000,36099999 + 36105000,36105001 + 37000000,37099999 + 38000000,38099999 + 38400000,38415999 + 39400001,39409999 + 40000000,42499999 + 42700000,42705999 + 44000000,44100999 + 45000000,45104999 + 49000000,49099999 + 50000000,50999999 + 52000000,53989998 + 54700000,54799999 + 51000000,51999999 + 54000000,54699999 + 54800000,54999999 + 55590000,55599999 + 57010000,57089999 + 58000001,58099999 + 58400001,58439999 + 59000001,59139999 + 60000000,60999999 + 64000000,64099999 + 65000000,65139999 + 66000000,66599999 + 66600000,67200999 + 68900000,68911639 + 68925000,68939999 + 69000000,69099999 + 69301000,69399999 + 69900001,69924999 + 70000000,71689999 + 71700000,71799999 + 71800000,72399999 + 74000000,74894999 + 74900000,74999999 + 76800001,76834999 + 77000000,77299999 + 78000000,78169999 + 79000000,79124999 + 80000000,81199999 + 81300000,81999999 + 82100000,82299999 + 82500000,83189999 + 83400000,83419999 + 84010000,84073999 + 85800000,85820999 + 85850000,85874999 + 86000000,86099999 + 86180000,86199999 + 87005000,87105999 + 88010000,88123999 + 88130000,88140999 + 88160000,88169999 + 88300000,88319999 + 88330000,88349999 + 88375000,88375999 + 89000000,89099999 + 89200000,89239999 + 90001000,90899999 + 91000000,91499999 + 92000000,92499999 + 93000000,93179999 + 93200000,93299999 + 93300000,93599999 + 95000000,95124999 + 96000000,96099999 + 97000000,97119999 + 99000000,99100000 + 30 50 0.5 + 50 + + 00000000,99999999 + 30 50 0.5 + 50 + + 00000000,99999999 + diff --git a/app/code/community/PedroTeixeira/Correios/etc/system.xml b/app/code/community/PedroTeixeira/Correios/etc/system.xml index 1436bbc..fe6d81f 100644 --- a/app/code/community/PedroTeixeira/Correios/etc/system.xml +++ b/app/code/community/PedroTeixeira/Correios/etc/system.xml @@ -334,6 +334,15 @@ 1 O pacote é dividido, caso o carrinho exceda os limites de peso e tamanho, para todos os serviços. A divisão se repete até que os limites sejam válidos, para um ou mais serviços. + + + select + adminhtml/system_config_source_yesno + 269 + 1 + 1 + 1 + text diff --git a/app/code/community/PedroTeixeira/Correios/sql/pedroteixeira_correios_setup/install-4.5.0.php b/app/code/community/PedroTeixeira/Correios/sql/pedroteixeira_correios_setup/install-4.5.0.php new file mode 100644 index 0000000..5b2fca8 --- /dev/null +++ b/app/code/community/PedroTeixeira/Correios/sql/pedroteixeira_correios_setup/install-4.5.0.php @@ -0,0 +1,105 @@ + + * @copyright 2015 Pedro Teixeira (http://pedroteixeira.io) + * @license http://opensource.org/licenses/MIT MIT + * @link https://github.com/pedro-teixeira/correios + */ + +/** @var $installer Mage_Core_Model_Resource_Setup */ +$installer = $this; +$installer->startSetup(); + +/* @var $installer Mage_Catalog_Model_Resource_Eav_Mysql4_Setup */ +$setup = new Mage_Eav_Model_Entity_Setup('core_setup'); + +// Add volume to prduct attribute set +$codigo = 'volume_comprimento'; +$config = array( + 'position' => 1, + 'required' => 0, + 'label' => 'Comprimento (cm)', + 'type' => 'int', + 'input' => 'text', + 'apply_to' => 'simple,bundle,grouped,configurable', + 'note' => 'Comprimento da embalagem do produto (Para cálculo dos Correios)' +); + +$setup->addAttribute('catalog_product', $codigo, $config); + +// Add volume to prduct attribute set +$codigo = 'volume_altura'; +$config = array( + 'position' => 1, + 'required' => 0, + 'label' => 'Altura (cm)', + 'type' => 'int', + 'input' => 'text', + 'apply_to' => 'simple,bundle,grouped,configurable', + 'note' => 'Altura da embalagem do produto (Para cálculo dos Correios)' +); + +$setup->addAttribute('catalog_product', $codigo, $config); + +// Add volume to prduct attribute set +$codigo = 'volume_largura'; +$config = array( + 'position' => 1, + 'required' => 0, + 'label' => 'Largura (cm)', + 'type' => 'int', + 'input' => 'text', + 'apply_to' => 'simple,bundle,grouped,configurable', + 'note' => 'Largura da embalagem do produto (Para cálculo dos Correios)' +); + +$setup->addAttribute('catalog_product', $codigo, $config); + +$codigo = 'postmethods'; +$config = array( + 'position' => 1, + 'required' => 0, + 'label' => 'Serviços de Entrega', + 'type' => 'text', + 'input' => 'multiselect', + 'source' => 'pedroteixeira_correios/source_postMethods', + 'backend' => 'eav/entity_attribute_backend_array', + 'apply_to' => 'simple,bundle,grouped,configurable', + 'note' => 'Selecione os serviços apropriados para o produto.' +); + +$setup->addAttribute('catalog_product', $codigo, $config); + +$codigo = 'fit_size'; +$config = array( + 'position' => 1, + 'required' => 0, + 'label' => 'Diferença do Encaixe (cm)', + 'type' => 'varchar', + 'input' => 'text', + 'apply_to' => 'simple,bundle,grouped,configurable', + 'note' => 'Exemplo: Se 1 item mede 10cm de altura, e 2 itens encaixados medem 11cm. A diferença é de 1cm.' +); + +$setup->addAttribute('catalog_product', $codigo, $config); + +$codigo = 'posting_days'; +$config = array( + 'position' => 1, + 'required' => 0, + 'label' => 'Prazo de Postagem', + 'type' => 'int', + 'input' => 'text', + 'apply_to' => 'simple,bundle,grouped,configurable', + 'note' => 'O prazo total é o Prazo dos Correios acrescido do maior Prazo de Postagem dos produtos no carrinho.' +); + +$setup->addAttribute('catalog_product', $codigo, $config); + +$installer->endSetup(); diff --git a/app/code/community/PedroTeixeira/Correios/sql/pedroteixeira_correios_setup/upgrade-4.4.0-4.5.0.php b/app/code/community/PedroTeixeira/Correios/sql/pedroteixeira_correios_setup/upgrade-4.4.0-4.5.0.php new file mode 100644 index 0000000..dd7b680 --- /dev/null +++ b/app/code/community/PedroTeixeira/Correios/sql/pedroteixeira_correios_setup/upgrade-4.4.0-4.5.0.php @@ -0,0 +1,35 @@ + + * @copyright 2015 Pedro Teixeira (http://pedroteixeira.io) + * @license http://opensource.org/licenses/MIT MIT + * @link https://github.com/pedro-teixeira/correios + */ + +/** @var $installer Mage_Core_Model_Resource_Setup */ +$installer = $this; +$installer->startSetup(); + +/* @var $installer Mage_Catalog_Model_Resource_Eav_Mysql4_Setup */ +$setup = new Mage_Eav_Model_Entity_Setup('core_setup'); + +$codigo = 'posting_days'; +$config = array( + 'position' => 1, + 'required' => 0, + 'label' => 'Prazo de Postagem', + 'type' => 'int', + 'input' => 'text', + 'apply_to' => 'simple,bundle,grouped,configurable', + 'note' => 'O prazo total é o Prazo dos Correios acrescido do maior Prazo de Postagem dos produtos no carrinho.' +); + +$setup->addAttribute('catalog_product', $codigo, $config); + +$installer->endSetup();