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

C610::strToFloat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
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
9
class C610 extends Element implements ElementInterface
10
{
11
    const REG = 'C610';
12
    const LEVEL = 3;
13
    const PARENT = 'C600';
14
15
    protected $parameters = [
16
        'COD_CLASS' => [
17
            'type' => 'numeric',
18
            'regex' => '^(\d{4})$',
19
            'required' => false,
20
            'info' => 'Código de classificação do item de energia elétrica',
21
            'format' => ''
22
        ],
23
        'COD_ITEM' => [
24
            'type' => 'string',
25
            'regex' => '^.{1,60}$',
26
            'required' => true,
27
            'info' => 'Código do item (campo 02 do Registro 0200)',
28
            'format' => ''
29
        ],
30
        'QTD' => [
31
            'type' => 'numeric',
32
            'regex' => '^\d+(\.\d*)?|\.\d+$',
33
            'required' => true,
34
            'info' => 'Quantidade acumulada do item',
35
            'format' => '15v3'
36
        ],
37
        'UNID' => [
38
            'type' => 'string',
39
            'regex' => '^.{1,6}$',
40
            'required' => true,
41
            'info' => 'Unidade do item (Campo 02 do registro 0190)',
42
            'format' => ''
43
        ],
44
        'VL_ITEM' => [
45
            'type' => 'numeric',
46
            'regex' => '^\d+(\.\d*)?|\.\d+$',
47
            'required' => true,
48
            'info' => 'Valor acumulado do item',
49
            'format' => '15v2'
50
        ],
51
        'VL_DESC' => [
52
            'type' => 'numeric',
53
            'regex' => '^\d+(\.\d*)?|\.\d+$',
54
            'required' => false,
55
            'info' => 'Valor acumulado dos descontos',
56
            'format' => '15v2'
57
        ],
58
        'CST_ICMS' => [
59
            'type' => 'numeric',
60
            'regex' => '^(\d{3})$',
61
            'required' => true,
62
            'info' => 'Código da Situação Tributária, conforme a Tabela indicada no item 4.3.1',
63
            'format' => ''
64
        ],
65
        'CFOP' => [
66
            'type' => 'numeric',
67
            'regex' => '^(\d{4})$',
68
            'required' => true,
69
            'info' => 'Código Fiscal de Operação e Prestação conforme tabela indicada no item 4.2.2.',
70
            'format' => ''
71
        ],
72
        'ALIQ_ICMS' => [
73
            'type' => 'numeric',
74
            'regex' => '^\d+(\.\d*)?|\.\d+$',
75
            'required' => false,
76
            'info' => 'Alíquota do ICMS',
77
            'format' => '6v2'
78
        ],
79
        'VL_BC_ICMS' => [
80
            'type' => 'numeric',
81
            'regex' => '^\d+(\.\d*)?|\.\d+$',
82
            'required' => false,
83
            'info' => 'Valor acumulado da base de cálculo do ICMS',
84
            'format' => '15v2'
85
        ],
86
        'VL_ICMS' => [
87
            'type' => 'numeric',
88
            'regex' => '^\d+(\.\d*)?|\.\d+$',
89
            'required' => false,
90
            'info' => 'Valor acumulado do ICMS debitado',
91
            'format' => '15v2'
92
        ],
93
        'VL_BC_ICMS_ST' => [
94
            'type' => 'numeric',
95
            'regex' => '^\d+(\.\d*)?|\.\d+$',
96
            'required' => false,
97
            'info' => 'Valor da base de cálculo do ICMS substituição tributária',
98
            'format' => '15v2'
99
        ],
100
        'VL_ICMS_ST' => [
101
            'type' => 'numeric',
102
            'regex' => '^\d+(\.\d*)?|\.\d+$',
103
            'required' => false,
104
            'info' => 'Valor do ICMS retido por substituição tributária',
105
            'format' => '15v2'
106
        ],
107
        'VL_PIS' => [
108
            'type' => 'numeric',
109
            'regex' => '^\d+(\.\d*)?|\.\d+$',
110
            'required' => false,
111
            'info' => 'Valor do PIS',
112
            'format' => '15v2'
113
        ],
114
        'VL_COFINS' => [
115
            'type' => 'numeric',
116
            'regex' => '^\d+(\.\d*)?|\.\d+$',
117
            'required' => false,
118
            'info' => 'Valor da COFINS',
119
            'format' => '15v2'
120
        ],
121
        'COD_CTA' => [
122
            'type' => 'string',
123
            'regex' => '^(.*)$',
124
            'required' => false,
125
            'info' => 'Código da conta analítica contábil debitada/creditada',
126
            'format' => ''
127
        ],
128
129
    ];
130
131
    /**
132
     * Constructor
133
     * @param \stdClass $std
134
     */
135
    public function __construct(\stdClass $std)
136
    {
137
        parent::__construct(self::REG);
138
        $this->std = $this->standarize($std);
139
        $this->postValidation();
140
    }
141
142
143
    /**
144
     * Transforma o valor com virgula em float para poder fazer os calculos de verificacao
145
     * @param $vlr
146
     * @return mixed
147
     */
148
    private function strToFloat($vlr)
149
    {
150
        return (float)str_replace(',', '.', $this->std->$vlr);
151
    }
152
153
154
    public function postValidation()
155
    {
156 View Code Duplication
        if ($this->strToFloat('qtd') <= 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
157
            throw new \InvalidArgumentException("[" . self::REG . "] " .
158
                " O do campo QTD deve ser maior do que 0");
159
        }
160
161
        //transforma os valores em float
162
        $vlBcIcms = $this->strToFloat('vl_bc_icms');
163
        $aliqIcms = $this->strToFloat('aliq_icms');
164
        $vlIcms = $this->strToFloat('vl_icms');
165
166
        //pega o fim da string do CST_ICMS e faz a verificacao
167
        $cstIcmsLast = (int)substr($this->std-> cst_icms, -2);
168
        if (in_array($cstIcmsLast, [30, 40, 41, 50, 60])) {
169
            if ($vlBcIcms != 0) {
170
                throw new \InvalidArgumentException("[" . self::REG . "] " .
171
                    " O do campo VL_BC_ICMS deve ser Igual 0");
172
            }
173
            if ($aliqIcms != 0) {
174
                throw new \InvalidArgumentException("[" . self::REG . "] " .
175
                    " O do campo VL_ICMS deve ser Igual 0");
176
            }
177
            if ($vlIcms != 0) {
178
                throw new \InvalidArgumentException("[" . self::REG . "] " .
179
                    " O do campo ALIQ_ICMS deve ser Igual 0");
180
            }
181
        } elseif (!in_array($cstIcmsLast, [51, 90])) {
182
            if ($vlBcIcms <= 0) {
183
                throw new \InvalidArgumentException("[" . self::REG . "] " .
184
                    " O do campo VL_BC_ICMS deve ser maior do que 0");
185
            }
186
            if ($aliqIcms <= 0) {
187
                throw new \InvalidArgumentException("[" . self::REG . "] " .
188
                    " O do campo ALIQ_ICMS deve ser maior do que 0");
189
            }
190
            if ($vlIcms <= 0) {
191
                throw new \InvalidArgumentException("[" . self::REG . "] " .
192
                    " O do campo VL_ICMS deve ser maior do que 0");
193
            }
194
        }
195
    }
196
}
197