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\Database; |
29
|
|
|
|
30
|
|
|
use NFe\Common\Util; |
31
|
|
|
use NFe\Core\Nota; |
32
|
|
|
|
33
|
|
|
class Estatico extends Banco |
34
|
|
|
{ |
35
|
|
|
private $ibpt; |
36
|
|
|
private $uf_codes; |
37
|
|
|
private $mun_codes; |
38
|
|
|
private $servicos; |
39
|
|
|
private $data_dir; |
40
|
|
|
|
41
|
105 |
|
public function __construct($estatico = []) |
42
|
|
|
{ |
43
|
105 |
|
parent::__construct($estatico); |
44
|
105 |
|
$this->data_dir = __DIR__ . '/data'; |
45
|
105 |
|
$this->load(); |
46
|
105 |
|
} |
47
|
|
|
|
48
|
105 |
|
public function load() |
49
|
|
|
{ |
50
|
105 |
|
$json = file_get_contents($this->data_dir . '/uf_ibge_code.json'); |
51
|
105 |
|
$this->uf_codes = json_decode($json, true); |
52
|
105 |
|
if ($this->uf_codes === false || is_null($this->uf_codes)) { |
53
|
|
|
throw new \Exception('Falha ao carregar os códigos das unidades federadas', json_last_error()); |
54
|
|
|
} |
55
|
105 |
|
$json = file_get_contents($this->data_dir . '/municipio_ibge_code.json'); |
56
|
105 |
|
$this->mun_codes = json_decode($json, true); |
57
|
105 |
|
if ($this->mun_codes === false || is_null($this->mun_codes)) { |
58
|
|
|
throw new \Exception('Falha ao carregar os códigos dos municípios', json_last_error()); |
59
|
|
|
} |
60
|
105 |
|
$json = file_get_contents($this->data_dir . '/servicos.json'); |
61
|
105 |
|
$this->servicos = json_decode($json, true); |
62
|
105 |
|
if ($this->servicos === false || is_null($this->servicos)) { |
63
|
|
|
throw new \Exception('Falha ao carregar serviços da SEFAZ', json_last_error()); |
64
|
|
|
} |
65
|
105 |
|
} |
66
|
|
|
|
67
|
38 |
|
public function getIBPT() |
68
|
|
|
{ |
69
|
38 |
|
return $this->ibpt; |
70
|
|
|
} |
71
|
|
|
|
72
|
105 |
|
public function setIBPT($ibpt) |
73
|
|
|
{ |
74
|
105 |
|
$this->ibpt = $ibpt; |
75
|
105 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Obtém o código IBGE do estado |
80
|
|
|
*/ |
81
|
47 |
|
public function getCodigoEstado($uf) |
82
|
|
|
{ |
83
|
47 |
|
if (!isset($this->uf_codes['estados'][strtoupper($uf)])) { |
84
|
2 |
|
throw new \Exception( |
85
|
2 |
|
sprintf('Não foi encontrado o código do IBGE para o estado "%s"', $uf), |
86
|
2 |
|
404 |
87
|
|
|
); |
88
|
|
|
} |
89
|
46 |
|
$codigo = $this->uf_codes['estados'][strtoupper($uf)]; |
90
|
46 |
|
return intval($codigo); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Obtém o código do orgão por estado |
95
|
|
|
*/ |
96
|
5 |
|
public function getCodigoOrgao($uf) |
97
|
|
|
{ |
98
|
5 |
|
if (!isset($this->uf_codes['orgaos'][strtoupper($uf)])) { |
99
|
1 |
|
throw new \Exception( |
100
|
1 |
|
sprintf('Não foi encontrado o código do orgão para o estado "%s"', $uf), |
101
|
1 |
|
404 |
102
|
|
|
); |
103
|
|
|
} |
104
|
5 |
|
$codigo = $this->uf_codes['orgaos'][strtoupper($uf)]; |
105
|
5 |
|
return intval($codigo); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Obtém a aliquota do imposto de acordo com o tipo |
110
|
|
|
*/ |
111
|
36 |
|
public function getImpostoAliquota($ncm, $uf, $ex = null, $cnpj = null, $token = null) |
112
|
|
|
{ |
113
|
36 |
|
return $this->getIBPT()->getImposto($cnpj, $token, $ncm, $uf, $ex); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Obtém o código IBGE do município |
118
|
|
|
*/ |
119
|
14 |
|
public function getCodigoMunicipio($municipio, $uf) |
120
|
|
|
{ |
121
|
14 |
|
if (!isset($this->mun_codes['municipios'][strtoupper($uf)])) { |
122
|
1 |
|
throw new \Exception( |
123
|
1 |
|
sprintf('Não exite municípios para o estado "%s"', $uf), |
124
|
1 |
|
404 |
125
|
|
|
); |
126
|
|
|
} |
127
|
13 |
|
$array = $this->mun_codes['municipios'][strtoupper($uf)]; |
128
|
13 |
|
$elem = ['nome' => $municipio]; |
129
|
|
|
$o = Util::binarySearch($elem, $array, function ($o1, $o2) { |
|
|
|
|
130
|
13 |
|
$n1 = Util::removeAccent($o1['nome']); |
131
|
13 |
|
$n2 = Util::removeAccent($o2['nome']); |
132
|
13 |
|
return strcasecmp($n1, $n2); |
133
|
13 |
|
}); |
134
|
13 |
|
if ($o === false) { |
135
|
1 |
|
throw new \Exception( |
136
|
1 |
|
sprintf('Não foi encontrado o código do IBGE para o município "%s" do estado "%s"', $municipio, $uf), |
137
|
1 |
|
404 |
138
|
|
|
); |
139
|
|
|
} |
140
|
13 |
|
return $o['codigo']; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Obtém as notas pendentes de envio, em contingência e corrigidas após |
145
|
|
|
* rejeitadas |
146
|
|
|
*/ |
147
|
2 |
|
public function getNotasAbertas($inicio = null, $quantidade = null) |
148
|
|
|
{ |
149
|
2 |
|
return []; // TODO implementar |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Obtém as notas em processamento para consulta e possível protocolação |
154
|
|
|
*/ |
155
|
2 |
|
public function getNotasPendentes($inicio = null, $quantidade = null) |
156
|
|
|
{ |
157
|
2 |
|
return []; // TODO implementar |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Obtém as tarefas de inutilização, cancelamento e consulta de notas |
162
|
|
|
* pendentes que entraram em contingência |
163
|
|
|
*/ |
164
|
2 |
|
public function getNotasTarefas($inicio = null, $quantidade = null) |
165
|
|
|
{ |
166
|
2 |
|
return []; // TODO implementar |
167
|
|
|
} |
168
|
|
|
|
169
|
41 |
|
public function getInformacaoServico($emissao, $uf, $modelo = null, $ambiente = null) |
170
|
|
|
{ |
171
|
|
|
switch ($emissao) { |
172
|
41 |
|
case '1': |
173
|
1 |
|
$emissao = Nota::EMISSAO_NORMAL; |
174
|
1 |
|
break; |
175
|
41 |
|
case '9': |
176
|
1 |
|
$emissao = Nota::EMISSAO_CONTINGENCIA; |
177
|
1 |
|
break; |
178
|
|
|
} |
179
|
|
|
switch ($modelo) { |
180
|
41 |
|
case '55': |
181
|
1 |
|
$modelo = Nota::MODELO_NFE; |
182
|
1 |
|
break; |
183
|
41 |
|
case '65': |
184
|
1 |
|
$modelo = Nota::MODELO_NFCE; |
185
|
1 |
|
break; |
186
|
|
|
} |
187
|
41 |
|
if ($modelo == Nota::MODELO_NFCE) { |
188
|
39 |
|
$emissao = Nota::EMISSAO_NORMAL; // NFCe envia contingência pelo webservice normal |
189
|
|
|
} |
190
|
41 |
|
if (!isset($this->servicos[$emissao])) { |
191
|
1 |
|
throw new \Exception( |
192
|
1 |
|
sprintf('Falha ao obter o serviço da SEFAZ para o tipo de emissão "%s"', $emissao), |
193
|
1 |
|
404 |
194
|
|
|
); |
195
|
|
|
} |
196
|
40 |
|
$array = $this->servicos[$emissao]; |
197
|
40 |
|
if (!isset($array[strtoupper($uf)])) { |
198
|
1 |
|
throw new \Exception( |
199
|
1 |
|
sprintf('Falha ao obter o serviço da SEFAZ para a UF "%s"', $uf), |
200
|
1 |
|
404 |
201
|
|
|
); |
202
|
|
|
} |
203
|
40 |
|
$array = $array[strtoupper($uf)]; |
204
|
40 |
|
if (!is_array($array)) { |
205
|
|
|
$array = $this->getInformacaoServico($emissao, $array); |
206
|
|
|
} |
207
|
40 |
|
$_modelos = [Nota::MODELO_NFE, Nota::MODELO_NFCE]; |
208
|
40 |
|
foreach ($_modelos as $_modelo) { |
209
|
40 |
|
if (!isset($array[$_modelo])) { |
210
|
1 |
|
continue; |
211
|
|
|
} |
212
|
40 |
|
$node = $array[$_modelo]; |
213
|
40 |
|
if (!is_array($node)) { |
214
|
|
|
$node = $this->getInformacaoServico($emissao, $node, $_modelo); |
215
|
|
|
} |
216
|
40 |
|
if (isset($node['base'])) { |
217
|
3 |
|
$base = $this->getInformacaoServico($emissao, $node['base'], $_modelo); |
218
|
3 |
|
$node = array_replace_recursive($node, $base); |
219
|
|
|
} |
220
|
40 |
|
$array[$_modelo] = $node; |
221
|
|
|
} |
222
|
40 |
|
if (!is_null($modelo)) { |
223
|
40 |
|
if (!isset($array[$modelo])) { |
224
|
1 |
|
throw new \Exception( |
225
|
1 |
|
sprintf('Falha ao obter o serviço da SEFAZ para o modelo de nota "%s"', $modelo), |
226
|
1 |
|
404 |
227
|
|
|
); |
228
|
|
|
} |
229
|
40 |
|
$array = $array[$modelo]; |
230
|
|
|
} |
231
|
|
|
switch ($ambiente) { |
232
|
40 |
|
case '1': |
233
|
1 |
|
$ambiente = Nota::AMBIENTE_PRODUCAO; |
234
|
1 |
|
break; |
235
|
40 |
|
case '2': |
236
|
1 |
|
$ambiente = Nota::AMBIENTE_HOMOLOGACAO; |
237
|
1 |
|
break; |
238
|
|
|
} |
239
|
40 |
|
if (!is_null($modelo) && !is_null($ambiente)) { |
240
|
39 |
|
if (!isset($array[$ambiente])) { |
241
|
1 |
|
throw new \Exception( |
242
|
1 |
|
sprintf('Falha ao obter o serviço da SEFAZ para o ambiente "%s"', $ambiente), |
243
|
1 |
|
404 |
244
|
|
|
); |
245
|
|
|
} |
246
|
38 |
|
$array = $array[$ambiente]; |
247
|
|
|
} |
248
|
40 |
|
return $array; |
249
|
|
|
} |
250
|
|
|
|
251
|
3 |
|
public function toArray($recursive = false) |
252
|
|
|
{ |
253
|
3 |
|
$estatico = parent::toArray($recursive); |
254
|
3 |
|
$estatico['ibpt'] = $this->getIBPT(); |
255
|
3 |
|
return $estatico; |
256
|
|
|
} |
257
|
|
|
|
258
|
105 |
|
public function fromArray($estatico = []) |
259
|
|
|
{ |
260
|
105 |
|
if ($estatico instanceof Estatico) { |
261
|
3 |
|
$estatico = $estatico->toArray(); |
262
|
105 |
|
} elseif (!is_array($estatico)) { |
263
|
2 |
|
return $this; |
264
|
|
|
} |
265
|
105 |
|
parent::fromArray($estatico); |
266
|
105 |
|
$this->setIBPT(new IBPT(isset($estatico['ibpt']) ? $estatico['ibpt'] : [])); |
|
|
|
|
267
|
105 |
|
return $this; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: