|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gbbs\NfeCalculos; |
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
use Gbbs\NfeCalculos\Exception\InvalidCSTException; |
|
9
|
|
|
use Gbbs\NfeCalculos\Exception\NotImplementedCSTException; |
|
10
|
|
|
|
|
11
|
|
|
class IPI |
|
12
|
|
|
{ |
|
13
|
|
|
public $CNPJProd; |
|
14
|
|
|
public $cSelo; |
|
15
|
|
|
public $qSelo; |
|
16
|
|
|
public $cEnq; |
|
17
|
|
|
public $CST; |
|
18
|
|
|
public $vBC; |
|
19
|
|
|
public $pIPI; |
|
20
|
|
|
public $vIPI; |
|
21
|
|
|
public $qUnid; |
|
22
|
|
|
public $vUnid; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param IPI $IPI |
|
27
|
|
|
* @throws NotImplementedCSTException|InvalidCSTException |
|
28
|
|
|
*/ |
|
29
|
|
|
function calcularIPI(IPI $IPI): IPI |
|
30
|
|
|
{ |
|
31
|
15 |
|
$adValorem = ['50']; |
|
32
|
15 |
|
$isento = ['51']; |
|
33
|
|
|
$notImplemented = [ |
|
34
|
15 |
|
'00', '01', '02', '03', '04', '05', '49', '52', '53', '54', '55', '99' |
|
35
|
|
|
]; |
|
36
|
15 |
|
if (in_array($IPI->CST, $adValorem, true)) { |
|
37
|
1 |
|
return adValoremIPI($IPI); |
|
38
|
14 |
|
} elseif (in_array($IPI->CST, $isento, true)) { |
|
39
|
1 |
|
return calcIsentoIPI($IPI); |
|
40
|
13 |
|
} elseif (in_array($IPI->CST, $notImplemented, true)) { |
|
41
|
12 |
|
throw new NotImplementedCSTException($IPI->CST); |
|
42
|
|
|
} |
|
43
|
1 |
|
throw new InvalidCSTException($IPI->CST); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* ISENTO |
|
48
|
|
|
* @param IPI $IPI |
|
49
|
|
|
*/ |
|
50
|
|
|
function calcIsentoIPI(IPI $IPI): IPI |
|
51
|
|
|
{ |
|
52
|
1 |
|
$calculado = new IPI(); |
|
53
|
1 |
|
$calculado->CST = $IPI->CST; |
|
54
|
1 |
|
$calculado->vBC = 0.0; |
|
55
|
1 |
|
$calculado->pIPI = 0.0; |
|
56
|
1 |
|
$calculado->vIPI = 0.0; |
|
57
|
1 |
|
return $calculado; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Calcula o Valor IPI Ad Valoren |
|
62
|
|
|
* @param IPI $IPI |
|
63
|
|
|
*/ |
|
64
|
|
|
function adValoremIPI(IPI $IPI): IPI |
|
65
|
|
|
{ |
|
66
|
1 |
|
$calculado = new IPI(); |
|
67
|
1 |
|
$calculado->CST = $IPI->CST; |
|
68
|
1 |
|
$calculado->vBC = $IPI->vBC; |
|
69
|
1 |
|
$calculado->vIPI = $IPI->vBC * ($IPI->pIPI / 100); |
|
70
|
1 |
|
$calculado->pIPI = $IPI->pIPI; |
|
71
|
1 |
|
return $calculado; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $ncm |
|
76
|
|
|
*/ |
|
77
|
|
|
function pIPIFromNCM(string $ncm): float |
|
78
|
|
|
{ |
|
79
|
15 |
|
$path = realpath(__DIR__ . '/../storage') . '/'; |
|
80
|
15 |
|
$tipiFile = file_get_contents($path . 'tipi.json'); |
|
81
|
15 |
|
$tipiList = json_decode($tipiFile, true); |
|
82
|
15 |
|
foreach ($tipiList as $tipi) { |
|
83
|
15 |
|
if ($tipi['NCMNum'] === $ncm) { |
|
84
|
15 |
|
return (float) ($tipi['NCMAli'] === 'NT'? 0 : $tipi['NCMAli']); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
1 |
|
throw new Exception('NCM inexistente: ' . $ncm); |
|
88
|
|
|
} |
|
89
|
|
|
|