Completed
Push — master ( 15ee14...3f43fb )
by Francimar
17:55
created

Emitente::loadNode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 2
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\Entity;
29
30
use NFe\Common\Util;
31
32
/**
33
 * Empresa que irá emitir as notas fiscais
34
 */
35
class Emitente extends Pessoa
36
{
37
38
    /**
39
     * Código de Regime Tributário. Este campo será obrigatoriamente preenchido
40
     * com: 1 – Simples Nacional; 2 – Simples Nacional – excesso de sublimite
41
     * de receita bruta; 3 – Regime Normal.
42
     */
43
    const REGIME_SIMPLES = 'simples';
44
    const REGIME_EXCESSO = 'excesso';
45
    const REGIME_NORMAL = 'normal';
46
47
    private $fantasia;
48
    private $regime;
49
50 86
    public function __construct($emitente = array())
51
    {
52 86
        parent::__construct($emitente);
53 86
    }
54
55
    /**
56
     * Nome fantasia do da empresa emitente
57
     */
58 29
    public function getFantasia($normalize = false)
59
    {
60 29
        if (!$normalize) {
61 29
            return $this->fantasia;
62
        }
63 29
        return $this->fantasia;
64
    }
65
66 86
    public function setFantasia($fantasia)
67
    {
68 86
        $this->fantasia = $fantasia;
69 86
        return $this;
70
    }
71
72
    /**
73
     * Código de Regime Tributário. Este campo será obrigatoriamente preenchido
74
     * com: 1 – Simples Nacional; 2 – Simples Nacional – excesso de sublimite
75
     * de receita bruta; 3 – Regime Normal.
76
     */
77 29
    public function getRegime($normalize = false)
78
    {
79 29
        if (!$normalize) {
80 4
            return $this->regime;
81
        }
82 29
        switch ($this->regime) {
83 29
            case self::REGIME_SIMPLES:
84 3
                return '1';
85 26
            case self::REGIME_EXCESSO:
86
                return '2';
87 26
            case self::REGIME_NORMAL:
88
                return '3';
89
        }
90 26
        return $this->regime;
91
    }
92
93 86
    public function setRegime($regime)
94
    {
95 86
        $this->regime = $regime;
96 86
        return $this;
97
    }
98
99 4
    public function toArray($recursive = false)
100
    {
101 4
        $emitente = parent::toArray($recursive);
102 4
        $emitente['fantasia'] = $this->getFantasia();
103 4
        $emitente['regime'] = $this->getRegime();
104 4
        return $emitente;
105
    }
106
107 86
    public function fromArray($emitente = array())
108
    {
109 86
        if ($emitente instanceof Emitente) {
110 3
            $emitente = $emitente->toArray();
111 86
        } elseif (!is_array($emitente)) {
112 3
            return $this;
113
        }
114 86
        parent::fromArray($emitente);
115 86
        if (is_null($this->getEndereco())) {
116
            $this->setEndereco(new Endereco());
117
        }
118 86
        if (isset($emitente['fantasia'])) {
119 3
            $this->setFantasia($emitente['fantasia']);
120
        } else {
121 86
            $this->setFantasia(null);
122
        }
123 86
        if (!isset($emitente['regime']) || is_null($emitente['regime'])) {
124 86
            $this->setRegime(self::REGIME_SIMPLES);
125
        } else {
126 3
            $this->setRegime($emitente['regime']);
127
        }
128 86
        return $this;
129
    }
130
131 29
    public function getNode($name = null)
132
    {
133 29
        $dom = new \DOMDocument('1.0', 'UTF-8');
134 29
        $element = $dom->createElement(is_null($name)?'emit':$name);
135 29
        Util::appendNode($element, 'CNPJ', $this->getCNPJ(true));
136 29
        Util::appendNode($element, 'xNome', $this->getRazaoSocial(true));
137 29
        if (!is_null($this->getFantasia())) {
138 29
            Util::appendNode($element, 'xFant', $this->getFantasia(true));
139
        }
140 29
        $endereco = $this->getEndereco()->getNode('enderEmit');
141 29
        $endereco = $dom->importNode($endereco, true);
142 29
        if (!is_null($this->getTelefone())) {
143 29
            Util::appendNode($endereco, 'fone', $this->getTelefone(true));
144
        }
145 29
        $element->appendChild($endereco);
146 29
        Util::appendNode($element, 'IE', $this->getIE(true));
147 29
        if (!is_null($this->getIM())) {
148 29
            Util::appendNode($element, 'IM', $this->getIM(true));
149
        }
150 29
        Util::appendNode($element, 'CRT', $this->getRegime(true));
151 29
        return $element;
152
    }
153
154 26
    public function loadNode($element, $name = null)
155
    {
156 26
        $name = is_null($name)?'emit':$name;
157 26
        $element = parent::loadNode($element, $name);
158 26
        $this->setFantasia(Util::loadNode($element, 'xFant'));
159 26
        $this->setRegime(
160 26
            Util::loadNode(
161 26
                $element,
162 26
                'CRT',
163 26
                'Tag "CRT" do campo "Regime" não encontrada'
164
            )
165
        );
166 26
        return $element;
167
    }
168
}
169