Passed
Push — master ( 76f52e...93f64e )
by João
02:26
created

isentoIPI()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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