Completed
Pull Request — master (#274)
by
unknown
10:53
created

C590   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 137
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A strToFloat() 0 4 1
B postValidation() 0 37 9
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
9
class C590 extends Element implements ElementInterface
10
{
11
    const REG = 'C590';
12
    const LEVEL = 3;
13
    const PARENT = 'C500';
14
15
    protected $parameters = [
16
        'CST_ICMS' => [
17
            'type' => 'numeric',
18
            'regex' => '^(\d{3})$',
19
            'required' => true,
20
            'info' => 'Código da Situação Tributária, conforme a Tabela indicada no item 4.3.1.',
21
            'format' => ''
22
        ],
23
        'CFOP' => [
24
            'type' => 'numeric',
25
            'regex' => '^(\d{4})$',
26
            'required' => true,
27
            'info' => 'Código Fiscal de agrupamento de itens',
28
            'format' => ''
29
        ],
30
        'ALIQ_ICMS' => [
31
            'type' => 'numeric',
32
            'regex' => '^\d+(\.\d*)?|\.\d+$',
33
            'required' => false,
34
            'info' => 'Alíquota do ICMS',
35
            'format' => '6v2'
36
        ],
37
        'VL_OPR' => [
38
            'type' => 'numeric',
39
            'regex' => '^\d+(\.\d*)?|\.\d+$',
40
            'required' => true,
41
            'info' => 'Valor da operação correspondente à combinação de CST_ICMS, CFOP, e alíquota do ICMS.',
42
            'format' => '15v2'
43
        ],
44
        'VL_BC_ICMS' => [
45
            'type' => 'numeric',
46
            'regex' => '^\d+(\.\d*)?|\.\d+$',
47
            'required' => false,
48
            'info' => 'Parcela correspondente ao "Valor da base de cálculo do ICMS" 
49
            referente à combinação de CST_ICMS, CFOP e alíquota do ICMS.',
50
            'format' => '15v2'
51
        ],
52
        'VL_ICMS' => [
53
            'type' => 'numeric',
54
            'regex' => '^\d+(\.\d*)?|\.\d+$',
55
            'required' => false,
56
            'info' => 'Parcela correspondente ao "Valor do ICMS" referente à combinação 
57
            de CST_ICMS, CFOP e alíquota do ICMS.',
58
            'format' => '15v2'
59
        ],
60
        'VL_BC_ICMS_ST' => [
61
            'type' => 'numeric',
62
            'regex' => '^\d+(\.\d*)?|\.\d+$',
63
            'required' => false,
64
            'info' => 'Parcela correspondente ao "Valor da base de cálculo do ICMS" da substituição 
65
            tributária referente à combinação de CST_ICMS, CFOP e alíquota do ICMS.',
66
            'format' => '15v2'
67
        ],
68
        'VL_RED_BC' => [
69
            'type' => 'numeric',
70
            'regex' => '^\d+(\.\d*)?|\.\d+$',
71
            'required' => false,
72
            'info' => 'Valor não tributado em função da redução da base de cálculo do ICMS, 
73
            referente à combinação de CST_ICMS, CFOP e alíquota do ICMS.',
74
            'format' => '15v2'
75
        ],
76
        'COD_OBS' => [
77
            'type' => 'string',
78
            'regex' => '^.{0,6}$',
79
            'required' => false,
80
            'info' => 'Código da observação do lançamento fiscal (campo 02 do Registro 0460)',
81
            'format' => ''
82
        ],
83
84
    ];
85
86
    /**
87
     * Constructor
88
     * @param \stdClass $std
89
     */
90
    public function __construct(\stdClass $std)
91
    {
92
        parent::__construct(self::REG);
93
        $this->std = $this->standarize($std);
94
    }
95
96
97
    /**
98
     * Transforma o valor com virgula em float para poder fazer os calculos de verificacao
99
     * @param $vlr
100
     * @return mixed
101
     */
102
    private function strToFloat($vlr)
103
    {
104
        return (float)str_replace(',', '.', $this->std->$vlr);
105
    }
106
107
108
    public function postValidation()
109
    {
110
        //transforma os valores em float
111
        $vlBcIcms = $this->strToFloat('vl_bc_icms');
112
        $aliqIcms = $this->strToFloat('aliq_icms');
113
        $vlIcms = $this->strToFloat('vl_icms');
114
115
        //pega o fim da string do CST_ICMS e faz a verificacao
116
        $cstIcmsLast = (int)substr($this->std-> cst_icms, -2);
117
        if (in_array($cstIcmsLast, [30, 40, 41, 50, 60])) {
118
            if ($vlBcIcms != 0) {
119
                throw new \InvalidArgumentException("[" . self::REG . "] " .
120
                    " O do campo VL_BC_ICMS deve ser Igual 0");
121
            }
122
            if ($aliqIcms != 0) {
123
                throw new \InvalidArgumentException("[" . self::REG . "] " .
124
                    " O do campo VL_ICMS deve ser Igual 0");
125
            }
126
            if ($vlIcms != 0) {
127
                throw new \InvalidArgumentException("[" . self::REG . "] " .
128
                    " O do campo ALIQ_ICMS deve ser Igual 0");
129
            }
130
        } elseif (!in_array($cstIcmsLast, [51, 90])) {
131
            if ($vlBcIcms <= 0) {
132
                throw new \InvalidArgumentException("[" . self::REG . "] " .
133
                    " O do campo VL_BC_ICMS deve ser maior do que 0");
134
            }
135
            if ($aliqIcms <= 0) {
136
                throw new \InvalidArgumentException("[" . self::REG . "] " .
137
                    " O do campo ALIQ_ICMS deve ser maior do que 0");
138
            }
139
            if ($vlIcms <= 0) {
140
                throw new \InvalidArgumentException("[" . self::REG . "] " .
141
                    " O do campo VL_ICMS deve ser maior do que 0");
142
            }
143
        }
144
    }
145
}
146