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

Reducao::loadNode()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 9
cts 10
cp 0.9
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 4
nop 2
crap 3.009
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;
29
30
use NFe\Common\Util;
31
32
/**
33
 * Tributção pelo ICMS
34
 * 20 - Com redução de base de cálculo, estende de
35
 * Normal
36
 */
37
class Reducao extends Normal
38
{
39
40
    private $reducao;
41
42 38
    public function __construct($reducao = array())
43
    {
44 38
        parent::__construct($reducao);
45 38
        $this->setTributacao('20');
46 38
    }
47
48 17
    public function getReducao($normalize = false)
49
    {
50 17
        if (!$normalize) {
51 11
            return $this->reducao;
52
        }
53 16
        return Util::toFloat($this->reducao);
54
    }
55
56 38
    public function setReducao($reducao)
57
    {
58 38
        if (trim($reducao) != '') {
59 15
            $reducao = floatval($reducao);
60 15
        }
61 38
        $this->reducao = $reducao;
62 38
        return $this;
63
    }
64
65
    /**
66
     * Calcula o valor do reduzido da base de cálculo
67
     */
68 5
    public function getReduzido($normalize = false)
69
    {
70 5
        if ($normalize) {
71
            return Util::toCurrency($this->getReduzido());
72
        }
73 5
        return ($this->getBase() * (100.0 - $this->getReducao())) / 100.0;
74
    }
75
76 4
    public function toArray()
77
    {
78 4
        $reducao = parent::toArray();
79 4
        $reducao['reducao'] = $this->getReducao();
80 4
        return $reducao;
81
    }
82
83 38
    public function fromArray($reducao = array())
84
    {
85 38
        if ($reducao instanceof Reducao) {
86 1
            $reducao = $reducao->toArray();
87 38
        } elseif (!is_array($reducao)) {
88 1
            return $this;
89
        }
90 38
        parent::fromArray($reducao);
91 38
        if (isset($reducao['reducao'])) {
92 2
            $this->setReducao($reducao['reducao']);
93 2
        } else {
94 38
            $this->setReducao(null);
95
        }
96 38
        return $this;
97
    }
98
99 16
    public function getNode($name = null)
100
    {
101 16
        $element = parent::getNode(is_null($name)?'ICMS20':$name);
102 16
        $dom = $element->ownerDocument;
103 16
        $element->appendChild($dom->createElement('pRedBC', $this->getReducao(true)));
104 16
        return $element;
105
    }
106
107 7
    public function loadNode($element, $name = null)
108
    {
109 7
        $name = is_null($name)?'ICMS20':$name;
110 7
        $element = parent::loadNode($element, $name);
111 7
        $_fields = $element->getElementsByTagName('pRedBC');
112 7
        if ($_fields->length > 0) {
113 7
            $reducao = $_fields->item(0)->nodeValue;
114 7
        } else {
115
            throw new \Exception('Tag "pRedBC" do campo "Reducao" não encontrada na Reducao', 404);
116
        }
117 7
        $this->setReducao($reducao);
118 7
        return $element;
119
    }
120
}
121