1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* MIT License |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 MZ Desenvolvimento de Sistemas LTDA |
6
|
|
|
* |
7
|
|
|
* @author Francimar Alves <[email protected]> |
8
|
|
|
* |
9
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
10
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
11
|
|
|
* in the Software without restriction, including without limitation the rights |
12
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
14
|
|
|
* furnished to do so, subject to the following conditions: |
15
|
|
|
* |
16
|
|
|
* The above copyright notice and this permission notice shall be included in all |
17
|
|
|
* copies or substantial portions of the Software. |
18
|
|
|
* |
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
25
|
|
|
* SOFTWARE. |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
namespace NFe\Task; |
29
|
|
|
|
30
|
|
|
use NFe\Core\Nota; |
31
|
|
|
use NFe\Common\Util; |
32
|
|
|
use NFe\Common\Node; |
33
|
|
|
use NFe\Entity\Estado; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Status das respostas de envios para os servidores da SEFAZ |
37
|
|
|
*/ |
38
|
|
|
class Status implements Node |
39
|
|
|
{ |
40
|
|
|
|
41
|
|
|
private $ambiente; |
42
|
|
|
private $versao; |
43
|
|
|
private $status; |
44
|
|
|
private $motivo; |
45
|
|
|
private $uf; |
|
|
|
|
46
|
|
|
|
47
|
30 |
|
public function __construct($status = array()) |
48
|
|
|
{ |
49
|
30 |
|
$this->fromArray($status); |
50
|
30 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Identificação do Ambiente: |
54
|
|
|
* 1 - Produção |
55
|
|
|
* 2 - Homologação |
56
|
|
|
*/ |
57
|
22 |
|
public function getAmbiente($normalize = false) |
58
|
|
|
{ |
59
|
22 |
|
if (!$normalize) { |
60
|
18 |
|
return $this->ambiente; |
61
|
|
|
} |
62
|
18 |
|
switch ($this->ambiente) { |
63
|
18 |
|
case Nota::AMBIENTE_PRODUCAO: |
64
|
|
|
return '1'; |
65
|
18 |
|
case Nota::AMBIENTE_HOMOLOGACAO: |
66
|
17 |
|
return '2'; |
67
|
1 |
|
} |
68
|
1 |
|
return $this->ambiente; |
69
|
|
|
} |
70
|
|
|
|
71
|
30 |
|
public function setAmbiente($ambiente) |
72
|
|
|
{ |
73
|
|
|
switch ($ambiente) { |
74
|
30 |
|
case '1': |
75
|
|
|
$ambiente = Nota::AMBIENTE_PRODUCAO; |
76
|
|
|
break; |
77
|
30 |
|
case '2': |
78
|
17 |
|
$ambiente = Nota::AMBIENTE_HOMOLOGACAO; |
79
|
17 |
|
break; |
80
|
|
|
} |
81
|
30 |
|
$this->ambiente = $ambiente; |
82
|
30 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Versão do Aplicativo que processou a NF-e |
87
|
|
|
*/ |
88
|
12 |
|
public function getVersao($normalize = false) |
89
|
|
|
{ |
90
|
12 |
|
if (!$normalize) { |
91
|
11 |
|
return $this->versao; |
92
|
|
|
} |
93
|
5 |
|
return $this->versao; |
94
|
|
|
} |
95
|
|
|
|
96
|
30 |
|
public function setVersao($versao) |
97
|
|
|
{ |
98
|
30 |
|
$this->versao = $versao; |
99
|
30 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Código do status da mensagem enviada. |
104
|
|
|
*/ |
105
|
19 |
|
public function getStatus($normalize = false) |
106
|
|
|
{ |
107
|
19 |
|
if (!$normalize) { |
108
|
19 |
|
return $this->status; |
109
|
|
|
} |
110
|
5 |
|
return $this->status; |
111
|
|
|
} |
112
|
|
|
|
113
|
30 |
|
public function setStatus($status) |
114
|
|
|
{ |
115
|
30 |
|
$this->status = $status; |
116
|
30 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Descrição literal do status do serviço solicitado. |
121
|
|
|
*/ |
122
|
13 |
|
public function getMotivo($normalize = false) |
123
|
|
|
{ |
124
|
13 |
|
if (!$normalize) { |
125
|
12 |
|
return $this->motivo; |
126
|
|
|
} |
127
|
5 |
|
return $this->motivo; |
128
|
|
|
} |
129
|
|
|
|
130
|
30 |
|
public function setMotivo($motivo) |
131
|
|
|
{ |
132
|
30 |
|
$this->motivo = $motivo; |
133
|
30 |
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* código da UF de atendimento |
138
|
|
|
*/ |
139
|
14 |
|
public function getUF($normalize = false) |
140
|
|
|
{ |
141
|
14 |
|
if (!$normalize || is_numeric($this->uf)) { |
142
|
12 |
|
return $this->uf; |
143
|
|
|
} |
144
|
|
|
|
145
|
4 |
|
$estado = new Estado(); |
146
|
4 |
|
$estado->setUF($this->uf); |
147
|
4 |
|
$estado->checkCodigos(); |
148
|
4 |
|
return $estado->getCodigo(); |
149
|
|
|
} |
150
|
|
|
|
151
|
30 |
|
public function setUF($uf) |
|
|
|
|
152
|
|
|
{ |
153
|
30 |
|
$this->uf = $uf; |
154
|
30 |
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Gera um número único com 15 dígitos |
159
|
|
|
* @return string Número com 15 dígitos |
160
|
|
|
*/ |
161
|
16 |
|
public static function genLote() |
162
|
|
|
{ |
163
|
16 |
|
return substr(Util::padText(number_format(microtime(true)*1000000, 0, '', ''), 15), 0, 15); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
11 |
|
public function toArray($recursive = false) |
167
|
|
|
{ |
168
|
11 |
|
$status = array(); |
169
|
11 |
|
$status['ambiente'] = $this->getAmbiente(); |
170
|
11 |
|
$status['versao'] = $this->getVersao(); |
171
|
11 |
|
$status['status'] = $this->getStatus(); |
172
|
11 |
|
$status['motivo'] = $this->getMotivo(); |
173
|
11 |
|
$status['uf'] = $this->getUF(); |
174
|
11 |
|
return $status; |
175
|
|
|
} |
176
|
|
|
|
177
|
30 |
|
public function fromArray($status = array()) |
178
|
|
|
{ |
179
|
30 |
|
if ($status instanceof Status) { |
180
|
1 |
|
$status = $status->toArray(); |
181
|
30 |
|
} elseif (!is_array($status)) { |
182
|
1 |
|
return $this; |
183
|
|
|
} |
184
|
30 |
|
if (isset($status['ambiente'])) { |
185
|
8 |
|
$this->setAmbiente($status['ambiente']); |
186
|
8 |
|
} else { |
187
|
30 |
|
$this->setAmbiente(null); |
188
|
|
|
} |
189
|
30 |
|
if (isset($status['versao'])) { |
190
|
8 |
|
$this->setVersao($status['versao']); |
191
|
8 |
|
} else { |
192
|
30 |
|
$this->setVersao(null); |
193
|
|
|
} |
194
|
30 |
|
if (isset($status['status'])) { |
195
|
8 |
|
$this->setStatus($status['status']); |
196
|
8 |
|
} else { |
197
|
30 |
|
$this->setStatus(null); |
198
|
|
|
} |
199
|
30 |
|
if (isset($status['motivo'])) { |
200
|
8 |
|
$this->setMotivo($status['motivo']); |
201
|
8 |
|
} else { |
202
|
30 |
|
$this->setMotivo(null); |
203
|
|
|
} |
204
|
30 |
|
if (isset($status['uf'])) { |
205
|
5 |
|
$this->setUF($status['uf']); |
206
|
5 |
|
} else { |
207
|
30 |
|
$this->setUF(null); |
208
|
|
|
} |
209
|
30 |
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
5 |
|
public function getNode($name = null) |
213
|
|
|
{ |
214
|
5 |
|
$dom = new \DOMDocument('1.0', 'UTF-8'); |
215
|
5 |
|
$element = $dom->createElement(is_null($name)?'Status':$name); |
216
|
5 |
|
Util::appendNode($element, 'tpAmb', $this->getAmbiente(true)); |
217
|
5 |
|
Util::appendNode($element, 'verAplic', $this->getVersao(true)); |
218
|
5 |
|
Util::appendNode($element, 'cStat', $this->getStatus(true)); |
219
|
5 |
|
Util::appendNode($element, 'xMotivo', $this->getMotivo(true)); |
220
|
5 |
|
if (!is_null($this->getUF())) { |
221
|
2 |
|
Util::appendNode($element, 'cUF', $this->getUF(true)); |
222
|
2 |
|
} |
223
|
5 |
|
return $element; |
224
|
|
|
} |
225
|
|
|
|
226
|
18 |
|
public function loadNode($element, $name = null) |
227
|
|
|
{ |
228
|
18 |
|
$name = is_null($name)?'Status':$name; |
229
|
18 |
|
if ($element->nodeName != $name) { |
230
|
18 |
|
$_fields = $element->getElementsByTagName($name); |
231
|
18 |
|
if ($_fields->length == 0) { |
232
|
1 |
|
throw new \Exception('Tag "'.$name.'" do Status não encontrada', 404); |
233
|
|
|
} |
234
|
17 |
|
$element = $_fields->item(0); |
235
|
17 |
|
} |
236
|
17 |
|
$this->setAmbiente( |
237
|
17 |
|
Util::loadNode( |
238
|
17 |
|
$element, |
239
|
17 |
|
'tpAmb', |
240
|
|
|
'Tag "tpAmb" não encontrada no Status' |
241
|
17 |
|
) |
242
|
17 |
|
); |
243
|
17 |
|
$this->setVersao( |
244
|
17 |
|
Util::loadNode( |
245
|
17 |
|
$element, |
246
|
17 |
|
'verAplic', |
247
|
|
|
'Tag "verAplic" não encontrada no Status' |
248
|
17 |
|
) |
249
|
17 |
|
); |
250
|
17 |
|
$this->setStatus( |
251
|
17 |
|
Util::loadNode( |
252
|
17 |
|
$element, |
253
|
17 |
|
'cStat', |
254
|
|
|
'Tag "cStat" não encontrada no Status' |
255
|
17 |
|
) |
256
|
17 |
|
); |
257
|
17 |
|
$this->setMotivo( |
258
|
17 |
|
Util::loadNode( |
259
|
17 |
|
$element, |
260
|
17 |
|
'xMotivo', |
261
|
|
|
'Tag "xMotivo" não encontrada no Status' |
262
|
17 |
|
) |
263
|
17 |
|
); |
264
|
17 |
|
$this->setUF(Util::loadNode($element, 'cUF')); |
265
|
17 |
|
return $element; |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.