C850::postValidation()   B
last analyzed

Complexity

Conditions 9
Paths 9

Size

Total Lines 32

Duplication

Lines 32
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 32
loc 32
ccs 0
cts 31
cp 0
rs 8.0555
c 0
b 0
f 0
cc 9
nc 9
nop 0
crap 90
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
/**
11
 * REGISTRO C850: REGISTRO ANALÍTICO DO CF-e-SAT (CODIGO 59)
12
 * @package NFePHP\EFD\Elements\ICMSIPI
13
 */
14 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...
15
{
16
    const REG = 'C850';
17
    const LEVEL = 3;
18
    const PARENT = 'C800';
19
20
    protected $parameters = [
21
        'CST_ICMS' => [
22
            'type' => 'numeric',
23
            'regex' => '^(\d{1,3})$',
24
            'required' => true,
25
            'info' => 'Código da Situação Tributária, conforme a Tabela indicada no item 4.3.1',
26
            'format' => ''
27
        ],
28
        'CFOP' => [
29
            'type' => 'numeric',
30
            'regex' => '^(\d{1,4})$',
31
            'required' => true,
32
            'info' => 'Código Fiscal de Operação e Prestação do agrupamento de itens',
33
            'format' => ''
34
        ],
35
        'ALIQ_ICMS' => [
36
            'type' => 'numeric',
37
            'regex' => '^\d+(\.\d*)?|\.\d+$',
38
            'required' => false,
39
            'info' => 'Alíquota do ICMS',
40
            'format' => '6v2'
41
        ],
42
        'VL_OPR' => [
43
            'type' => 'numeric',
44
            'regex' => '^\d+(\.\d*)?|\.\d+$',
45
            'required' => true,
46
            'info' => '“Valor total do CF-e” na combinação de CST_ICMS, CFOP e alíquota do ICMS, 
47
            correspondente ao somatório do valor líquido dos itens.',
48
            'format' => '15v2'
49
        ],
50
        'VL_BC_ICMS' => [
51
            'type' => 'string',
52
            'regex' => '^.{1,2}$',
53
            'required' => true,
54
            'info' => 'N',
55
            'format' => '15v2'
56
        ],
57
        'VL_ICMS' => [
58
            'type' => 'numeric',
59
            'regex' => '^\d+(\.\d*)?|\.\d+$',
60
            'required' => true,
61
            'info' => 'Parcela correspondente ao "Valor do ICMS" referente à 
62
            combinação de CST_ICMS, CFOP e alíquota do ICMS.',
63
            'format' => '15v2'
64
        ],
65
        'COD_OBS' => [
66
            'type' => 'string',
67
            'regex' => '^.{0,6}$',
68
            'required' => false,
69
            'info' => 'Código da observação do lançamento fiscal',
70
            'format' => ''
71
        ],
72
73
    ];
74
75
    /**
76
     * Constructor
77
     * @param \stdClass $std
78
     */
79
    public function __construct(\stdClass $std)
80
    {
81
        parent::__construct(self::REG);
82
        $this->std = $this->standarize($std);
83
        $this->postValidation();
84
    }
85
86
    public function postValidation()
87
    {
88
        //pega o fim da string do CST_ICMS e faz a verificacao
89
        $cstIcmsLast = (int) substr($this->std->cst_icms, -2);
90
        if (in_array($cstIcmsLast, [40, 41, 50, 60])) {
91
            if ($this->values->vl_bc_icms != 0) {
92
                throw new \InvalidArgumentException("[" . self::REG . "] " .
93
                    " O do campo VL_BC_ICMS deve ser Igual 0");
94
            }
95
            if ($this->values->aliq_icms != 0) {
96
                throw new \InvalidArgumentException("[" . self::REG . "] " .
97
                    " O do campo VL_ICMS deve ser Igual 0");
98
            }
99
            if ($this->values->vl_icms != 0) {
100
                throw new \InvalidArgumentException("[" . self::REG . "] " .
101
                    " O do campo ALIQ_ICMS deve ser Igual 0");
102
            }
103
        } elseif (!in_array($cstIcmsLast, [51, 90])) {
104
            if ($this->values->vl_bc_icms <= 0) {
105
                throw new \InvalidArgumentException("[" . self::REG . "] " .
106
                    " O do campo VL_BC_ICMS deve ser maior do que 0");
107
            }
108
            if ($this->values->aliq_icms <= 0) {
109
                throw new \InvalidArgumentException("[" . self::REG . "] " .
110
                    " O do campo ALIQ_ICMS deve ser maior do que 0");
111
            }
112
            if ($this->values->vl_icms <= 0) {
113
                throw new \InvalidArgumentException("[" . self::REG . "] " .
114
                    " O do campo VL_ICMS deve ser maior do que 0");
115
            }
116
        }
117
    }
118
}
119