C490::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 6
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 6
loc 6
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace NFePHP\EFD\Elements\ICMSIPI;
4
5
use NFePHP\EFD\Common\Element;
6
use NFePHP\EFD\Common\ElementInterface;
7
use \stdClass;
8
use function Safe\substr;
9
10 View Code Duplication
class C490 extends Element implements ElementInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
11
{
12
    const REG = 'C490';
13
    const LEVEL = 4;
14
    const PARENT = 'C400';
15
16
    protected $parameters = [
17
        'CST_ICMS' => [
18
            'type' => 'numeric',
19
            'regex' => '^(\d{3})$',
20
            'required' => true,
21
            'info' => 'Código da Situação Tributária,',
22
            'format' => ''
23
        ],
24
        'CFOP' => [
25
            'type' => 'numeric',
26
            'regex' => '^(\d{4})$',
27
            'required' => true,
28
            'info' => 'Código Fiscal de Operação e Prestação',
29
            'format' => ''
30
        ],
31
        'ALIQ_ICMS' => [
32
            'type' => 'numeric',
33
            'regex' => '^\d+(\.\d*)?|\.\d+$',
34
            'required' => false,
35
            'info' => 'Alíquota do ICMS',
36
            'format' => '6v2'
37
        ],
38
        'VL_OPR' => [
39
            'type' => 'numeric',
40
            'regex' => '^\d+(\.\d*)?|\.\d+$',
41
            'required' => true,
42
            'info' => 'Valor da operação correspondente à combinação de CST_ICMS, CFOP, e alíquota do ICMS, 
43
            incluídas as despesas acessórias e acréscimos',
44
            'format' => '15v2'
45
        ],
46
        'VL_BC_ICMS' => [
47
            'type' => 'numeric',
48
            'regex' => '^\d+(\.\d*)?|\.\d+$',
49
            'required' => true,
50
            'info' => 'Valor acumulado da base de cálculo do ICMS, referente à combinação 
51
            de CST_ICMS, CFOP, e alíquota do ICMS.',
52
            'format' => '15v2'
53
        ],
54
        'VL_ICMS' => [
55
            'type' => 'numeric',
56
            'regex' => '^\d+(\.\d*)?|\.\d+$',
57
            'required' => true,
58
            'info' => 'Valor acumulado do ICMS, referente à combinação de CST_ICMS, CFOP e alíquota do ICMS.',
59
            'format' => '15v2'
60
        ],
61
        'COD_OBS' => [
62
            'type' => 'string',
63
            'regex' => '^.{0,6}$',
64
            'required' => false,
65
            'info' => 'Código da observação do lançamento fiscal (campo 02 do Registro 0460)',
66
            'format' => ''
67
        ],
68
    ];
69
70
    /**
71
     * Constructor
72
     * @param \stdClass $std
73
     */
74
    public function __construct(\stdClass $std)
75
    {
76
        parent::__construct(self::REG);
77
        $this->std = $this->standarize($std);
78
        $this->postValidation();
79
    }
80
81
    public function postValidation()
82
    {
83
        //pega o fim da string do CST_ICMS e faz a verificacao
84
        $cstIcmsLast = (int) substr($this->std->cst_icms, -2);
85
        if (in_array($cstIcmsLast, [30, 40, 41, 50, 60])) {
86
            if ($this->values->vl_bc_icms != 0) {
87
                throw new \InvalidArgumentException("[" . self::REG . "] " .
88
                    " O do campo VL_BC_ICMS deve ser Igual 0");
89
            }
90
            if ($this->values->aliq_icms != 0) {
91
                throw new \InvalidArgumentException("[" . self::REG . "] " .
92
                    " O do campo VL_ICMS deve ser Igual 0");
93
            }
94
            if ($this->values->vl_icms != 0) {
95
                throw new \InvalidArgumentException("[" . self::REG . "] " .
96
                    " O do campo ALIQ_ICMS deve ser Igual 0");
97
            }
98
        } elseif (!in_array($cstIcmsLast, [51, 90])) {
99
            if ($this->values->vl_bc_icms <= 0) {
100
                throw new \InvalidArgumentException("[" . self::REG . "] " .
101
                    " O do campo VL_BC_ICMS deve ser maior do que 0");
102
            }
103
            if ($this->values->aliq_icms <= 0) {
104
                throw new \InvalidArgumentException("[" . self::REG . "] " .
105
                    " O do campo ALIQ_ICMS deve ser maior do que 0");
106
            }
107
            if ($this->values->vl_icms <= 0) {
108
                throw new \InvalidArgumentException("[" . self::REG . "] " .
109
                    " O do campo VL_ICMS deve ser maior do que 0");
110
            }
111
        }
112
    }
113
}
114