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\Node; |
31
|
|
|
use NFe\Common\Util; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Classe base para preenchimento de informações de pessoas físicas e |
35
|
|
|
* empresas |
36
|
|
|
*/ |
37
|
|
|
abstract class Pessoa implements Node |
38
|
|
|
{ |
39
|
|
|
|
40
|
|
|
private $razao_social; |
41
|
|
|
private $cnpj; |
42
|
|
|
private $ie; |
|
|
|
|
43
|
|
|
private $im; |
|
|
|
|
44
|
|
|
private $endereco; |
45
|
|
|
private $telefone; |
46
|
|
|
|
47
|
89 |
|
public function __construct($pessoa = array()) |
48
|
|
|
{ |
49
|
89 |
|
$this->fromArray($pessoa); |
50
|
89 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Número identificador da pessoa |
54
|
|
|
*/ |
55
|
|
|
public function getID($normalize = false) |
56
|
|
|
{ |
57
|
|
|
return $this->getCNPJ($normalize); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Razão Social ou Nome |
62
|
|
|
*/ |
63
|
43 |
|
public function getRazaoSocial($normalize = false) |
64
|
|
|
{ |
65
|
43 |
|
if (!$normalize) { |
66
|
33 |
|
return $this->razao_social; |
67
|
|
|
} |
68
|
41 |
|
return $this->razao_social; |
69
|
|
|
} |
70
|
|
|
|
71
|
89 |
|
public function setRazaoSocial($razao_social) |
72
|
|
|
{ |
73
|
89 |
|
$this->razao_social = $razao_social; |
74
|
89 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Identificador da pessoa na receita |
79
|
|
|
*/ |
80
|
48 |
|
public function getCNPJ($normalize = false) |
81
|
|
|
{ |
82
|
48 |
|
if (!$normalize) { |
83
|
47 |
|
return $this->cnpj; |
84
|
|
|
} |
85
|
39 |
|
return $this->cnpj; |
86
|
|
|
} |
87
|
|
|
|
88
|
89 |
|
public function setCNPJ($cnpj) |
89
|
|
|
{ |
90
|
89 |
|
$this->cnpj = $cnpj; |
91
|
89 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Inscrição Estadual |
96
|
|
|
*/ |
97
|
41 |
|
public function getIE($normalize = false) |
98
|
|
|
{ |
99
|
41 |
|
if (!$normalize) { |
100
|
9 |
|
return $this->ie; |
101
|
|
|
} |
102
|
39 |
|
return $this->ie; |
103
|
|
|
} |
104
|
|
|
|
105
|
89 |
|
public function setIE($ie) |
|
|
|
|
106
|
|
|
{ |
107
|
89 |
|
$this->ie = $ie; |
108
|
89 |
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Inscrição Municipal |
113
|
|
|
*/ |
114
|
31 |
|
public function getIM($normalize = false) |
115
|
|
|
{ |
116
|
31 |
|
if (!$normalize) { |
117
|
31 |
|
return $this->im; |
118
|
|
|
} |
119
|
27 |
|
return $this->im; |
120
|
|
|
} |
121
|
|
|
|
122
|
89 |
|
public function setIM($im) |
|
|
|
|
123
|
|
|
{ |
124
|
89 |
|
$this->im = $im; |
125
|
89 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Dados do endereço |
130
|
|
|
*/ |
131
|
88 |
|
public function getEndereco() |
132
|
|
|
{ |
133
|
88 |
|
return $this->endereco; |
134
|
|
|
} |
135
|
|
|
|
136
|
89 |
|
public function setEndereco($endereco) |
137
|
|
|
{ |
138
|
89 |
|
$this->endereco = $endereco; |
139
|
89 |
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
33 |
|
public function getTelefone($normalize = false) |
143
|
|
|
{ |
144
|
33 |
|
if (!$normalize) { |
145
|
33 |
|
return $this->telefone; |
146
|
|
|
} |
147
|
29 |
|
return $this->telefone; |
148
|
|
|
} |
149
|
|
|
|
150
|
89 |
|
public function setTelefone($telefone) |
151
|
|
|
{ |
152
|
89 |
|
$this->telefone = $telefone; |
153
|
89 |
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
8 |
|
public function toArray($recursive = false) |
157
|
|
|
{ |
158
|
8 |
|
$pessoa = array(); |
159
|
8 |
|
$pessoa['razao_social'] = $this->getRazaoSocial(); |
160
|
8 |
|
$pessoa['cnpj'] = $this->getCNPJ(); |
161
|
8 |
|
$pessoa['ie'] = $this->getIE(); |
162
|
8 |
|
$pessoa['im'] = $this->getIM(); |
163
|
8 |
|
if (!is_null($this->getEndereco()) && $recursive) { |
164
|
1 |
|
$pessoa['endereco'] = $this->getEndereco()->toArray($recursive); |
165
|
1 |
|
} else { |
166
|
7 |
|
$pessoa['endereco'] = $this->getEndereco(); |
167
|
|
|
} |
168
|
8 |
|
$pessoa['telefone'] = $this->getTelefone(); |
169
|
8 |
|
return $pessoa; |
170
|
|
|
} |
171
|
|
|
|
172
|
89 |
|
public function fromArray($pessoa = array()) |
173
|
|
|
{ |
174
|
89 |
|
if ($pessoa instanceof Pessoa) { |
175
|
|
|
$pessoa = $pessoa->toArray(); |
176
|
89 |
|
} elseif (!is_array($pessoa)) { |
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
89 |
|
if (isset($pessoa['razao_social'])) { |
180
|
6 |
|
$this->setRazaoSocial($pessoa['razao_social']); |
181
|
6 |
|
} else { |
182
|
89 |
|
$this->setRazaoSocial(null); |
183
|
|
|
} |
184
|
89 |
|
if (isset($pessoa['cnpj'])) { |
185
|
5 |
|
$this->setCNPJ($pessoa['cnpj']); |
186
|
5 |
|
} else { |
187
|
89 |
|
$this->setCNPJ(null); |
188
|
|
|
} |
189
|
89 |
|
if (isset($pessoa['ie'])) { |
190
|
5 |
|
$this->setIE($pessoa['ie']); |
191
|
5 |
|
} else { |
192
|
89 |
|
$this->setIE(null); |
193
|
|
|
} |
194
|
89 |
|
if (isset($pessoa['im'])) { |
195
|
3 |
|
$this->setIM($pessoa['im']); |
196
|
3 |
|
} else { |
197
|
89 |
|
$this->setIM(null); |
198
|
|
|
} |
199
|
89 |
|
if (!array_key_exists('endereco', $pessoa)) { |
200
|
89 |
|
$this->setEndereco(new Endereco()); |
201
|
89 |
|
} else { |
202
|
7 |
|
$this->setEndereco($pessoa['endereco']); |
203
|
|
|
} |
204
|
89 |
|
if (isset($pessoa['telefone'])) { |
205
|
4 |
|
$this->setTelefone($pessoa['telefone']); |
206
|
4 |
|
} else { |
207
|
89 |
|
$this->setTelefone(null); |
208
|
|
|
} |
209
|
89 |
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
27 |
|
public function loadNode($element, $name = null) |
|
|
|
|
213
|
|
|
{ |
214
|
27 |
|
$name = is_null($name)?'emit':$name; |
215
|
27 |
|
if ($element->nodeName != $name) { |
216
|
|
|
$_fields = $element->getElementsByTagName($name); |
217
|
|
|
if ($_fields->length == 0) { |
218
|
|
|
throw new \Exception('Tag "'.$name.'" não encontrada', 404); |
219
|
|
|
} |
220
|
|
|
$element = $_fields->item(0); |
221
|
|
|
} |
222
|
27 |
|
$razao_social = Util::loadNode($element, 'xNome'); |
223
|
27 |
|
if (is_null($razao_social) && $this instanceof Emitente) { |
224
|
|
|
throw new \Exception('Tag "xNome" do campo "RazaoSocial" não encontrada', 404); |
225
|
|
|
} |
226
|
27 |
|
$this->setRazaoSocial($razao_social); |
227
|
27 |
|
$cnpj = Util::loadNode($element, 'CNPJ'); |
228
|
27 |
|
if (is_null($cnpj) && $this instanceof Emitente) { |
229
|
|
|
throw new \Exception('Tag "CNPJ" do campo "CNPJ" não encontrada', 404); |
230
|
|
|
} |
231
|
27 |
|
$this->setCNPJ($cnpj); |
232
|
27 |
|
$ie = Util::loadNode($element, 'IE'); |
|
|
|
|
233
|
27 |
|
if (is_null($ie) && $this instanceof Emitente) { |
234
|
|
|
throw new \Exception('Tag "IE" do campo "IE" não encontrada', 404); |
235
|
|
|
} |
236
|
27 |
|
$this->setIE($ie); |
237
|
27 |
|
$this->setIM(Util::loadNode($element, 'IM')); |
238
|
27 |
|
if ($this instanceof Emitente) { |
239
|
24 |
|
$tag_ender = 'enderEmit'; |
240
|
24 |
|
} else { |
241
|
26 |
|
$tag_ender = 'enderDest'; |
242
|
|
|
} |
243
|
27 |
|
$endereco = null; |
244
|
27 |
|
$_fields = $element->getElementsByTagName($tag_ender); |
245
|
27 |
|
if ($_fields->length > 0) { |
246
|
26 |
|
$endereco = new Endereco(); |
247
|
26 |
|
$endereco->loadNode($_fields->item(0), $tag_ender); |
248
|
27 |
|
} elseif ($this instanceof Emitente) { |
249
|
|
|
throw new \Exception('Tag "'.$tag_ender.'" do objeto "Endereco" não encontrada', 404); |
250
|
|
|
} |
251
|
27 |
|
$this->setEndereco($endereco); |
252
|
27 |
|
$telefone = null; |
253
|
27 |
|
if ($_fields->length > 0) { |
254
|
26 |
|
$telefone = Util::loadNode($_fields->item(0), 'fone'); |
255
|
26 |
|
} |
256
|
27 |
|
$this->setTelefone($telefone); |
257
|
27 |
|
return $element; |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.