|
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\Core\SEFAZ; |
|
32
|
|
|
use NFe\Common\Util; |
|
33
|
|
|
use NFe\Exception\ValidationException; |
|
34
|
|
|
use FR3D\XmlDSig\Adapter\AdapterInterface; |
|
35
|
|
|
use FR3D\XmlDSig\Adapter\XmlseclibsAdapter; |
|
36
|
|
|
|
|
37
|
|
|
class Evento extends Retorno |
|
|
|
|
|
|
38
|
|
|
{ |
|
39
|
|
|
|
|
40
|
|
|
const VERSAO = '1.00'; |
|
41
|
|
|
|
|
42
|
|
|
const TIPO_CANCELAMENTO = '110111'; |
|
43
|
|
|
const TAG_RETORNO = 'retEvento'; |
|
44
|
|
|
const TAG_RETORNO_ENVIO = 'retEnvEvento'; |
|
45
|
|
|
|
|
46
|
|
|
private $id; |
|
47
|
|
|
private $orgao; |
|
48
|
|
|
private $identificador; |
|
49
|
|
|
private $chave; |
|
50
|
|
|
private $data; |
|
51
|
|
|
private $tipo; |
|
52
|
|
|
private $sequencia; |
|
53
|
|
|
private $descricao; |
|
54
|
|
|
private $numero; |
|
55
|
|
|
private $justificativa; |
|
56
|
|
|
private $email; |
|
57
|
|
|
private $modelo; |
|
58
|
|
|
private $informacao; |
|
59
|
|
|
|
|
60
|
8 |
|
public function __construct($evento = array()) |
|
61
|
|
|
{ |
|
62
|
8 |
|
parent::__construct($evento); |
|
63
|
8 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Identificador da TAG a ser assinada, a regra de formação do Id é: "ID" + |
|
67
|
|
|
* tpEvento + chave da NF-e + nSeqEvento |
|
68
|
|
|
*/ |
|
69
|
7 |
|
public function getID($normalize = false) |
|
70
|
|
|
{ |
|
71
|
7 |
|
if (!$normalize) { |
|
72
|
2 |
|
return $this->id; |
|
73
|
|
|
} |
|
74
|
6 |
|
return 'ID'.$this->id; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
8 |
|
public function setID($id) |
|
78
|
|
|
{ |
|
79
|
8 |
|
$this->id = $id; |
|
80
|
8 |
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Código do órgão de recepção do Evento. Utilizar a Tabela do IBGE |
|
85
|
|
|
* extendida, utilizar 91 para identificar o Ambiente Nacional |
|
86
|
|
|
*/ |
|
87
|
6 |
|
public function getOrgao($normalize = false) |
|
88
|
|
|
{ |
|
89
|
6 |
|
if (!$normalize || is_numeric($this->orgao)) { |
|
90
|
4 |
|
return $this->orgao; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
4 |
|
$db = SEFAZ::getInstance()->getConfiguracao()->getBanco(); |
|
|
|
|
|
|
94
|
4 |
|
return $db->getCodigoOrgao($this->orgao); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
8 |
|
public function setOrgao($orgao) |
|
98
|
|
|
{ |
|
99
|
8 |
|
$this->orgao = $orgao; |
|
100
|
8 |
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Identificação do autor do evento |
|
105
|
|
|
*/ |
|
106
|
6 |
|
public function getIdentificador($normalize = false) |
|
107
|
|
|
{ |
|
108
|
6 |
|
if (!$normalize) { |
|
109
|
6 |
|
return $this->identificador; |
|
110
|
|
|
} |
|
111
|
6 |
|
return $this->identificador; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
8 |
|
public function setIdentificador($identificador) |
|
115
|
|
|
{ |
|
116
|
8 |
|
$this->identificador = $identificador; |
|
117
|
8 |
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Chave de Acesso da NF-e vinculada ao evento |
|
122
|
|
|
*/ |
|
123
|
6 |
|
public function getChave($normalize = false) |
|
124
|
|
|
{ |
|
125
|
6 |
|
if (!$normalize) { |
|
126
|
2 |
|
return $this->chave; |
|
127
|
|
|
} |
|
128
|
6 |
|
return $this->chave; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
8 |
|
public function setChave($chave) |
|
132
|
|
|
{ |
|
133
|
8 |
|
$this->chave = $chave; |
|
134
|
8 |
|
return $this; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Data e Hora do Evento, formato UTC (AAAA-MM-DDThh:mm:ssTZD, onde TZD = |
|
139
|
|
|
* +hh:mm ou -hh:mm) |
|
140
|
|
|
*/ |
|
141
|
6 |
|
public function getData($normalize = false) |
|
142
|
|
|
{ |
|
143
|
6 |
|
if (!$normalize) { |
|
144
|
1 |
|
return $this->data; |
|
145
|
|
|
} |
|
146
|
6 |
|
return Util::toDateTime($this->data); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
8 |
|
public function setData($data) |
|
150
|
|
|
{ |
|
151
|
8 |
|
if (!is_numeric($data)) { |
|
152
|
8 |
|
$data = strtotime($data); |
|
153
|
8 |
|
} |
|
154
|
8 |
|
$this->data = $data; |
|
155
|
8 |
|
return $this; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Tipo do Evento |
|
160
|
|
|
*/ |
|
161
|
6 |
|
public function getTipo($normalize = false) |
|
162
|
|
|
{ |
|
163
|
6 |
|
if (!$normalize) { |
|
164
|
1 |
|
return $this->tipo; |
|
165
|
|
|
} |
|
166
|
6 |
|
return $this->tipo; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
8 |
|
public function setTipo($tipo) |
|
170
|
|
|
{ |
|
171
|
8 |
|
$this->tipo = $tipo; |
|
172
|
8 |
|
return $this; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Seqüencial do evento para o mesmo tipo de evento. Para maioria dos |
|
177
|
|
|
* eventos será 1, nos casos em que possa existir mais de um evento, como é |
|
178
|
|
|
* o caso da carta de correção, o autor do evento deve numerar de forma |
|
179
|
|
|
* seqüencial. |
|
180
|
|
|
*/ |
|
181
|
6 |
|
public function getSequencia($normalize = false) |
|
182
|
|
|
{ |
|
183
|
6 |
|
if (!$normalize) { |
|
184
|
1 |
|
return $this->sequencia; |
|
185
|
|
|
} |
|
186
|
6 |
|
return $this->sequencia; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
8 |
|
public function setSequencia($sequencia) |
|
190
|
|
|
{ |
|
191
|
8 |
|
$this->sequencia = $sequencia; |
|
192
|
8 |
|
return $this; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Descrição do Evento |
|
197
|
|
|
*/ |
|
198
|
6 |
|
public function getDescricao($normalize = false) |
|
199
|
|
|
{ |
|
200
|
6 |
|
if (!$normalize) { |
|
201
|
1 |
|
return $this->descricao; |
|
202
|
|
|
} |
|
203
|
6 |
|
return $this->descricao; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
8 |
|
public function setDescricao($descricao) |
|
207
|
|
|
{ |
|
208
|
8 |
|
$this->descricao = $descricao; |
|
209
|
8 |
|
return $this; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Número do Protocolo de Status da NF-e. 1 posição (1 – Secretaria de |
|
214
|
|
|
* Fazenda Estadual 2 – Receita Federal); 2 posições ano; 10 seqüencial no |
|
215
|
|
|
* ano. |
|
216
|
|
|
*/ |
|
217
|
6 |
|
public function getNumero($normalize = false) |
|
218
|
|
|
{ |
|
219
|
6 |
|
if (!$normalize) { |
|
220
|
1 |
|
return $this->numero; |
|
221
|
|
|
} |
|
222
|
6 |
|
return $this->numero; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
8 |
|
public function setNumero($numero) |
|
226
|
|
|
{ |
|
227
|
8 |
|
$this->numero = $numero; |
|
228
|
8 |
|
return $this; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Justificativa do cancelamento |
|
233
|
|
|
*/ |
|
234
|
6 |
|
public function getJustificativa($normalize = false) |
|
235
|
|
|
{ |
|
236
|
6 |
|
if (!$normalize) { |
|
237
|
1 |
|
return $this->justificativa; |
|
238
|
|
|
} |
|
239
|
6 |
|
return $this->justificativa; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
8 |
|
public function setJustificativa($justificativa) |
|
243
|
|
|
{ |
|
244
|
8 |
|
$this->justificativa = $justificativa; |
|
245
|
8 |
|
return $this; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* email do destinatário |
|
250
|
|
|
*/ |
|
251
|
4 |
|
public function getEmail($normalize = false) |
|
252
|
|
|
{ |
|
253
|
4 |
|
if (!$normalize) { |
|
254
|
4 |
|
return $this->email; |
|
255
|
|
|
} |
|
256
|
|
|
return $this->email; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
8 |
|
public function setEmail($email) |
|
260
|
|
|
{ |
|
261
|
8 |
|
$this->email = $email; |
|
262
|
8 |
|
return $this; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Código do modelo do Documento Fiscal. 55 = NF-e; 65 = NFC-e. |
|
267
|
|
|
* @param boolean $normalize informa se o modelo deve estar no formato do XML |
|
268
|
|
|
* @return mixed modelo do Envio |
|
269
|
|
|
*/ |
|
270
|
3 |
|
public function getModelo($normalize = false) |
|
271
|
|
|
{ |
|
272
|
3 |
|
if (!$normalize) { |
|
273
|
3 |
|
return $this->modelo; |
|
274
|
|
|
} |
|
275
|
|
|
switch ($this->modelo) { |
|
276
|
|
|
case Nota::MODELO_NFE: |
|
277
|
|
|
return '55'; |
|
278
|
|
|
case Nota::MODELO_NFCE: |
|
279
|
|
|
return '65'; |
|
280
|
|
|
} |
|
281
|
|
|
return $this->modelo; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Altera o valor do Modelo para o informado no parâmetro |
|
286
|
|
|
* @param mixed $modelo novo valor para Modelo |
|
287
|
|
|
* @return Envio A própria instância da classe |
|
288
|
|
|
*/ |
|
289
|
8 |
|
public function setModelo($modelo) |
|
290
|
|
|
{ |
|
291
|
|
|
switch ($modelo) { |
|
292
|
8 |
|
case '55': |
|
293
|
|
|
$modelo = Nota::MODELO_NFE; |
|
294
|
|
|
break; |
|
295
|
8 |
|
case '65': |
|
296
|
|
|
$modelo = Nota::MODELO_NFCE; |
|
297
|
|
|
break; |
|
298
|
|
|
} |
|
299
|
8 |
|
$this->modelo = $modelo; |
|
300
|
8 |
|
return $this; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* Resposta de informação do evento |
|
305
|
|
|
*/ |
|
306
|
5 |
|
public function getInformacao() |
|
307
|
|
|
{ |
|
308
|
5 |
|
return $this->informacao; |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
8 |
|
public function setInformacao($informacao) |
|
312
|
|
|
{ |
|
313
|
8 |
|
$this->informacao = $informacao; |
|
314
|
8 |
|
return $this; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
/** |
|
318
|
|
|
* Informa se a identificação é um CNPJ |
|
319
|
|
|
*/ |
|
320
|
6 |
|
public function isCNPJ() |
|
321
|
|
|
{ |
|
322
|
6 |
|
return strlen($this->getIdentificador()) == 14; |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
/** |
|
326
|
|
|
* Informa se o lote já foi processado e já tem um protocolo |
|
327
|
|
|
*/ |
|
328
|
3 |
|
public function isProcessado() |
|
329
|
|
|
{ |
|
330
|
3 |
|
return $this->getStatus() == '128'; |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
/** |
|
334
|
|
|
* Informa se a nota foi cancelada com sucesso |
|
335
|
|
|
*/ |
|
336
|
2 |
|
public function isCancelado() |
|
337
|
|
|
{ |
|
338
|
2 |
|
return in_array($this->getStatus(), array('135', '155')); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
1 |
|
public function toArray($recursive = false) |
|
342
|
|
|
{ |
|
343
|
1 |
|
$evento = parent::toArray($recursive); |
|
344
|
1 |
|
$evento['id'] = $this->getID(); |
|
345
|
1 |
|
$evento['orgao'] = $this->getOrgao(); |
|
346
|
1 |
|
$evento['identificador'] = $this->getIdentificador(); |
|
347
|
1 |
|
$evento['chave'] = $this->getChave(); |
|
348
|
1 |
|
$evento['data'] = $this->getData(); |
|
349
|
1 |
|
$evento['tipo'] = $this->getTipo(); |
|
350
|
1 |
|
$evento['sequencia'] = $this->getSequencia(); |
|
351
|
1 |
|
$evento['descricao'] = $this->getDescricao(); |
|
352
|
1 |
|
$evento['numero'] = $this->getNumero(); |
|
353
|
1 |
|
$evento['justificativa'] = $this->getJustificativa(); |
|
354
|
1 |
|
$evento['email'] = $this->getEmail(); |
|
355
|
1 |
|
$evento['modelo'] = $this->getModelo(); |
|
356
|
1 |
|
$evento['informacao'] = $this->getInformacao(); |
|
357
|
1 |
|
return $evento; |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
8 |
|
public function fromArray($evento = array()) |
|
|
|
|
|
|
361
|
|
|
{ |
|
362
|
8 |
|
if ($evento instanceof Evento) { |
|
363
|
1 |
|
$evento = $evento->toArray(); |
|
364
|
8 |
|
} elseif (!is_array($evento)) { |
|
365
|
1 |
|
return $this; |
|
366
|
|
|
} |
|
367
|
8 |
|
parent::fromArray($evento); |
|
368
|
8 |
|
if (isset($evento['id'])) { |
|
369
|
1 |
|
$this->setID($evento['id']); |
|
370
|
1 |
|
} else { |
|
371
|
8 |
|
$this->setID(null); |
|
372
|
|
|
} |
|
373
|
8 |
|
if (isset($evento['orgao'])) { |
|
374
|
1 |
|
$this->setOrgao($evento['orgao']); |
|
375
|
1 |
|
} else { |
|
376
|
8 |
|
$this->setOrgao(null); |
|
377
|
|
|
} |
|
378
|
8 |
|
if (isset($evento['identificador'])) { |
|
379
|
1 |
|
$this->setIdentificador($evento['identificador']); |
|
380
|
1 |
|
} else { |
|
381
|
8 |
|
$this->setIdentificador(null); |
|
382
|
|
|
} |
|
383
|
8 |
|
if (isset($evento['chave'])) { |
|
384
|
1 |
|
$this->setChave($evento['chave']); |
|
385
|
1 |
|
} else { |
|
386
|
8 |
|
$this->setChave(null); |
|
387
|
|
|
} |
|
388
|
8 |
|
if (isset($evento['data'])) { |
|
389
|
1 |
|
$this->setData($evento['data']); |
|
390
|
1 |
|
} else { |
|
391
|
8 |
|
$this->setData(null); |
|
392
|
|
|
} |
|
393
|
8 |
|
if (!isset($evento['tipo']) || is_null($evento['tipo'])) { |
|
394
|
8 |
|
$this->setTipo(self::TIPO_CANCELAMENTO); |
|
395
|
8 |
|
} else { |
|
396
|
1 |
|
$this->setTipo($evento['tipo']); |
|
397
|
|
|
} |
|
398
|
8 |
|
if (!isset($evento['sequencia']) || is_null($evento['sequencia'])) { |
|
399
|
8 |
|
$this->setSequencia(1); |
|
400
|
8 |
|
} else { |
|
401
|
1 |
|
$this->setSequencia($evento['sequencia']); |
|
402
|
|
|
} |
|
403
|
8 |
|
if (!isset($evento['descricao']) || is_null($evento['descricao'])) { |
|
404
|
8 |
|
$this->setDescricao('Cancelamento'); |
|
405
|
8 |
|
} else { |
|
406
|
1 |
|
$this->setDescricao($evento['descricao']); |
|
407
|
|
|
} |
|
408
|
8 |
|
if (isset($evento['numero'])) { |
|
409
|
1 |
|
$this->setNumero($evento['numero']); |
|
410
|
1 |
|
} else { |
|
411
|
8 |
|
$this->setNumero(null); |
|
412
|
|
|
} |
|
413
|
8 |
|
if (isset($evento['justificativa'])) { |
|
414
|
1 |
|
$this->setJustificativa($evento['justificativa']); |
|
415
|
1 |
|
} else { |
|
416
|
8 |
|
$this->setJustificativa(null); |
|
417
|
|
|
} |
|
418
|
8 |
|
if (isset($evento['email'])) { |
|
419
|
|
|
$this->setEmail($evento['email']); |
|
420
|
|
|
} else { |
|
421
|
8 |
|
$this->setEmail(null); |
|
422
|
|
|
} |
|
423
|
8 |
|
if (isset($evento['modelo'])) { |
|
424
|
1 |
|
$this->setModelo($evento['modelo']); |
|
425
|
1 |
|
} else { |
|
426
|
8 |
|
$this->setModelo(null); |
|
427
|
|
|
} |
|
428
|
8 |
|
if (isset($evento['informacao'])) { |
|
429
|
1 |
|
$this->setInformacao($evento['informacao']); |
|
430
|
1 |
|
} else { |
|
431
|
8 |
|
$this->setInformacao(null); |
|
432
|
|
|
} |
|
433
|
8 |
|
return $this; |
|
434
|
|
|
} |
|
435
|
|
|
|
|
436
|
|
|
/** |
|
437
|
|
|
* Gera o ID, a regra de formação do Id é: "ID" + |
|
438
|
|
|
* tpEvento + chave da NF-e + nSeqEvento |
|
439
|
|
|
*/ |
|
440
|
6 |
|
public function gerarID() |
|
441
|
|
|
{ |
|
442
|
6 |
|
$id = sprintf( |
|
443
|
6 |
|
'%s%s%02d', |
|
444
|
6 |
|
$this->getTipo(true), |
|
445
|
6 |
|
$this->getChave(true), |
|
446
|
6 |
|
$this->getSequencia(true) |
|
447
|
6 |
|
); |
|
448
|
6 |
|
return $id; |
|
449
|
|
|
} |
|
450
|
|
|
|
|
451
|
6 |
|
public function getNode($name = null) |
|
452
|
|
|
{ |
|
453
|
6 |
|
$this->setID($this->gerarID()); |
|
454
|
|
|
|
|
455
|
6 |
|
$dom = new \DOMDocument('1.0', 'UTF-8'); |
|
456
|
6 |
|
$element = $dom->createElement(is_null($name)?'evento':$name); |
|
457
|
6 |
|
$element->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns', Nota::PORTAL); |
|
458
|
6 |
|
$versao = $dom->createAttribute('versao'); |
|
459
|
6 |
|
$versao->value = self::VERSAO; |
|
460
|
6 |
|
$element->appendChild($versao); |
|
461
|
|
|
|
|
462
|
6 |
|
$info = $dom->createElement('infEvento'); |
|
463
|
6 |
|
$dom = $element->ownerDocument; |
|
464
|
6 |
|
$id = $dom->createAttribute('Id'); |
|
465
|
6 |
|
$id->value = $this->getID(true); |
|
466
|
6 |
|
$info->appendChild($id); |
|
467
|
|
|
|
|
468
|
6 |
|
Util::appendNode($info, 'cOrgao', $this->getOrgao(true)); |
|
469
|
6 |
|
Util::appendNode($info, 'tpAmb', $this->getAmbiente(true)); |
|
470
|
6 |
|
if ($this->isCNPJ()) { |
|
471
|
6 |
|
Util::appendNode($info, 'CNPJ', $this->getIdentificador(true)); |
|
472
|
6 |
|
} else { |
|
473
|
4 |
|
Util::appendNode($info, 'CPF', $this->getIdentificador(true)); |
|
474
|
|
|
} |
|
475
|
6 |
|
Util::appendNode($info, 'chNFe', $this->getChave(true)); |
|
476
|
6 |
|
Util::appendNode($info, 'dhEvento', $this->getData(true)); |
|
477
|
6 |
|
Util::appendNode($info, 'tpEvento', $this->getTipo(true)); |
|
478
|
6 |
|
Util::appendNode($info, 'nSeqEvento', $this->getSequencia(true)); |
|
479
|
6 |
|
Util::appendNode($info, 'verEvento', self::VERSAO); |
|
480
|
|
|
|
|
481
|
6 |
|
$detalhes = $dom->createElement('detEvento'); |
|
482
|
6 |
|
$versao = $dom->createAttribute('versao'); |
|
483
|
6 |
|
$versao->value = self::VERSAO; |
|
484
|
6 |
|
$detalhes->appendChild($versao); |
|
485
|
|
|
|
|
486
|
6 |
|
Util::appendNode($detalhes, 'descEvento', $this->getDescricao(true)); |
|
487
|
6 |
|
Util::appendNode($detalhes, 'nProt', $this->getNumero(true)); |
|
488
|
6 |
|
Util::appendNode($detalhes, 'xJust', $this->getJustificativa(true)); |
|
489
|
6 |
|
$info->appendChild($detalhes); |
|
490
|
|
|
|
|
491
|
6 |
|
$element->appendChild($info); |
|
492
|
6 |
|
$dom->appendChild($element); |
|
493
|
6 |
|
return $element; |
|
494
|
|
|
} |
|
495
|
|
|
|
|
496
|
2 |
|
public function loadNode($element, $name = null) |
|
497
|
|
|
{ |
|
498
|
2 |
|
$root = $element; |
|
499
|
2 |
|
$element = Util::findNode($element, 'evento'); |
|
500
|
2 |
|
$name = is_null($name)?'infEvento':$name; |
|
501
|
2 |
|
$element = Util::findNode($element, $name); |
|
502
|
2 |
|
$this->setOrgao( |
|
503
|
2 |
|
Util::loadNode( |
|
504
|
2 |
|
$element, |
|
505
|
2 |
|
'cOrgao', |
|
506
|
|
|
'Tag "cOrgao" não encontrada no Evento' |
|
507
|
2 |
|
) |
|
508
|
2 |
|
); |
|
509
|
2 |
|
$this->setAmbiente( |
|
510
|
2 |
|
Util::loadNode( |
|
511
|
2 |
|
$element, |
|
512
|
2 |
|
'tpAmb', |
|
513
|
|
|
'Tag "tpAmb" não encontrada no Evento' |
|
514
|
2 |
|
) |
|
515
|
2 |
|
); |
|
516
|
2 |
|
if (Util::nodeExists($element, 'CNPJ')) { |
|
517
|
2 |
|
$this->setIdentificador( |
|
518
|
2 |
|
Util::loadNode( |
|
519
|
2 |
|
$element, |
|
520
|
2 |
|
'CNPJ', |
|
521
|
|
|
'Tag "CNPJ" não encontrada no Evento' |
|
522
|
2 |
|
) |
|
523
|
2 |
|
); |
|
524
|
2 |
|
} else { |
|
525
|
|
|
$this->setIdentificador( |
|
526
|
|
|
Util::loadNode( |
|
527
|
|
|
$element, |
|
528
|
|
|
'CPF', |
|
529
|
|
|
'Tag "CPF" não encontrada no Evento' |
|
530
|
|
|
) |
|
531
|
|
|
); |
|
532
|
|
|
} |
|
533
|
2 |
|
$this->setChave( |
|
534
|
2 |
|
Util::loadNode( |
|
535
|
2 |
|
$element, |
|
536
|
2 |
|
'chNFe', |
|
537
|
|
|
'Tag "chNFe" não encontrada no Evento' |
|
538
|
2 |
|
) |
|
539
|
2 |
|
); |
|
540
|
2 |
|
$this->setData( |
|
541
|
2 |
|
Util::loadNode( |
|
542
|
2 |
|
$element, |
|
543
|
2 |
|
'dhEvento', |
|
544
|
|
|
'Tag "dhEvento" não encontrada no Evento' |
|
545
|
2 |
|
) |
|
546
|
2 |
|
); |
|
547
|
2 |
|
$this->setTipo( |
|
548
|
2 |
|
Util::loadNode( |
|
549
|
2 |
|
$element, |
|
550
|
2 |
|
'tpEvento', |
|
551
|
|
|
'Tag "tpEvento" não encontrada no Evento' |
|
552
|
2 |
|
) |
|
553
|
2 |
|
); |
|
554
|
2 |
|
$this->setSequencia( |
|
555
|
2 |
|
Util::loadNode( |
|
556
|
2 |
|
$element, |
|
557
|
2 |
|
'nSeqEvento', |
|
558
|
|
|
'Tag "nSeqEvento" não encontrada no Evento' |
|
559
|
2 |
|
) |
|
560
|
2 |
|
); |
|
561
|
|
|
|
|
562
|
2 |
|
$detalhes = Util::findNode($element, 'detEvento'); |
|
563
|
2 |
|
$this->setDescricao( |
|
564
|
2 |
|
Util::loadNode( |
|
565
|
2 |
|
$detalhes, |
|
566
|
2 |
|
'descEvento', |
|
567
|
|
|
'Tag "descEvento" não encontrada no Evento' |
|
568
|
2 |
|
) |
|
569
|
2 |
|
); |
|
570
|
2 |
|
$this->setNumero( |
|
571
|
2 |
|
Util::loadNode( |
|
572
|
2 |
|
$detalhes, |
|
573
|
2 |
|
'nProt', |
|
574
|
|
|
'Tag "nProt" não encontrada no Evento' |
|
575
|
2 |
|
) |
|
576
|
2 |
|
); |
|
577
|
2 |
|
$this->setJustificativa( |
|
578
|
2 |
|
Util::loadNode( |
|
579
|
2 |
|
$detalhes, |
|
580
|
2 |
|
'xJust', |
|
581
|
|
|
'Tag "xJust" não encontrada no Evento' |
|
582
|
2 |
|
) |
|
583
|
2 |
|
); |
|
584
|
2 |
|
$informacao = null; |
|
585
|
2 |
|
if (Util::nodeExists($root, 'procEventoNFe')) { |
|
586
|
2 |
|
$informacao = $this->loadResponse($root); |
|
587
|
2 |
|
} |
|
588
|
2 |
|
$this->setInformacao($informacao); |
|
589
|
2 |
|
return $element; |
|
590
|
|
|
} |
|
591
|
|
|
|
|
592
|
5 |
|
public function loadResponse($resp) |
|
593
|
|
|
{ |
|
594
|
5 |
|
$retorno = new Evento(); |
|
595
|
5 |
|
$retorno->loadReturnNode($resp); |
|
596
|
5 |
|
$this->setInformacao($retorno); |
|
597
|
5 |
|
return $retorno; |
|
598
|
|
|
} |
|
599
|
|
|
|
|
600
|
5 |
|
public function loadStatusNode($element, $name = null) |
|
601
|
|
|
{ |
|
602
|
5 |
|
$name = is_null($name)?self::TAG_RETORNO_ENVIO:$name; |
|
603
|
5 |
|
$element = parent::loadNode($element, $name); |
|
|
|
|
|
|
604
|
5 |
|
$this->setOrgao( |
|
605
|
5 |
|
Util::loadNode( |
|
606
|
5 |
|
$element, |
|
607
|
5 |
|
'cOrgao', |
|
608
|
|
|
'Tag "cOrgao" do campo "Orgao" não encontrada' |
|
609
|
5 |
|
) |
|
610
|
5 |
|
); |
|
611
|
5 |
|
return $element; |
|
612
|
|
|
} |
|
613
|
|
|
|
|
614
|
4 |
|
public function getReturnNode() |
|
615
|
|
|
{ |
|
616
|
4 |
|
$outros = parent::getNode('infEvento'); |
|
|
|
|
|
|
617
|
4 |
|
$element = $this->getNode(self::TAG_RETORNO); |
|
618
|
4 |
|
$dom = $element->ownerDocument; |
|
619
|
4 |
|
$element->removeAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns'); |
|
620
|
4 |
|
$info = $dom->getElementsByTagName('infEvento')->item(0); |
|
621
|
4 |
|
$info->removeAttribute('Id'); |
|
622
|
4 |
|
$removeTags = array('detEvento', 'verEvento', 'dhEvento', 'CNPJ', 'CPF', 'cOrgao'); |
|
|
|
|
|
|
623
|
4 |
|
foreach ($removeTags as $key) { |
|
|
|
|
|
|
624
|
4 |
|
$_fields = $info->getElementsByTagName($key); |
|
625
|
4 |
|
if ($_fields->length == 0) { |
|
626
|
4 |
|
continue; |
|
627
|
|
|
} |
|
628
|
4 |
|
$node = $_fields->item(0); |
|
629
|
4 |
|
$info->removeChild($node); |
|
630
|
4 |
|
} |
|
631
|
4 |
|
$chave = $info->getElementsByTagName('chNFe')->item(0); |
|
632
|
4 |
|
foreach ($outros->childNodes as $node) { |
|
633
|
4 |
|
$node = $dom->importNode($node, true); |
|
634
|
4 |
|
$list = $info->getElementsByTagName($node->nodeName); |
|
635
|
4 |
|
if ($list->length == 1) { |
|
636
|
4 |
|
continue; |
|
637
|
|
|
} |
|
638
|
4 |
|
$info->insertBefore($node, $chave); |
|
639
|
4 |
|
} |
|
640
|
4 |
|
$status = $info->getElementsByTagName('cStat')->item(0); |
|
641
|
4 |
|
Util::appendNode($info, 'cOrgao', $this->getOrgao(true), $status); |
|
642
|
4 |
|
$sequencia = $info->getElementsByTagName('nSeqEvento')->item(0); |
|
643
|
4 |
|
Util::appendNode($info, 'xEvento', $this->getDescricao(true), $sequencia); |
|
644
|
4 |
|
if (!is_null($this->getIdentificador())) { |
|
645
|
|
|
if ($this->isCNPJ()) { |
|
646
|
|
|
Util::appendNode($info, 'CNPJDest', $this->getIdentificador(true)); |
|
647
|
|
|
} else { |
|
648
|
|
|
Util::appendNode($info, 'CPFDest', $this->getIdentificador(true)); |
|
649
|
|
|
} |
|
650
|
|
|
} |
|
651
|
4 |
|
if (!is_null($this->getEmail())) { |
|
652
|
|
|
Util::appendNode($info, 'emailDest', $this->getEmail(true)); |
|
653
|
|
|
} |
|
654
|
4 |
|
Util::appendNode($info, 'dhRegEvento', $this->getData(true)); |
|
655
|
4 |
|
Util::appendNode($info, 'nProt', $this->getNumero(true)); |
|
656
|
4 |
|
return $element; |
|
657
|
|
|
} |
|
658
|
|
|
|
|
659
|
5 |
|
public function loadReturnNode($element, $name = null) |
|
660
|
|
|
{ |
|
661
|
5 |
|
$element = Util::findNode($element, Evento::TAG_RETORNO); |
|
662
|
5 |
|
$name = is_null($name)?'infEvento':$name; |
|
663
|
5 |
|
$element = parent::loadNode($element, $name); |
|
|
|
|
|
|
664
|
5 |
|
$this->setOrgao( |
|
665
|
5 |
|
Util::loadNode( |
|
666
|
5 |
|
$element, |
|
667
|
5 |
|
'cOrgao', |
|
668
|
|
|
'Tag "cOrgao" do campo "Orgao" não encontrada' |
|
669
|
5 |
|
) |
|
670
|
5 |
|
); |
|
671
|
5 |
|
$this->setChave(Util::loadNode($element, 'chNFe')); |
|
672
|
5 |
|
$this->setTipo(Util::loadNode($element, 'tpEvento')); |
|
673
|
5 |
|
$this->setDescricao(Util::loadNode($element, 'xEvento')); |
|
674
|
5 |
|
$this->setSequencia(Util::loadNode($element, 'nSeqEvento')); |
|
675
|
5 |
|
if ($element->getElementsByTagName('CNPJDest')->length > 0) { |
|
676
|
|
|
$this->setIdentificador(Util::loadNode($element, 'CNPJDest')); |
|
677
|
|
|
} else { |
|
678
|
5 |
|
$this->setIdentificador(Util::loadNode($element, 'CPFDest')); |
|
679
|
|
|
} |
|
680
|
5 |
|
$this->setEmail(Util::loadNode($element, 'emailDest')); |
|
681
|
5 |
|
$this->setData( |
|
682
|
5 |
|
Util::loadNode( |
|
683
|
5 |
|
$element, |
|
684
|
5 |
|
'dhRegEvento', |
|
685
|
|
|
'Tag "dhRegEvento" do campo "Data" não encontrada' |
|
686
|
5 |
|
) |
|
687
|
5 |
|
); |
|
688
|
5 |
|
$this->setNumero(Util::loadNode($element, 'nProt')); |
|
689
|
5 |
|
return $element; |
|
690
|
|
|
} |
|
691
|
|
|
|
|
692
|
3 |
|
private function getConteudo($dom) |
|
693
|
|
|
{ |
|
694
|
3 |
|
$config = SEFAZ::getInstance()->getConfiguracao(); |
|
|
|
|
|
|
695
|
3 |
|
$dob = new \DOMDocument('1.0', 'UTF-8'); |
|
696
|
3 |
|
$envio = $dob->createElement('envEvento'); |
|
697
|
3 |
|
$envio->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns', Nota::PORTAL); |
|
698
|
3 |
|
$versao = $dob->createAttribute('versao'); |
|
699
|
3 |
|
$versao->value = self::VERSAO; |
|
700
|
3 |
|
$envio->appendChild($versao); |
|
701
|
3 |
|
Util::appendNode($envio, 'idLote', self::genLote()); |
|
702
|
|
|
// Corrige xmlns:default |
|
703
|
|
|
// $data = $dob->importNode($dom->documentElement, true); |
|
704
|
|
|
// $envio->appendChild($data); |
|
705
|
3 |
|
Util::appendNode($envio, 'evento', 0); |
|
706
|
3 |
|
$dob->appendChild($envio); |
|
707
|
|
|
// Corrige xmlns:default |
|
708
|
|
|
// return $dob; |
|
709
|
3 |
|
$xml = $dob->saveXML($dob->documentElement); |
|
710
|
3 |
|
return str_replace('<evento>0</evento>', $dom->saveXML($dom->documentElement), $xml); |
|
711
|
|
|
} |
|
712
|
|
|
|
|
713
|
3 |
|
public function envia($dom) |
|
714
|
|
|
{ |
|
715
|
3 |
|
$envio = new Envio(); |
|
716
|
3 |
|
$envio->setServico(Envio::SERVICO_EVENTO); |
|
717
|
3 |
|
$envio->setAmbiente($this->getAmbiente()); |
|
718
|
3 |
|
$envio->setModelo($this->getModelo()); |
|
719
|
3 |
|
$envio->setEmissao(Nota::EMISSAO_NORMAL); |
|
720
|
3 |
|
$envio->setConteudo($this->getConteudo($dom)); |
|
721
|
3 |
|
$resp = $envio->envia(); |
|
722
|
3 |
|
$this->loadStatusNode($resp); |
|
723
|
3 |
|
if (!$this->isProcessado()) { |
|
724
|
|
|
throw new \Exception($this->getMotivo(), $this->getStatus()); |
|
725
|
|
|
} |
|
726
|
3 |
|
return $this->loadResponse($resp); |
|
727
|
|
|
} |
|
728
|
|
|
|
|
729
|
|
|
/** |
|
730
|
|
|
* Adiciona a informação no XML do evento |
|
731
|
|
|
*/ |
|
732
|
5 |
|
public function addInformacao($dom) |
|
733
|
|
|
{ |
|
734
|
5 |
|
if (is_null($this->getInformacao())) { |
|
735
|
1 |
|
throw new \Exception('A informação não foi informado no evento "'.$this->getID().'"', 404); |
|
736
|
|
|
} |
|
737
|
4 |
|
$evento = $dom->getElementsByTagName('evento')->item(0); |
|
738
|
|
|
// Corrige xmlns:default |
|
739
|
4 |
|
$evento_xml = $dom->saveXML($evento); |
|
740
|
|
|
|
|
741
|
4 |
|
$element = $dom->createElement('procEventoNFe'); |
|
742
|
4 |
|
$element->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns', Nota::PORTAL); |
|
743
|
4 |
|
$versao = $dom->createAttribute('versao'); |
|
744
|
4 |
|
$versao->value = self::VERSAO; |
|
745
|
4 |
|
$element->appendChild($versao); |
|
746
|
4 |
|
$dom->removeChild($evento); |
|
747
|
|
|
// Corrige xmlns:default |
|
748
|
4 |
|
$evento = $dom->createElement('evento', 0); |
|
749
|
|
|
|
|
750
|
4 |
|
$element->appendChild($evento); |
|
751
|
4 |
|
$info = $this->getInformacao()->getReturnNode(); |
|
752
|
4 |
|
$info = $dom->importNode($info, true); |
|
753
|
4 |
|
$element->appendChild($info); |
|
754
|
4 |
|
$dom->appendChild($element); |
|
755
|
|
|
// Corrige xmlns:default |
|
756
|
4 |
|
$xml = $dom->saveXML(); |
|
757
|
4 |
|
$xml = str_replace('<evento>0</evento>', $evento_xml, $xml); |
|
758
|
4 |
|
$dom->loadXML($xml); |
|
759
|
|
|
|
|
760
|
4 |
|
return $dom; |
|
761
|
|
|
} |
|
762
|
|
|
|
|
763
|
|
|
/** |
|
764
|
|
|
* Assina o XML com a assinatura eletrônica do tipo A1 |
|
765
|
|
|
*/ |
|
766
|
6 |
|
public function assinar($dom = null) |
|
767
|
|
|
{ |
|
768
|
6 |
|
if (is_null($dom)) { |
|
769
|
3 |
|
$xml = $this->getNode(); |
|
770
|
3 |
|
$dom = $xml->ownerDocument; |
|
771
|
3 |
|
} |
|
772
|
6 |
|
$config = SEFAZ::getInstance()->getConfiguracao(); |
|
773
|
|
|
|
|
774
|
6 |
|
$adapter = new XmlseclibsAdapter(); |
|
775
|
6 |
|
$adapter->setPrivateKey($config->getChavePrivada()); |
|
776
|
6 |
|
$adapter->setPublicKey($config->getChavePublica()); |
|
777
|
6 |
|
$adapter->addTransform(AdapterInterface::ENVELOPED); |
|
778
|
6 |
|
$adapter->addTransform(AdapterInterface::XML_C14N); |
|
779
|
6 |
|
$adapter->sign($dom, 'infEvento'); |
|
780
|
6 |
|
return $dom; |
|
781
|
|
|
} |
|
782
|
|
|
|
|
783
|
|
|
/** |
|
784
|
|
|
* Valida o documento após assinar |
|
785
|
|
|
*/ |
|
786
|
6 |
|
public function validar($dom) |
|
787
|
|
|
{ |
|
788
|
6 |
|
$dom->loadXML($dom->saveXML()); |
|
789
|
6 |
|
$xsd_path = dirname(__DIR__) . '/Core/schema'; |
|
790
|
6 |
|
$xsd_file = $xsd_path . '/cancelamento/eventoCancNFe_v1.00.xsd'; |
|
791
|
6 |
|
if (!file_exists($xsd_file)) { |
|
792
|
|
|
throw new \Exception('O arquivo "'.$xsd_file.'" de esquema XSD não existe!', 404); |
|
793
|
|
|
} |
|
794
|
|
|
// Enable user error handling |
|
795
|
6 |
|
$save = libxml_use_internal_errors(true); |
|
796
|
6 |
|
if ($dom->schemaValidate($xsd_file)) { |
|
797
|
5 |
|
libxml_use_internal_errors($save); |
|
798
|
5 |
|
return $dom; |
|
799
|
|
|
} |
|
800
|
1 |
|
$msg = array(); |
|
801
|
1 |
|
$errors = libxml_get_errors(); |
|
802
|
1 |
|
foreach ($errors as $error) { |
|
803
|
1 |
|
$msg[] = 'Não foi possível validar o XML: '.$error->message; |
|
804
|
1 |
|
} |
|
805
|
1 |
|
libxml_clear_errors(); |
|
806
|
1 |
|
libxml_use_internal_errors($save); |
|
807
|
1 |
|
throw new ValidationException($msg); |
|
808
|
|
|
} |
|
809
|
|
|
} |
|
810
|
|
|
|
The class complexity is the sum of the complexity of all methods. A very high value is usually an indication that your class does not follow the single reponsibility principle and does more than one job.
Some resources for further reading:
You can also find more detailed suggestions for refactoring in the “Code” section of your repository.