Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Atualização personalizada dos Códigos de Serviços de Postagem via SIGEPWEB #217

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,47 @@
<?php

/**
* This source file is subject to the MIT License.
* It is also available through http://opensource.org/licenses/MIT
*
* @category PedroTeixeira
* @package PedroTeixeira_Correios
* @author Pedro Teixeira <hello@pedroteixeira.io>
* @copyright 2015 Pedro Teixeira (http://pedroteixeira.io)
* @license http://opensource.org/licenses/MIT MIT
* @link https://github.com/pedro-teixeira/correios
*/
class PedroTeixeira_Correios_Block_Adminhtml_System_Config_Form_Button
extends Mage_Adminhtml_Block_System_Config_Form_Field
{
/**
* Retrieve the html code for Element
*
* @param Varien_Data_Form_Element_Abstract $element Element
*
* @return string
*/
protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
{
return $this->getButton()->toHtml();
}

/**
* Retrieve an adminhtml button
*
* @return string
*/
public function getButton()
{
$location = Mage::helper('adminhtml')->getUrl('*/sigepweb/postmethodsUpdate');
$updateButton = $this->getLayout()->createBlock('adminhtml/widget_button');
$updateButton->setData(
array(
'id' => 'btn-update-postmethods',
'label' => $this->helper('adminhtml')->__('Update'),
'onclick' => "setLocation('{$location}')",
)
);
return $updateButton;
}
}
44 changes: 44 additions & 0 deletions app/code/community/PedroTeixeira/Correios/Helper/Data.php
Expand Up @@ -13,4 +13,48 @@
*/
class PedroTeixeira_Correios_Helper_Data extends Mage_Core_Helper_Abstract
{
/**
* Gets the configuration value by path
*
* @param string $path System Config Path
*
* @return mixed
*/
public function getConfigData($path)
{
$moduleName = strtolower($this->_getModuleName());
return Mage::getStoreConfig("carriers/{$moduleName}/{$path}");
}

/**
* Get a text for option value
*
* @param string|int $value Method Code
*
* @return string|bool
*/
public function getShippingLabel($value)
{
$source = Mage::getSingleton('pedroteixeira_correios/source_postMethods');
return $source->getOptionText($value);
}

/**
* Retrieve stream context as a Soap parameter
*
* @return array
*/
public function getStreamContext()
{
return array(
'stream_context' => stream_context_create(
array(
'http' => array(
'protocol_version'=>'1.1',
'header' => 'Connection: Close'
)
)
)
);
}
}
Expand Up @@ -268,7 +268,6 @@ protected function _inicialCheck(Mage_Shipping_Model_Rate_Request $request)
protected function _getCorreiosReturn()
{
$filename = $this->getConfigData('url_ws_correios');
$contratoCodes = explode(',', $this->getConfigData('contrato_codes'));

try {
$client = new Zend_Http_Client($filename);
Expand Down Expand Up @@ -309,21 +308,11 @@ protected function _getCorreiosReturn()
$client->setParameterGet('nVlValorDeclarado', 0);
}

$contrato = false;
foreach ($contratoCodes as $contratoEach) {
if (in_array($contratoEach, $this->_postMethodsExplode)) {
$contrato = true;
}
}

if ($contrato) {
if ($this->getConfigData('cod_admin') == '' || $this->getConfigData('senha_admin') == '') {
$this->_throwError('coderror', 'Need correios admin data', __LINE__);
return false;
} else {
$client->setParameterGet('nCdEmpresa', $this->getConfigData('cod_admin'));
$client->setParameterGet('sDsSenha', $this->getConfigData('senha_admin'));
}
$nCdEmpresa = $this->getConfigData('cod_admin');
$sDsSenha = $this->getConfigData('senha_admin');
if (!empty($nCdEmpresa) && !empty($sDsSenha)) {
$client->setParameterGet('nCdEmpresa', $nCdEmpresa);
$client->setParameterGet('sDsSenha', $sDsSenha);
}

$content = $client->request()->getBody();
Expand Down Expand Up @@ -376,10 +365,11 @@ protected function _appendShippingReturn(SimpleXMLElement $servico)
$shippingCost = $shippingPrice;
$shippingPrice = $shippingPrice + $this->getConfigData('handling_fee');

$shippingData = explode(',', $this->getConfigData('serv_' . $shippingMethod));
$shippingData = Mage::helper('pedroteixeira_correios')->getShippingLabel($shippingMethod);
$shippingData = Mage::helper('pedroteixeira_correios')->__($shippingData);

if ($shippingMethod == $this->getConfigData('acobrar_code')) {
$shippingData[0] = $shippingData[0] . ' ( R$' . number_format($shippingPrice, 2, ',', '.') . ' )';
$shippingData = $shippingData . ' ( R$' . number_format($shippingPrice, 2, ',', '.') . ' )';
$shippingPrice = 0;
}

Expand All @@ -388,21 +378,13 @@ protected function _appendShippingReturn(SimpleXMLElement $servico)
$method->setMethodTitle(
sprintf(
$this->getConfigData('msgprazo'),
$shippingData[0],
$shippingData,
(int) ($correiosDelivery + $this->getConfigData('add_prazo') + $this->_postingDays)
)
);
} else {
$method->setMethodTitle(
sprintf(
$this->getConfigData('msgprazo'),
$shippingData[0],
(int) ($shippingData[1] + $this->getConfigData('add_prazo') + $this->_postingDays)
)
);
}
} else {
$method->setMethodTitle($shippingData[0]);
$method->setMethodTitle($shippingData);
}

