|
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 Curl\Curl; |
|
31
|
|
|
use NFe\Log\Logger; |
|
32
|
|
|
|
|
33
|
|
|
class IBPT |
|
34
|
|
|
{ |
|
35
|
|
|
|
|
36
|
|
|
private $tabela; |
|
37
|
|
|
private $offline; |
|
38
|
|
|
|
|
39
|
9 |
|
public function __construct() |
|
40
|
|
|
{ |
|
41
|
9 |
|
$this->tabela = array(); |
|
42
|
9 |
|
$this->offline = false; |
|
43
|
9 |
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
public function isOffline() |
|
46
|
|
|
{ |
|
47
|
1 |
|
return $this->offline; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
public function setOffline($offline) |
|
51
|
|
|
{ |
|
52
|
1 |
|
$this->offline = $offline; |
|
53
|
1 |
|
} |
|
54
|
|
|
|
|
55
|
9 |
|
private function load($uf) |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
9 |
|
if (isset($this->tabela[$uf])) { |
|
58
|
9 |
|
return $this->tabela[$uf]; |
|
59
|
|
|
} |
|
60
|
3 |
|
$file = __DIR__ . '/data/IBPT/'.$uf.'.json'; |
|
61
|
3 |
|
if (!file_exists($file)) { |
|
62
|
2 |
|
return false; |
|
63
|
|
|
} |
|
64
|
2 |
|
$content = file_get_contents($file); |
|
65
|
2 |
|
if ($content === false) { |
|
66
|
|
|
return false; |
|
67
|
|
|
} |
|
68
|
2 |
|
$data = json_decode($content, true); |
|
69
|
2 |
|
$this->tabela[$uf] = $data; |
|
70
|
2 |
|
return $data; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
9 |
|
private function getImpostoOffline($ncm, $uf, $ex) |
|
|
|
|
|
|
74
|
|
|
{ |
|
75
|
9 |
|
$data = $this->load($uf); |
|
76
|
9 |
|
if ($data === false) { |
|
77
|
2 |
|
return false; |
|
78
|
|
|
} |
|
79
|
9 |
|
$key = $ncm.'.'.sprintf('%02s', $ex); |
|
80
|
9 |
|
$o = $data['estados'][$uf][$key]; |
|
|
|
|
|
|
81
|
9 |
|
if (is_null($o)) { |
|
82
|
|
|
return false; |
|
83
|
|
|
} |
|
84
|
9 |
|
$o['info'] = $data['info']; |
|
85
|
9 |
|
$o['info']['origem'] = 'Tabela offline'; |
|
86
|
9 |
|
return $o; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
private function getImpostoOnline($cnpj, $token, $ncm, $uf, $ex) |
|
|
|
|
|
|
90
|
|
|
{ |
|
91
|
1 |
|
if ($this->isOffline()) { |
|
92
|
1 |
|
return false; |
|
93
|
|
|
} |
|
94
|
1 |
|
$url = 'http://iws.ibpt.org.br/api/Produtos'; |
|
95
|
|
|
$params = array( |
|
96
|
1 |
|
'token' => $token, |
|
97
|
1 |
|
'cnpj' => $cnpj, |
|
98
|
1 |
|
'codigo' => $ncm, |
|
99
|
1 |
|
'uf' => $uf, |
|
100
|
1 |
|
'ex' => intval($ex) |
|
101
|
1 |
|
); |
|
102
|
1 |
|
$curl = new Curl($url); |
|
103
|
1 |
|
$curl->setConnectTimeout(2); |
|
104
|
1 |
|
$curl->setTimeout(3); |
|
105
|
1 |
|
$data = $curl->get($params); |
|
106
|
1 |
|
if ($curl->error) { |
|
107
|
1 |
|
Logger::warning('IBPT.getImpostoOnline('.$curl->errorCode.') - '.$curl->errorMessage); |
|
|
|
|
|
|
108
|
1 |
|
$this->setOffline(true); |
|
109
|
1 |
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
$o = array( |
|
|
|
|
|
|
112
|
|
|
'importado' => $data->Importado, |
|
113
|
1 |
|
'nacional' => $data->Nacional, |
|
114
|
|
|
'estadual' => $data->Estadual, |
|
115
|
|
|
'municipal' => $data->Municipal, |
|
116
|
|
|
'tipo' => $data->Tipo |
|
117
|
|
|
); |
|
118
|
|
|
$vigenciainicio = date_create_from_format('d/m/Y', $data->VigenciaInicio); |
|
119
|
|
|
$vigenciafim = date_create_from_format('d/m/Y', $data->VigenciaFim); |
|
120
|
|
|
$info = array( |
|
121
|
|
|
'origem' => 'API IBPT', |
|
122
|
|
|
'fonte' => $data->Fonte, |
|
123
|
|
|
'versao' => $data->Versao, |
|
124
|
|
|
'chave' => $data->Chave, |
|
125
|
|
|
'vigencia' => array( |
|
126
|
|
|
'inicio' => date_format($vigenciainicio, 'Y-m-d'), |
|
127
|
|
|
'fim' => date_format($vigenciafim, 'Y-m-d') |
|
128
|
|
|
) |
|
129
|
|
|
); |
|
130
|
|
|
$o['info'] = $info; |
|
131
|
|
|
return $o; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
9 |
|
public function getImposto($cnpj, $token, $ncm, $uf, $ex) |
|
|
|
|
|
|
135
|
|
|
{ |
|
136
|
9 |
|
$uf = strtoupper($uf); |
|
137
|
9 |
|
$uf = preg_replace('/[^A-Z]/', '', $uf); |
|
138
|
9 |
|
if (is_null($cnpj) || is_null($token)) { |
|
139
|
9 |
|
return $this->getImpostoOffline($ncm, $uf, $ex); |
|
140
|
|
|
} |
|
141
|
1 |
|
$o = $this->getImpostoOnline($cnpj, $token, $ncm, $uf, $ex); |
|
|
|
|
|
|
142
|
1 |
|
if ($o === false) { |
|
143
|
1 |
|
return $this->getImpostoOffline($ncm, $uf, $ex); |
|
144
|
|
|
} |
|
145
|
|
|
return $o; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.