Completed
Pull Request — master (#275)
by Roberto
28:14 queued 13:14
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' => '15v2'
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
    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, [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