Completed
Push — master ( b8adf7...397fc8 )
by Francimar
13:07
created

Protocolo::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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\Common\Util;
32
33
/**
34
 * Protocolo de autorização da nota, é retornado pela autorização, recibo
35
 * ou situação e anexado à nota
36
 */
37
class Protocolo extends Retorno
38
{
39
40
    private $chave;
41
    private $validacao;
42
    private $numero;
43
44 9
    public function __construct($protocolo = array())
45
    {
46 9
        parent::__construct($protocolo);
47 9
    }
48
49
    /**
50
     * Chaves de acesso da NF-e, compostas por: UF do emitente, AAMM da emissão
51
     * da NFe, CNPJ do emitente, modelo, série e número da NF-e e código
52
     * numérico+DV.
53
     */
54 7
    public function getChave($normalize = false)
55
    {
56 7
        if (!$normalize) {
57 7
            return $this->chave;
58
        }
59 2
        return $this->chave;
60
    }
61
62 9
    public function setChave($chave)
63
    {
64 9
        $this->chave = $chave;
65 9
        return $this;
66
    }
67
68
    /**
69
     * Digest Value da NF-e processada. Utilizado para conferir a integridade
70
     * da NF-e original.
71
     */
72 3
    public function getValidacao($normalize = false)
73
    {
74 3
        if (!$normalize) {
75 3
            return $this->validacao;
76
        }
77 2
        return $this->validacao;
78
    }
79
80 9
    public function setValidacao($validacao)
81
    {
82 9
        $this->validacao = $validacao;
83 9
        return $this;
84
    }
85
86
    /**
87
     * Número do Protocolo de Status da NF-e. 1 posição (1 – Secretaria de
88
     * Fazenda Estadual 2 – Receita Federal); 2 - códiga da UF - 2 posições
89
     * ano; 10 seqüencial no ano.
90
     */
91 3
    public function getNumero($normalize = false)
92
    {
93 3
        if (!$normalize) {
94 3
            return $this->numero;
95
        }
96 2
        return $this->numero;
97
    }
98
99 9
    public function setNumero($numero)
100
    {
101 9
        $this->numero = $numero;
102 9
        return $this;
103
    }
104
105 3
    public function toArray($recursive = false)
106
    {
107 3
        $protocolo = parent::toArray($recursive);
108 3
        $protocolo['chave'] = $this->getChave();
109 3
        $protocolo['validacao'] = $this->getValidacao();
110 3
        $protocolo['numero'] = $this->getNumero();
111 3
        return $protocolo;
112
    }
113
114 9
    public function fromArray($protocolo = array())
115
    {
116 9
        if ($protocolo instanceof Protocolo) {
117 2
            $protocolo = $protocolo->toArray();
118 9
        } elseif (!is_array($protocolo)) {
119 2
            return $this;
120
        }
121 9
        parent::fromArray($protocolo);
122 9
        if (isset($protocolo['chave'])) {
123 2
            $this->setChave($protocolo['chave']);
124
        } else {
125 9
            $this->setChave(null);
126
        }
127 9
        if (isset($protocolo['validacao'])) {
128 2
            $this->setValidacao($protocolo['validacao']);
129
        } else {
130 9
            $this->setValidacao(null);
131
        }
132 9
        if (isset($protocolo['numero'])) {
133 2
            $this->setNumero($protocolo['numero']);
134
        } else {
135 9
            $this->setNumero(null);
136
        }
137 9
        return $this;
138
    }
139
140 8
    public function loadNode($element, $name = null)
141
    {
142 8
        $name = is_null($name)?'infProt':$name;
143 8
        $element = parent::loadNode($element, $name);
144 8
        $this->setChave(
145 8
            Util::loadNode(
146 8
                $element,
147 8
                'chNFe',
148 8
                'Tag "chNFe" não encontrada no Protocolo'
149
            )
150
        );
151 8
        $this->setValidacao(Util::loadNode($element, 'digVal'));
152 8
        $this->setNumero(Util::loadNode($element, 'nProt'));
153 8
        return $element;
154
    }
155
156 2
    public function getNode($name = null)
157
    {
158 2
        $old_uf = $this->getUF();
159 2
        $this->setUF(null);
160 2
        $info = parent::getNode('infProt');
161 2
        $this->setUF($old_uf);
162 2
        $dom = $info->ownerDocument;
163 2
        $element = $dom->createElement(is_null($name)?'protNFe':$name);
164 2
        $versao = $dom->createAttribute('versao');
165 2
        $versao->value = Nota::VERSAO;
166 2
        $element->appendChild($versao);
167
168 2
        $id = $dom->createAttribute('Id');
169 2
        $id->value = 'ID'.$this->getNumero(true);
170 2
        $info->appendChild($id);
171
172 2
        $status = $info->getElementsByTagName('cStat')->item(0);
173 2
        Util::appendNode($info, 'nProt', $this->getNumero(true), $status);
174 2
        Util::appendNode($info, 'digVal', $this->getValidacao(true), $status);
175 2
        $nodes = $info->getElementsByTagName('dhRecbto');
176 2
        if ($nodes->length > 0) {
177 2
            $recebimento = $nodes->item(0);
178
        } else {
179
            $recebimento = $status;
180
        }
181 2
        Util::appendNode($info, 'chNFe', $this->getChave(true), $recebimento);
182 2
        $element->appendChild($info);
183 2
        return $element;
184
    }
185
}
186