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