Passed
Push — master ( a117b5...cf3061 )
by Francimar
07:27
created

Inutilizacao::getID()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * MIT License
4
 *
5
 * Copyright (c) 2016 MZ Desenvolvimento de Sistemas LTDA
6
 *
7
 * @author Francimar Alves <[email protected]>
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
 * SOFTWARE.
26
 *
27
 */
28
namespace NFe\Task;
29
30
use NFe\Core\Nota;
31
use NFe\Core\SEFAZ;
32
use NFe\Common\Util;
33
use NFe\Exception\ValidationException;
34
use FR3D\XmlDSig\Adapter\AdapterInterface;
35
use FR3D\XmlDSig\Adapter\XmlseclibsAdapter;
36
37
class Inutilizacao extends Retorno
38
{
39
    private $id;
40
    private $ano;
41
    private $cnpj;
42
    private $modelo;
43
    private $serie;
44
    private $inicio;
45
    private $final;
46
    private $justificativa;
47
    private $numero;
48
49 9
    public function __construct($inutilizacao = [])
50
    {
51 9
        parent::__construct($inutilizacao);
52 9
    }
53
54
    /**
55
     * Formado por:
56
     * ID = Literal
57
     * 43 = Código Estado
58
     * 15 = Ano
59
     *
60
     * 00000000000000 = CNPJ
61
     * 55 = Modelo
62
     * 001 = Série
63
     * 000000411 =
64
     * Número Inicial
65
     * 000000411 = Número Final
66
     */
67 6
    public function getID($normalize = false)
68
    {
69 6
        if (!$normalize) {
70 2
            return $this->id;
71
        }
72 6
        return 'ID' . $this->id;
73
    }
74
75 9
    public function setID($id)
76
    {
77 9
        $this->id = $id;
78 9
        return $this;
79
    }
80
81 6
    public function getAno($normalize = false)
82
    {
83 6
        if (!$normalize) {
84 1
            return $this->ano;
85
        }
86 6
        return $this->ano % 100;
87
    }
88
89 9
    public function setAno($ano)
90
    {
91 9
        $this->ano = $ano;
92 9
        return $this;
93
    }
94
95 6
    public function getCNPJ($normalize = false)
96
    {
97 6
        if (!$normalize) {
98 1
            return $this->cnpj;
99
        }
100 6
        return $this->cnpj;
101
    }
102
103 9
    public function setCNPJ($cnpj)
104
    {
105 9
        $this->cnpj = $cnpj;
106 9
        return $this;
107
    }
108
109
    /**
110
     * Código do modelo do Documento Fiscal. 55 = NF-e; 65 = NFC-e.
111
     * @param boolean $normalize informa se o modelo deve estar no formato do XML
112
     * @return mixed modelo do Envio
113
     */
114 7
    public function getModelo($normalize = false)
115
    {
116 7
        if (!$normalize) {
117 6
            return $this->modelo;
118
        }
119 7
        switch ($this->modelo) {
120
            case Nota::MODELO_NFE:
121 1
                return '55';
122
            case Nota::MODELO_NFCE:
123 5
                return '65';
124
        }
125 2
        return $this->modelo;
126
    }
127
128
    /**
129
     * Altera o valor do Modelo para o informado no parâmetro
130
     * @param mixed $modelo novo valor para Modelo
131
     * @return Envio A própria instância da classe
132
     */
133 9
    public function setModelo($modelo)
134
    {
135
        switch ($modelo) {
136 9
            case '55':
137 1
                $modelo = Nota::MODELO_NFE;
138 1
                break;
139 9
            case '65':
140 6
                $modelo = Nota::MODELO_NFCE;
141 6
                break;
142
        }
143 9
        $this->modelo = $modelo;
144 9
        return $this;
145
    }
146
147 6
    public function getSerie($normalize = false)
148
    {
149 6
        if (!$normalize) {
150 1
            return $this->serie;
151
        }
152 6
        return $this->serie;
153
    }
154
155 9
    public function setSerie($serie)
156
    {
157 9
        $this->serie = $serie;
158 9
        return $this;
159
    }
160
161 7
    public function getInicio($normalize = false)
162
    {
163 7
        if (!$normalize) {
164 5
            return $this->inicio;
165
        }
166 6
        return $this->inicio;
167
    }
168
169 9
    public function setInicio($inicio)
170
    {
171 9
        $this->inicio = $inicio;
172 9
        return $this;
173
    }
174
175 6
    public function getFinal($normalize = false)
176
    {
177 6
        if (!$normalize) {
178 1
            return $this->final;
179
        }
180 6
        return $this->final;
181
    }
182
183 9
    public function setFinal($final)
184
    {
185 9
        $this->final = $final;
186 9
        return $this;
187
    }
188
189 6
    public function getJustificativa($normalize = false)
190
    {
191 6
        if (!$normalize) {
192 1
            return $this->justificativa;
193
        }
194 6
        return $this->justificativa;
195
    }
196
197 9
    public function setJustificativa($justificativa)
198
    {
199 9
        $this->justificativa = $justificativa;
200 9
        return $this;
201
    }
202
203 3
    public function getNumero($normalize = false)
204
    {
205 3
        if (!$normalize) {
206 2
            return $this->numero;
207
        }
208 3
        return $this->numero;
209
    }
210
211 9
    public function setNumero($numero)
212
    {
213 9
        $this->numero = $numero;
214 9
        return $this;
215
    }
216
217
    /**
218
     * Informa se os números foram inutilizados
219
     */
220 4
    public function isInutilizado()
221
    {
222 4
        return in_array($this->getStatus(), ['102', '563']);
223
    }
224
225 1
    public function toArray($recursive = false)
226
    {
227 1
        $inutilizacao = parent::toArray($recursive);
228 1
        $inutilizacao['id'] = $this->getID();
229 1
        $inutilizacao['ano'] = $this->getAno();
230 1
        $inutilizacao['cnpj'] = $this->getCNPJ();
231 1
        $inutilizacao['modelo'] = $this->getModelo();
232 1
        $inutilizacao['serie'] = $this->getSerie();
233 1
        $inutilizacao['inicio'] = $this->getInicio();
234 1
        $inutilizacao['final'] = $this->getFinal();
235 1
        $inutilizacao['justificativa'] = $this->getJustificativa();
236 1
        $inutilizacao['numero'] = $this->getNumero();
237 1
        return $inutilizacao;
238
    }
239
240 9
    public function fromArray($inutilizacao = [])
241
    {
242 9
        if ($inutilizacao instanceof Inutilizacao) {
243 1
            $inutilizacao = $inutilizacao->toArray();
244 9
        } elseif (!is_array($inutilizacao)) {
245 1
            return $this;
246
        }
247 9
        parent::fromArray($inutilizacao);
248 9
        if (isset($inutilizacao['id'])) {
249 1
            $this->setID($inutilizacao['id']);
250
        } else {
251 9
            $this->setID(null);
252
        }
253 9
        if (isset($inutilizacao['ano'])) {
254 1
            $this->setAno($inutilizacao['ano']);
255
        } else {
256 9
            $this->setAno(null);
257
        }
258 9
        if (isset($inutilizacao['cnpj'])) {
259 1
            $this->setCNPJ($inutilizacao['cnpj']);
260
        } else {
261 9
            $this->setCNPJ(null);
262
        }
263 9
        if (isset($inutilizacao['modelo'])) {
264 1
            $this->setModelo($inutilizacao['modelo']);
265
        } else {
266 9
            $this->setModelo(null);
267
        }
268 9
        if (isset($inutilizacao['serie'])) {
269 1
            $this->setSerie($inutilizacao['serie']);
270
        } else {
271 9
            $this->setSerie(null);
272
        }
273 9
        if (isset($inutilizacao['inicio'])) {
274 1
            $this->setInicio($inutilizacao['inicio']);
275
        } else {
276 9
            $this->setInicio(null);
277
        }
278 9
        if (isset($inutilizacao['final'])) {
279 1
            $this->setFinal($inutilizacao['final']);
280
        } else {
281 9
            $this->setFinal(null);
282
        }
283 9
        if (isset($inutilizacao['justificativa'])) {
284 1
            $this->setJustificativa($inutilizacao['justificativa']);
285
        } else {
286 9
            $this->setJustificativa(null);
287
        }
288 9
        if (isset($inutilizacao['numero'])) {
289 1
            $this->setNumero($inutilizacao['numero']);
290
        } else {
291 9
            $this->setNumero(null);
292
        }
293 9
        return $this;
294
    }
295
296 7
    public function gerarID()
297
    {
298 7
        $id = sprintf(
299 7
            '%02d%02d%s%02d%03d%09d%09d',
300 7
            $this->getUF(true),
301 6
            $this->getAno(true), // 2 dígitos
302 6
            $this->getCNPJ(true),
303 6
            $this->getModelo(true),
304 6
            $this->getSerie(true),
305 6
            $this->getInicio(true),
306 6
            $this->getFinal(true)
307
        );
308 6
        return $id;
309
    }
310
311 7
    public function getNode($name = null)
312
    {
313 7
        $this->setID($this->gerarID());
314
315 6
        $dom = new \DOMDocument('1.0', 'UTF-8');
316 6
        $element = $dom->createElement(is_null($name) ? 'inutNFe' : $name);
317 6
        $element->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns', Nota::PORTAL);
318 6
        $versao = $dom->createAttribute('versao');
319 6
        $versao->value = Nota::VERSAO;
320 6
        $element->appendChild($versao);
321
322 6
        $info = $dom->createElement('infInut');
323 6
        $id = $dom->createAttribute('Id');
324 6
        $id->value = $this->getID(true);
325 6
        $info->appendChild($id);
326
327 6
        Util::appendNode($info, 'tpAmb', $this->getAmbiente(true));
328 6
        Util::appendNode($info, 'xServ', 'INUTILIZAR');
329 6
        Util::appendNode($info, 'cUF', $this->getUF(true));
330 6
        Util::appendNode($info, 'ano', $this->getAno(true));
331 6
        Util::appendNode($info, 'CNPJ', $this->getCNPJ(true));
332 6
        Util::appendNode($info, 'mod', $this->getModelo(true));
333 6
        Util::appendNode($info, 'serie', $this->getSerie(true));
334 6
        Util::appendNode($info, 'nNFIni', $this->getInicio(true));
335 6
        Util::appendNode($info, 'nNFFin', $this->getFinal(true));
336 6
        Util::appendNode($info, 'xJust', $this->getJustificativa(true));
337 6
        $element->appendChild($info);
338 6
        $dom->appendChild($element);
339 6
        return $element;
340
    }
341
342 3
    public function getReturnNode()
343
    {
344 3
        $outros = parent::getNode('infInut');
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getNode() instead of getReturnNode()). Are you sure this is correct? If so, you might want to change this to $this->getNode().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
345 3
        $element = $this->getNode('retInutNFe');
346 3
        $dom = $element->ownerDocument;
347 3
        $info = $dom->getElementsByTagName('infInut')->item(0);
348 3
        $info->removeAttribute('Id');
349 3
        $remove_tags = ['tpAmb', 'xServ', 'xJust'];
350 3
        foreach ($remove_tags as $key) {
351 3
            $node = $info->getElementsByTagName($key)->item(0);
352 3
            $info->removeChild($node);
353
        }
354 3
        $uf = $info->getElementsByTagName('cUF')->item(0);
355 3
        foreach ($outros->childNodes as $node) {
356 3
            $node = $dom->importNode($node, true);
357 3
            $list = $info->getElementsByTagName($node->nodeName);
358 3
            if ($list->length == 1) {
359 3
                continue;
360
            }
361 3
            switch ($node->nodeName) {
362 3
                case 'dhRecbto':
363 3
                    $info->appendChild($node);
364 3
                    break;
365
                default:
366 3
                    $info->insertBefore($node, $uf);
367
            }
368
        }
369 3
        Util::appendNode($info, 'nProt', $this->getNumero(true));
370 3
        return $element;
371
    }
