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

C850::strToFloat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 4
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 View Code Duplication
class C850 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...
10
{
11
    const REG = 'C850';
12
    const LEVEL = 3;
13
    const PARENT = 'C800';
14
15
    protected $parameters = [
16
        'CST_ICMS' => [
17
            'type' => 'numeric',
18
            'regex' => '^(\d{1,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{1,4})$',
26
            'required' => true,
27
            'info' => 'Código Fiscal de Operação e Prestação do 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 total do CF-e” na combinação de CST_ICMS, CFOP e alíquota do ICMS, 
42
            correspondente ao somatório do valor líquido dos itens.',
43
            'format' => '15v2'
44
        ],
45
        'VL_BC_ICMS' => [
46
            'type' => 'string',
47
            'regex' => '^.{1,2}$',
48
            'required' => true,
49
            'info' => 'N',
50
            'format' => ''
51
        ],
52
        'VL_ICMS' => [
53
            'type' => 'numeric',
54
            'regex' => '^\d+(\.\d*)?|\.\d+$',
55
            'required' => true,
56
            'info' => 'Parcela correspondente ao "Valor do ICMS" referente à 
57
            combinação de CST_ICMS, CFOP e alíquota do ICMS.',
58
            'format' => '15v2'
59
        ],
60
        'COD_OBS' => [
61
            'type' => 'string',
62
            'regex' => '^.{0,6}$',
63
            'required' => false,
64
            'info' => 'Código da observação do lançamento fiscal',
65
            'format' => ''
66
        ],
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
82
83
    /**
84
     * Transforma o valor com virgula em float para poder fazer os calculos de verificacao
85
     * @param $vlr
86
     * @return mixed
87
     */
88
    private function strToFloat($vlr)
89
    {
90
        return (float)str_replace(',', '.', $this->std->$vlr);
91
    }
92
93
94
    public function postValidation()
95
    {
96
        //transforma os valores em float
97
        $vlBcIcms = $this->strToFloat('vl_bc_icms');
98
        $aliqIcms = $this->strToFloat('aliq_icms');
99
        $vlIcms = $this->strToFloat('vl_icms');
100
101
        //pega o fim da string do CST_ICMS e faz a verificacao
102
        $cstIcmsLast = (int)substr($this->std-> cst_icms, -2);
103
        if (in_array($cstIcmsLast, [40, 41, 50, 60])) {
104
            if ($vlBcIcms != 0) {
105
                throw new \InvalidArgumentException("[" . self::REG . "] " .
106
                    " O do campo VL_BC_ICMS deve ser Igual 0");
107
            }
108
            if ($aliqIcms != 0) {
109
                throw new \InvalidArgumentException("[" . self::REG . "] " .
110
                    " O do campo VL_ICMS deve ser Igual 0");
111
            }
112
            if ($vlIcms != 0) {
113
                throw new \InvalidArgumentException("[" . self::REG . "] " .
114
                    " O do campo ALIQ_ICMS deve ser Igual 0");
115
            }
116
        } elseif (!in_array($cstIcmsLast, [51, 90])) {
117
            if ($vlBcIcms <= 0) {
118
                throw new \InvalidArgumentException("[" . self::REG . "] " .
119
                    " O do campo VL_BC_ICMS deve ser maior do que 0");
120
            }
121
            if ($aliqIcms <= 0) {
122
                throw new \InvalidArgumentException("[" . self::REG . "] " .
123
                    " O do campo ALIQ_ICMS deve ser maior do que 0");
124
            }
125
            if ($vlIcms <= 0) {
126
                throw new \InvalidArgumentException("[" . self::REG . "] " .
127
                    " O do campo VL_ICMS deve ser maior do que 0");
128
            }
129
        }
130
    }
131
}
132