Completed
Push — master ( 61245a...b8adf7 )
by Francimar
12:38
created

Cobranca::setNormal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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\Entity\Imposto\ICMS\Simples;
29
30
use NFe\Common\Util;
31
32
/**
33
 * Tributada pelo Simples Nacional com permissão de crédito e com cobrança
34
 * do ICMS por substituição tributária
35
 */
36
class Cobranca extends Parcial
37
{
38
39
    private $normal;
40
41 10
    public function __construct($cobranca = array())
42
    {
43 10
        parent::__construct($cobranca);
44 10
        $this->setTributacao('201');
45 10
    }
46
47 10
    public function getNormal()
48
    {
49 10
        return $this->normal;
50
    }
51
52 10
    public function setNormal($normal)
53
    {
54 10
        $this->normal = $normal;
55 10
        return $this;
56
    }
57
58
    /**
59
     * Calcula o valor do imposto com base na aliquota e valor base
60
     */
61 4
    public function getValor($normalize = false)
62
    {
63 4
        if (!$normalize) {
64 4
            return ($this->getBase() * $this->getAliquota()) / 100.0 - $this->getNormal()->getValor();
65
        }
66 2
        $valor = $this->getValor();
67 2
        return Util::toCurrency($valor);
68
    }
69
70
    /**
71
     * Obtém o valor total do imposto
72
     */
73
    public function getTotal($normalize = false)
74
    {
75
        return $this->getNormal()->getValor($normalize);
76
    }
77
78 4
    public function toArray($recursive = false)
79
    {
80 4
        $cobranca = parent::toArray($recursive);
81 4
        if (!is_null($this->getNormal()) && $recursive) {
82
            $cobranca['normal'] = $this->getNormal()->toArray($recursive);
83
        } else {
84 4
            $cobranca['normal'] = $this->getNormal();
85
        }
86 4
        return $cobranca;
87
    }
88
89 10
    public function fromArray($cobranca = array())
90
    {
91 10
        if ($cobranca instanceof Cobranca) {
92 1
            $cobranca = $cobranca->toArray();
93 10
        } elseif (!is_array($cobranca)) {
94 1
            return $this;
95
        }
96 10
        parent::fromArray($cobranca);
97 10
        if (!isset($cobranca['normal']) || is_null($cobranca['normal'])) {
98 10
            $this->setNormal(new Normal());
99 10
        } else {
100 4
            $this->setNormal($cobranca['normal']);
101
        }
102 10
        return $this;
103
    }
104
105 2
    public function getNode($name = null)
0 ignored issues
show
Complexity introduced by
This operation has 300 execution paths which exceeds the configured maximum of 200.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

You can also find more information in the “Code” section of your repository.

Loading history...
106
    {
107 2
        $element = $this->getNormal()->getNode(is_null($name)?'ICMSSN201':$name);
108 2
        if (is_null($this->getModalidade())) {
109
            return $element;
110
        }
111 2
        $dom = $element->ownerDocument;
112 2
        $parcial = parent::getNode(is_null($name)?'ICMSSN201':$name);
113 2
        if (is_null($this->getNormal()->getModalidade())) {
114
            return $parcial;
115
        }
116 2
        foreach ($parcial->childNodes as $node) {
117 2
            $node = $dom->importNode($node, true);
118 2
            $list = $element->getElementsByTagName($node->nodeName);
119 2
            if ($list->length == 1) {
120 2
                $element->replaceChild($node, $list->item(0));
121 2
            } else {
122 2
                $element->appendChild($node);
123
            }
124 2
        }
125 2
        return $element;
126
    }
127
128 1
    public function loadNode($element, $name = null)
129
    {
130 1
        $name = is_null($name)?'ICMSSN201':$name;
131 1
        if ($element->nodeName != $name) {
132
            $_fields = $element->getElementsByTagName($name);
133
            if ($_fields->length == 0) {
134
                throw new \Exception('Tag "'.$name.'" não encontrada', 404);
135
            }
136
            $element = $_fields->item(0);
137
        }
138 1
        $normal = $this->getNormal();
139 1
        if (is_null($normal)) {
140
            $normal = new Normal();
141
        }
142 1
        $this->setNormal($normal);
143 1
        $_fields = $element->getElementsByTagName('modBCST');
144 1
        if ($_fields->length == 0) {
145
            $normal->loadNode($element, $name);
146
            return $element;
147
        }
148 1
        $element = parent::loadNode($element, $name);
149 1
        $_fields = $element->getElementsByTagName('pCredSN');
150 1
        if ($_fields->length == 0) {
151
            return $element;
152
        }
153 1
        $normal->setModalidade(Normal::MODALIDADE_OPERACAO); // forçar escrita
154 1
        $normal->loadNode($element, $name);
155 1
        return $element;
156
    }
157
}
158