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; |
29
|
|
|
|
30
|
|
|
use NFe\Core\SEFAZ; |
31
|
|
|
use NFe\Common\Node; |
32
|
|
|
use NFe\Common\Util; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Produto ou serviço que está sendo vendido ou prestado e será adicionado |
36
|
|
|
* na nota fiscal |
37
|
|
|
*/ |
38
|
|
|
class Produto extends Total |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Unidade do produto, Não informar a grandeza |
43
|
|
|
*/ |
44
|
|
|
const UNIDADE_UNIDADE = 'unidade'; |
45
|
|
|
const UNIDADE_PECA = 'peca'; |
46
|
|
|
const UNIDADE_METRO = 'metro'; |
47
|
|
|
const UNIDADE_GRAMA = 'grama'; |
48
|
|
|
const UNIDADE_LITRO = 'litro'; |
49
|
|
|
|
50
|
|
|
private $item; |
51
|
|
|
private $pedido; |
52
|
|
|
private $codigo; |
53
|
|
|
private $codigo_tributario; |
54
|
|
|
private $codigo_barras; |
55
|
|
|
private $descricao; |
56
|
|
|
private $unidade; |
57
|
|
|
private $multiplicador; |
58
|
|
|
private $quantidade; |
59
|
|
|
private $tributada; |
60
|
|
|
private $peso; |
61
|
|
|
private $excecao; |
62
|
|
|
private $cfop; |
63
|
|
|
private $ncm; |
64
|
|
|
private $cest; |
65
|
|
|
private $impostos; |
66
|
|
|
|
67
|
31 |
|
public function __construct($produto = array()) |
68
|
|
|
{ |
69
|
31 |
|
$this->fromArray($produto); |
70
|
31 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Número do Item do Pedido de Compra - Identificação do número do item do |
74
|
|
|
* pedido de Compra |
75
|
|
|
*/ |
76
|
30 |
|
public function getItem($normalize = false) |
77
|
|
|
{ |
78
|
30 |
|
if (!$normalize) { |
79
|
29 |
|
return $this->item; |
80
|
|
|
} |
81
|
29 |
|
return $this->item; |
82
|
|
|
} |
83
|
|
|
|
84
|
31 |
|
public function setItem($item) |
85
|
|
|
{ |
86
|
31 |
|
if (trim($item) != '') { |
87
|
30 |
|
$item = intval($item); |
88
|
30 |
|
} |
89
|
31 |
|
$this->item = $item; |
90
|
31 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* informar o número do pedido de compra, o campo é de livre uso do emissor |
95
|
|
|
*/ |
96
|
29 |
|
public function getPedido($normalize = false) |
97
|
|
|
{ |
98
|
29 |
|
if (!$normalize) { |
99
|
29 |
|
return $this->pedido; |
100
|
|
|
} |
101
|
2 |
|
return $this->pedido; |
102
|
|
|
} |
103
|
|
|
|
104
|
31 |
|
public function setPedido($pedido) |
105
|
|
|
{ |
106
|
31 |
|
$this->pedido = $pedido; |
107
|
31 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Código do produto ou serviço. Preencher com CFOP caso se trate de itens |
112
|
|
|
* não relacionados com mercadorias/produto e que o contribuinte não possua |
113
|
|
|
* codificação própria |
114
|
|
|
* Formato ”CFOP9999”. |
115
|
|
|
*/ |
116
|
29 |
|
public function getCodigo($normalize = false) |
117
|
|
|
{ |
118
|
29 |
|
if (!$normalize) { |
119
|
4 |
|
return $this->codigo; |
120
|
|
|
} |
121
|
29 |
|
return $this->codigo; |
122
|
|
|
} |
123
|
|
|
|
124
|
31 |
|
public function setCodigo($codigo) |
125
|
|
|
{ |
126
|
31 |
|
$this->codigo = $codigo; |
127
|
31 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Código do produto ou serviço. Preencher com CFOP caso se trate de itens |
132
|
|
|
* não relacionados com mercadorias/produto e que o contribuinte não possua |
133
|
|
|
* codificação própria |
134
|
|
|
* Formato ”CFOP9999”. |
135
|
|
|
*/ |
136
|
29 |
|
public function getCodigoTributario($normalize = false) |
137
|
|
|
{ |
138
|
29 |
|
if (!$normalize) { |
139
|
4 |
|
return $this->codigo_tributario; |
140
|
|
|
} |
141
|
29 |
|
return $this->codigo_tributario; |
142
|
|
|
} |
143
|
|
|
|
144
|
31 |
|
public function setCodigoTributario($codigo_tributario) |
145
|
|
|
{ |
146
|
31 |
|
$this->codigo_tributario = $codigo_tributario; |
147
|
31 |
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* GTIN (Global Trade Item Number) do produto, antigo código EAN ou código |
152
|
|
|
* de barras |
153
|
|
|
*/ |
154
|
29 |
|
public function getCodigoBarras($normalize = false) |
155
|
|
|
{ |
156
|
29 |
|
if (!$normalize) { |
157
|
4 |
|
return $this->codigo_barras; |
158
|
|
|
} |
159
|
29 |
|
return $this->codigo_barras; |
160
|
|
|
} |
161
|
|
|
|
162
|
31 |
|
public function setCodigoBarras($codigo_barras) |
163
|
|
|
{ |
164
|
31 |
|
$this->codigo_barras = $codigo_barras; |
165
|
31 |
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Descrição do produto ou serviço |
170
|
|
|
*/ |
171
|
30 |
|
public function getDescricao($normalize = false) |
172
|
|
|
{ |
173
|
30 |
|
if (!$normalize) { |
174
|
5 |
|
return $this->descricao; |
175
|
|
|
} |
176
|
29 |
|
return $this->descricao; |
177
|
|
|
} |
178
|
|
|
|
179
|
31 |
|
public function setDescricao($descricao) |
180
|
|
|
{ |
181
|
31 |
|
$this->descricao = $descricao; |
182
|
31 |
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Unidade do produto, Não informar a grandeza |
187
|
|
|
*/ |
188
|
29 |
|
public function getUnidade($normalize = false) |
189
|
|
|
{ |
190
|
29 |
|
if (!$normalize) { |
191
|
4 |
|
return $this->unidade; |
192
|
|
|
} |
193
|
29 |
|
switch ($this->unidade) { |
194
|
29 |
|
case self::UNIDADE_UNIDADE: |
195
|
29 |
|
return 'UN'; |
196
|
|
|
case self::UNIDADE_PECA: |
197
|
|
|
return 'PC'; |
198
|
|
|
case self::UNIDADE_METRO: |
199
|
|
|
return 'm'; |
200
|
|
|
case self::UNIDADE_GRAMA: |
201
|
|
|
return 'g'; |
202
|
|
|
case self::UNIDADE_LITRO: |
203
|
|
|
return 'L'; |
204
|
|
|
} |
205
|
|
|
return $this->unidade; |
206
|
|
|
} |
207
|
|
|
|
208
|
31 |
|
public function setUnidade($unidade) |
209
|
|
|
{ |
210
|
|
|
switch ($unidade) { |
211
|
31 |
|
case 'UN': |
212
|
27 |
|
$unidade = self::UNIDADE_UNIDADE; |
213
|
27 |
|
break; |
214
|
31 |
|
case 'PC': |
215
|
|
|
$unidade = self::UNIDADE_PECA; |
216
|
|
|
break; |
217
|
31 |
|
case 'm': |
218
|
|
|
$unidade = self::UNIDADE_METRO; |
219
|
|
|
break; |
220
|
31 |
|
case 'g': |
221
|
|
|
$unidade = self::UNIDADE_GRAMA; |
222
|
|
|
break; |
223
|
31 |
|
case 'L': |
224
|
|
|
$unidade = self::UNIDADE_LITRO; |
225
|
|
|
break; |
226
|
|
|
} |
227
|
31 |
|
$this->unidade = $unidade; |
228
|
31 |
|
return $this; |
229
|
|
|
} |
230
|
|
|
|
231
|
29 |
|
public function getMultiplicador($normalize = false) |
232
|
|
|
{ |
233
|
29 |
|
if (!$normalize) { |
234
|
28 |
|
return $this->multiplicador; |
235
|
|
|
} |
236
|
29 |
|
return $this->multiplicador; |
237
|
|
|
} |
238
|
|
|
|
239
|
31 |
|
public function setMultiplicador($multiplicador) |
240
|
|
|
{ |
241
|
31 |
|
if (trim($multiplicador) != '') { |
242
|
31 |
|
$multiplicador = intval($multiplicador); |
243
|
31 |
|
} |
244
|
31 |
|
$this->multiplicador = $multiplicador; |
245
|
31 |
|
return $this; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Valor unitário de comercialização - alterado para aceitar 0 a 10 casas |
250
|
|
|
* decimais e 11 inteiros |
251
|
|
|
*/ |
252
|
30 |
|
public function getPreco($normalize = false) |
253
|
|
|
{ |
254
|
30 |
|
return parent::getProdutos($normalize); |
|
|
|
|
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Altera o preço total do produto para o informado no parâmetro |
259
|
|
|
* @param mixed $preco novo preço para o Produto |
260
|
|
|
* @return Produto A própria instância da classe |
261
|
|
|
*/ |
262
|
31 |
|
public function setPreco($preco) |
263
|
|
|
{ |
264
|
31 |
|
return parent::setProdutos($preco); |
|
|
|
|
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Quantidade Comercial do produto, alterado para aceitar de 0 a 4 casas |
269
|
|
|
* decimais e 11 inteiros. |
270
|
|
|
*/ |
271
|
29 |
|
public function getQuantidade($normalize = false) |
272
|
|
|
{ |
273
|
29 |
|
if (!$normalize) { |
274
|
29 |
|
return $this->quantidade; |
275
|
|
|
} |
276
|
29 |
|
return Util::toFloat($this->quantidade); |
277
|
|
|
} |
278
|
|
|
|
279
|
31 |
|
public function setQuantidade($quantidade) |
280
|
|
|
{ |
281
|
31 |
|
if (trim($quantidade) != '') { |
282
|
30 |
|
$quantidade = floatval($quantidade); |
283
|
30 |
|
} |
284
|
31 |
|
$this->quantidade = $quantidade; |
285
|
31 |
|
return $this; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Informa a quantidade tributada |
290
|
|
|
*/ |
291
|
29 |
|
public function getTributada($normalize = false) |
292
|
|
|
{ |
293
|
29 |
|
if (!$normalize) { |
294
|
29 |
|
return is_null($this->tributada)?$this->getQuantidade():$this->tributada; |
295
|
|
|
} |
296
|
29 |
|
return Util::toFloat($this->getTributada()); |
297
|
|
|
} |
298
|
|
|
|
299
|
31 |
|
public function setTributada($tributada) |
300
|
|
|
{ |
301
|
31 |
|
if (trim($tributada) != '') { |
302
|
30 |
|
$tributada = floatval($tributada); |
303
|
30 |
|
} |
304
|
31 |
|
$this->tributada = $tributada; |
305
|
31 |
|
return $this; |
306
|
|
|
} |
307
|
|
|
|
308
|
4 |
|
public function getPeso() |
309
|
|
|
{ |
310
|
4 |
|
return $this->peso; |
311
|
|
|
} |
312
|
|
|
|
313
|
31 |
|
public function setPeso($peso) |
314
|
|
|
{ |
315
|
31 |
|
$this->peso = $peso; |
316
|
31 |
|
return $this; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Código EX TIPI |
321
|
|
|
*/ |
322
|
30 |
|
public function getExcecao($normalize = false) |
323
|
|
|
{ |
324
|
30 |
|
if (!$normalize) { |
325
|
30 |
|
return $this->excecao; |
326
|
|
|
} |
327
|
|
|
return Util::padDigit($this->excecao, 2); |
328
|
|
|
} |
329
|
|
|
|
330
|
31 |
|
public function setExcecao($excecao) |
331
|
|
|
{ |
332
|
31 |
|
$this->excecao = $excecao; |
333
|
31 |
|
return $this; |
334
|
|
|
} |
335
|
|
|
|
336
|
29 |
|
public function getCFOP($normalize = false) |
337
|
|
|
{ |
338
|
29 |
|
if (!$normalize) { |
339
|
4 |
|
return $this->cfop; |
340
|
|
|
} |
341
|
29 |
|
return $this->cfop; |
342
|
|
|
} |
343
|
|
|
|
344
|
31 |
|
public function setCFOP($cfop) |
345
|
|
|
{ |
346
|
31 |
|
if (trim($cfop) != '') { |
347
|
30 |
|
$cfop = intval($cfop); |
348
|
30 |
|
} |
349
|
31 |
|
$this->cfop = $cfop; |
350
|
31 |
|
return $this; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Código NCM (8 posições), será permitida a informação do gênero (posição |
355
|
|
|
* do capítulo do NCM) quando a operação não for de comércio exterior |
356
|
|
|
* (importação/exportação) ou o produto não seja tributado pelo IPI. Em |
357
|
|
|
* caso de item de serviço ou item que não tenham produto (Ex. |
358
|
|
|
* transferência de crédito, crédito do ativo imobilizado, etc.), informar |
359
|
|
|
* o código 00 (zeros) (v2.0) |
360
|
|
|
*/ |
361
|
30 |
|
public function getNCM($normalize = false) |
362
|
|
|
{ |
363
|
30 |
|
if (!$normalize) { |
364
|
30 |
|
return $this->ncm; |
365
|
|
|
} |
366
|
29 |
|
return $this->ncm; |
367
|
|
|
} |
368
|
|
|
|
369
|
31 |
|
public function setNCM($ncm) |
370
|
|
|
{ |
371
|
31 |
|
$this->ncm = $ncm; |
372
|
31 |
|
return $this; |
373
|
|
|
} |
374
|
|
|
|
375
|
29 |
|
public function getCEST($normalize = false) |
376
|
|
|
{ |
377
|
29 |
|
if (!$normalize) { |
378
|
29 |
|
return $this->cest; |
379
|
|
|
} |
380
|
29 |
|
return $this->cest; |
381
|
|
|
} |
382
|
|
|
|
383
|
31 |
|
public function setCEST($cest) |
384
|
|
|
{ |
385
|
31 |
|
$this->cest = $cest; |
386
|
31 |
|
return $this; |
387
|
|
|
} |
388
|
|
|
|
389
|
29 |
|
public function getImpostos() |
390
|
|
|
{ |
391
|
29 |
|
return $this->impostos; |
392
|
|
|
} |
393
|
|
|
|
394
|
31 |
|
public function setImpostos($impostos) |
395
|
|
|
{ |
396
|
31 |
|
$this->impostos = $impostos; |
397
|
31 |
|
return $this; |
398
|
|
|
} |
399
|
|
|
|
400
|
3 |
|
public function addImposto($imposto) |
401
|
|
|
{ |
402
|
3 |
|
$this->impostos[] = $imposto; |
403
|
3 |
|
return $this; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* Valor unitário |
408
|
|
|
*/ |
409
|
29 |
|
public function getPrecoUnitario($normalize = false) |
410
|
|
|
{ |
411
|
29 |
|
if (!$normalize) { |
412
|
29 |
|
return $this->getPreco() / $this->getQuantidade(); |
413
|
|
|
} |
414
|
29 |
|
return Util::toCurrency($this->getPrecoUnitario(), 10); |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* Valor tributável |
419
|
|
|
*/ |
420
|
29 |
|
public function getPrecoTributavel($normalize = false) |
421
|
|
|
{ |
422
|
29 |
|
if (!$normalize) { |
423
|
29 |
|
return $this->getPreco() / $this->getTributada(); |
424
|
|
|
} |
425
|
29 |
|
return Util::toCurrency($this->getPrecoTributavel(), 10); |
426
|
|
|
} |
427
|
|
|
|
428
|
30 |
|
public function getBase($normalize = false) |
429
|
|
|
{ |
430
|
30 |
|
if (!$normalize) { |
431
|
30 |
|
return $this->getPreco() - $this->getDesconto(); |
432
|
|
|
} |
433
|
|
|
return Util::toCurrency($this->getBase()); |
434
|
|
|
} |
435
|
|
|
|
436
|
30 |
|
public function getImpostoInfo() |
437
|
|
|
{ |
438
|
30 |
|
$config = SEFAZ::getInstance()->getConfiguracao(); |
439
|
30 |
|
$db = $config->getBanco(); |
|
|
|
|
440
|
30 |
|
$endereco = $config->getEmitente()->getEndereco(); |
441
|
30 |
|
$info = array('total' => 0.00); |
442
|
|
|
$tipos = array( |
443
|
|
|
// Imposto::TIPO_IMPORTADO, // TODO: determinar quando usar |
444
|
30 |
|
Imposto::TIPO_NACIONAL, |
445
|
30 |
|
Imposto::TIPO_ESTADUAL, |
446
|
|
|
Imposto::TIPO_MUNICIPAL |
447
|
30 |
|
); |
448
|
30 |
|
$imposto = new \NFe\Entity\Imposto\Total(); |
449
|
30 |
|
$imposto->setBase($this->getBase()); |
450
|
30 |
|
$aliquota = $db->getImpostoAliquota( |
451
|
30 |
|
$this->getNCM(), |
452
|
30 |
|
$endereco->getMunicipio()->getEstado()->getUF(), |
453
|
30 |
|
$this->getExcecao(), |
454
|
30 |
|
$config->getEmitente()->getCNPJ(), |
455
|
30 |
|
$config->getTokenIBPT() |
456
|
30 |
|
); |
457
|
30 |
|
if ($aliquota === false) { |
458
|
1 |
|
throw new \Exception( |
459
|
1 |
|
sprintf( |
460
|
1 |
|
'NCM inválido no item %d - "%s"', |
461
|
1 |
|
$this->getItem(), $this->getDescricao() |
462
|
1 |
|
), |
463
|
|
|
404 |
464
|
1 |
|
); |
465
|
|
|
} |
466
|
29 |
|
foreach ($tipos as $tipo) { |
467
|
29 |
|
$imposto->setAliquota($aliquota[$tipo]); |
468
|
29 |
|
$tributo = round($imposto->getTotal(), 2); |
469
|
29 |
|
$info[$tipo] = $tributo; |
470
|
29 |
|
$info['total'] += $tributo; |
471
|
29 |
|
} |
472
|
29 |
|
$info['info'] = $aliquota['info']; |
473
|
29 |
|
return $info; |
474
|
|
|
} |
475
|
|
|
|
476
|
4 |
|
public function toArray($recursive = false) |
477
|
|
|
{ |
478
|
4 |
|
$produto = parent::toArray($recursive); |
479
|
4 |
|
unset($produto['produtos']); |
480
|
4 |
|
$produto['item'] = $this->getItem(); |
481
|
4 |
|
$produto['pedido'] = $this->getPedido(); |
482
|
4 |
|
$produto['codigo'] = $this->getCodigo(); |
483
|
4 |
|
$produto['codigo_tributario'] = $this->getCodigoTributario(); |
484
|
4 |
|
$produto['codigo_barras'] = $this->getCodigoBarras(); |
485
|
4 |
|
$produto['descricao'] = $this->getDescricao(); |
486
|
4 |
|
$produto['unidade'] = $this->getUnidade(); |
487
|
4 |
|
$produto['multiplicador'] = $this->getMultiplicador(); |
488
|
4 |
|
$produto['preco'] = $this->getPreco(); |
489
|
4 |
|
$produto['quantidade'] = $this->getQuantidade(); |
490
|
4 |
|
$produto['tributada'] = $this->getTributada(); |
491
|
4 |
|
if (!is_null($this->getPeso()) && $recursive) { |
492
|
1 |
|
$produto['peso'] = $this->getPeso()->toArray($recursive); |
493
|
1 |
|
} else { |
494
|
3 |
|
$produto['peso'] = $this->getPeso(); |
495
|
|
|
} |
496
|
4 |
|
$produto['excecao'] = $this->getExcecao(); |
497
|
4 |
|
$produto['cfop'] = $this->getCFOP(); |
498
|
4 |
|
$produto['ncm'] = $this->getNCM(); |
499
|
4 |
|
$produto['cest'] = $this->getCEST(); |
500
|
4 |
|
if ($recursive) { |
501
|
1 |
|
$impostos = array(); |
502
|
1 |
|
$_impostos = $this->getImpostos(); |
503
|
1 |
|
foreach ($_impostos as $_imposto) { |
504
|
1 |
|
$impostos[] = $_imposto->toArray($recursive); |
505
|
1 |
|
} |
506
|
1 |
|
$produto['impostos'] = $impostos; |
507
|
1 |
|
} else { |
508
|
3 |
|
$produto['impostos'] = $this->getImpostos(); |
509
|
|
|
} |
510
|
4 |
|
return $produto; |
511
|
|
|
} |
512
|
|
|
|
513
|
31 |
|
public function fromArray($produto = array()) |
|
|
|
|
514
|
|
|
{ |
515
|
31 |
|
if ($produto instanceof Produto) { |
516
|
3 |
|
$produto = $produto->toArray(); |
517
|
31 |
|
} elseif (!is_array($produto)) { |
518
|
3 |
|
return $this; |
519
|
|
|
} |
520
|
31 |
|
parent::fromArray($produto); |
521
|
31 |
|
if (isset($produto['item'])) { |
522
|
1 |
|
$this->setItem($produto['item']); |
523
|
1 |
|
} else { |
524
|
31 |
|
$this->setItem(null); |
525
|
|
|
} |
526
|
31 |
|
if (isset($produto['pedido'])) { |
527
|
1 |
|
$this->setPedido($produto['pedido']); |
528
|
1 |
|
} else { |
529
|
31 |
|
$this->setPedido(null); |
530
|
|
|
} |
531
|
31 |
|
if (isset($produto['codigo'])) { |
532
|
3 |
|
$this->setCodigo($produto['codigo']); |
533
|
3 |
|
} else { |
534
|
31 |
|
$this->setCodigo(null); |
535
|
|
|
} |
536
|
31 |
|
if (isset($produto['codigo_tributario'])) { |
537
|
1 |
|
$this->setCodigoTributario($produto['codigo_tributario']); |
538
|
1 |
|
} else { |
539
|
31 |
|
$this->setCodigoTributario(null); |
540
|
|
|
} |
541
|
31 |
|
if (isset($produto['codigo_barras'])) { |
542
|
3 |
|
$this->setCodigoBarras($produto['codigo_barras']); |
543
|
3 |
|
} else { |
544
|
31 |
|
$this->setCodigoBarras(null); |
545
|
|
|
} |
546
|
31 |
|
if (isset($produto['descricao'])) { |
547
|
3 |
|
$this->setDescricao($produto['descricao']); |
548
|
3 |
|
} else { |
549
|
31 |
|
$this->setDescricao(null); |
550
|
|
|
} |
551
|
31 |
|
if (!isset($produto['unidade']) || is_null($produto['unidade'])) { |
552
|
31 |
|
$this->setUnidade(self::UNIDADE_UNIDADE); |
553
|
31 |
|
} else { |
554
|
3 |
|
$this->setUnidade($produto['unidade']); |
555
|
|
|
} |
556
|
31 |
|
if (!isset($produto['multiplicador']) || is_null($produto['multiplicador'])) { |
557
|
31 |
|
$this->setMultiplicador(1); |
558
|
31 |
|
} else { |
559
|
3 |
|
$this->setMultiplicador($produto['multiplicador']); |
560
|
|
|
} |
561
|
31 |
|
if (isset($produto['preco'])) { |
562
|
3 |
|
$this->setPreco($produto['preco']); |
563
|
3 |
|
} else { |
564
|
31 |
|
$this->setPreco(null); |
565
|
|
|
} |
566
|
31 |
|
if (isset($produto['quantidade'])) { |
567
|
3 |
|
$this->setQuantidade($produto['quantidade']); |
568
|
3 |
|
} else { |
569
|
31 |
|
$this->setQuantidade(null); |
570
|
|
|
} |
571
|
31 |
|
if (isset($produto['tributada'])) { |
572
|
3 |
|
$this->setTributada($produto['tributada']); |
573
|
3 |
|
} else { |
574
|
31 |
|
$this->setTributada(null); |
575
|
|
|
} |
576
|
31 |
|
if (!isset($produto['peso']) || is_null($produto['peso'])) { |
577
|
31 |
|
$this->setPeso(new Peso()); |
578
|
31 |
|
} else { |
579
|
3 |
|
$this->setPeso($produto['peso']); |
580
|
|
|
} |
581
|
31 |
|
if (isset($produto['excecao'])) { |
582
|
|
|
$this->setExcecao($produto['excecao']); |
583
|
|
|
} else { |
584
|
31 |
|
$this->setExcecao(null); |
585
|
|
|
} |
586
|
31 |
|
if (isset($produto['cfop'])) { |
587
|
3 |
|
$this->setCFOP($produto['cfop']); |
588
|
3 |
|
} else { |
589
|
31 |
|
$this->setCFOP(null); |
590
|
|
|
} |
591
|
31 |
|
if (isset($produto['ncm'])) { |
592
|
3 |
|
$this->setNCM($produto['ncm']); |
593
|
3 |
|
} else { |
594
|
31 |
|
$this->setNCM(null); |
595
|
|
|
} |
596
|
31 |
|
if (isset($produto['cest'])) { |
597
|
3 |
|
$this->setCEST($produto['cest']); |
598
|
3 |
|
} else { |
599
|
31 |
|
$this->setCEST(null); |
600
|
|
|
} |
601
|
31 |
|
if (!isset($produto['impostos']) || is_null($produto['impostos'])) { |
602
|
31 |
|
$this->setImpostos(array()); |
603
|
31 |
|
} else { |
604
|
3 |
|
$this->setImpostos($produto['impostos']); |
605
|
|
|
} |
606
|
31 |
|
return $this; |
607
|
|
|
} |
608
|
|
|
|
609
|
29 |
|
public static function addNodeInformacoes($tributos, $element, $name = null) |
610
|
|
|
{ |
611
|
29 |
|
$detalhes = array(); |
612
|
|
|
$formatos = array( |
613
|
29 |
|
Imposto::TIPO_IMPORTADO => '%s Importado', |
614
|
29 |
|
Imposto::TIPO_NACIONAL => '%s Federal', |
615
|
29 |
|
Imposto::TIPO_ESTADUAL => '%s Estadual', |
616
|
29 |
|
Imposto::TIPO_MUNICIPAL => '%s Municipal' |
617
|
29 |
|
); |
618
|
29 |
|
foreach ($formatos as $tipo => $formato) { |
619
|
29 |
|
if (!isset($tributos[$tipo])) { |
620
|
29 |
|
continue; |
621
|
|
|
} |
622
|
29 |
|
if (!Util::isGreater($tributos[$tipo], 0.00)) { |
623
|
29 |
|
continue; |
624
|
|
|
} |
625
|
29 |
|
$detalhes[] = sprintf($formato, Util::toMoney($tributos[$tipo])); |
626
|
29 |
|
} |
627
|
29 |
|
if (count($detalhes) == 0) { |
628
|
|
|
return null; |
629
|
|
|
} |
630
|
29 |
|
$fonte = 'Fonte: '.$tributos['info']['fonte'].' '.$tributos['info']['chave']; |
631
|
29 |
|
$ultimo = ''; |
632
|
29 |
|
if (count($detalhes) > 1) { |
633
|
29 |
|
$ultimo = ' e '.array_pop($detalhes); |
634
|
29 |
|
} |
635
|
29 |
|
$texto = 'Trib. aprox.: '.implode(', ', $detalhes).$ultimo.'. '.$fonte; |
636
|
29 |
|
Util::appendNode($element, is_null($name)?'infAdProd':$name, $texto); |
637
|
29 |
|
return $texto; |
638
|
|
|
} |
639
|
|
|
|
640
|
29 |
|
public function getNode($name = null) |
|
|
|
|
641
|
|
|
{ |
642
|
29 |
|
$dom = new \DOMDocument('1.0', 'UTF-8'); |
643
|
29 |
|
$element = $dom->createElement(is_null($name)?'det':$name); |
644
|
29 |
|
$attr = $dom->createAttribute('nItem'); |
645
|
29 |
|
$attr->value = $this->getItem(true); |
646
|
29 |
|
$element->appendChild($attr); |
647
|
|
|
|
648
|
29 |
|
$produto = $dom->createElement('prod'); |
649
|
29 |
|
Util::appendNode($produto, 'cProd', $this->getCodigo(true)); |
650
|
29 |
|
Util::appendNode($produto, 'cEAN', $this->getCodigoBarras(true)); |
651
|
29 |
|
Util::appendNode($produto, 'xProd', $this->getDescricao(true)); |
652
|
29 |
|
Util::appendNode($produto, 'NCM', $this->getNCM(true)); |
653
|
|
|
// Util::appendNode($produto, 'NVE', $this->getNVE(true)); |
654
|
29 |
|
if (!is_null($this->getCEST())) { |
655
|
29 |
|
Util::appendNode($produto, 'CEST', $this->getCEST(true)); |
656
|
29 |
|
} |
657
|
29 |
|
if (!is_null($this->getExcecao())) { |
658
|
|
|
Util::appendNode($produto, 'EXTIPI', $this->getExcecao(true)); |
659
|
|
|
} |
660
|
29 |
|
Util::appendNode($produto, 'CFOP', $this->getCFOP(true)); |
661
|
29 |
|
Util::appendNode($produto, 'uCom', $this->getUnidade(true)); |
662
|
29 |
|
Util::appendNode($produto, 'qCom', $this->getQuantidade(true)); |
663
|
29 |
|
Util::appendNode($produto, 'vUnCom', $this->getPrecoUnitario(true)); |
664
|
29 |
|
Util::appendNode($produto, 'vProd', $this->getPreco(true)); |
665
|
29 |
|
Util::appendNode($produto, 'cEANTrib', $this->getCodigoTributario(true)); |
666
|
29 |
|
Util::appendNode($produto, 'uTrib', $this->getUnidade(true)); |
667
|
29 |
|
Util::appendNode($produto, 'qTrib', $this->getTributada(true)); |
668
|
29 |
|
Util::appendNode($produto, 'vUnTrib', $this->getPrecoTributavel(true)); |
669
|
29 |
|
if (Util::isGreater($this->getFrete(), 0.00)) { |
670
|
2 |
|
Util::appendNode($produto, 'vFrete', $this->getFrete(true)); |
671
|
2 |
|
} |
672
|
29 |
|
if (Util::isGreater($this->getSeguro(), 0.00)) { |
673
|
2 |
|
Util::appendNode($produto, 'vSeg', $this->getSeguro(true)); |
674
|
2 |
|
} |
675
|
29 |
|
if (Util::isGreater($this->getDesconto(), 0.00)) { |
676
|
29 |
|
Util::appendNode($produto, 'vDesc', $this->getDesconto(true)); |
677
|
29 |
|
} |
678
|
29 |
|
if (Util::isGreater($this->getDespesas(), 0.00)) { |
679
|
2 |
|
Util::appendNode($produto, 'vOutro', $this->getDespesas(true)); |
680
|
2 |
|
} |
681
|
29 |
|
Util::appendNode($produto, 'indTot', $this->getMultiplicador(true)); |
682
|
|
|
// Util::appendNode($produto, 'DI', $this->getImportacoes(true)); |
683
|
|
|
// Util::appendNode($produto, 'detExport', $this->getDetalhes(true)); |
684
|
29 |
|
if (!is_null($this->getPedido())) { |
685
|
2 |
|
Util::appendNode($produto, 'xPed', $this->getPedido(true)); |
686
|
2 |
|
} |
687
|
29 |
|
Util::appendNode($produto, 'nItemPed', $this->getItem(true)); |
688
|
|
|
// Util::appendNode($produto, 'nFCI', $this->getControle(true)); |
689
|
29 |
|
$element->appendChild($produto); |
690
|
|
|
|
691
|
29 |
|
$imposto = $dom->createElement('imposto'); |
692
|
29 |
|
$grupos = array(); |
693
|
29 |
|
$_impostos = $this->getImpostos(); |
694
|
29 |
|
foreach ($_impostos as $_imposto) { |
695
|
29 |
|
if (is_null($_imposto->getBase())) { |
696
|
2 |
|
$_imposto->setBase($this->getBase()); |
697
|
2 |
|
} |
698
|
29 |
|
$grupos[$_imposto->getGrupo(true)][] = $_imposto; |
699
|
29 |
|
} |
700
|
29 |
|
$imposto_info = $this->getImpostoInfo(); |
701
|
29 |
|
$this->setTributos($imposto_info['total']); |
702
|
29 |
|
Util::appendNode($imposto, 'vTotTrib', Util::toCurrency($imposto_info['total'])); |
703
|
29 |
|
foreach ($grupos as $tag => $_grupo) { |
704
|
29 |
|
$grupo = $dom->createElement($tag); |
705
|
29 |
|
foreach ($_grupo as $_imposto) { |
706
|
29 |
|
$node = $_imposto->getNode(); |
707
|
29 |
|
$node = $dom->importNode($node, true); |
708
|
29 |
|
$grupo->appendChild($node); |
709
|
29 |
|
} |
710
|
29 |
|
$imposto->appendChild($grupo); |
711
|
29 |
|
} |
712
|
29 |
|
$element->appendChild($imposto); |
713
|
|
|
// TODO: verificar se é obrigatório a informação adicional abaixo |
714
|
29 |
|
$complemento = self::addNodeInformacoes($imposto_info, $element); |
715
|
29 |
|
$this->setComplemento($complemento); |
716
|
29 |
|
return $element; |
717
|
|
|
} |
718
|
|
|
|
719
|
27 |
|
public function loadNode($element, $name = null) |
|
|
|
|
720
|
|
|
{ |
721
|
27 |
|
$name = is_null($name)?'det':$name; |
722
|
27 |
|
if ($element->nodeName != $name) { |
723
|
|
|
$_fields = $element->getElementsByTagName($name); |
724
|
|
|
if ($_fields->length == 0) { |
725
|
|
|
throw new \Exception('Tag "'.$name.'" do Produto não encontrada', 404); |
726
|
|
|
} |
727
|
|
|
$element = $_fields->item(0); |
728
|
|
|
} |
729
|
27 |
|
$root = $element; |
730
|
27 |
|
$element = parent::loadNode($element, $name); |
731
|
27 |
|
$this->setItem(Util::loadNode($element, 'nItemPed')); |
732
|
27 |
|
$this->setPedido(Util::loadNode($element, 'xPed')); |
733
|
27 |
|
$this->setCodigo( |
734
|
27 |
|
Util::loadNode( |
735
|
27 |
|
$element, |
736
|
27 |
|
'cProd', |
737
|
|
|
'Tag "cProd" do campo "Codigo" não encontrada no Produto' |
738
|
27 |
|
) |
739
|
27 |
|
); |
740
|
27 |
|
$this->setCodigoTributario( |
741
|
27 |
|
Util::loadNode( |
742
|
27 |
|
$element, |
743
|
27 |
|
'cEANTrib', |
744
|
|
|
'Tag "cEANTrib" do campo "CodigoTributario" não encontrada no Produto' |
745
|
27 |
|
) |
746
|
27 |
|
); |
747
|
27 |
|
$this->setCodigoBarras( |
748
|
27 |
|
Util::loadNode( |
749
|
27 |
|
$element, |
750
|
27 |
|
'cEAN', |
751
|
|
|
'Tag "cEAN" do campo "CodigoBarras" não encontrada no Produto' |
752
|
27 |
|
) |
753
|
27 |
|
); |
754
|
27 |
|
$this->setDescricao( |
755
|
27 |
|
Util::loadNode( |
756
|
27 |
|
$element, |
757
|
27 |
|
'xProd', |
758
|
|
|
'Tag "xProd" do campo "Descricao" não encontrada no Produto' |
759
|
27 |
|
) |
760
|
27 |
|
); |
761
|
27 |
|
$this->setUnidade( |
762
|
27 |
|
Util::loadNode( |
763
|
27 |
|
$element, |
764
|
27 |
|
'uCom', |
765
|
|
|
'Tag "uCom" do campo "Unidade" não encontrada no Produto' |
766
|
27 |
|
) |
767
|
27 |
|
); |
768
|
27 |
|
$this->setMultiplicador( |
769
|
27 |
|
Util::loadNode( |
770
|
27 |
|
$element, |
771
|
27 |
|
'indTot', |
772
|
|
|
'Tag "indTot" do campo "Multiplicador" não encontrada no Produto' |
773
|
27 |
|
) |
774
|
27 |
|
); |
775
|
27 |
|
$this->setQuantidade( |
776
|
27 |
|
Util::loadNode( |
777
|
27 |
|
$element, |
778
|
27 |
|
'qCom', |
779
|
|
|
'Tag "qCom" do campo "Quantidade" não encontrada no Produto' |
780
|
27 |
|
) |
781
|
27 |
|
); |
782
|
27 |
|
$this->setTributada( |
783
|
27 |
|
Util::loadNode( |
784
|
27 |
|
$element, |
785
|
27 |
|
'qTrib', |
786
|
|
|
'Tag "qTrib" do campo "Tributada" não encontrada no Produto' |
787
|
27 |
|
) |
788
|
27 |
|
); |
789
|
27 |
|
$this->setExcecao(Util::loadNode($element, 'EXTIPI')); |
790
|
27 |
|
$this->setCFOP( |
791
|
27 |
|
Util::loadNode( |
792
|
27 |
|
$element, |
793
|
27 |
|
'CFOP', |
794
|
|
|
'Tag "CFOP" do campo "CFOP" não encontrada no Produto' |
795
|
27 |
|
) |
796
|
27 |
|
); |
797
|
27 |
|
$this->setNCM( |
798
|
27 |
|
Util::loadNode( |
799
|
27 |
|
$element, |
800
|
27 |
|
'NCM', |
801
|
|
|
'Tag "NCM" do campo "NCM" não encontrada no Produto' |
802
|
27 |
|
) |
803
|
27 |
|
); |
804
|
27 |
|
$this->setCEST(Util::loadNode($element, 'CEST')); |
805
|
27 |
|
$impostos = array(); |
806
|
27 |
|
$_fields = $root->getElementsByTagName('imposto'); |
807
|
27 |
|
if ($_fields->length == 0) { |
808
|
|
|
throw new \Exception('Tag "imposto" da lista de "Impostos" não encontrada no Produto', 404); |
809
|
|
|
} |
810
|
27 |
|
$imposto_node = $_fields->item(0); |
811
|
27 |
|
$this->setTributos(Util::loadNode($imposto_node, 'vTotTrib')); |
812
|
27 |
|
$_items = $imposto_node->childNodes; |
813
|
27 |
|
$total = new \NFe\Entity\Imposto\Total(); |
814
|
27 |
|
foreach ($_items as $_item) { |
815
|
27 |
|
if (!$_item->hasChildNodes() || $_item->nodeType !== XML_ELEMENT_NODE) { |
816
|
24 |
|
continue; |
817
|
|
|
} |
818
|
27 |
|
$total->setGrupo($_item->nodeName); |
819
|
27 |
|
foreach ($_item->childNodes as $_subitem) { |
820
|
27 |
|
if ($_subitem->nodeType !== XML_ELEMENT_NODE) { |
821
|
27 |
|
continue; |
822
|
|
|
} |
823
|
27 |
|
$imposto = Imposto::loadImposto($_subitem, $total->getGrupo()); |
824
|
27 |
|
if ($imposto === false) { |
825
|
|
|
continue; |
826
|
|
|
} |
827
|
27 |
|
$imposto->setGrupo($total->getGrupo()); |
828
|
27 |
|
$impostos[] = $imposto; |
829
|
27 |
|
} |
830
|
27 |
|
} |
831
|
27 |
|
$this->setImpostos($impostos); |
832
|
27 |
|
$this->setComplemento(Util::loadNode($root, 'infAdProd')); |
833
|
27 |
|
return $element; |
834
|
|
|
} |
835
|
|
|
} |
836
|
|
|
|
The number of this metric differs depending on the chosen design (inheritance vs. composition). For inheritance, the number should generally be a bit lower.
A high number indicates a reusable class. It might also make the class harder to change without breaking other classes though.