372
373 5
    public function loadNode($element, $name = null)
374
    {
375 5
        $name = is_null($name) ? 'infInut' : $name;
376 5
        $element = parent::loadNode($element, $name);
377 4
        if (!$this->isInutilizado()) {
378 1
            return $element;
379
        }
380 3
        $this->setAno(Util::loadNode($element, 'ano'));
381 3
        $this->setCNPJ(Util::loadNode($element, 'CNPJ'));
382 3
        $this->setModelo(Util::loadNode($element, 'mod'));
383 3
        $this->setSerie(Util::loadNode($element, 'serie'));
384 3
        $this->setInicio(Util::loadNode($element, 'nNFIni'));
385 3
        $this->setFinal(Util::loadNode($element, 'nNFFin'));
386 3
        $this->setNumero(Util::loadNode($element, 'nProt'));
387 3
        return $element;
388
    }
389
390 5
    public function envia($dom)
391
    {
392 5
        $envio = new Envio();
393 5
        $envio->setServico(Envio::SERVICO_INUTILIZACAO);
394 5
        $envio->setAmbiente($this->getAmbiente());
395 5
        $envio->setModelo($this->getModelo());
396 5
        $envio->setEmissao(Nota::EMISSAO_NORMAL);
397 5
        $this->setVersao($envio->getVersao());
398 5
        $dom = $this->validar($dom);
399 5
        $envio->setConteudo($dom);
400 5
        $resp = $envio->envia();
401 5
        $this->loadNode($resp);
402 4
        if (!$this->isInutilizado()) {
403 1
            throw new \Exception($this->getMotivo(), $this->getStatus());
404
        }
405 3
        return $this->getReturnNode()->ownerDocument;
406
    }
