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

C470::postValidation()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 0
cp 0
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 12
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 C470 extends Element implements ElementInterface
10
{
11
    const REG = 'C470';
12
    const LEVEL = 5;
13
    const PARENT = 'C400';
14
15
    protected $parameters = [
16
        'COD_ITEM' => [
17
            'type' => 'string',
18
            'regex' => '^.{1,60}$',
19
            'required' => true,
20
            'info' => 'Código do item (campo 02 do Registro 0200)',
21
            'format' => ''
22
        ],
23
        'QTD' => [
24
            'type' => 'numeric',
25
            'regex' => '^\d+(\.\d*)?|\.\d+$',
26
            'required' => true,
27
            'info' => 'Quantidade do item',
28
            'format' => '15v3'
29
        ],
30
        'QTD_CANC' => [
31
            'type' => 'numeric',
32
            'regex' => '^\d+(\.\d*)?|\.\d+$',
33
            'required' => false,
34
            'info' => 'Quantidade cancelada, no caso de cancelamento parcial de 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 total do item',
49
            'format' => '15v2'
50
        ],
51
        'CST_ICMS' => [
52
            'type' => 'numeric',
53
            'regex' => '^(\d{3})$',
54
            'required' => true,
55
            'info' => 'Código da Situação Tributária',
56
            'format' => ''
57
        ],
58
        'CFOP' => [
59
            'type' => 'numeric',
60
            'regex' => '^(\d{4})$',
61
            'required' => true,
62
            'info' => 'Código Fiscal de Operação e Prestação',
63
            'format' => ''
64
        ],
65
        'ALIQ_ICMS' => [
66
            'type' => 'numeric',
67
            'regex' => '^\d+(\.\d*)?|\.\d+$',
68
            'required' => false,
69
            'info' => 'Alíquota do ICMS – Carga tributária efetiva em percentual',
70
            'format' => '6v2'
71
        ],
72
        'VL_PIS' => [
73
            'type' => 'numeric',
74
            'regex' => '^\d+(\.\d*)?|\.\d+$',
75
            'required' => false,
76
            'info' => 'Valor do PIS',
77
            'format' => '15v2'
78
        ],
79
        'VL_COFINS' => [
80
            'type' => 'numeric',
81
            'regex' => '^\d+(\.\d*)?|\.\d+$',
82
            'required' => false,
83
            'info' => 'Valor da COFINS',
84
            'format' => '15v2'
85
        ],
86
    ];
87
88
    /**
89
     * Constructor
90
     * @param \stdClass $std
91
     */
92
    public function __construct(\stdClass $std)
93
    {
94
        parent::__construct(self::REG);
95
        $this->std = $this->standarize($std);
96
        $this->postValidation();
97
    }
98
99
100
101
    /**
102
     * Transforma o valor com virgula em float para poder fazer os calculos de verificacao
103
     * @param $vlr
104
     * @return mixed
105
     */
106
    private function strToFloat($vlr)
107
    {
108
        return (float)str_replace(',', '.', $this->std->$vlr);
109
    }
110
111
112
    public function postValidation()
113
    {
114
        $vlItem = $this->strToFloat('vl_item');
115
        if ($vlItem <= 0) {
116
            throw new \InvalidArgumentException("[" . self::REG . "] " .
117
                " O Valor total do item" .
118
                "(VL_ITEM) deve ser maior que 0");
119
        }
120
121
        $vlQtd = $this->strToFloat('qtd');
122
        if ($vlQtd <= 0) {
123
            throw new \InvalidArgumentException("[" . self::REG . "] " .
124
                " Quantidade total do item" .
125
                "(QTD) deve ser maior que 0");
126
        }
127
    }
128
}
129