Skip to content

Commit

Permalink
#136 Atualização para rastreamento, e correção do build
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelpatro committed Jun 16, 2016
1 parent 9580f0e commit 8271e38
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 91 deletions.
Expand Up @@ -583,94 +583,101 @@ public function getTracking($trackings)
}

/**
* Protected Get Tracking, opens the request to Correios
*
* Loads the parameters and calls the webservice using SOAP
*
* @param string $code Code
*
* @return bool
*
* @return bool|array
*
* @throws Exception
*/
protected function _getTracking($code)
protected function _getTrackingRequest($code)
{
$error = Mage::getModel('shipping/tracking_result_error');
$error->setTracking($code);
$error->setCarrier($this->_code);
$error->setCarrierTitle($this->getConfigData('title'));
$error->setErrorMessage($this->getConfigData('urlerror'));
$response = false;
$params = array(
'usuario' => $this->getConfigData('sro_username'),
'senha' => $this->getConfigData('sro_password'),
'tipo' => $this->getConfigData('sro_type'),
'resultado' => 'T',
'lingua' => $this->getConfigData('sro_language'),
'objetos' => $code,
);

$url = 'http://websro.correios.com.br/sro_bin/txect01$.QueryList';
$url .= '?P_LINGUA=001&P_TIPO=001&P_COD_UNI=' . $code;
try {
$client = new Zend_Http_Client();
$client->setUri($url);
$content = $client->request();
$body = $content->getBody();
$client = new SoapClient($this->getConfigData('url_sro_correios'));
$response = $client->buscaEventos($params);
if (empty($response)) {
throw new Exception("Empty response");
}
} catch (Exception $e) {
$this->_result->append($error);
return false;
}

if (!preg_match('#<table ([^>]+)>(.*?)</table>#is', $body, $matches)) {
$this->_result->append($error);
return false;
}
$table = $matches[2];

if (!preg_match_all('/<tr>(.*)<\/tr>/i', $table, $columns, PREG_SET_ORDER)) {
$this->_result->append($error);
return false;
Mage::log("Soap Error: {$e->getMessage()}");
}
return $response;
}

