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 Situacao extends Retorno |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
private $chave; |
38
|
|
|
private $modelo; |
39
|
|
|
|
40
|
|
|
const TAG_RETORNO = 'retConsSitNFe'; |
41
|
|
|
|
42
|
9 |
|
public function __construct($situacao = []) |
43
|
|
|
{ |
44
|
9 |
|
parent::__construct($situacao); |
45
|
9 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Chaves de acesso da NF-e, compostas por: UF do emitente, AAMM da emissão |
49
|
|
|
* da NFe, CNPJ do emitente, modelo, série e número da NF-e e código |
50
|
|
|
* numérico+DV. |
51
|
|
|
*/ |
52
|
6 |
|
public function getChave($normalize = false) |
53
|
|
|
{ |
54
|
6 |
|
if (!$normalize) { |
55
|
2 |
|
return $this->chave; |
56
|
|
|
} |
57
|
6 |
|
return $this->chave; |
58
|
|
|
} |
59
|
|
|
|
60
|
9 |
|
public function setChave($chave) |
61
|
|
|
{ |
62
|
9 |
|
$this->chave = $chave; |
63
|
9 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Código do modelo do Documento Fiscal. 55 = NF-e; 65 = NFC-e. |
68
|
|
|
* @param boolean $normalize informa se o modelo deve estar no formato do XML |
69
|
|
|
* @return mixed modelo do Envio |
70
|
|
|
*/ |
71
|
7 |
|
public function getModelo($normalize = false) |
72
|
|
|
{ |
73
|
7 |
|
if (!$normalize) { |
74
|
7 |
|
return $this->modelo; |
75
|
|
|
} |
76
|
1 |
|
switch ($this->modelo) { |
77
|
1 |
|
case Nota::MODELO_NFE: |
78
|
1 |
|
return '55'; |
79
|
1 |
|
case Nota::MODELO_NFCE: |
80
|
1 |
|
return '65'; |
81
|
|
|
} |
82
|
1 |
|
return $this->modelo; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Altera o valor do Modelo para o informado no parâmetro |
87
|
|
|
* @param mixed $modelo novo valor para Modelo |
88
|
|
|
* @return Envio A própria instância da classe |
89
|
|
|
*/ |
90
|
9 |
|
public function setModelo($modelo) |
91
|
|
|
{ |
92
|
|
|
switch ($modelo) { |
93
|
9 |
|
case '55': |
94
|
1 |
|
$modelo = Nota::MODELO_NFE; |
95
|
1 |
|
break; |
96
|
9 |
|
case '65': |
97
|
1 |
|
$modelo = Nota::MODELO_NFCE; |
98
|
1 |
|
break; |
99
|
|
|
} |
100
|
9 |
|
$this->modelo = $modelo; |
101
|
9 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
public function toArray($recursive = false) |
105
|
|
|
{ |
106
|
2 |
|
$situacao = parent::toArray($recursive); |
107
|
2 |
|
$situacao['chave'] = $this->getChave(); |
108
|
2 |
|
$situacao['modelo'] = $this->getModelo(); |
109
|
2 |
|
return $situacao; |
110
|
|
|
} |
111
|
|
|
|
112
|
9 |
|
public function fromArray($situacao = []) |
113
|
|
|
{ |
114
|
9 |
|
if ($situacao instanceof Situacao) { |
115
|
1 |
|
$situacao = $situacao->toArray(); |
116
|
9 |
|
} elseif (!is_array($situacao)) { |
117
|
1 |
|
return $this; |
118
|
|
|
} |
119
|
9 |
|
parent::fromArray($situacao); |
120
|
9 |
|
if (isset($situacao['chave'])) { |
121
|
1 |
|
$this->setChave($situacao['chave']); |
122
|
|
|
} else { |
123
|
9 |
|
$this->setChave(null); |
124
|
|
|
} |
125
|
9 |
|
if (isset($situacao['modelo'])) { |
126
|
1 |
|
$this->setModelo($situacao['modelo']); |
127
|
|
|
} else { |
128
|
9 |
|
$this->setModelo(null); |
129
|
|
|
} |
130
|
9 |
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
6 |
|
public function envia($dom) |
134
|
|
|
{ |
135
|
6 |
|
$envio = new Envio(); |
136
|
6 |
|
$envio->setServico(Envio::SERVICO_PROTOCOLO); |
137
|
6 |
|
$envio->setAmbiente($this->getAmbiente()); |
138
|
6 |
|
$envio->setModelo($this->getModelo()); |
139
|
6 |
|
$envio->setEmissao(Nota::EMISSAO_NORMAL); |
140
|
6 |
|
$this->setVersao($envio->getVersao()); |
141
|
6 |
|
$dom = $this->validar($dom); |
142
|
5 |
|
$envio->setConteudo($dom); |
143
|
5 |
|
$resp = $envio->envia(); |
144
|
5 |
|
$this->loadNode($resp); |
145
|
5 |
|
if ($this->isAutorizado()) { |
146
|
2 |
|
$protocolo = new Protocolo(); |
147
|
2 |
|
$protocolo->loadNode($resp); |
148
|
2 |
|
return $protocolo; |
149
|
3 |
|
} elseif ($this->isCancelado()) { |
150
|
2 |
|
$evento = new Evento(); |
151
|
2 |
|
$evento->loadStatusNode($resp, self::TAG_RETORNO); |
152
|
2 |
|
$evento->loadNode($resp); |
153
|
2 |
|
return $evento; |
154
|
|
|
} |
155
|
1 |
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
6 |
|
public function consulta($nota = null) |
159
|
|
|
{ |
160
|
6 |
|
if (!is_null($nota)) { |
161
|
6 |
|
$this->setChave($nota->getID()); |
162
|
6 |
|
$this->setAmbiente($nota->getAmbiente()); |
163
|
6 |
|
$this->setModelo($nota->getModelo()); |
164
|
|
|
} |
165
|
6 |
|
$dom = $this->getNode()->ownerDocument; |
166
|
6 |
|
$retorno = $this->envia($dom); |
167
|
5 |
|
if ($retorno instanceof Protocolo && $retorno->isAutorizado() && !is_null($nota)) { |
168
|
2 |
|
$nota->setProtocolo($retorno); |
169
|
|
|
} |
170
|
5 |
|
return $retorno; |
171
|
|
|
} |
172
|
|
|
|
173
|
6 |
|
public function getNode($name = null) |
174
|
|
|
{ |
175
|
6 |
|
$dom = new \DOMDocument('1.0', 'UTF-8'); |
176
|
6 |
|
$element = $dom->createElement(is_null($name)?'consSitNFe':$name); |
177
|
6 |
|
$element->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns', Nota::PORTAL); |
178
|
6 |
|
$versao = $dom->createAttribute('versao'); |
179
|
6 |
|
$versao->value = Nota::VERSAO; |
180
|
6 |
|
$element->appendChild($versao); |
181
|
|
|
|
182
|
6 |
|
Util::appendNode($element, 'tpAmb', $this->getAmbiente(true)); |
183
|
6 |
|
Util::appendNode($element, 'xServ', 'CONSULTAR'); |
184
|
6 |
|
Util::appendNode($element, 'chNFe', $this->getChave(true)); |
185
|
6 |
|
$dom->appendChild($element); |
186
|
6 |
|
return $element; |
187
|
|
|
} |
188
|
|
|
|
189
|
5 |
|
public function loadNode($element, $name = null) |
190
|
|
|
{ |
191
|
5 |
|
$name = is_null($name)?self::TAG_RETORNO:$name; |
192
|
5 |
|
$element = parent::loadNode($element, $name); |
193
|
5 |
|
$this->setChave(Util::loadNode($element, 'chNFe')); |
194
|
5 |
|
return $element; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Valida o documento após assinar |
199
|
|
|
*/ |
200
|
7 |
|
public function validar($dom) |
201
|
|
|
{ |
202
|
7 |
|
$dom->loadXML($dom->saveXML()); |
203
|
7 |
|
$xsd_path = dirname(__DIR__) . '/Core/schema'; |
204
|
7 |
|
$xsd_file = $xsd_path . '/consSitNFe_v'.$this->getVersao().'.xsd'; |
205
|
7 |
|
if (!file_exists($xsd_file)) { |
206
|
1 |
|
throw new \Exception(sprintf('O arquivo "%s" de esquema XSD não existe!', $xsd_file), 404); |
207
|
|
|
} |
208
|
|
|
// Enable user error handling |
209
|
6 |
|
$save = libxml_use_internal_errors(true); |
210
|
6 |
|
if ($dom->schemaValidate($xsd_file)) { |
211
|
5 |
|
libxml_use_internal_errors($save); |
212
|
5 |
|
return $dom; |
213
|
|
|
} |
214
|
1 |
|
$msg = []; |
215
|
1 |
|
$errors = libxml_get_errors(); |
216
|
1 |
|
foreach ($errors as $error) { |
217
|
1 |
|
$msg[] = 'Não foi possível validar o XML: '.$error->message; |
218
|
|
|
} |
219
|
1 |
|
libxml_clear_errors(); |
220
|
1 |
|
libxml_use_internal_errors($save); |
221
|
1 |
|
throw new ValidationException($msg); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|