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
|
|
|
* @param IPI $IPI |
26
|
|
|
* @return IPI |
27
|
|
|
* @throws NotImplementedCSTException|InvalidCSTException |
28
|
|
|
*/ |
29
|
7 |
|
public static function calcularIPI(IPI $IPI): IPI |
30
|
|
|
{ |
31
|
7 |
|
$adValorem = ['00', '50']; |
32
|
7 |
|
$isento = ['01', '03', '04', '51', '53', '54', '55', '99']; |
33
|
7 |
|
$notImplemented = ['02', '05', '49', '52']; |
34
|
7 |
|
if (in_array($IPI->CST, $adValorem, true)) { |
35
|
3 |
|
return IPI::adValoremIPI($IPI); |
36
|
|
|
} |
37
|
4 |
|
if (in_array($IPI->CST, $isento, true)) { |
38
|
2 |
|
return IPI::isentoIPI($IPI); |
39
|
|
|
} |
40
|
2 |
|
if (in_array($IPI->CST, $notImplemented, true)) { |
41
|
1 |
|
throw new NotImplementedCSTException($IPI->CST); |
42
|
|
|
} |
43
|
1 |
|
throw new InvalidCSTException($IPI->CST); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* ISENTO |
48
|
|
|
* @param IPI $IPI |
49
|
|
|
* @return IPI |
50
|
|
|
*/ |
51
|
2 |
|
private static function isentoIPI(IPI $IPI): IPI |
52
|
|
|
{ |
53
|
2 |
|
$calculado = new IPI(); |
54
|
2 |
|
$calculado->cEnq = $IPI->cEnq; |
55
|
2 |
|
$calculado->CST = $IPI->CST; |
56
|
2 |
|
$calculado->vBC = 0.0; |
57
|
2 |
|
$calculado->pIPI = 0.0; |
58
|
2 |
|
$calculado->vIPI = 0.0; |
59
|
2 |
|
return $calculado; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Calcula o Valor IPI Ad Valoren |
64
|
|
|
* @param IPI $IPI |
65
|
|
|
* @return IPI |
66
|
|
|
*/ |
67
|
3 |
|
private static function adValoremIPI(IPI $IPI): IPI |
68
|
|
|
{ |
69
|
3 |
|
$calculado = new IPI(); |
70
|
3 |
|
$calculado->cEnq = $IPI->cEnq; |
71
|
3 |
|
$calculado->CST = $IPI->CST; |
72
|
3 |
|
$calculado->vBC = $IPI->vBC; |
73
|
3 |
|
$calculado->pIPI = $IPI->pIPI; |
74
|
3 |
|
$calculado->vIPI = $IPI->vBC * ($IPI->pIPI / 100); |
75
|
3 |
|
return $calculado; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $ncm |
80
|
|
|
* @return float |
81
|
|
|
* @throws Exception |
82
|
|
|
*/ |
83
|
8 |
|
public static function pIPIFromNCM(string $ncm): float |
84
|
|
|
{ |
85
|
8 |
|
$path = realpath(__DIR__ . '/../storage') . '/'; |
86
|
8 |
|
$tipiFile = file_get_contents($path . 'tipi.json'); |
87
|
8 |
|
$tipiList = json_decode($tipiFile, true); |
88
|
8 |
|
foreach ($tipiList as $tipi) { |
89
|
8 |
|
if ($tipi['NCMNum'] === $ncm) { |
90
|
7 |
|
return (float) ($tipi['NCMAli'] === 'NT' ? 0 : $tipi['NCMAli']); |
91
|
|
|
} |
92
|
|
|
} |
93
|
1 |
|
throw new Exception('NCM inexistente: ' . $ncm); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|