/**
* Loads progress data using the WSDL response
*
* @param string $request Request response
*
* @return array
*/
protected function _getTrackingProgress($request)
{
$track = array();
$progress = array();
for ($i = 0; $i < count($columns); $i++) {
$column = $columns[$i][1];

$description = '';
$found = false;
if (preg_match('/<td rowspan="?2"?/i', $column)
&& preg_match(
'/<td rowspan="?2"?>(.*)<\/td><td>(.*)<\/td><td><font color="[A-Z0-9]{6}">(.*)<\/font><\/td>/i',
$column,
$matches
)
) {
if (preg_match('/<td colspan="?2"?>(.*)<\/td>/i', $columns[$i + 1][1], $matchesDescription)) {
$description = str_replace(' ', '', $matchesDescription[1]);
}
$eventTypes = explode(',', $this->getConfigData("sro_event_type_last"));

$found = true;
} elseif (preg_match(
'/<td rowspan="?1"?>(.*)<\/td><td>(.*)<\/td><td><font color="[A-Z0-9]{6}">(.*)<\/font><\/td>/i',
$column,
$matches
)
) {
$found = true;
foreach ($request->return->objeto->evento as $evento) {
$date = new Zend_Date($evento->data, 'dd/MM/YYYY', new Zend_Locale('pt_BR'));
$msg = array($evento->descricao);
if (isset($evento->destino) && isset($evento->destino->local)) {
$msg = array("{$evento->descricao} para {$evento->destino->local}");
}

if ($found) {
$datetime = explode(' ', $matches[1]);
$locale = new Zend_Locale('pt_BR');
$date = '';
$date = new Zend_Date($datetime[0], 'dd/MM/YYYY', $locale);

if ((int) $evento->status < 2 && in_array($evento->tipo, $eventTypes)) {
$track = array(
'deliverydate' => $date->toString('YYYY-MM-dd'),
'deliverytime' => $datetime[1] . ':00',
'deliverylocation' => htmlentities($matches[2], ENT_IGNORE, 'ISO-8859-1'),
'status' => htmlentities($matches[3], ENT_IGNORE, 'ISO-8859-1'),
'activity' => htmlentities($matches[3], ENT_IGNORE, 'ISO-8859-1')
'deliverydate' => $date->toString('YYYY-MM-dd'),
'deliverytime' => $evento->hora . ':00',
'status' => $evento->descricao,
);
}
$progress[] = array(
'deliverydate' => $date->toString('YYYY-MM-dd'),
'deliverytime' => $evento->hora . ':00',
'deliverylocation' => "{$evento->local} - {$evento->cidade}/{$evento->uf}",
'status' => $evento->descricao,
'activity' => implode(' | ', $msg),
);
}

if ($description !== '') {
$track['activity'] = $matches[3] . ' - ' . htmlentities($description, ENT_IGNORE, 'ISO-8859-1');
}
$progress[] = $track;
return $progress;
}

$progress[] = $track;
}
/**
* Protected Get Tracking, opens the request to Correios
*
* @param string $code Code
*
* @return bool
*/
protected function _getTracking($code)
{
$error = Mage::getModel('shipping/tracking_result_error');
$error->setTracking($code);
$error->setCarrier($this->_code);
$error->setCarrierTitle($this->getConfigData('title'));
$error->setErrorMessage($this->getConfigData('urlerror'));

$request = $this->_getTrackingRequest($code);
if (!isset($request->return)) {
$this->_result->append($error);
return false;
}

$progress = $this->_getTrackingProgress($request);
if (!empty($progress)) {
$track = $progress[0];
$track = array_pop($progress);
$track['progressdetail'] = $progress;

$tracking = Mage::getModel('shipping/tracking_result_status');
Expand Down
41 changes: 20 additions & 21 deletions app/code/community/PedroTeixeira/Correios/Model/Sro.php
Expand Up @@ -48,38 +48,37 @@ public function getShippedTracks()
/**
* Load XML response from Correios
*
* @param string $number Tracking Code
* @param string $trackingCode Tracking Code
*
* @throws Zend_Http_Client_Adapter_Exception
* @throws Exception
*
* @link http://www.correios.com.br/para-voce/correios-de-a-a-z/pdf/rastreamento-de-objetos/
* Manual_SROXML_28fev14.pdf
* @link http://www.corporativo.correios.com.br/encomendas/sigepweb/doc/
* Manual_de_Implementacao_do_Web_Service_SIGEPWEB_Logistica_Reversa.pdf
*
* @return SimpleXMLElement
*/
public function request($number)
public function request($trackingCode)
{
$client = new Zend_Http_Client($this->getConfigData("url_sro_correios"));
$client->setParameterPost('Usuario', $this->getConfigData('sro_username'));
$client->setParameterPost('Senha', $this->getConfigData('sro_password'));
$client->setParameterPost('Tipo', $this->getConfigData('sro_type'));
$client->setParameterPost('Resultado', $this->getConfigData('sro_result'));
$client->setParameterPost('Objetos', $number);
$params = array(
'usuario' => $this->getConfigData('sro_username'),
'senha' => $this->getConfigData('sro_password'),
'tipo' => $this->getConfigData('sro_type'),
'resultado' => $this->getConfigData('sro_result'),
'lingua' => $this->getConfigData('sro_language'),
'objetos' => $trackingCode,
);

try {
$response = $client->request(Zend_Http_Client::POST)->getBody();
$client = new SoapClient($this->getConfigData('url_sro_correios'));
$response = $client->buscaEventos($params);
if (empty($response)) {
throw new Zend_Http_Client_Adapter_Exception("Empty response");
}
libxml_use_internal_errors(true);
$this->_xml = simplexml_load_string($response);
if (!$this->_xml || !isset($this->_xml->objeto)) {
throw new Zend_Http_Client_Adapter_Exception("Invalid XML");
throw new Exception("Empty response");
}
} catch (Zend_Http_Exception $e) {
Mage::log("{$e->getMessage()}");
Mage::log("TRACKING: {$number}");
Mage::log("RESPONSE: {$response}");
return false;
$this->_xml = $response->return;
} catch (Exception $e) {
Mage::log("Soap Error: {$e->getMessage()}");
}
return $this;
}
Expand Down

0 comments on commit 8271e38

Please sign in to comment.