|
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\Common; |
|
29
|
|
|
|
|
30
|
|
|
use NFe\Task\Tarefa; |
|
31
|
|
|
use NFe\Core\Nota; |
|
32
|
|
|
use FR3D\XmlDSig\Adapter\XmlseclibsAdapter; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Configurações padrão para emissão de nota fiscal |
|
36
|
|
|
*/ |
|
37
|
|
|
class Ajuste extends Configuracao implements Evento |
|
|
|
|
|
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* Caminho onde será salvo os XMLs |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
private $pasta_xml_base; |
|
44
|
|
|
/** |
|
45
|
|
|
* Subpasta onde será salvo os XMLs das numerações inutilizadas |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
private $pasta_xml_inutilizado; |
|
49
|
|
|
/** |
|
50
|
|
|
* Subpasta onde será salvo os XMLs dos eventos de cancelamentos |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
private $pasta_xml_cancelado; |
|
54
|
|
|
/** |
|
55
|
|
|
* Subpasta onde será salvo os XMLs das notas pendentes de envio |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
private $pasta_xml_pendente; |
|
59
|
|
|
/** |
|
60
|
|
|
* Subpasta onde será salvo os XMLs das notas denegadas pela SEFAZ |
|
61
|
|
|
* @var string |
|
62
|
|
|
*/ |
|
63
|
|
|
private $pasta_xml_denegado; |
|
64
|
|
|
/** |
|
65
|
|
|
* Subpasta onde será salvo os XMLs das notas rejeitadas |
|
66
|
|
|
* @var string |
|
67
|
|
|
*/ |
|
68
|
|
|
private $pasta_xml_rejeitado; |
|
69
|
|
|
/** |
|
70
|
|
|
* Subpasta onde será salvo os XMLs das notas autorizadas |
|
71
|
|
|
* @var string |
|
72
|
|
|
*/ |
|
73
|
|
|
private $pasta_xml_autorizado; |
|
74
|
|
|
/** |
|
75
|
|
|
* Subpasta onde será salvo os XMLs das notas em processamento na SEFAZ |
|
76
|
|
|
* @var string |
|
77
|
|
|
*/ |
|
78
|
|
|
private $pasta_xml_processamento; |
|
79
|
|
|
/** |
|
80
|
|
|
* Subpasta onde será salvo os XMLs das notas assinadas com certificado digital |
|
81
|
|
|
* @var string |
|
82
|
|
|
*/ |
|
83
|
|
|
private $pasta_xml_assinado; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param mixed $ajuste array ou intância |
|
87
|
|
|
*/ |
|
88
|
102 |
|
public function __construct($ajuste = []) |
|
89
|
|
|
{ |
|
90
|
102 |
|
parent::__construct($ajuste); |
|
91
|
102 |
|
$this->setEvento($this); |
|
92
|
102 |
|
$this->setTempoLimite(30); |
|
93
|
102 |
|
$root_path = dirname(dirname(dirname(__DIR__))); |
|
94
|
102 |
|
$cert_dir = $root_path . '/storage/certs'; |
|
95
|
102 |
|
$this->setArquivoChavePublica($cert_dir . '/public.pem'); |
|
|
|
|
|
|
96
|
102 |
|
$this->setArquivoChavePrivada($cert_dir . '/private.pem'); |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
102 |
|
$this->setPastaXmlBase($root_path . '/storage/xml'); |
|
99
|
102 |
|
$this->setPastaXmlInutilizado('{ambiente}/inutilizado'); |
|
100
|
102 |
|
$this->setPastaXmlCancelado('{ambiente}/cancelado'); |
|
101
|
102 |
|
$this->setPastaXmlPendente('{ambiente}/pendente'); |
|
102
|
102 |
|
$this->setPastaXmlDenegado('{ambiente}/denegado'); |
|
103
|
102 |
|
$this->setPastaXmlRejeitado('{ambiente}/rejeitado'); |
|
104
|
102 |
|
$this->setPastaXmlAutorizado('{ambiente}/autorizado'); |
|
105
|
102 |
|
$this->setPastaXmlProcessamento('{ambiente}/processamento'); |
|
106
|
102 |
|
$this->setPastaXmlAssinado('{ambiente}/assinado'); |
|
107
|
102 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Caminho da pasta base para armazenamento dos XML |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
5 |
|
public function getPastaXmlBase() |
|
114
|
|
|
{ |
|
115
|
5 |
|
return $this->pasta_xml_base; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param string $pasta_xml_base |
|
120
|
|
|
* @return self |
|
121
|
|
|
*/ |
|
122
|
102 |
|
public function setPastaXmlBase($pasta_xml_base) |
|
123
|
|
|
{ |
|
124
|
102 |
|
$this->pasta_xml_base = $pasta_xml_base; |
|
125
|
102 |
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Pasta onde ficam os XML das inutilizações de números de notas |
|
130
|
|
|
* @param string $ambiente |
|
131
|
|
|
* @param string $caminho |
|
132
|
|
|
* @return string |
|
133
|
|
|
*/ |
|
134
|
3 |
|
protected function aplicaAmbiente($ambiente, $caminho) |
|
135
|
|
|
{ |
|
136
|
|
|
switch ($ambiente) { |
|
137
|
3 |
|
case '1': |
|
138
|
|
|
$ambiente = Nota::AMBIENTE_PRODUCAO; |
|
139
|
|
|
break; |
|
140
|
3 |
|
case '2': |
|
141
|
|
|
$ambiente = Nota::AMBIENTE_HOMOLOGACAO; |
|
142
|
|
|
break; |
|
143
|
|
|
} |
|
144
|
3 |
|
$path = rtrim($this->getPastaXmlBase(), '/') . '/' . ltrim($caminho, '/'); |
|
145
|
3 |
|
return rtrim(str_replace('{ambiente}', $ambiente, $path), '/'); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Exclui os arquivos XML desnecessários |
|
150
|
|
|
* @param \NFe\Core\Nota $nota nota |
|
151
|
|
|
*/ |
|
152
|
2 |
|
protected function deleteXmlAnteriores($nota) |
|
153
|
|
|
{ |
|
154
|
2 |
|
$filename = $this->getPastaXmlRejeitado($nota->getAmbiente()) . '/' . $nota->getID() . '.xml'; |
|
155
|
2 |
|
if (file_exists($filename)) { |
|
156
|
|
|
unlink($filename); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
2 |
|
$filename = $this->getPastaXmlProcessamento($nota->getAmbiente()) . '/' . $nota->getID() . '.xml'; |
|
160
|
2 |
|
if (file_exists($filename)) { |
|
161
|
|
|
unlink($filename); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
2 |
|
$filename = $this->getPastaXmlPendente($nota->getAmbiente()) . '/' . $nota->getID() . '.xml'; |
|
165
|
2 |
|
if (file_exists($filename)) { |
|
166
|
1 |
|
unlink($filename); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
2 |
|
$filename = $this->getPastaXmlAssinado($nota->getAmbiente()) . '/' . $nota->getID() . '.xml'; |
|
170
|
2 |
|
if (file_exists($filename)) { |
|
171
|
2 |
|
unlink($filename); |
|
172
|
|
|
} |
|
173
|
2 |
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Pasta onde ficam os XML das inutilizações de números de notas |
|
177
|
|
|
* @param string $ambiente |
|
178
|
|
|
* @return string |
|
179
|
|
|
*/ |
|
180
|
3 |
|
public function getPastaXmlInutilizado($ambiente = null) |
|
181
|
|
|
{ |
|
182
|
3 |
|
if (is_null($ambiente)) { |
|
183
|
2 |
|
return $this->pasta_xml_inutilizado; |
|
184
|
|
|
} |
|
185
|
1 |
|
return $this->aplicaAmbiente($ambiente, $this->pasta_xml_inutilizado); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @param string $pasta_xml_inutilizado |
|
190
|
|
|
* @return self |
|
191
|
|
|
*/ |
|
192
|
102 |
|
public function setPastaXmlInutilizado($pasta_xml_inutilizado) |
|
|
|
|
|
|
193
|
|
|
{ |
|
194
|
102 |
|
$this->pasta_xml_inutilizado = $pasta_xml_inutilizado; |
|
195
|
102 |
|
return $this; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Pasta onde ficam os XML das notas após serem aceitas e depois canceladas |
|
200
|
|
|
* @param string $ambiente |
|
201
|
|
|
* @return string |
|
202
|
|
|
*/ |
|
203
|
2 |
|
public function getPastaXmlCancelado($ambiente = null) |
|
204
|
|
|
{ |
|
205
|
2 |
|
if (is_null($ambiente)) { |
|
206
|
2 |
|
return $this->pasta_xml_cancelado; |
|
207
|
|
|
} |
|
208
|
|
|
return $this->aplicaAmbiente($ambiente, $this->pasta_xml_cancelado); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @param string $pasta_xml_cancelado |
|
213
|
|
|
* @return self |
|
214
|
|
|
*/ |
|
215
|
102 |
|
public function setPastaXmlCancelado($pasta_xml_cancelado) |
|
216
|
|
|
{ |
|
217
|
102 |
|
$this->pasta_xml_cancelado = $pasta_xml_cancelado; |
|
218
|
102 |
|
return $this; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Pasta onde ficam os XML das notas pendentes de consulta |
|
223
|
|
|
* @param string $ambiente |
|
224
|
|
|
* @return string |
|
225
|
|
|
*/ |
|
226
|
4 |
|
public function getPastaXmlPendente($ambiente = null) |
|
227
|
|
|
{ |
|
228
|
4 |
|
if (is_null($ambiente)) { |
|
229
|
2 |
|
return $this->pasta_xml_pendente; |
|
230
|
|
|
} |
|
231
|
2 |
|
return $this->aplicaAmbiente($ambiente, $this->pasta_xml_pendente); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* @param string $pasta_xml_pendente |
|
236
|
|
|
* @return self |
|
237
|
|
|
*/ |
|
238
|
102 |
|
public function setPastaXmlPendente($pasta_xml_pendente) |
|
239
|
|
|
{ |
|
240
|
102 |
|
$this->pasta_xml_pendente = $pasta_xml_pendente; |
|
241
|
102 |
|
return $this; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Pasta onde ficam os XMLs após enviados e denegados |
|
246
|
|
|
* @param string $ambiente |
|
247
|
|
|
* @return string |
|
248
|
|
|
*/ |
|
249
|
2 |
|
public function getPastaXmlDenegado($ambiente = null) |
|
250
|
|
|
{ |
|
251
|
2 |
|
if (is_null($ambiente)) { |
|
252
|
2 |
|
return $this->pasta_xml_denegado; |
|
253
|
|
|
} |
|
254
|
|
|
return $this->aplicaAmbiente($ambiente, $this->pasta_xml_denegado); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* @param string $pasta_xml_denegado |
|
259
|
|
|
* @return self |
|
260
|
|
|
*/ |
|
261
|
102 |
|
public function setPastaXmlDenegado($pasta_xml_denegado) |
|
262
|
|
|
{ |
|
263
|
102 |
|
$this->pasta_xml_denegado = $pasta_xml_denegado; |
|
264
|
102 |
|
return $this; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* Pasta onde ficam os XML das notas após serem enviadas e rejeitadas |
|
269
|
|
|
* @param string $ambiente |
|
270
|
|
|
* @return string |
|
271
|
|
|
*/ |
|
272
|
4 |
|
public function getPastaXmlRejeitado($ambiente = null) |
|
273
|
|
|
{ |
|
274
|
4 |
|
if (is_null($ambiente)) { |
|
275
|
2 |
|
return $this->pasta_xml_rejeitado; |
|
276
|
|
|
} |
|
277
|
2 |
|
return $this->aplicaAmbiente($ambiente, $this->pasta_xml_rejeitado); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* @param string $pasta_xml_rejeitado |
|
282
|
|
|
* @return self |
|
283
|
|
|
*/ |
|
284
|
102 |
|
public function setPastaXmlRejeitado($pasta_xml_rejeitado) |
|
285
|
|
|
{ |
|
286
|
102 |
|
$this->pasta_xml_rejeitado = $pasta_xml_rejeitado; |
|
287
|
102 |
|
return $this; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* Pasta onde ficam os XML das notas após serem enviados e aceitos pela |
|
292
|
|
|
* @param string $ambiente |
|
293
|
|
|
* @return string |
|
294
|
|
|
* SEFAZ |
|
295
|
|
|
*/ |
|
296
|
3 |
|
public function getPastaXmlAutorizado($ambiente = null) |
|
297
|
|
|
{ |
|
298
|
3 |
|
if (is_null($ambiente)) { |
|
299
|
2 |
|
return $this->pasta_xml_autorizado; |
|
300
|
|
|
} |
|
301
|
1 |
|
return $this->aplicaAmbiente($ambiente, $this->pasta_xml_autorizado); |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* @param string $pasta_xml_autorizado |
|
306
|
|
|
* @return self |
|
307
|
|
|
*/ |
|
308
|
102 |
|
public function setPastaXmlAutorizado($pasta_xml_autorizado) |
|
309
|
|
|
{ |
|
310
|
102 |
|
$this->pasta_xml_autorizado = $pasta_xml_autorizado; |
|
311
|
102 |
|
return $this; |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
/** |
|
315
|
|
|
* Pasta onde ficam os XML das notas em processamento de retorno de |
|
316
|
|
|
* @param string $ambiente |
|
317
|
|
|
* @return string |
|
318
|
|
|
* autorização |
|
319
|
|
|
*/ |
|
320
|
4 |
|
public function getPastaXmlProcessamento($ambiente = null) |
|
321
|
|
|
{ |
|
322
|
4 |
|
if (is_null($ambiente)) { |
|
323
|
2 |
|
return $this->pasta_xml_processamento; |
|
324
|
|
|
} |
|
325
|
2 |
|
return $this->aplicaAmbiente($ambiente, $this->pasta_xml_processamento); |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
/** |
|
329
|
|
|
* @param string $pasta_xml_processamento |
|
330
|
|
|
* @return self |
|
331
|
|
|
*/ |
|
332
|
102 |
|
public function setPastaXmlProcessamento($pasta_xml_processamento) |
|
|
|
|
|
|
333
|
|
|
{ |
|
334
|
102 |
|
$this->pasta_xml_processamento = $pasta_xml_processamento; |
|
335
|
102 |
|
return $this; |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
/** |
|
339
|
|
|
* Pasta onde ficam os XMLs após assinado e antes de serem enviados |
|
340
|
|
|
* @param string $ambiente |
|
341
|
|
|
* @return string |
|
342
|
|
|
*/ |
|
343
|
4 |
|
public function getPastaXmlAssinado($ambiente = null) |
|
344
|
|
|
{ |
|
345
|
4 |
|
if (is_null($ambiente)) { |
|
346
|
2 |
|
return $this->pasta_xml_assinado; |
|
347
|
|
|
} |
|
348
|
2 |
|
return $this->aplicaAmbiente($ambiente, $this->pasta_xml_assinado); |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
/** |
|
352
|
|
|
* @param string $pasta_xml_assinado |
|
353
|
|
|
* @return self |
|
354
|
|
|
*/ |
|
355
|
102 |
|
public function setPastaXmlAssinado($pasta_xml_assinado) |
|
356
|
|
|
{ |
|
357
|
102 |
|
$this->pasta_xml_assinado = $pasta_xml_assinado; |
|
358
|
102 |
|
return $this; |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
|
|
/** |
|
362
|
|
|
* Chamado quando o XML da nota foi gerado, |
|
363
|
|
|
* aqui pode ser atualizado a chave da nota, data de emissão, além do estado da nota |
|
364
|
|
|
* O registro da nota pode ser encontrada pela chave ou pelo código |
|
365
|
|
|
* @param \NFe\Core\Nota $nota |
|
366
|
|
|
* @param \DOMDocument $xml |
|
367
|
|
|
*/ |
|
368
|
2 |
|
public function onNotaGerada($nota, $xml) |
|
369
|
|
|
{ |
|
370
|
2 |
|
} |
|
371
|
|
|
|
|
372
|
|
|
/** |
|
373
|
|
|
* Chamado após o XML da nota ser assinado |
|
374
|
|
|
* @param \NFe\Core\Nota $nota |
|
375
|
|
|
* @param \DOMDocument $xml |
|
376
|
|
|
*/ |
|
377
|
2 |
|
public function onNotaAssinada($nota, $xml) |
|
378
|
|
|
{ |
|
379
|
2 |
|
} |
|
380
|
|
|
|
|
381
|
|
|
/** |
|
382
|
|
|
* Chamado após o XML da nota ser validado com sucesso |
|
383
|
|
|
* Nesse ponto pode ser salvo o QR Code, URL de consulta, tributos e complementos da nota |
|
384
|
|
|
* além de atualizar o estado da nota para assinado |
|
385
|
|
|
* @param \NFe\Core\Nota $nota |
|
386
|
|
|
* @param \DOMDocument $xml |
|
387
|
|
|
*/ |
|
388
|
2 |
|
public function onNotaValidada($nota, $xml) |
|
389
|
|
|
{ |
|
390
|
2 |
|
$this->deleteXmlAnteriores($nota); |
|
391
|
2 |
|
$filename = $this->getPastaXmlAssinado($nota->getAmbiente()) . '/' . $nota->getID() . '.xml'; |
|
392
|
2 |
|
Util::createDirectory(dirname($filename)); |
|
393
|
2 |
|
file_put_contents($filename, $xml->saveXML()); |
|
394
|
2 |
|
} |
|
395
|
|
|
|
|
396
|
|
|
/** |
|
397
|
|
|
* Chamado antes de enviar a nota para a SEFAZ |
|
398
|
|
|
* @param \NFe\Core\Nota $nota |
|
399
|
|
|
* @param \DOMDocument $xml |
|
400
|
|
|
*/ |
|
401
|
2 |
|
public function onNotaEnviando($nota, $xml) |
|
402
|
|
|
{ |
|
403
|
2 |
|
} |
|
404
|
|
|
|
|
405
|
|
|
/** |
|
406
|
|
|
* Chamado quando a forma de emissão da nota fiscal muda para contingência |
|
407
|
|
|
* Atualizar no banco flag de contingência informando a data de contingência e motivo |
|
408
|
|
|
* @param \NFe\Core\Nota $nota |
|
409
|
|
|
* @param bool $offline |
|
410
|
|
|
* @param \Exception $exception |
|
411
|
|
|
*/ |
|
412
|
1 |
|
public function onNotaContingencia($nota, $offline, $exception) |
|
413
|
|
|
{ |
|
414
|
1 |
|
$this->deleteXmlAnteriores($nota); |
|
415
|
1 |
|
} |
|
416
|
|
|
|
|
417
|
|
|
/** |
|
418
|
|
|
* Chamado quando a nota foi enviada e aceita pela SEFAZ |
|
419
|
|
|
* Além de salvar o XML, a data de autorização, protocolo e estado devem ser salvos no banco |
|
420
|
|
|
* Nesse ponto essa nota deve ser marcada como concluída no banco de dados |
|
421
|
|
|
* Atenção: se a ação dessa nota for para cancelar, não troque a flag de conclusão |
|
422
|
|
|
* @param \NFe\Core\Nota $nota |
|
423
|
|
|
* @param \DOMDocument $xml |
|
424
|
|
|
* @param \NFe\Task\Retorno $retorno |
|
425
|
|
|
*/ |
|
426
|
1 |
|
public function onNotaAutorizada($nota, $xml, $retorno) |
|
427
|
|
|
{ |
|
428
|
1 |
|
$this->deleteXmlAnteriores($nota); |
|
429
|
1 |
|
$filename = $this->getPastaXmlAutorizado($nota->getAmbiente()) . '/' . $nota->getID() . '.xml'; |
|
430
|
1 |
|
Util::createDirectory(dirname($filename)); |
|
431
|
1 |
|
file_put_contents($filename, $xml->saveXML()); |
|
432
|
1 |
|
} |
|
433
|
|
|
|
|
434
|
|
|
/** |
|
435
|
|
|
* Chamado quando a emissão da nota foi concluída com sucesso independente |
|
436
|
|
|
* da forma de emissão |
|
437
|
|
|
* @param \NFe\Core\Nota $nota |
|
438
|
|
|
* @param \DOMDocument $xml |
|
439
|
|
|
*/ |
|
440
|
2 |
|
public function onNotaCompleto($nota, $xml) |
|
441
|
|
|
{ |
|
442
|
2 |
|
} |
|
443
|
|
|
|
|
444
|
|
|
/** |
|
445
|
|
|
* Chamado quando uma nota é rejeitada pela SEFAZ, a nota deve ser |
|
446
|
|
|
* corrigida para depois ser enviada novamente |
|
447
|
|
|
* Se a ação da nota for cancelar, deve-se mudar a ação para inutilizar se for rejeição de nota inexistente |
|
448
|
|
|
* Caso contrário, deve marcar a flag para corrigir a nota antes de enviar novamente |
|
449
|
|
|
* Além de marcar o estado como rejeitado |
|
450
|
|
|
* @param \NFe\Core\Nota $nota |
|
451
|
|
|
* @param \DOMDocument $xml |
|
452
|
|
|
* @param \NFe\Task\Retorno $retorno |
|
453
|
|
|
*/ |
|
454
|
|
|
public function onNotaRejeitada($nota, $xml, $retorno) |
|
455
|
|
|
{ |
|
456
|
|
|
$this->deleteXmlAnteriores($nota); |
|
457
|
|
|
$filename = $this->getPastaXmlRejeitado($nota->getAmbiente()) . '/' . $nota->getID() . '.xml'; |
|
458
|
|
|
Util::createDirectory(dirname($filename)); |
|
459
|
|
|
file_put_contents($filename, $xml->saveXML()); |
|
460
|
|
|
} |
|
461
|
|
|
|
|
462
|
|
|
/** |
|
463
|
|
|
* Chamado quando a nota é denegada e não pode ser utilizada (outra nota |
|
464
|
|
|
* deve ser gerada) |
|
465
|
|
|
* Atualizar o estado da nota e marcar como concluída mas não corrigida |
|
466
|
|
|
* @param \NFe\Core\Nota $nota |
|
467
|
|
|
* @param \DOMDocument $xml |
|
468
|
|
|
* @param \NFe\Task\Retorno $retorno |
|
469
|
|
|
*/ |
|
470
|
|
|
public function onNotaDenegada($nota, $xml, $retorno) |
|
471
|
|
|
{ |
|
472
|
|
|
$this->deleteXmlAnteriores($nota); |
|
473
|
|
|
$filename = $this->getPastaXmlDenegado($nota->getAmbiente()) . '/' . $nota->getID() . '.xml'; |
|
474
|
|
|
Util::createDirectory(dirname($filename)); |
|
475
|
|
|
file_put_contents($filename, $xml->saveXML()); |
|
476
|
|
|
} |
|
477
|
|
|
|
|
478
|
|
|
/** |
|
479
|
|
|
* Chamado após tentar enviar uma nota e não ter certeza se ela foi |
|
480
|
|
|
* recebida ou não (problemas técnicos), deverá ser feito uma consulta pela |
|
481
|
|
|
* chave para obter o estado da nota, |
|
482
|
|
|
* Aqui deve ser cancelada a nota incerta e gerar outra em contingência |
|
483
|
|
|
* Atenção: Nota em contingência pode ficar pendente também, nesse caso não se deve criar outra nota |
|
484
|
|
|
* Atualizar a nota incerta mudando a ação para cancelar e status para pendente |
|
485
|
|
|
* Criar outra nota baseado na nota incerta incrementando sua numeração para uma disponível |
|
486
|
|
|
* Atualizar o número da $nota e seu ID com a função gerarID, salvar também a chave na nova nota |
|
487
|
|
|
* @param \NFe\Core\Nota $nota |
|
488
|
|
|
* @param \DOMDocument $xml |
|
489
|
|
|
* @param \Exception $exception |
|
490
|
|
|
*/ |
|
491
|
1 |
|
public function onNotaPendente($nota, $xml, $exception) |
|
492
|
|
|
{ |
|
493
|
1 |
|
$this->deleteXmlAnteriores($nota); |
|
494
|
1 |
|
$filename = $this->getPastaXmlPendente($nota->getAmbiente()) . '/' . $nota->getID() . '.xml'; |
|
495
|
1 |
|
Util::createDirectory(dirname($filename)); |
|
496
|
1 |
|
file_put_contents($filename, $xml->saveXML()); |
|
497
|
1 |
|
} |
|
498
|
|
|
|
|
499
|
|
|
/** |
|
500
|
|
|
* Chamado quando uma nota é enviada, mas não retornou o protocolo que será |
|
501
|
|
|
* consultado mais tarde |
|
502
|
|
|
* Salvar o recibo na nota para consultar mais tarde além atualizar o status |
|
503
|
|
|
* @param \NFe\Core\Nota $nota |
|
504
|
|
|
* @param \DOMDocument $xml |
|
505
|
|
|
* @param \NFe\Task\Retorno $retorno |
|
506
|
|
|
*/ |
|
507
|
|
|
public function onNotaProcessando($nota, $xml, $retorno) |
|
508
|
|
|
{ |
|
509
|
|
|
$this->deleteXmlAnteriores($nota); |
|
510
|
|
|
$filename = $this->getPastaXmlProcessamento($nota->getAmbiente()) . '/' . $nota->getID() . '.xml'; |
|
511
|
|
|
Util::createDirectory(dirname($filename)); |
|
512
|
|
|
file_put_contents($filename, $xml->saveXML()); |
|
513
|
|
|
} |
|
514
|
|
|
|
|
515
|
|
|
/** |
|
516
|
|
|
* Chamado quando uma nota autorizada é cancelada na SEFAZ |
|
517
|
|
|
* Atualizar o protocolo do retorno, estado e flag de conclusão da nota |
|
518
|
|
|
* @param \NFe\Core\Nota $nota |
|
519
|
|
|
* @param \DOMDocument $xml |
|
520
|
|
|
* @param \NFe\Task\Retorno $retorno |
|
521
|
|
|
*/ |
|
522
|
|
|
public function onNotaCancelada($nota, $xml, $retorno) |
|
523
|
|
|
{ |
|
524
|
|
|
$this->deleteXmlAnteriores($nota); |
|
525
|
|
|
$filename = $this->getPastaXmlCancelado($nota->getAmbiente()) . '/' . $nota->getID() . '.xml'; |
|
526
|
|
|
Util::createDirectory(dirname($filename)); |
|
527
|
|
|
file_put_contents($filename, $xml->saveXML()); |
|
528
|
|
|
} |
|
529
|
|
|
|
|
530
|
|
|
/** |
|
531
|
|
|
* Chamado quando ocorre um erro nas etapas de geração e envio da nota (Não |
|
532
|
|
|
* é chamado quando entra em contigência) |
|
533
|
|
|
* Alterar a flag para corrigir a nota se não for problema de rede |
|
534
|
|
|
* @param \NFe\Core\Nota $nota |
|
535
|
|
|
* @param \Exception $exception |
|
536
|
|
|
*/ |
|
537
|
|
|
public function onNotaErro($nota, $exception) |
|
538
|
|
|
{ |
|
539
|
|
|
} |
|
540
|
|
|
|
|
541
|
|
|
/** |
|
542
|
|
|
* Chamado quando um ou mais números de notas forem inutilizados |
|
543
|
|
|
* @param \NFe\Task\Inutilizacao $inutilizacao |
|
544
|
|
|
* @param \DOMDocument $xml |
|
545
|
|
|
*/ |
|
546
|
1 |
|
public function onInutilizado($inutilizacao, $xml) |
|
547
|
|
|
{ |
|
548
|
1 |
|
$filename = $this->getPastaXmlInutilizado($inutilizacao->getAmbiente()) . '/' . $inutilizacao->getID() . '.xml'; |
|
549
|
1 |
|
Util::createDirectory(dirname($filename)); |
|
550
|
1 |
|
file_put_contents($filename, $xml->saveXML()); |
|
551
|
1 |
|
} |
|
552
|
|
|
|
|
553
|
|
|
/** |
|
554
|
|
|
* Chamado quando uma tarefa é executada com sucesso |
|
555
|
|
|
* @param \NFe\Task\Tarefa $tarefa |
|
556
|
|
|
* @param \NFe\Task\Retorno $retorno |
|
557
|
|
|
*/ |
|
558
|
1 |
|
public function onTarefaExecutada($tarefa, $retorno) |
|
559
|
|
|
{ |
|
560
|
|
|
// não precisa implementar, pois a consulta já processa a nota internamente |
|
561
|
|
|
// se a intenção da consulta for para cancelar ou inutilizar |
|
562
|
|
|
// os eventos já estão preparados para manter a ação correta para posterior processamento |
|
563
|
|
|
// Pode acontecer de uma nota cancelada ser consultada |
|
564
|
1 |
|
$cancelamento = $tarefa->getAcao() == \NFe\Task\Tarefa::ACAO_CONSULTAR && $retorno->isCancelado(); |
|
565
|
|
|
|
|
566
|
1 |
|
if ($tarefa->getAcao() == \NFe\Task\Tarefa::ACAO_INUTILIZAR) { |
|
567
|
|
|
// implementar aqui pois o evento de inutilização não devolve o ID da nota no banco |
|
568
|
|
|
// atualizar a chave, protocolo, data de autorização, flag de conclusão e estado da inutilização |
|
569
|
|
|
} elseif ($tarefa->getAcao() == \NFe\Task\Tarefa::ACAO_CANCELAR || $cancelamento) { |
|
570
|
|
|
// salva um XML diferenciado e não embutido no XML da nota |
|
571
|
|
|
$nota = $tarefa->getNota(); |
|
572
|
|
|
$path = $this->getPastaXmlCancelado($nota->getAmbiente()); |
|
573
|
|
|
$filename = $path . '/' . $nota->getID() . '-procEventoNFe.xml'; |
|
574
|
|
|
$xml = $tarefa->getDocumento(); |
|
575
|
|
|
Util::createDirectory(dirname($filename)); |
|
576
|
|
|
file_put_contents($filename, $xml->saveXML()); |
|
577
|
|
|
} |
|
578
|
1 |
|
} |
|
579
|
|
|
|
|
580
|
|
|
/** |
|
581
|
|
|
* Chamado quando ocorre uma falha na execução de uma tarefa |
|
582
|
|
|
* Altera a flag de correção da nota para evitar ficar enviando a nota a todo momento |
|
583
|
|
|
* Atenção: Não alterar a flag de correção se o erro da tarefa for causado por problema de rede |
|
584
|
|
|
* @param \NFe\Task\Tarefa $tarefa |
|
585
|
|
|
* @param \Exception $exception |
|
586
|
|
|
*/ |
|
587
|
1 |
|
public function onTarefaErro($tarefa, $exception) |
|
588
|
|
|
{ |
|
589
|
1 |
|
} |
|
590
|
|
|
|
|
591
|
|
|
/** |
|
592
|
|
|
* @param bool $recursive |
|
593
|
|
|
* @return array |
|
594
|
|
|
*/ |
|
595
|
2 |
|
public function toArray($recursive = false) |
|
596
|
|
|
{ |
|
597
|
2 |
|
$ajuste = parent::toArray($recursive); |
|
598
|
2 |
|
$ajuste['pasta_xml_base'] = $this->getPastaXmlBase(); |
|
599
|
2 |
|
$ajuste['pasta_xml_inutilizado'] = $this->getPastaXmlInutilizado(); |
|
600
|
2 |
|
$ajuste['pasta_xml_cancelado'] = $this->getPastaXmlCancelado(); |
|
601
|
2 |
|
$ajuste['pasta_xml_pendente'] = $this->getPastaXmlPendente(); |
|
602
|
2 |
|
$ajuste['pasta_xml_denegado'] = $this->getPastaXmlDenegado(); |
|
603
|
2 |
|
$ajuste['pasta_xml_rejeitado'] = $this->getPastaXmlRejeitado(); |
|
604
|
2 |
|
$ajuste['pasta_xml_autorizado'] = $this->getPastaXmlAutorizado(); |
|
605
|
2 |
|
$ajuste['pasta_xml_processamento'] = $this->getPastaXmlProcessamento(); |
|
606
|
2 |
|
$ajuste['pasta_xml_assinado'] = $this->getPastaXmlAssinado(); |
|
607
|
2 |
|
return $ajuste; |
|
608
|
|
|
} |
|
609
|
|
|
|
|
610
|
|
|
/** |
|
611
|
|
|
* @param mixed $ajuste array ou instância |
|
612
|
|
|
* @return self |
|
613
|
|
|
*/ |
|
614
|
102 |
|
public function fromArray($ajuste = []) |
|
|
|
|
|
|
615
|
|
|
{ |
|
616
|
102 |
|
if ($ajuste instanceof Ajuste) { |
|
617
|
2 |
|
$ajuste = $ajuste->toArray(); |
|
618
|
102 |
|
} elseif (!is_array($ajuste)) { |
|
619
|
1 |
|
return $this; |
|
620
|
|
|
} |
|
621
|
102 |
|
parent::fromArray($ajuste); |
|
622
|
102 |
|
if (isset($ajuste['pasta_xml_base'])) { |
|
623
|
2 |
|
$this->setPastaXmlBase($ajuste['pasta_xml_base']); |
|
624
|
|
|
} else { |
|
625
|
102 |
|
$this->setPastaXmlBase(null); |
|
626
|
|
|
} |
|
627
|
102 |
|
if (isset($ajuste['pasta_xml_inutilizado'])) { |
|
628
|
2 |
|
$this->setPastaXmlInutilizado($ajuste['pasta_xml_inutilizado']); |
|
629
|
|
|
} else { |
|
630
|
102 |
|
$this->setPastaXmlInutilizado(null); |
|
631
|
|
|
} |
|
632
|
102 |
|
if (isset($ajuste['pasta_xml_cancelado'])) { |
|
633
|
2 |
|
$this->setPastaXmlCancelado($ajuste['pasta_xml_cancelado']); |
|
634
|
|
|
} else { |
|
635
|
102 |
|
$this->setPastaXmlCancelado(null); |
|
636
|
|
|
} |
|
637
|
102 |
|
if (isset($ajuste['pasta_xml_pendente'])) { |
|
638
|
2 |
|
$this->setPastaXmlPendente($ajuste['pasta_xml_pendente']); |
|
639
|
|
|
} else { |
|
640
|
102 |
|
$this->setPastaXmlPendente(null); |
|
641
|
|
|
} |
|
642
|
102 |
|
if (isset($ajuste['pasta_xml_denegado'])) { |
|
643
|
2 |
|
$this->setPastaXmlDenegado($ajuste['pasta_xml_denegado']); |
|
644
|
|
|
} else { |
|
645
|
102 |
|
$this->setPastaXmlDenegado(null); |
|
646
|
|
|
} |
|
647
|
102 |
|
if (isset($ajuste['pasta_xml_rejeitado'])) { |
|
648
|
2 |
|
$this->setPastaXmlRejeitado($ajuste['pasta_xml_rejeitado']); |
|
649
|
|
|
} else { |
|
650
|
102 |
|
$this->setPastaXmlRejeitado(null); |
|
651
|
|
|
} |
|
652
|
102 |
|
if (isset($ajuste['pasta_xml_autorizado'])) { |
|
653
|
2 |
|
$this->setPastaXmlAutorizado($ajuste['pasta_xml_autorizado']); |
|
654
|
|
|
} else { |
|
655
|
102 |
|
$this->setPastaXmlAutorizado(null); |
|
656
|
|
|
} |
|
657
|
102 |
|
if (isset($ajuste['pasta_xml_processamento'])) { |
|
658
|
2 |
|
$this->setPastaXmlProcessamento($ajuste['pasta_xml_processamento']); |
|
659
|
|
|
} else { |
|
660
|
102 |
|
$this->setPastaXmlProcessamento(null); |
|
661
|
|
|
} |
|
662
|
102 |
|
if (isset($ajuste['pasta_xml_assinado'])) { |
|
663
|
2 |
|
$this->setPastaXmlAssinado($ajuste['pasta_xml_assinado']); |
|
664
|
|
|
} else { |
|
665
|
102 |
|
$this->setPastaXmlAssinado(null); |
|
666
|
|
|
} |
|
667
|
102 |
|
return $this; |
|
668
|
|
|
} |
|
669
|
|
|
} |
|
670
|
|
|
|
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.