Passed
Push — master ( 86bb06...22a4bb )
by Francimar
08:25
created

Retorno::isInexistente()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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\Task;
29
30
use NFe\Common\Util;
31
32
class Retorno extends Status
33
{
34
35
    private $data_recebimento;
36
37 1
    public function __construct($retorno = array())
38
    {
39 1
        parent::__construct($retorno);
40 1
    }
41
42
    public function getDataRecebimento($normalize = false)
43
    {
44
        if (!$normalize) {
45
            return $this->data_recebimento;
46
        }
47
        return Util::toDateTime($this->data_recebimento);
48
    }
49
50 1
    public function setDataRecebimento($data_recebimento)
51
    {
52 1
        $this->data_recebimento = $data_recebimento;
53 1
        return $this;
54
    }
55
56
    /**
57
     * Informa se a nota foi autorizada no prazo ou fora do prazo
58
     */
59
    public function isAutorizado()
60
    {
61
        return in_array($this->getStatus(), array('100', '150'));
62
    }
63
64
    /**
65
     * Informa se a nota está cancelada
66
     */
67
    public function isCancelado()
68
    {
69
        return in_array($this->getStatus(), array('101', '151'));
70
    }
71
72
    /**
73
     * Informa se o lote já foi processado e já tem um protocolo
74
     */
75
    public function isProcessado()
76
    {
77
        return $this->getStatus() == '104';
78
    }
79
80
    /**
81
     * Informa se o lote foi recebido com sucesso
82
     */
83
    public function isRecebido()
84
    {
85
        return in_array($this->getStatus(), array('103', '105'));
86
    }
87
88
    /**
89
     * Informa se a nota foi denegada
90
     */
91
    public function isDenegada()
92
    {
93
        return in_array($this->getStatus(), array('110', '301', '302', '303'));
94
    }
95
96
    /**
97
     * Informa se a nota da consulta não foi autorizada ou se não existe
98
     */
99
    public function isInexistente()
100
    {
101
        return $this->getStatus() == '217';
102
    }
103
104
    public function toArray()
105
    {
106
        $retorno = parent::toArray();
107
        $retorno['data_recebimento'] = $this->getDataRecebimento();
108
        return $retorno;
109
    }
110
111 1
    public function fromArray($retorno = array())
112
    {
113 1
        if ($retorno instanceof Retorno) {
114
            $retorno = $retorno->toArray();
115 1
        } elseif (!is_array($retorno)) {
116
            return $this;
117
        }
118 1
        parent::fromArray($retorno);
119 1
        if (isset($retorno['data_recebimento'])) {
120
            $this->setDataRecebimento($retorno['data_recebimento']);
121
        } else {
122 1
            $this->setDataRecebimento(null);
123
        }
124 1
        return $this;
125
    }
126
127
    public function getNode($name = null)
128
    {
129
        $element = parent::getNode(is_null($name)?'':$name);
130
        $dom = $element->ownerDocument;
131
        $status = $element->getElementsByTagName('cStat')->item(0);
132
        if (!is_null($this->getDataRecebimento())) {
133
            $element->insertBefore($dom->createElement('dhRecbto', $this->getDataRecebimento(true)), $status);
134
        }
135
        return $element;
136
    }
137
138
    public function loadNode($element, $name = null)
139
    {
140
        $name = is_null($name)?'Retorno':$name;
141
        $retorno = parent::loadNode($element, $name);
142
        $nodes = $retorno->getElementsByTagName('dhRecbto');
143
        $data_recebimento = null;
144
        if ($nodes->length > 0) {
145
            $data_recebimento = strtotime($nodes->item(0)->nodeValue);
146
        }
147
        $this->setDataRecebimento($data_recebimento);
148
        return $retorno;
149
    }
150
}
151