|
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
|
|
|
class Tarefa |
|
|
|
|
|
|
31
|
|
|
{ |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Ação a ser realizada sobre o objeto ou recibo |
|
35
|
|
|
*/ |
|
36
|
|
|
const ACAO_CONSULTAR = 'consultar'; |
|
37
|
|
|
const ACAO_INUTILIZAR = 'inutilizar'; |
|
38
|
|
|
const ACAO_CANCELAR = 'cancelar'; |
|
39
|
|
|
|
|
40
|
|
|
private $id; |
|
41
|
|
|
private $acao; |
|
42
|
|
|
private $nota; |
|
43
|
|
|
private $documento; |
|
44
|
|
|
private $agente; |
|
45
|
|
|
private $resposta; |
|
46
|
|
|
|
|
47
|
13 |
|
public function __construct($tarefa = array()) |
|
48
|
|
|
{ |
|
49
|
13 |
|
$this->fromArray($tarefa); |
|
50
|
13 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Código aleatório e opcional que identifica a tarefa |
|
54
|
|
|
*/ |
|
55
|
12 |
|
public function getID() |
|
56
|
|
|
{ |
|
57
|
12 |
|
return $this->id; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
13 |
|
public function setID($id) |
|
61
|
|
|
{ |
|
62
|
13 |
|
$this->id = $id; |
|
63
|
13 |
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Ação a ser realizada sobre o objeto ou recibo |
|
68
|
|
|
*/ |
|
69
|
12 |
|
public function getAcao() |
|
70
|
|
|
{ |
|
71
|
12 |
|
return $this->acao; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
13 |
|
public function setAcao($acao) |
|
75
|
|
|
{ |
|
76
|
13 |
|
$this->acao = $acao; |
|
77
|
13 |
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Nota que será processada se informado |
|
82
|
|
|
*/ |
|
83
|
12 |
|
public function getNota() |
|
84
|
|
|
{ |
|
85
|
12 |
|
return $this->nota; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
13 |
|
public function setNota($nota) |
|
89
|
|
|
{ |
|
90
|
13 |
|
$this->nota = $nota; |
|
91
|
13 |
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Informa o XML do objeto, quando não informado o XML é gerado a partir do |
|
96
|
|
|
* objeto |
|
97
|
|
|
*/ |
|
98
|
4 |
|
public function getDocumento() |
|
99
|
|
|
{ |
|
100
|
4 |
|
return $this->documento; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
13 |
|
public function setDocumento($documento) |
|
104
|
|
|
{ |
|
105
|
13 |
|
$this->documento = $documento; |
|
106
|
13 |
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Agente que obteve ou vai obter a resposta, podendo ser: pedido de |
|
111
|
|
|
* inutilização(NF\Inutilizacao), recibo(NF\Recibo) ou pedido de |
|
112
|
|
|
* cancelamento(NF\Evento) |
|
113
|
|
|
*/ |
|
114
|
12 |
|
public function getAgente() |
|
115
|
|
|
{ |
|
116
|
12 |
|
return $this->agente; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
13 |
|
public function setAgente($agente) |
|
120
|
|
|
{ |
|
121
|
13 |
|
$this->agente = $agente; |
|
122
|
13 |
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Resposta da tarefa após ser executada |
|
127
|
|
|
*/ |
|
128
|
5 |
|
public function getResposta() |
|
129
|
|
|
{ |
|
130
|
5 |
|
return $this->resposta; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
13 |
|
public function setResposta($resposta) |
|
134
|
|
|
{ |
|
135
|
13 |
|
$this->resposta = $resposta; |
|
136
|
13 |
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
2 |
|
public function toArray($recursive = false) |
|
140
|
|
|
{ |
|
141
|
2 |
|
$tarefa = array(); |
|
142
|
2 |
|
$tarefa['id'] = $this->getID(); |
|
143
|
2 |
|
$tarefa['acao'] = $this->getAcao(); |
|
144
|
2 |
|
if (!is_null($this->getNota()) && $recursive) { |
|
145
|
1 |
|
$tarefa['nota'] = $this->getNota()->toArray($recursive); |
|
146
|
1 |
|
} else { |
|
147
|
1 |
|
$tarefa['nota'] = $this->getNota(); |
|
148
|
|
|
} |
|
149
|
2 |
|
if (!is_null($this->getDocumento()) && $recursive) { |
|
150
|
|
|
$tarefa['documento'] = $this->getDocumento()->saveXML(); |
|
151
|
|
|
} else { |
|
152
|
2 |
|
$tarefa['documento'] = $this->getDocumento(); |
|
153
|
|
|
} |
|
154
|
2 |
|
if (!is_null($this->getAgente()) && $recursive) { |
|
155
|
1 |
|
$tarefa['agente'] = $this->getAgente()->toArray($recursive); |
|
156
|
1 |
|
} else { |
|
157
|
1 |
|
$tarefa['agente'] = $this->getAgente(); |
|
158
|
|
|
} |
|
159
|
2 |
|
if (!is_null($this->getResposta()) && $recursive) { |
|
160
|
1 |
|
$tarefa['resposta'] = $this->getResposta()->toArray($recursive); |
|
161
|
1 |
|
} else { |
|
162
|
1 |
|
$tarefa['resposta'] = $this->getResposta(); |
|
163
|
|
|
} |
|
164
|
2 |
|
return $tarefa; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
13 |
|
public function fromArray($tarefa = array()) |
|
168
|
|
|
{ |
|
169
|
13 |
|
if ($tarefa instanceof Tarefa) { |
|
170
|
1 |
|
$tarefa = $tarefa->toArray(); |
|
171
|
13 |
|
} elseif (!is_array($tarefa)) { |
|
172
|
1 |
|
return $this; |
|
173
|
|
|
} |
|
174
|
13 |
|
if (isset($tarefa['id'])) { |
|
175
|
1 |
|
$this->setID($tarefa['id']); |
|
176
|
1 |
|
} else { |
|
177
|
13 |
|
$this->setID(null); |
|
178
|
|
|
} |
|
179
|
13 |
|
if (isset($tarefa['acao'])) { |
|
180
|
1 |
|
$this->setAcao($tarefa['acao']); |
|
181
|
1 |
|
} else { |
|
182
|
13 |
|
$this->setAcao(null); |
|
183
|
|
|
} |
|
184
|
13 |
|
if (isset($tarefa['nota'])) { |
|
185
|
1 |
|
$this->setNota($tarefa['nota']); |
|
186
|
1 |
|
} else { |
|
187
|
13 |
|
$this->setNota(null); |
|
188
|
|
|
} |
|
189
|
13 |
|
if (isset($tarefa['documento'])) { |
|
190
|
1 |
|
$this->setDocumento($tarefa['documento']); |
|
191
|
1 |
|
} else { |
|
192
|
13 |
|
$this->setDocumento(null); |
|
193
|
|
|
} |
|
194
|
13 |
|
if (isset($tarefa['agente'])) { |
|
195
|
1 |
|
$this->setAgente($tarefa['agente']); |
|
196
|
1 |
|
} else { |
|
197
|
13 |
|
$this->setAgente(null); |
|
198
|
|
|
} |
|
199
|
13 |
|
if (isset($tarefa['resposta'])) { |
|
200
|
1 |
|
$this->setResposta($tarefa['resposta']); |
|
201
|
1 |
|
} else { |
|
202
|
13 |
|
$this->setResposta(null); |
|
203
|
|
|
} |
|
204
|
13 |
|
return $this; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Resposta da tarefa após ser executada |
|
209
|
|
|
*/ |
|
210
|
12 |
|
public function executa() |
|
211
|
|
|
{ |
|
212
|
12 |
|
if (is_null($this->getID())) { |
|
213
|
12 |
|
$this->setID(Status::genLote()); |
|
214
|
12 |
|
} |
|
215
|
12 |
|
$retorno = null; |
|
216
|
12 |
|
switch ($this->getAcao()) { |
|
217
|
12 |
|
case self::ACAO_CANCELAR: |
|
218
|
4 |
|
$retorno = $this->cancela(); |
|
219
|
1 |
|
break; |
|
220
|
8 |
|
case self::ACAO_INUTILIZAR: |
|
221
|
3 |
|
$retorno = $this->inutiliza(); |
|
222
|
1 |
|
break; |
|
223
|
5 |
|
case self::ACAO_CONSULTAR: |
|
224
|
5 |
|
$retorno = $this->consulta(); |
|
225
|
3 |
|
break; |
|
226
|
5 |
|
} |
|
227
|
5 |
|
$this->setResposta($retorno); |
|
228
|
5 |
|
return $this->getResposta(); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
4 |
|
private function cancela() |
|
232
|
|
|
{ |
|
233
|
4 |
|
$nota = $this->getNota(); |
|
234
|
4 |
|
$evento = $this->getAgente(); |
|
235
|
4 |
|
if (is_null($evento)) { |
|
236
|
3 |
|
if (is_null($nota)) { |
|
237
|
1 |
|
throw new \Exception('A nota não foi informada na tarefa de cancelamento "'.$this->getID().'"', 404); |
|
238
|
|
|
} |
|
239
|
2 |
|
if (is_null($nota->getProtocolo())) { |
|
240
|
1 |
|
throw new \Exception('A nota não possui protocolo de autorização para o cancelamento "'. |
|
241
|
1 |
|
$this->getID().'"', 404); |
|
242
|
|
|
} |
|
243
|
1 |
|
$evento = new Evento(); |
|
244
|
1 |
|
$evento->setData(time()); |
|
245
|
1 |
|
$evento->setOrgao($nota->getEmitente()->getEndereco()-> |
|
246
|
1 |
|
getMunicipio()->getEstado()->getUF()); |
|
247
|
1 |
|
$evento->setJustificativa($nota->getJustificativa()); |
|
248
|
1 |
|
$this->setAgente($evento); |
|
249
|
2 |
|
} elseif (!($evento instanceof Evento)) { |
|
250
|
1 |
|
throw new \Exception('O agente informado não é um evento', 500); |
|
251
|
|
|
} |
|
252
|
1 |
|
if (!is_null($nota)) { |
|
253
|
1 |
|
$evento->setAmbiente($nota->getAmbiente()); |
|
254
|
1 |
|
$evento->setModelo($nota->getModelo()); |
|
255
|
1 |
|
$evento->setIdentificador($nota->getEmitente()->getCNPJ()); |
|
256
|
1 |
|
if (!is_null($nota->getProtocolo())) { |
|
257
|
1 |
|
$evento->setNumero($nota->getProtocolo()->getNumero()); |
|
258
|
1 |
|
} |
|
259
|
1 |
|
$evento->setChave($nota->getID()); |
|
260
|
1 |
|
} |
|
261
|
1 |
|
$dom = $evento->getNode()->ownerDocument; |
|
262
|
1 |
|
$dom = $evento->assinar($dom); |
|
263
|
1 |
|
$dom = $evento->validar($dom); |
|
264
|
1 |
|
$retorno = $evento->envia($dom); |
|
265
|
1 |
|
if ($retorno->isCancelado()) { |
|
266
|
1 |
|
$dom = $evento->addInformacao($dom); |
|
267
|
1 |
|
$this->setDocumento($dom); |
|
268
|
1 |
|
} |
|
269
|
1 |
|
return $retorno; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
3 |
|
private function inutiliza() |
|
273
|
|
|
{ |
|
274
|
3 |
|
$nota = $this->getNota(); |
|
275
|
3 |
|
$inutilizacao = $this->getAgente(); |
|
276
|
3 |
|
if (is_null($inutilizacao)) { |
|
277
|
2 |
|
if (is_null($nota)) { |
|
278
|
1 |
|
throw new \Exception('A nota não foi informada na tarefa de inutilização "'.$this->getID().'"', 404); |
|
279
|
|
|
} |
|
280
|
1 |
|
$inutilizacao = new Inutilizacao(); |
|
281
|
1 |
|
$inutilizacao->setAno(date('Y')); |
|
282
|
1 |
|
$inutilizacao->setJustificativa($nota->getJustificativa()); |
|
283
|
1 |
|
$this->setAgente($inutilizacao); |
|
284
|
2 |
|
} elseif (!($inutilizacao instanceof Inutilizacao)) { |
|
285
|
1 |
|
throw new \Exception('O agente informado não é uma inutilização', 500); |
|
286
|
|
|
} |
|
287
|
1 |
|
if (!is_null($nota)) { |
|
288
|
1 |
|
$inutilizacao->setCNPJ($nota->getEmitente()->getCNPJ()); |
|
289
|
1 |
|
$inutilizacao->setSerie($nota->getSerie()); |
|
290
|
1 |
|
$inutilizacao->setInicio($nota->getNumero()); |
|
291
|
1 |
|
$inutilizacao->setFinal($nota->getNumero()); |
|
292
|
1 |
|
$inutilizacao->setUF($nota->getEmitente()->getEndereco()-> |
|
293
|
1 |
|
getMunicipio()->getEstado()->getUF()); |
|
294
|
1 |
|
$inutilizacao->setAmbiente($nota->getAmbiente()); |
|
295
|
1 |
|
$inutilizacao->setModelo($nota->getModelo()); |
|
296
|
1 |
|
} |
|
297
|
1 |
|
$dom = $inutilizacao->getNode()->ownerDocument; |
|
298
|
1 |
|
$dom = $inutilizacao->assinar($dom); |
|
299
|
1 |
|
$dom = $inutilizacao->validar($dom); |
|
300
|
1 |
|
$dom = $inutilizacao->envia($dom); |
|
301
|
1 |
|
$this->setDocumento($dom); |
|
302
|
1 |
|
return $inutilizacao; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
5 |
|
private function consulta() |
|
306
|
|
|
{ |
|
307
|
5 |
|
$nota = $this->getNota(); |
|
308
|
5 |
|
$agente = $this->getAgente(); |
|
309
|
5 |
|
if (is_null($agente)) { |
|
310
|
3 |
|
if (is_null($nota)) { |
|
311
|
1 |
|
throw new \Exception('A nota não foi informada na tarefa de consulta "'.$this->getID().'"', 404); |
|
312
|
|
|
} |
|
313
|
2 |
|
$agente = new Situacao(); |
|
314
|
2 |
|
$agente->setChave($nota->getID()); |
|
315
|
2 |
|
$this->setAgente($agente); |
|
316
|
4 |
|
} elseif (!($agente instanceof Situacao) && !($agente instanceof Recibo)) { |
|
317
|
1 |
|
throw new \Exception('O agente informado não é uma consulta de situação e nem um recibo', 500); |
|
318
|
|
|
} |
|
319
|
3 |
|
if (!is_null($nota)) { |
|
320
|
3 |
|
$agente->setAmbiente($nota->getAmbiente()); |
|
321
|
3 |
|
$agente->setModelo($nota->getModelo()); |
|
322
|
3 |
|
} |
|
323
|
3 |
|
$retorno = $agente->consulta($this->getNota()); |
|
324
|
3 |
|
if ($agente->isCancelado()) { |
|
325
|
|
|
// TODO: carregar assinatura do XML para evitar usar outro certificado |
|
326
|
1 |
|
$dom = $retorno->assinar(); |
|
327
|
1 |
|
$dom = $retorno->validar($dom); |
|
328
|
|
|
// $dom = $retorno->getNode()->ownerDocument; // descomentar essa linha quando implementar |
|
329
|
|
|
// TODO: Fim do problema de assinatura |
|
330
|
1 |
|
$dom = $retorno->addInformacao($dom); |
|
331
|
1 |
|
$this->setDocumento($dom); |
|
332
|
1 |
|
$retorno = $retorno->getInformacao(); |
|
333
|
1 |
|
} |
|
334
|
3 |
|
return $retorno; |
|
335
|
|
|
} |
|
336
|
|
|
} |
|
337
|
|
|
|
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.