Completed
Push — master ( c523cc...f6da78 )
by Francimar
05:08
created

Emitente::loadNode()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4.0039

Importance

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