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
|
|
|
use NFe\Common\Util; |
31
|
|
|
use NFe\Common\Node; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Grupo de informações do responsável técnico pelo sistema |
35
|
|
|
*/ |
36
|
|
|
class Responsavel extends Pessoa implements Node |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Informar o nome da pessoa a ser contatada na empresa desenvolvedora do |
41
|
|
|
* sistema utilizado na emissão do documento fiscal eletrônico. |
42
|
|
|
*/ |
43
|
|
|
private $contato; |
44
|
|
|
private $email; |
45
|
|
|
/** |
46
|
|
|
* Identificador do CSRT utilizado para montar o hash do CSRT |
47
|
|
|
*/ |
48
|
|
|
private $idcsrt; |
49
|
|
|
/** |
50
|
|
|
* O hashCSRT é o resultado da função hash (SHA-1 – Base64) do CSRT |
51
|
|
|
* fornecido pelo fisco mais a Chave de Acesso da NFe. |
52
|
|
|
*/ |
53
|
|
|
private $hash_csrt; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Constroi uma instância de Responsavel vazia |
57
|
|
|
* @param array $responsavel Array contendo dados do Responsavel |
58
|
|
|
*/ |
59
|
40 |
|
public function __construct($responsavel = []) |
60
|
|
|
{ |
61
|
40 |
|
$this->fromArray($responsavel); |
62
|
40 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Informar o nome da pessoa a ser contatada na empresa desenvolvedora do |
66
|
|
|
* sistema utilizado na emissão do documento fiscal eletrônico. |
67
|
|
|
* @param boolean $normalize informa se o contato deve estar no formato do XML |
68
|
|
|
* @return mixed contato do Responsavel |
69
|
|
|
*/ |
70
|
36 |
|
public function getContato($normalize = false) |
71
|
|
|
{ |
72
|
36 |
|
if (!$normalize) { |
73
|
10 |
|
return $this->contato; |
74
|
|
|
} |
75
|
34 |
|
return $this->contato; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Altera o valor do Contato para o informado no parâmetro |
80
|
|
|
* @param mixed $contato novo valor para Contato |
81
|
|
|
* @return Responsavel A própria instância da classe |
82
|
|
|
*/ |
83
|
40 |
|
public function setContato($contato) |
84
|
|
|
{ |
85
|
40 |
|
$this->contato = $contato; |
86
|
40 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
36 |
|
public function getEmail($normalize = false) |
90
|
|
|
{ |
91
|
36 |
|
if (!$normalize) { |
92
|
10 |
|
return $this->email; |
93
|
|
|
} |
94
|
34 |
|
return $this->email; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Altera o valor da Email para o informado no parâmetro |
99
|
|
|
* @param mixed $email novo valor para Email |
100
|
|
|
* @return Responsavel A própria instância da classe |
101
|
|
|
*/ |
102
|
40 |
|
public function setEmail($email) |
103
|
|
|
{ |
104
|
40 |
|
$this->email = $email; |
105
|
40 |
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Identificador do CSRT utilizado para montar o hash do CSRT |
110
|
|
|
* @param boolean $normalize informa se a id_csrt deve estar no formato do XML |
111
|
|
|
* @return mixed id_csrt do Responsavel |
112
|
|
|
*/ |
113
|
36 |
|
public function getIDCsrt($normalize = false) |
114
|
|
|
{ |
115
|
36 |
|
if (!$normalize) { |
116
|
36 |
|
return $this->idcsrt; |
117
|
|
|
} |
118
|
34 |
|
return $this->idcsrt; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Altera o valor da IDCsrt para o informado no parâmetro |
123
|
|
|
* @param mixed $idcsrt novo valor para IDCsrt |
124
|
|
|
* @return Responsavel A própria instância da classe |
125
|
|
|
*/ |
126
|
40 |
|
public function setIDCsrt($idcsrt) |
127
|
|
|
{ |
128
|
40 |
|
if (trim($idcsrt) != '') { |
129
|
35 |
|
$idcsrt = intval($idcsrt); |
130
|
|
|
} |
131
|
40 |
|
$this->idcsrt = $idcsrt; |
132
|
40 |
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* O hashCSRT é o resultado da função hash (SHA-1 – Base64) do CSRT |
137
|
|
|
* fornecido pelo fisco mais a Chave de Acesso da NFe. |
138
|
|
|
* @param boolean $normalize informa se a hash_csrt deve estar no formato do XML |
139
|
|
|
* @return mixed hash_csrt do Responsavel |
140
|
|
|
*/ |
141
|
36 |
|
public function getHashCsrt($normalize = false) |
142
|
|
|
{ |
143
|
36 |
|
if (!$normalize) { |
144
|
36 |
|
return $this->hash_csrt; |
145
|
|
|
} |
146
|
34 |
|
return $this->hash_csrt; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Altera o valor da HashCsrt para o informado no parâmetro |
151
|
|
|
* @param mixed $hash_csrt novo valor para HashCsrt |
152
|
|
|
* @return Responsavel A própria instância da classe |
153
|
|
|
*/ |
154
|
40 |
|
public function setHashCsrt($hash_csrt) |
155
|
|
|
{ |
156
|
40 |
|
$this->hash_csrt = $hash_csrt; |
157
|
40 |
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Converte a instância da classe para um array de campos com valores |
162
|
|
|
* @return array Array contendo todos os campos e valores da instância |
163
|
|
|
*/ |
164
|
9 |
|
public function toArray($recursive = false) |
165
|
|
|
{ |
166
|
9 |
|
$responsavel = []; |
167
|
9 |
|
$responsavel['cnpj'] = $this->getCNPJ(); |
168
|
9 |
|
$responsavel['contato'] = $this->getContato(); |
169
|
9 |
|
$responsavel['email'] = $this->getEmail(); |
170
|
9 |
|
$responsavel['telefone'] = $this->getTelefone(); |
171
|
9 |
|
$responsavel['id_csrt'] = $this->getIDCsrt(); |
172
|
9 |
|
$responsavel['hash_csrt'] = $this->getHashCsrt(); |
173
|
9 |
|
return $responsavel; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Atribui os valores do array para a instância atual |
178
|
|
|
* @param mixed $responsavel Array ou instância de Responsavel, para copiar os valores |
179
|
|
|
* @return Responsavel A própria instância da classe |
180
|
|
|
*/ |
181
|
40 |
|
public function fromArray($responsavel = []) |
182
|
|
|
{ |
183
|
40 |
|
if ($responsavel instanceof Responsavel) { |
184
|
7 |
|
$responsavel = $responsavel->toArray(); |
185
|
40 |
|
} elseif (!is_array($responsavel)) { |
186
|
7 |
|
return $this; |
187
|
|
|
} |
188
|
40 |
|
if (!isset($responsavel['cnpj'])) { |
189
|
40 |
|
$this->setCNPJ(null); |
190
|
|
|
} else { |
191
|
7 |
|
$this->setCNPJ($responsavel['cnpj']); |
192
|
|
|
} |
193
|
40 |
|
if (!isset($responsavel['contato'])) { |
194
|
40 |
|
$this->setContato(null); |
195
|
|
|
} else { |
196
|
7 |
|
$this->setContato($responsavel['contato']); |
197
|
|
|
} |
198
|
40 |
|
if (!isset($responsavel['email'])) { |
199
|
40 |
|
$this->setEmail(null); |
200
|
|
|
} else { |
201
|
7 |
|
$this->setEmail($responsavel['email']); |
202
|
|
|
} |
203
|
40 |
|
if (!isset($responsavel['telefone'])) { |
204
|
40 |
|
$this->setTelefone(null); |
205
|
|
|
} else { |
206
|
7 |
|
$this->setTelefone($responsavel['telefone']); |
207
|
|
|
} |
208
|
40 |
|
if (!array_key_exists('id_csrt', $responsavel)) { |
209
|
40 |
|
$this->setIDCsrt(null); |
210
|
|
|
} else { |
211
|
7 |
|
$this->setIDCsrt($responsavel['id_csrt']); |
212
|
|
|
} |
213
|
40 |
|
if (!array_key_exists('hash_csrt', $responsavel)) { |
214
|
40 |
|
$this->setHashCsrt(null); |
215
|
|
|
} else { |
216
|
7 |
|
$this->setHashCsrt($responsavel['hash_csrt']); |
217
|
|
|
} |
218
|
40 |
|
return $this; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Cria um nó XML do responsavel de acordo com o leiaute da NFe |
223
|
|
|
* @param string $name Nome do nó que será criado |
224
|
|
|
* @return DOMElement Nó que contém todos os campos da classe |
225
|
|
|
*/ |
226
|
34 |
|
public function getNode($name = null) |
227
|
|
|
{ |
228
|
34 |
|
$dom = new \DOMDocument('1.0', 'UTF-8'); |
229
|
34 |
|
$element = $dom->createElement(is_null($name)?'infRespTec':$name); |
230
|
34 |
|
Util::appendNode($element, 'CNPJ', $this->getCNPJ(true)); |
231
|
34 |
|
Util::appendNode($element, 'xContato', $this->getContato(true)); |
232
|
34 |
|
Util::appendNode($element, 'email', $this->getEmail(true)); |
233
|
34 |
|
Util::appendNode($element, 'fone', $this->getTelefone(true)); |
234
|
34 |
|
if (!is_null($this->getIDCsrt())) { |
235
|
34 |
|
Util::appendNode($element, 'idCSRT', $this->getIDCsrt(true)); |
236
|
|
|
} |
237
|
34 |
|
if (!is_null($this->getHashCsrt())) { |
238
|
34 |
|
Util::appendNode($element, 'hashCSRT', $this->getHashCsrt(true)); |
239
|
|
|
} |
240
|
34 |
|
return $element; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Carrega as informações do nó e preenche a instância da classe |
245
|
|
|
* @param DOMElement $element Nó do xml com todos as tags dos campos |
246
|
|
|
* @param string $name Nome do nó que será carregado |
247
|
|
|
* @return DOMElement Instância do nó que foi carregado |
248
|
|
|
*/ |
249
|
30 |
|
public function loadNode($element, $name = null) |
250
|
|
|
{ |
251
|
30 |
|
$name = is_null($name)?'infRespTec':$name; |
252
|
30 |
|
if ($element->nodeName != $name) { |
253
|
2 |
|
$_fields = $element->getElementsByTagName($name); |
254
|
2 |
|
if ($_fields->length == 0) { |
255
|
1 |
|
throw new \Exception('Tag "'.$name.'" do Responsavel não encontrada', 404); |
256
|
|
|
} |
257
|
1 |
|
$element = $_fields->item(0); |
258
|
|
|
} |
259
|
29 |
|
$this->setCNPJ(Util::loadNode($element, 'CNPJ', 'Tag "CNPJ" não encontrada no Responsavel')); |
260
|
29 |
|
$this->setContato(Util::loadNode($element, 'xContato', 'Tag "xContato" não encontrada no Responsavel')); |
261
|
29 |
|
$this->setEmail(Util::loadNode($element, 'email', 'Tag "email" não encontrada no Responsavel')); |
262
|
29 |
|
$this->setTelefone(Util::loadNode($element, 'fone', 'Tag "fone" não encontrada no Responsavel')); |
263
|
29 |
|
$this->setIDCsrt(Util::loadNode($element, 'idCSRT')); |
264
|
29 |
|
$this->setHashCsrt(Util::loadNode($element, 'hashCSRT')); |
265
|
29 |
|
return $element; |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|