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\Entity\Imposto; |
29
|
|
|
|
30
|
|
|
use NFe\Common\Util; |
31
|
|
|
use NFe\Entity\Imposto; |
32
|
|
|
use NFe\Exception\ValidationException; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Este grupo só precisa ser informado por emissores que sejam |
36
|
|
|
* contribuintes do IPI ou em uma operação de importação que tenha |
37
|
|
|
* incidência de IPI |
38
|
|
|
*/ |
39
|
|
|
class IPI extends Imposto |
40
|
|
|
{ |
41
|
|
|
|
42
|
|
|
private $classe; |
43
|
|
|
private $cnpj; |
44
|
|
|
private $selo; |
45
|
|
|
private $quantidade; |
46
|
|
|
private $enquadramento; |
47
|
|
|
private $tributo; |
48
|
|
|
|
49
|
7 |
|
public function __construct($ipi = array()) |
50
|
|
|
{ |
51
|
7 |
|
parent::__construct($ipi); |
52
|
7 |
|
$this->setGrupo(self::GRUPO_IPI); |
53
|
7 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* classe de enquadramento do IPI para Cigarros e Bebidas conforme Atos |
57
|
|
|
* Normativos editados pela Receita Federal do Brasil. |
58
|
|
|
*/ |
59
|
6 |
|
public function getClasse($normalize = false) |
60
|
|
|
{ |
61
|
6 |
|
if (!$normalize) { |
62
|
6 |
|
return $this->classe; |
63
|
|
|
} |
64
|
|
|
return $this->classe; |
65
|
|
|
} |
66
|
|
|
|
67
|
7 |
|
public function setClasse($classe) |
68
|
|
|
{ |
69
|
7 |
|
$this->classe = $classe; |
70
|
7 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* CNPJ do produtor da mercadoria, quando diferente do emitente nas |
75
|
|
|
* exportações direta ou indireta. |
76
|
|
|
*/ |
77
|
6 |
|
public function getCNPJ($normalize = false) |
78
|
|
|
{ |
79
|
6 |
|
if (!$normalize) { |
80
|
6 |
|
return $this->cnpj; |
81
|
|
|
} |
82
|
|
|
return $this->cnpj; |
83
|
|
|
} |
84
|
|
|
|
85
|
7 |
|
public function setCNPJ($cnpj) |
86
|
|
|
{ |
87
|
7 |
|
$this->cnpj = $cnpj; |
88
|
7 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* código do Selo de Controle do IPI conforme Atos Normativos editados pela |
93
|
|
|
* Receita Federal do Brasil. |
94
|
|
|
*/ |
95
|
6 |
|
public function getSelo($normalize = false) |
96
|
|
|
{ |
97
|
6 |
|
if (!$normalize) { |
98
|
6 |
|
return $this->selo; |
99
|
|
|
} |
100
|
|
|
return $this->selo; |
101
|
|
|
} |
102
|
|
|
|
103
|
7 |
|
public function setSelo($selo) |
104
|
|
|
{ |
105
|
7 |
|
$this->selo = $selo; |
106
|
7 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* quantidade de Selo de Controle do IPI utilizados. |
111
|
|
|
*/ |
112
|
6 |
|
public function getQuantidade($normalize = false) |
113
|
|
|
{ |
114
|
6 |
|
if (!$normalize) { |
115
|
6 |
|
return $this->quantidade; |
116
|
|
|
} |
117
|
|
|
return Util::toFloat($this->quantidade); |
118
|
|
|
} |
119
|
|
|
|
120
|
7 |
|
public function setQuantidade($quantidade) |
121
|
|
|
{ |
122
|
7 |
|
$this->quantidade = $quantidade; |
123
|
7 |
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Código de Enquadramento Legal do IPI, informar 999 enquanto a tabela não |
128
|
|
|
* tiver sido criada pela Receita Federal do Brasil |
129
|
|
|
*/ |
130
|
6 |
|
public function getEnquadramento($normalize = false) |
131
|
|
|
{ |
132
|
6 |
|
if (!$normalize) { |
133
|
3 |
|
return $this->enquadramento; |
134
|
|
|
} |
135
|
6 |
|
return $this->enquadramento; |
136
|
|
|
} |
137
|
|
|
|
138
|
7 |
|
public function setEnquadramento($enquadramento) |
139
|
|
|
{ |
140
|
7 |
|
$this->enquadramento = $enquadramento; |
141
|
7 |
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Informa o imposto aplicado |
146
|
|
|
*/ |
147
|
6 |
|
public function getTributo() |
148
|
|
|
{ |
149
|
6 |
|
return $this->tributo; |
150
|
|
|
} |
151
|
|
|
|
152
|
7 |
|
public function setTributo($tributo) |
153
|
|
|
{ |
154
|
7 |
|
$this->tributo = $tributo; |
155
|
7 |
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Calcula o valor do imposto com base no tributo |
160
|
|
|
*/ |
161
|
3 |
|
public function getValor($normalize = false) |
162
|
|
|
{ |
163
|
3 |
|
if (!$normalize) { |
164
|
3 |
|
return $this->getTributo()->getValor(); |
165
|
|
|
} |
166
|
|
|
return Util::toCurrency($this->getValor()); |
167
|
|
|
} |
168
|
|
|
|
169
|
3 |
|
public function toArray($recursive = false) |
170
|
|
|
{ |
171
|
3 |
|
$ipi = parent::toArray($recursive); |
172
|
3 |
|
$ipi['classe'] = $this->getClasse(); |
173
|
3 |
|
$ipi['cnpj'] = $this->getCNPJ(); |
174
|
3 |
|
$ipi['selo'] = $this->getSelo(); |
175
|
3 |
|
$ipi['quantidade'] = $this->getQuantidade(); |
176
|
3 |
|
$ipi['enquadramento'] = $this->getEnquadramento(); |
177
|
3 |
|
if (!is_null($this->getTributo()) && $recursive) { |
178
|
|
|
$ipi['tributo'] = $this->getTributo()->toArray($recursive); |
179
|
|
|
} else { |
180
|
3 |
|
$ipi['tributo'] = $this->getTributo(); |
181
|
|
|
} |
182
|
3 |
|
return $ipi; |
183
|
|
|
} |
184
|
|
|
|
185
|
7 |
|
public function fromArray($ipi = array()) |
|
|
|
|
186
|
|
|
{ |
187
|
7 |
|
if ($ipi instanceof IPI) { |
188
|
3 |
|
$ipi = $ipi->toArray(); |
189
|
7 |
|
} elseif (!is_array($ipi)) { |
190
|
3 |
|
return $this; |
191
|
|
|
} |
192
|
7 |
|
parent::fromArray($ipi); |
193
|
7 |
|
if (isset($ipi['classe'])) { |
194
|
|
|
$this->setClasse($ipi['classe']); |
195
|
|
|
} else { |
196
|
7 |
|
$this->setClasse(null); |
197
|
|
|
} |
198
|
7 |
|
if (isset($ipi['cnpj'])) { |
199
|
|
|
$this->setCNPJ($ipi['cnpj']); |
200
|
|
|
} else { |
201
|
7 |
|
$this->setCNPJ(null); |
202
|
|
|
} |
203
|
7 |
|
if (isset($ipi['selo'])) { |
204
|
|
|
$this->setSelo($ipi['selo']); |
205
|
|
|
} else { |
206
|
7 |
|
$this->setSelo(null); |
207
|
|
|
} |
208
|
7 |
|
if (isset($ipi['quantidade'])) { |
209
|
|
|
$this->setQuantidade($ipi['quantidade']); |
210
|
|
|
} else { |
211
|
7 |
|
$this->setQuantidade(null); |
212
|
|
|
} |
213
|
7 |
|
if (!isset($ipi['enquadramento']) || is_null($ipi['enquadramento'])) { |
214
|
7 |
|
$this->setEnquadramento('999'); |
215
|
7 |
|
} else { |
216
|
3 |
|
$this->setEnquadramento($ipi['enquadramento']); |
217
|
|
|
} |
218
|
7 |
|
if (isset($ipi['tributo'])) { |
219
|
3 |
|
$this->setTributo($ipi['tributo']); |
220
|
3 |
|
} else { |
221
|
7 |
|
$this->setTributo(null); |
222
|
|
|
} |
223
|
7 |
|
return $this; |
224
|
|
|
} |
225
|
|
|
|
226
|
6 |
|
public function getNode($name = null) |
227
|
|
|
{ |
228
|
6 |
|
$dom = new \DOMDocument('1.0', 'UTF-8'); |
229
|
6 |
|
$element = $dom->createElement(is_null($name)?'IPI':$name); |
230
|
6 |
|
if (!is_null($this->getClasse())) { |
231
|
|
|
Util::appendNode($element, 'clEnq', $this->getClasse(true)); |
232
|
|
|
} |
233
|
6 |
|
if (!is_null($this->getCNPJ())) { |
234
|
|
|
Util::appendNode($element, 'CNPJProd', $this->getCNPJ(true)); |
235
|
|
|
} |
236
|
6 |
|
if (!is_null($this->getSelo())) { |
237
|
|
|
Util::appendNode($element, 'cSelo', $this->getSelo(true)); |
238
|
|
|
} |
239
|
6 |
|
if (!is_null($this->getQuantidade())) { |
240
|
|
|
Util::appendNode($element, 'qSelo', $this->getQuantidade(true)); |
241
|
|
|
} |
242
|
6 |
|
Util::appendNode($element, 'cEnq', $this->getEnquadramento(true)); |
243
|
6 |
|
if (is_null($this->getTributo())) { |
244
|
|
|
throw new ValidationException(array('tributo' => 'O tributo do imposto IPI não foi informado')); |
245
|
|
|
} |
246
|
6 |
|
$tributo = $this->getTributo()->getNode(); |
247
|
6 |
|
$tributo = $dom->importNode($tributo, true); |
248
|
6 |
|
$element->appendChild($tributo); |
249
|
6 |
|
return $element; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
|
253
|
3 |
|
public function loadNode($element, $name = null) |
254
|
|
|
{ |
255
|
3 |
|
$name = is_null($name)?'IPI':$name; |
256
|
3 |
|
if ($element->nodeName != $name) { |
257
|
|
|
$_fields = $element->getElementsByTagName($name); |
258
|
|
|
if ($_fields->length == 0) { |
259
|
|
|
throw new \Exception('Tag "'.$name.'" não encontrada', 404); |
260
|
|
|
} |
261
|
|
|
$element = $_fields->item(0); |
262
|
|
|
} |
263
|
3 |
|
$this->setClasse(Util::loadNode($element, 'clEnq')); |
264
|
3 |
|
$this->setCNPJ(Util::loadNode($element, 'CNPJProd')); |
265
|
3 |
|
$this->setSelo(Util::loadNode($element, 'cSelo')); |
266
|
3 |
|
$this->setQuantidade(Util::loadNode($element, 'qSelo')); |
267
|
3 |
|
$this->setEnquadramento( |
268
|
3 |
|
Util::loadNode( |
269
|
3 |
|
$element, |
270
|
3 |
|
'cEnq', |
271
|
|
|
'Tag "cEnq" do campo "Enquadramento" não encontrada' |
272
|
3 |
|
) |
273
|
3 |
|
); |
274
|
3 |
|
$_fields = $element->getElementsByTagName('IPITrib'); |
275
|
3 |
|
if ($_fields->length == 0) { |
276
|
1 |
|
$_fields = $element->getElementsByTagName('IPINT'); |
277
|
1 |
|
} |
278
|
3 |
|
if ($_fields->length > 0) { |
279
|
3 |
|
$tributo = Imposto::loadImposto($_fields->item(0)); |
280
|
3 |
|
} else { |
281
|
|
|
throw new \Exception('Tag "IPITrib" ou "IPINT" do objeto "Tributo" não encontrada', 404); |
282
|
|
|
} |
283
|
3 |
|
$this->setTributo($tributo); |
284
|
3 |
|
return $element; |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.
You can also find more information in the “Code” section of your repository.