407
408
    /**
409
     * Assina o XML com a assinatura eletrônica do tipo A1
410
     */
411 6
    public function assinar($dom = null)
412
    {
413 6
        if (is_null($dom)) {
414 2
            $xml = $this->getNode();
415 2
            $dom = $xml->ownerDocument;
416
        }
417 6
        $config = SEFAZ::getInstance()->getConfiguracao();
418 6
        $config->verificaValidadeCertificado();
419
420 6
        $adapter = new XmlseclibsAdapter();
421 6
        $adapter->setPrivateKey($config->getChavePrivada());
0 ignored issues
show
Deprecated Code introduced by
The method NFe\Common\Configuracao::getChavePrivada() has been deprecated with message: Use getCertificado()->getChavePrivada

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
422 6
        $adapter->setPublicKey($config->getChavePublica());
0 ignored issues
show
Deprecated Code introduced by
The method NFe\Common\Configuracao::getChavePublica() has been deprecated with message: Use getCertificado()->getChavePublica

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
423 6
        $adapter->addTransform(AdapterInterface::ENVELOPED);
424 6
        $adapter->addTransform(AdapterInterface::XML_C14N);
425 6
        $adapter->sign($dom, 'infInut');
426 6
        return $dom;
427
    }
428
429
    /**
430
     * Valida o documento após assinar
431
     */
432 6
    public function validar($dom)
433
    {
434 6
        $dom->loadXML($dom->saveXML());
435 6
        $xsd_path = dirname(__DIR__) . '/Core/schema';
436 6
        $xsd_file = $xsd_path . '/inutNFe_v' . $this->getVersao() . '.xsd';
437 6
        if (!file_exists($xsd_file)) {
438 1
            throw new \Exception(sprintf('O arquivo "%s" de esquema XSD não existe!', $xsd_file), 404);
439
        }
440
        // Enable user error handling
441 5
        $save = libxml_use_internal_errors(true);
442 5
        if ($dom->schemaValidate($xsd_file)) {
443 5
            libxml_use_internal_errors($save);
444 5
            return $dom;
445
        }
446
        $msg = [];
447
        $errors = libxml_get_errors();
448
        foreach ($errors as $error) {
449
            $msg[] = 'Não foi possível validar o XML: ' . $error->message;
450
        }
451
        libxml_clear_errors();
452
        libxml_use_internal_errors($save);
453
        throw new ValidationException($msg);
454
    }
455
}
456