$method->setPrice($shippingPrice);
Expand Down Expand Up @@ -605,17 +587,7 @@ protected function _getTrackingRequest($code)

try {
$client = new SoapClient(
$this->getConfigData('url_sro_correios'),
array(
'stream_context'=>stream_context_create(
array('http'=>
array(
'protocol_version'=>'1.1',
'header' => 'Connection: Close'
)
)
)
)
$this->getConfigData('url_sro_correios'), Mage::helper('pedroteixeira_correios')->getStreamContext()
);
$response = $client->buscaEventos($params);
if (empty($response)) {
Expand Down Expand Up @@ -730,19 +702,12 @@ protected function _getTracking($code)
*/
public function getAllowedMethods()
{
return array($this->_code => $this->getConfigData('title'),
'40019' =>'sedex(40019)',
'40096' =>'sedex(40096)',
'40436' =>'sedex(40436)',
'81019' =>'E-sedex(81019)',
'41106' =>'PAC(41106)',
'41068' =>'PAC(41068)',
'40215' =>'sedex 10(40215)',
'40290' =>'sedex hoje(40290)',
'40045' =>'sedex a cobrar(40045)',
'41300' =>'PAC GF(41300)',
'10065' =>'Carta comercial(10065)',
'10138' =>'Carta comercial registrada(10138)');
$output = array($this->_code => $this->getConfigData('title'));
$serviceObject = Mage::getSingleton('pedroteixeira_correios/postmethod');
foreach ($serviceObject->getCollection() as $service) {
$output[ $service->getMethodCode() ] = "{$service->getMethodCode()} - {$service->getMethodTitle()}";
}
return $output;
}

/**
Expand Down
35 changes: 35 additions & 0 deletions app/code/community/PedroTeixeira/Correios/Model/Postmethod.php
@@ -0,0 +1,35 @@
<?php

/**
* This source file is subject to the MIT License.
* It is also available through http://opensource.org/licenses/MIT
*
* @category PedroTeixeira
* @package PedroTeixeira_Correios
* @author Pedro Teixeira <hello@pedroteixeira.io>
* @copyright 2015 Pedro Teixeira (http://pedroteixeira.io)
* @license http://opensource.org/licenses/MIT MIT
* @link https://github.com/pedro-teixeira/correios
*
* @method string getMethodId()
* @method string getMethodCode()
* @method string getMethodTitle()
* @method PedroTeixeira_Correios_Model_Postmethod setMethodId(string|int $id)
* @method PedroTeixeira_Correios_Model_Postmethod setMethodCode(string $code)
* @method PedroTeixeira_Correios_Model_Postmethod setMethodTitle(string $title)
* @method PedroTeixeira_Correios_Model_Postmethod[] getCollection()
*/
class PedroTeixeira_Correios_Model_Postmethod extends Mage_Core_Model_Abstract
{
/**
* Internal constructor
*
* @see Varien_Object::_construct()
*
* @return void
*/
protected function _construct()
{
$this->_init('pedroteixeira_correios/postmethod');
}
}
@@ -0,0 +1,28 @@
<?php

/**
* This source file is subject to the MIT License.
* It is also available through http://opensource.org/licenses/MIT
*
* @category PedroTeixeira
* @package PedroTeixeira_Correios
* @author Pedro Teixeira <hello@pedroteixeira.io>
* @copyright 2015 Pedro Teixeira (http://pedroteixeira.io)
* @license http://opensource.org/licenses/MIT MIT
* @link https://github.com/pedro-teixeira/correios
*/
class PedroTeixeira_Correios_Model_Resource_Postmethod
extends Mage_Core_Model_Resource_Db_Abstract
{
/**
* Resource initialization
*
* @see Mage_Core_Model_Resource_Abstract::_construct()
*
* @return void
*/
protected function _construct()
{
$this->_init('pedroteixeira_correios/postmethod', 'method_id');
}
}
@@ -0,0 +1,28 @@
<?php

/**
* This source file is subject to the MIT License.
* It is also available through http://opensource.org/licenses/MIT
*
* @category PedroTeixeira
* @package PedroTeixeira_Correios
* @author Pedro Teixeira <hello@pedroteixeira.io>
* @copyright 2015 Pedro Teixeira (http://pedroteixeira.io)
* @license http://opensource.org/licenses/MIT MIT
* @link https://github.com/pedro-teixeira/correios
*/
class PedroTeixeira_Correios_Model_Resource_Postmethod_Collection
extends Mage_Core_Model_Resource_Db_Collection_Abstract
{
/**
* Resource initialization
*
* @see Mage_Core_Model_Resource_Db_Collection_Abstract::_construct()
*
* @return void
*/
protected function _construct()
{
$this->_init('pedroteixeira_correios/postmethod');
}
}
43 changes: 43 additions & 0 deletions app/code/community/PedroTeixeira/Correios/Model/Sigepweb.php
@@ -0,0 +1,43 @@
<?php

/**
* This source file is subject to the MIT License.
* It is also available through http://opensource.org/licenses/MIT
*
* @category PedroTeixeira
* @package PedroTeixeira_Correios
* @author Pedro Teixeira <hello@pedroteixeira.io>
* @copyright 2015 Pedro Teixeira (http://pedroteixeira.io)
* @license http://opensource.org/licenses/MIT MIT
* @link https://github.com/pedro-teixeira/correios
*/
class PedroTeixeira_Correios_Model_Sigepweb extends Mage_Core_Model_Abstract
{

/**
* Retrieve the module helper
*
* @return Pedroteixeira_Correios_Helper_Data
*/
public function helper()
{
return Mage::helper('pedroteixeira_correios');
}

/**
* Request Correios service codes using configuration fields
*
* @return SimpleXMLElement
*/
public function getBuscaCliente()
{
$params = array(
'idContrato' => $this->helper()->getConfigData('sigepweb_contract_code'),
'idCartaoPostagem' => $this->helper()->getConfigData('sigepweb_card_code'),
'usuario' => $this->helper()->getConfigData('sigepweb_username'),
'senha' => $this->helper()->getConfigData('sigepweb_password'),
);
$client = new SoapClient($this->helper()->getConfigData('url_sigepweb'), $this->helper()->getStreamContext());
return $client->buscaCliente($params);
}
}
Expand Up @@ -20,20 +20,16 @@ class PedroTeixeira_Correios_Model_Source_PostMethods extends Mage_Eav_Model_Ent
*/
public function toOptionArray()
{
return array(
array('value' => 40010, 'label' => Mage::helper('adminhtml')->__('Sedex Sem Contrato (40010)')),
array('value' => 40096, 'label' => Mage::helper('adminhtml')->__('Sedex Com Contrato (40096)')),
array('value' => 40436, 'label' => Mage::helper('adminhtml')->__('Sedex Com Contrato (40436)')),
array('value' => 81019, 'label' => Mage::helper('adminhtml')->__('E-Sedex Com Contrato (81019)')),
array('value' => 41106, 'label' => Mage::helper('adminhtml')->__('PAC Sem Contrato (41106)')),
array('value' => 41068, 'label' => Mage::helper('adminhtml')->__('PAC Com Contrato (41068)')),
array('value' => 40215, 'label' => Mage::helper('adminhtml')->__('Sedex 10 (40215)')),
array('value' => 40290, 'label' => Mage::helper('adminhtml')->__('Sedex HOJE (40290)')),
array('value' => 40045, 'label' => Mage::helper('adminhtml')->__('Sedex a Cobrar (40045)')),
array('value' => 41300, 'label' => Mage::helper('adminhtml')->__('PAC GF (41300)')),
array('value' => 10065, 'label' => Mage::helper('adminhtml')->__('Carta Comercial (10065)')),
array('value' => 10138, 'label' => Mage::helper('adminhtml')->__('Carta Comercial Registrada (10138)')),
);
$output = array();
$object = Mage::getSingleton('pedroteixeira_correios/postmethod');
$collection = $object->getCollection();
foreach ($collection as $method) {
$output[] = array(
'value' => $method->getMethodCode(),
'label' => "{$method->getMethodCode()} - {$method->getMethodTitle()}",
);
}
return $output;
}

/**
Expand Down