Passed
Push — master ( 22a4bb...4eeb09 )
by Francimar
05:29
created

Isento::fromArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 3
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\Imposto\COFINS;
29
30
use NFe\Common\Util;
31
use NFe\Entity\Imposto;
32
33
class Isento extends Imposto
34
{
35
36
    /**
37
     * Código de Situação Tributária do COFINS:
38
     * 04 - Operação Tributável -
39
     * Tributação Monofásica - (Alíquota Zero);
40
     * 05 - Operação Tributável
41
     * (ST);
42
     * 06 - Operação Tributável - Alíquota Zero;
43
     * 07 - Operação Isenta da
44
     * contribuição;
45
     * 08 - Operação Sem Incidência da contribuição;
46
     * 09 -
47
     * Operação com suspensão da contribuição;
48
     */
49
    const TRIBUTACAO_MONOFASICA = 'monofasica';
50
    const TRIBUTACAO_ST = 'st';
51
    const TRIBUTACAO_ZERO = 'zero';
52
    const TRIBUTACAO_ISENTA = 'isenta';
53
    const TRIBUTACAO_INCIDENCIA = 'incidencia';
54
    const TRIBUTACAO_SUSPENSAO = 'suspensao';
55
56 3
    public function __construct($cofins = array())
57
    {
58 3
        parent::__construct($cofins);
59 3
        $this->setGrupo(self::GRUPO_COFINS);
60 3
        $this->setBase(0.00);
61 3
        $this->setAliquota(0.0);
62 3
    }
63
64
    /**
65
     * Código de Situação Tributária do COFINS:
66
     * 04 - Operação Tributável -
67
     * Tributação Monofásica - (Alíquota Zero);
68
     * 05 - Operação Tributável
69
     * (ST);
70
     * 06 - Operação Tributável - Alíquota Zero;
71
     * 07 - Operação Isenta da
72
     * contribuição;
73
     * 08 - Operação Sem Incidência da contribuição;
74
     * 09 -
75
     * Operação com suspensão da contribuição;
76
     * @param boolean $normalize informa se a tributacao deve estar no formato do XML
77
     * @return mixed tributacao do Isento
78
     */
79 2
    public function getTributacao($normalize = false)
80
    {
81 2
        if (!$normalize) {
82 1
            return parent::getTributacao();
83
        }
84 2
        switch (parent::getTributacao()) {
85 2
            case self::TRIBUTACAO_MONOFASICA:
86 2
                return '04';
87
            case self::TRIBUTACAO_ST:
88
                return '05';
89
            case self::TRIBUTACAO_ZERO:
90
                return '06';
91
            case self::TRIBUTACAO_ISENTA:
92
                return '07';
93
            case self::TRIBUTACAO_INCIDENCIA:
94
                return '08';
95
            case self::TRIBUTACAO_SUSPENSAO:
96
                return '09';
97
        }
98
        return parent::getTributacao($normalize);
99
    }
100
    
101
    /**
102
     * Altera o valor da Tributacao para o informado no parâmetro
103
     * @param mixed $tributacao novo valor para Tributacao
104
     * @return Isento A própria instância da classe
105
     */
106 3
    public function setTributacao($tributacao)
107
    {
108
        switch ($tributacao) {
109 3
            case '04':
110 1
                $tributacao = self::TRIBUTACAO_MONOFASICA;
111 1
                break;
112 3
            case '05':
113
                $tributacao = self::TRIBUTACAO_ST;
114
                break;
115 3
            case '06':
116
                $tributacao = self::TRIBUTACAO_ZERO;
117
                break;
118 3
            case '07':
119
                $tributacao = self::TRIBUTACAO_ISENTA;
120
                break;
121 3
            case '08':
122
                $tributacao = self::TRIBUTACAO_INCIDENCIA;
123
                break;
124 3
            case '09':
125
                $tributacao = self::TRIBUTACAO_SUSPENSAO;
126
                break;
127
        }
128 3
        return parent::setTributacao($tributacao);
129
    }
130
131 1
    public function toArray($recursive = false)
132
    {
133 1
        $cofins = parent::toArray($recursive);
134 1
        return $cofins;
135
    }
136
137 3
    public function fromArray($cofins = array())
138
    {
139 3
        if ($cofins instanceof Isento) {
140 1
            $cofins = $cofins->toArray();
141 3
        } elseif (!is_array($cofins)) {
142 1
            return $this;
143
        }
144 3
        parent::fromArray($cofins);
145 3
        return $this;
146
    }
147
148 2
    public function getNode($name = null)
149
    {
150 2
        $dom = new \DOMDocument('1.0', 'UTF-8');
151 2
        $element = $dom->createElement(is_null($name)?'COFINSNT':$name);
152 2
        Util::appendNode($element, 'CST', $this->getTributacao(true));
153 2
        return $element;
154
    }
155
156 1
    public function loadNode($element, $name = null)
157
    {
158 1
        $name = is_null($name)?'COFINSNT':$name;
159 1
        if ($element->tagName != $name) {
160
            $_fields = $element->getElementsByTagName($name);
161
            if ($_fields->length == 0) {
162
                throw new \Exception('Tag "'.$name.'" não encontrada', 404);
163
            }
164
            $element = $_fields->item(0);
165
        }
166 1
        $this->setTributacao(
167 1
            Util::loadNode(
168
                $element,
169 1
                'CST',
170 1
                'Tag "CST" do campo "Tributacao" não encontrada'
171
            )
172
        );
173 1
        return $element;
174
    }
175
}
176