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