/
Tracking.php
161 lines (137 loc) · 4.56 KB
/
Tracking.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
* @method setTrackingValue()
* @method string getTrackingValue()
*/
class Storm_Correios_Model_Carrier_Shipping_Tracking extends Varien_Object
{
const TRACKING_URL = 'http://webservice.correios.com.br/service/rastro/Rastro.wsdl';
protected $_result;
public function __construct()
{
if(!isset($this->_result)) {
$this->_result = Mage::getModel('shipping/tracking_result');
}
}
/**
* Makes the request on the website of post office
* and returns the result of tracking
*
* @return Mage_Shipping_Tracking_Result
*/
public function request()
{
if($trackingData = $this->getTrackingData()) {
$status = Mage::getModel('shipping/tracking_result_status');
$status->setCarrier(Storm_Correios_Model_Carrier_Shipping::CODE)
->setCarrierTitle($this->_getHelper()->getConfigData('title'))
->setTracking($this->getTrackingValue())
->addData($trackingData);
$this->_result->append($status);
} else {
$error = Mage::getModel('shipping/tracking_result_error');
$error->setCarrier(Storm_Correios_Model_Carrier_Shipping::CODE);
$error->setCarrierTitle($this->_getHelper()->getConfigData('title'));
$error->setTracking($this->getTrackingValue());
$error->setErrorMessage($this->_getHelper()->__('Unable to retrieve tracking'));
$this->_result->append($error);
}
return $this->_result;
}
/**
* Returns the formatted information
* to the result of tracking magento
*
* @return array|boolean
*/
public function getTrackingData()
{
if($progress = $this->_parseProgressDetail()) {
$data = array(
'progressdetail' => $progress
);
return $data;
}
return false;
}
/**
* Parses the HTML page
* tracker objects postal
*
* @return array|boolean
*/
protected function _parseProgressDetail()
{
try {
$data = array();
if($object = $this->_requestEventTracking($this->getTrackingValue())) {
foreach ($object->evento as $event) {
$eventDate = implode('-', array_reverse(explode('/', $event->data)));
$data[] = array(
'activity' => $this->_parseEventMessage($event),
'deliverydate' => $eventDate,
'deliverytime' => sprintf('%s:00', $event->hora),
'deliverylocation' => $event->cidade . ' / ' . $event->uf
);
}
return $data;
}
} catch(Exception $error) {
Mage::log(sprintf('Error: %s.', $error->getMessage()));
}
return false;
}
/**
* Returns the instance of the helper module's main
*
* @return Storm_Correios_Helper_Data
*/
protected function _getHelper()
{
return Mage::helper('correios');
}
/**
* Request event tracking from webservice resources
*
* @param $tracking
* @return bool|void
*/
public function _requestEventTracking($tracking)
{
$params = array(
'usuario' => 'ECT',
'senha' => 'SRO',
'tipo' => 'L',
'resultado' => 'T',
'lingua' => '101',
'objetos' => $tracking
);
try {
$client = new SoapClient(self::TRACKING_URL);
if(!$response = $client->buscaEventos($params)) {
throw new Exception(sprintf('No response on requesting tracking %s.', $tracking));
}
return $response->return->objeto;
} catch (Exception $e) {
Mage::log('Soap Error: %s', $e->getMessage());
}
return false;
}
/**
* Change event message format
*
* @param $event
* @return string
*/
public function _parseEventMessage($event)
{
$message = $event->descricao;
if (isset($event->destino) && isset($event->destino->local)) {
$message = sprintf('%s para %s - %s / %s', $message, $event->destino->local,$event->destino->cidade, $event->destino->uf);
}
if (isset($event->detalhe) && !empty($event->detalhe)) {
$message = sprintf('%s %s', $message, $event->detalhe);
}
return $message;
}
}