Passed
Push — master ( c4d08f...d0db9a )
by João
03:33
created

IPI::isentoIPI()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 9
ccs 8
cts 8
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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
    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
    static function pIPIFromNCM(string $ncm): float
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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