C176::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
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\Common\Keys;
6
use NFePHP\EFD\Common\Element;
7
use NFePHP\EFD\Common\ElementInterface;
8
use \stdClass;
9
10
/**
11
 * REGISTRO C176: RESSARCIMENTO DE ICMS E FUNDO DE COMBATE À POBREZA
12
 * (FCP) EM OPERAÇÕES COM SUBSTITUIÇÃO TRIBUTÁRIA (CÓDIGO 01, 55)
13
 * Este registro deve ser informado quando da escrituração de documento fiscal, que acoberte operação que represente
14
 * desfazimento de substituição tributária realizada em operações anteriores.
15
 * @package NFePHP\EFD\Elements\ICMSIPI
16
 */
17
class C176 extends Element implements ElementInterface
18
{
19
    const REG = 'C176';
20
    const LEVEL = 4;
21
    const PARENT = 'C170';
22
23
    protected $parameters = [
24
        'COD_MOD_ULT_E' => [
25
            'type' => 'string',
26
            'regex' => '^(01|55)$',
27
            'required' => false,
28
            'info' => 'Código do modelo do documento fiscal relativa a última entrada',
29
            'format' => ''
30
        ],
31
        'NUM_DOC_ULT_E' => [
32
            'type' => 'numeric',
33
            'regex' => '^(\d{0,9})$',
34
            'required' => false,
35
            'info' => 'Número do documento fiscal relativa a última entrada',
36
            'format' => ''
37
        ],
38
        'SER_ULT_E' => [
39
            'type' => 'string',
40
            'regex' => '^.{0,3}$',
41
            'required' => false,
42
            'info' => 'Série do documento fiscal relativa a última entrada',
43
            'format' => ''
44
        ],
45
        'DT_ULT_E' => [
46
            'type' => 'string',
47
            'regex' => '^(0[1-9]|[1-2][0-9]|31(?!(?:0[2469]|11))|30(?!02))(0[1-9]|1[0-2])([12]\d{3})$',
48
            'required' => false,
49
            'info' => 'Data relativa a última entrada da mercadoria',
50
            'format' => ''
51
        ],
52
        'COD_PART_ULT_E' => [
53
            'type' => 'string',
54
            'regex' => '^.{0,60}$',
55
            'required' => false,
56
            'info' => 'Código do participante (do emitente do documento relativa a última entrada)',
57
            'format' => ''
58
        ],
59
        'QUANT_ULT_E' => [
60
            'type' => 'numeric',
61
            'regex' => '^\d+(\.\d*)?|\.\d+$',
62
            'required' => false,
63
            'info' => 'Quantidade do item relativa a última entrada',
64
            'format' => '15v3'
65
        ],
66
        'VL_UNIT_ULT_E' => [
67
            'type' => 'numeric',
68
            'regex' => '^\d+(\.\d*)?|\.\d+$',
69
            'required' => false,
70
            'info' => 'Valor unitário da mercadoria constante na NF relativa a última entrada.',
71
            'format' => '15v2'
72
        ],
73
        'VL_UNIT_BC_ST' => [
74
            'type' => 'numeric',
75
            'regex' => '^\d+(\.\d*)?|\.\d+$',
76
            'required' => false,
77
            'info' => 'Valor unitário da base de cálculo do imposto pago por substituição.',
78
            'format' => '15v2'
79
        ],
80
        'CHAVE_NFE_ULT_E' => [
81
            'type' => 'numeric',
82
            'regex' => '^([0-9]{44})?$',
83
            'required' => false,
84
            'info' => 'Número completo da chave da NFe relativo à última entrada',
85
            'format' => ''
86
        ],
87
        'NUM_ITEM_ULT_E' => [
88
            'type' => 'numeric',
89
            'regex' => '^(\d{0,3})$',
90
            'required' => false,
91
            'info' => 'Número sequencial do item na NF entrada',
92
            'format' => ''
93
        ],
94
        'VL_UNIT_BC_ICMS_ULT_E' => [
95
            'type' => 'numeric',
96
            'regex' => '^\d+(\.\d*)?|\.\d+$',
97
            'required' => false,
98
            'info' => 'Valor unitário da base de cálculo da operação própria do remetente 
99
            sob o regime comum de tributação',
100
            'format' => '15v2'
101
        ],
102
        'ALIQ_ICMS_ULT_E' => [
103
            'type' => 'numeric',
104
            'regex' => '^\d+(\.\d*)?|\.\d+$',
105
            'required' => false,
106
            'info' => 'Alíquota do ICMS aplicável à última entrada da mercadoria',
107
            'format' => '15v2'
108
        ],
109
        'VL_UNIT_LIMITE_BC_ICMS_ULT_E' => [
110
            'type' => 'numeric',
111
            'regex' => '^\d+(\.\d*)?|\.\d+$',
112
            'required' => false,
113
            'info' => 'Valor unitário da base de cálculo do ICMS relativo à última entrada da mercadoria',
114
            'format' => '15v2'
115
        ],
116
        'VL_UNIT_ICMS_ULT_E' => [
117
            'type' => 'numeric',
118
            'regex' => '^\d+(\.\d*)?|\.\d+$',
119
            'required' => false,
120
            'info' => 'Valor unitário do crédito de ICMS sobre operações próprias do remetente',
121
            'format' => '15v2'
122
        ],
123
        'ALIQ_ST_ULT_E' => [
124
            'type' => 'numeric',
125
            'regex' => '^\d+(\.\d*)?|\.\d+$',
126
            'required' => false,
127
            'info' => 'Alíquota do ICMS ST relativa à última entrada da mercadoria',
128
            'format' => '15v2'
129
        ],
130
        'VL_UNIT_RES' => [
131
            'type' => 'numeric',
132
            'regex' => '^\d+(\.\d*)?|\.\d+$',
133
            'required' => false,
134
            'info' => 'Valor unitário do ressarcimento (parcial ou completo) de ICMS decorrente da quebra da ST',
135
            'format' => '15v2'
136
        ],
137
        'COD_RESP_RET' => [
138
            'type' => 'numeric',
139
            'regex' => '^([1-3]{1})$',
140
            'required' => false,
141
            'info' => 'Código que indica o responsável pela retenção do ICMS-ST:',
142
            'format' => ''
143
        ],
144
        'COD_MOT_RES' => [
145
            'type' => 'numeric',
146
            'regex' => '^([1-6]{1}|9)$',
147
            'required' => false,
148
            'info' => 'Código do motivo do ressarcimento',
149
            'format' => ''
150
        ],
151
        'CHAVE_NFE_RET' => [
152
            'type' => 'numeric',
153
            'regex' => '^([0-9]{44})?$',
154
            'required' => false,
155
            'info' => 'Número completo da chave da NF-e emitida pelo substituto',
156
            'format' => ''
157
        ],
158
        'COD_PART_NFE_RET' => [
159
            'type' => 'string',
160
            'regex' => '^.{0,60}$',
161
            'required' => false,
162
            'info' => 'Código do participante do emitente da NF-e em que houve a retenção do ICMS-ST',
163
            'format' => ''
164
        ],
165
        'SER_NFE_RET' => [
166
            'type' => 'string',
167
            'regex' => '^.{0,3}$',
168
            'required' => false,
169
            'info' => 'Série da NF-e em que houve a retenção do ICMS- ST',
170
            'format' => ''
171
        ],
172
        'NUM_NFE_RET' => [
173
            'type' => 'numeric',
174
            'regex' => '^(\d{0,9})$',
175
            'required' => false,
176
            'info' => 'Número da NF-e em que houve a retenção do ICMS-ST',
177
            'format' => ''
178
        ],
179
        'ITEM_NFE_RET' => [
180
            'type' => 'numeric',
181
            'regex' => '^(\d{0,3})$',
182
            'required' => false,
183
            'info' => 'Número sequencial do item na NF-e em que houve a retenção do ICMS-ST',
184
            'format' => ''
185
        ],
186
        'COD_DA' => [
187
            'type' => 'string',
188
            'regex' => '^(0|1)$',
189
            'required' => false,
190
            'info' => 'Código do modelo do documento de arrecadação',
191
            'format' => ''
192
        ],
193
        'NUM_DA' => [
194
            'type' => 'string',
195
            'regex' => '^(.*)$',
196
            'required' => false,
197
            'info' => 'Número do documento de arrecadação estadual, se houver',
198
            'format' => ''
199
        ],
200
        'VL_UNIT_RES_FCP_ST' => [
201
            'type' => 'numeric',
202
            'regex' => '^\d+(\.\d*)?|\.\d+$',
203
            'required' => false,
204
            'info' => 'Valor unitário do ressarcimento (parcial ou completo) de FCP decorrente da quebra da ST',
205
            'format' => '15v2'
206
        ],
207
    ];
208
209
    /**
210
     * Constructor
211
     * @param \stdClass $std
212
     */
213
    public function __construct(\stdClass $std)
214
    {
215
        parent::__construct(self::REG);
216
        $this->std = $this->standarize($std);
217
        $this->postValidation();
218
    }
219
220
    public function postValidation()
221
    {
222
        //Valida se QUANT_ULT_E é maior que zero
223
        if ($this->values->quant_ult_e <= 0) {
224
            throw new \InvalidArgumentException("[" . self::REG . "] " .
225
                " Quantidade do item relativa a última entrada (QUANT_ULT_E) deve ser maior do que zero ");
226
        }
227
        //Valida se VL_UNIT_ULT_E é maior que zero
228
        if ($this->values->vl_unit_ult_e <= 0) {
229
            throw new \InvalidArgumentException("[" . self::REG . "] " .
230
                " Valor unitário da mercadoria constante na NF (VL_UNIT_ULT_E) deve ser maior do que zero ");
231
        }
232
        //Valida se VL_UNIT_BC_ST é maior que zero
233
        if ($this->values->vl_unit_bc_st <= 0) {
234
            throw new \InvalidArgumentException("[" . self::REG . "] " .
235
                " Valor unitário da base de cálculo do imposto " .
236
                "pago por substituição (VL_UNIT_BC_ST) deve ser maior do que zero ");
237
        }
238
        //Valida se VL_UNIT_BC_ICMS_ULT_E é maior que zero
239
        if ($this->values->vl_unit_bc_icms_ult_e <= 0) {
240
            throw new \InvalidArgumentException("[" . self::REG . "] " .
241
                " Valor unitário da base de cálculo do ICMS " .
242
                "relativo à última entrada da mercadoria, limitado " .
243
                "ao valor da BC da retenção (VL_UNIT_BC_ICMS_ULT_E) deve ser maior do que zero ");
244
        }
245
        //Valida se ALIQ_ICMS_ULT_E é maior que zero
246
        if ($this->values->aliq_icms_ult_e <= 0) {
247
            throw new \InvalidArgumentException("[" . self::REG . "] " .
248
                "Alíquota do ICMS aplicável à última entrada da mercadoria  " .
249
                "(ALIQ_ICMS_ULT_E) deve ser maior do que zero ");
250
        }
251
        if ($this->values->vl_unit_limite_bc_icms_ult_e <= 0 ||
252
            $this->values->vl_unit_limite_bc_icms_ult_e >= $this->values->vl_unit_bc_st ||
253
            $this->values->vl_unit_limite_bc_icms_ult_e >= $this->values->vl_unit_bc_icms_ult_e
254
        ) {
255
            throw new \InvalidArgumentException("[" . self::REG . "] " .
256
                "O campo  VL_UNIT_LIMITE_BC_ICMS_ULT_E deve ser maior que zero e menor que os " .
257
                "campos VL_UNIT_BC_ST e VL_UNIT_BC_ICMS_ULT_E");
258
        }
259
        $multplicacao = round(
260
            $this->values->aliq_icms_ult_e
261
            * $this->values->vl_unit_limite_bc_icms_ult_e,
262
            2
263
        );
264 View Code Duplication
        if (round($this->values->vl_unit_icms_ult_e, 2) != $multplicacao) {
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...
265
            throw new \InvalidArgumentException("[" . self::REG . "] " .
266
                "O campo  VL_UNIT_ICMS_ULT_E deve ser maior que zero e " .
267
                "deve corresponder a multiplicação entre os campos " .
268
                "campos ALIQ_ICMS_ULT_E e VL_UNIT_LIMITE_BC_ICMS_ULT_E");
269
        }
270
        if ($this->values->aliq_st_ult_e <= 0) {
271
            throw new \InvalidArgumentException("[" . self::REG . "] " .
272
                "O campo  ALIQ_ST_ULT_E deve ser maior que zero");
273
        }
274
        $calc = round(
275
            $this->values->vl_unit_bc_st
276
            * $this->values->aliq_st_ult_e
277
            - $this->values->vl_unit_icms_ult_e,
278
            2
279
        );
280 View Code Duplication
        if (round($this->values->vl_unit_res, 2) !== $calc) {
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...
281
            throw new \InvalidArgumentException("[" . self::REG . "] " .
282
                "o valor informado no campo VL_UNIT_RES deve ser maior ou igual que “0” (zero), e deve corresponder " .
283
                "a multiplicação entre os campos VL_UNIT_BC_ST e ALIQ_ST_ULT_E, subtraindo, deste resultado," .
284
                " o campo VL_UNIT_ICMS_ULT_E.");
285
        }
286
        if (!$this->std->cod_da and $this->std->cod_resp_ret == 3) {
287
            throw new \InvalidArgumentException("[" . self::REG . "] " .
288
                "Quando o campo COD_RESP_RET é igual a '3', o campo COD_DA é obrigatório");
289
        }
290
        if ($this->std->chave_nfe_ret and !Keys::isValid($this->std->chave_nfe_ret)) {
291
            throw new \InvalidArgumentException("[" . self::REG . "] " .
292
                " Dígito verificador incorreto no campo campo chave da " .
293
                "nota fiscal eletronica (CHAVE_NFE_RET)");
294
        }
295
    }
296
}
297