1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* MIT License |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 MZ Desenvolvimento de Sistemas LTDA |
6
|
|
|
* |
7
|
|
|
* @author Francimar Alves <[email protected]> |
8
|
|
|
* |
9
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
10
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
11
|
|
|
* in the Software without restriction, including without limitation the rights |
12
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
14
|
|
|
* furnished to do so, subject to the following conditions: |
15
|
|
|
* |
16
|
|
|
* The above copyright notice and this permission notice shall be included in all |
17
|
|
|
* copies or substantial portions of the Software. |
18
|
|
|
* |
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
25
|
|
|
* SOFTWARE. |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
namespace NFe\Task; |
29
|
|
|
|
30
|
|
|
use NFe\Core\Nota; |
31
|
|
|
use NFe\Common\Util; |
32
|
|
|
use NFe\Exception\ValidationException; |
33
|
|
|
|
34
|
|
|
class Recibo extends Retorno |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
const INFO_TAGNAME = 'infRec'; |
38
|
|
|
|
39
|
|
|
private $numero; |
40
|
|
|
private $tempo_medio; |
41
|
|
|
private $codigo; |
42
|
|
|
private $mensagem; |
43
|
|
|
private $modelo; |
44
|
|
|
|
45
|
10 |
|
public function __construct($recibo = array()) |
46
|
|
|
{ |
47
|
10 |
|
parent::__construct($recibo); |
48
|
10 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Número do Recibo |
52
|
|
|
*/ |
53
|
5 |
|
public function getNumero($normalize = false) |
54
|
|
|
{ |
55
|
5 |
|
if (!$normalize) { |
56
|
1 |
|
return $this->numero; |
57
|
|
|
} |
58
|
5 |
|
return $this->numero; |
59
|
|
|
} |
60
|
|
|
|
61
|
10 |
|
public function setNumero($numero) |
62
|
|
|
{ |
63
|
10 |
|
$this->numero = $numero; |
64
|
10 |
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Tempo médio de resposta do serviço (em segundos) dos últimos 5 minutos |
69
|
|
|
*/ |
70
|
1 |
|
public function getTempoMedio($normalize = false) |
71
|
|
|
{ |
72
|
1 |
|
if (!$normalize) { |
73
|
1 |
|
return $this->tempo_medio; |
74
|
|
|
} |
75
|
|
|
return $this->tempo_medio; |
76
|
|
|
} |
77
|
|
|
|
78
|
10 |
|
public function setTempoMedio($tempo_medio) |
79
|
|
|
{ |
80
|
10 |
|
$this->tempo_medio = $tempo_medio; |
81
|
10 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Código da Mensagem (v2.0) alterado para tamanho variavel 1-4. |
86
|
|
|
* (NT2011/004) |
87
|
|
|
*/ |
88
|
1 |
|
public function getCodigo($normalize = false) |
89
|
|
|
{ |
90
|
1 |
|
if (!$normalize) { |
91
|
1 |
|
return $this->codigo; |
92
|
|
|
} |
93
|
|
|
return $this->codigo; |
94
|
|
|
} |
95
|
|
|
|
96
|
10 |
|
public function setCodigo($codigo) |
97
|
|
|
{ |
98
|
10 |
|
$this->codigo = $codigo; |
99
|
10 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Mensagem da SEFAZ para o emissor. (v2.0) |
104
|
|
|
*/ |
105
|
1 |
|
public function getMensagem($normalize = false) |
106
|
|
|
{ |
107
|
1 |
|
if (!$normalize) { |
108
|
1 |
|
return $this->mensagem; |
109
|
|
|
} |
110
|
|
|
return $this->mensagem; |
111
|
|
|
} |
112
|
|
|
|
113
|
10 |
|
public function setMensagem($mensagem) |
114
|
|
|
{ |
115
|
10 |
|
$this->mensagem = $mensagem; |
116
|
10 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Código do modelo do Documento Fiscal. 55 = NF-e; 65 = NFC-e. |
121
|
|
|
* @param boolean $normalize informa se o modelo deve estar no formato do XML |
122
|
|
|
* @return mixed modelo do Envio |
123
|
|
|
*/ |
124
|
4 |
|
public function getModelo($normalize = false) |
125
|
|
|
{ |
126
|
4 |
|
if (!$normalize) { |
127
|
4 |
|
return $this->modelo; |
128
|
|
|
} |
129
|
|
|
switch ($this->modelo) { |
130
|
|
|
case Nota::MODELO_NFE: |
131
|
|
|
return '55'; |
132
|
|
|
case Nota::MODELO_NFCE: |
133
|
|
|
return '65'; |
134
|
|
|
} |
135
|
|
|
return $this->modelo; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Altera o valor do Modelo para o informado no parâmetro |
140
|
|
|
* @param mixed $modelo novo valor para Modelo |
141
|
|
|
* @return Envio A própria instância da classe |
142
|
|
|
*/ |
143
|
10 |
|
public function setModelo($modelo) |
144
|
|
|
{ |
145
|
|
|
switch ($modelo) { |
146
|
10 |
|
case '55': |
147
|
|
|
$modelo = Nota::MODELO_NFE; |
148
|
|
|
break; |
149
|
10 |
|
case '65': |
150
|
|
|
$modelo = Nota::MODELO_NFCE; |
151
|
|
|
break; |
152
|
|
|
} |
153
|
10 |
|
$this->modelo = $modelo; |
154
|
10 |
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
1 |
|
public function toArray($recursive = false) |
158
|
|
|
{ |
159
|
1 |
|
$recibo = parent::toArray($recursive); |
160
|
1 |
|
$recibo['numero'] = $this->getNumero(); |
161
|
1 |
|
$recibo['tempo_medio'] = $this->getTempoMedio(); |
162
|
1 |
|
$recibo['codigo'] = $this->getCodigo(); |
163
|
1 |
|
$recibo['mensagem'] = $this->getMensagem(); |
164
|
1 |
|
$recibo['modelo'] = $this->getModelo(); |
165
|
1 |
|
return $recibo; |
166
|
|
|
} |
167
|
|
|
|
168
|
10 |
|
public function fromArray($recibo = array()) |
169
|
|
|
{ |
170
|
10 |
|
if ($recibo instanceof Recibo) { |
171
|
1 |
|
$recibo = $recibo->toArray(); |
172
|
10 |
|
} elseif (!is_array($recibo)) { |
173
|
1 |
|
return $this; |
174
|
|
|
} |
175
|
10 |
|
parent::fromArray($recibo); |
176
|
10 |
|
if (isset($recibo['numero'])) { |
177
|
1 |
|
$this->setNumero($recibo['numero']); |
178
|
|
|
} else { |
179
|
10 |
|
$this->setNumero(null); |
180
|
|
|
} |
181
|
10 |
|
if (isset($recibo['tempo_medio'])) { |
182
|
|
|
$this->setTempoMedio($recibo['tempo_medio']); |
183
|
|
|
} else { |
184
|
10 |
|
$this->setTempoMedio(null); |
185
|
|
|
} |
186
|
10 |
|
if (isset($recibo['codigo'])) { |
187
|
|
|
$this->setCodigo($recibo['codigo']); |
188
|
|
|
} else { |
189
|
10 |
|
$this->setCodigo(null); |
190
|
|
|
} |
191
|
10 |
|
if (isset($recibo['mensagem'])) { |
192
|
|
|
$this->setMensagem($recibo['mensagem']); |
193
|
|
|
} else { |
194
|
10 |
|
$this->setMensagem(null); |
195
|
|
|
} |
196
|
10 |
|
if (isset($recibo['modelo'])) { |
197
|
1 |
|
$this->setModelo($recibo['modelo']); |
198
|
|
|
} else { |
199
|
10 |
|
$this->setModelo(null); |
200
|
|
|
} |
201
|
10 |
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
4 |
|
public function envia($dom) |
205
|
|
|
{ |
206
|
4 |
|
$envio = new Envio(); |
207
|
4 |
|
$envio->setServico(Envio::SERVICO_RETORNO); |
208
|
4 |
|
$envio->setAmbiente($this->getAmbiente()); |
209
|
4 |
|
$envio->setModelo($this->getModelo()); |
210
|
4 |
|
$envio->setEmissao(Nota::EMISSAO_NORMAL); |
211
|
4 |
|
$envio->setConteudo($dom); |
212
|
4 |
|
$resp = $envio->envia(); |
213
|
4 |
|
$this->loadNode($resp); |
214
|
4 |
|
if (!$this->isProcessado()) { |
215
|
1 |
|
return $this; |
216
|
|
|
} |
217
|
3 |
|
$protocolo = new Protocolo(); |
218
|
3 |
|
$protocolo->loadNode($resp); |
219
|
3 |
|
return $protocolo; |
220
|
|
|
} |
221
|
|
|
|
222
|
5 |
|
public function consulta($nota = null) |
223
|
|
|
{ |
224
|
5 |
|
if (!is_null($nota)) { |
225
|
5 |
|
$this->setAmbiente($nota->getAmbiente()); |
226
|
5 |
|
$this->setModelo($nota->getModelo()); |
227
|
|
|
} |
228
|
5 |
|
$dom = $this->getNode()->ownerDocument; |
229
|
5 |
|
$dom = $this->validar($dom); |
230
|
4 |
|
$retorno = $this->envia($dom); |
231
|
4 |
|
if ($retorno->isAutorizado() && !is_null($nota)) { |
232
|
2 |
|
$nota->setProtocolo($retorno); |
233
|
|
|
} |
234
|
4 |
|
return $retorno; |
235
|
|
|
} |
236
|
|
|
|
237
|
5 |
|
public function getNode($name = null) |
238
|
|
|
{ |
239
|
5 |
|
$dom = new \DOMDocument('1.0', 'UTF-8'); |
240
|
5 |
|
$element = $dom->createElement(is_null($name)?'consReciNFe':$name); |
241
|
5 |
|
$element->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns', Nota::PORTAL); |
242
|
5 |
|
$versao = $dom->createAttribute('versao'); |
243
|
5 |
|
$versao->value = Nota::VERSAO; |
244
|
5 |
|
$element->appendChild($versao); |
245
|
|
|
|
246
|
5 |
|
Util::appendNode($element, 'tpAmb', $this->getAmbiente(true)); |
247
|
5 |
|
Util::appendNode($element, 'nRec', $this->getNumero(true)); |
248
|
5 |
|
$dom->appendChild($element); |
249
|
5 |
|
return $element; |
250
|
|
|
} |
251
|
|
|
|
252
|
6 |
|
public function loadNode($element, $name = null) |
253
|
|
|
{ |
254
|
6 |
|
$name = is_null($name)?'retConsReciNFe':$name; |
255
|
6 |
|
if ($name == self::INFO_TAGNAME) { |
256
|
2 |
|
$_fields = $element->getElementsByTagName($name); |
257
|
2 |
|
if ($_fields->length == 0) { |
258
|
1 |
|
throw new \Exception('Tag "'.$name.'" não encontrada', 404); |
259
|
|
|
} |
260
|
1 |
|
$element = $_fields->item(0); |
261
|
|
|
} else { |
262
|
4 |
|
$element = parent::loadNode($element, $name); |
263
|
|
|
} |
264
|
5 |
|
$this->setNumero( |
265
|
5 |
|
Util::loadNode( |
266
|
5 |
|
$element, |
267
|
5 |
|
'nRec', |
268
|
5 |
|
'Tag "nRec" do campo "Numero" não encontrada' |
269
|
|
|
) |
270
|
|
|
); |
271
|
5 |
|
$this->setTempoMedio(Util::loadNode($element, 'tMed')); |
272
|
5 |
|
$this->setCodigo(Util::loadNode($element, 'cMsg')); |
273
|
5 |
|
$this->setMensagem(Util::loadNode($element, 'xMsg')); |
274
|
5 |
|
return $element; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Valida o documento após assinar |
279
|
|
|
*/ |
280
|
5 |
|
public function validar($dom) |
281
|
|
|
{ |
282
|
5 |
|
$dom->loadXML($dom->saveXML()); |
283
|
5 |
|
$xsd_path = dirname(__DIR__) . '/Core/schema'; |
284
|
5 |
|
$xsd_file = $xsd_path . '/consReciNFe_v3.10.xsd'; |
285
|
5 |
|
if (!file_exists($xsd_file)) { |
286
|
|
|
throw new \Exception('O arquivo "'.$xsd_file.'" de esquema XSD não existe!', 404); |
287
|
|
|
} |
288
|
|
|
// Enable user error handling |
289
|
5 |
|
$save = libxml_use_internal_errors(true); |
290
|
5 |
|
if ($dom->schemaValidate($xsd_file)) { |
291
|
4 |
|
libxml_use_internal_errors($save); |
292
|
4 |
|
return $dom; |
293
|
|
|
} |
294
|
1 |
|
$msg = array(); |
295
|
1 |
|
$errors = libxml_get_errors(); |
296
|
1 |
|
foreach ($errors as $error) { |
297
|
1 |
|
$msg[] = 'Não foi possível validar o XML: '.$error->message; |
298
|
|
|
} |
299
|
1 |
|
libxml_clear_errors(); |
300
|
1 |
|
libxml_use_internal_errors($save); |
301
|
1 |
|
throw new ValidationException($msg); |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|