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
|
36 |
|
public function __construct($pessoa = array()) |
48
|
|
|
{ |
49
|
36 |
|
$this->fromArray($pessoa); |
50
|
36 |
|
} |
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
|
23 |
|
public function getRazaoSocial($normalize = false) |
64
|
|
|
{ |
65
|
23 |
|
if (!$normalize) { |
66
|
13 |
|
return $this->razao_social; |
67
|
|
|
} |
68
|
21 |
|
return $this->razao_social; |
69
|
|
|
} |
70
|
|
|
|
71
|
36 |
|
public function setRazaoSocial($razao_social) |
72
|
|
|
{ |
73
|
36 |
|
$this->razao_social = $razao_social; |
74
|
36 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Identificador da pessoa na receita |
79
|
|
|
*/ |
80
|
25 |
|
public function getCNPJ($normalize = false) |
81
|
|
|
{ |
82
|
25 |
|
if (!$normalize) { |
83
|
24 |
|
return $this->cnpj; |
84
|
|
|
} |
85
|
19 |
|
return $this->cnpj; |
86
|
|
|
} |
87
|
|
|
|
88
|
36 |
|
public function setCNPJ($cnpj) |
89
|
|
|
{ |
90
|
36 |
|
$this->cnpj = $cnpj; |
91
|
36 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Inscrição Estadual |
96
|
|
|
*/ |
97
|
21 |
|
public function getIE($normalize = false) |
98
|
|
|
{ |
99
|
21 |
|
if (!$normalize) { |
100
|
7 |
|
return $this->ie; |
101
|
|
|
} |
102
|
19 |
|
return $this->ie; |
103
|
|
|
} |
104
|
|
|
|
105
|
36 |
|
public function setIE($ie) |
|
|
|
|
106
|
|
|
{ |
107
|
36 |
|
$this->ie = $ie; |
108
|
36 |
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Inscrição Municipal |
113
|
|
|
*/ |
114
|
11 |
|
public function getIM($normalize = false) |
115
|
|
|
{ |
116
|
11 |
|
if (!$normalize) { |
117
|
11 |
|
return $this->im; |
118
|
|
|
} |
119
|
7 |
|
return $this->im; |
120
|
|
|
} |
121
|
|
|
|
122
|
36 |
|
public function setIM($im) |
|
|
|
|
123
|
|
|
{ |
124
|
36 |
|
$this->im = $im; |
125
|
36 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Dados do endereço |
130
|
|
|
*/ |
131
|
33 |
|
public function getEndereco() |
132
|
|
|
{ |
133
|
33 |
|
return $this->endereco; |
134
|
|
|
} |
135
|
|
|
|
136
|
36 |
|
public function setEndereco($endereco) |
137
|
|
|
{ |
138
|
36 |
|
$this->endereco = $endereco; |
139
|
36 |
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
13 |
|
public function getTelefone($normalize = false) |
143
|
|
|
{ |
144
|
13 |
|
if (!$normalize) { |
145
|
13 |
|
return $this->telefone; |
146
|
|
|
} |
147
|
9 |
|
return $this->telefone; |
148
|
|
|
} |
149
|
|
|
|
150
|
36 |
|
public function setTelefone($telefone) |
151
|
|
|
{ |
152
|
36 |
|
$this->telefone = $telefone; |
153
|
36 |
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
7 |
|
public function toArray() |
157
|
|
|
{ |
158
|
7 |
|
$pessoa = array(); |
159
|
7 |
|
$pessoa['razao_social'] = $this->getRazaoSocial(); |
160
|
7 |
|
$pessoa['cnpj'] = $this->getCNPJ(); |
161
|
7 |
|
$pessoa['ie'] = $this->getIE(); |
162
|
7 |
|
$pessoa['im'] = $this->getIM(); |
163
|
7 |
|
$pessoa['endereco'] = $this->getEndereco(); |
164
|
7 |
|
$pessoa['telefone'] = $this->getTelefone(); |
165
|
7 |
|
return $pessoa; |
166
|
|
|
} |
167
|
|
|
|
168
|
36 |
|
public function fromArray($pessoa = array()) |
169
|
|
|
{ |
170
|
36 |
|
if ($pessoa instanceof Pessoa) { |
171
|
|
|
$pessoa = $pessoa->toArray(); |
172
|
36 |
|
} elseif (!is_array($pessoa)) { |
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
36 |
|
if (isset($pessoa['razao_social'])) { |
176
|
6 |
|
$this->setRazaoSocial($pessoa['razao_social']); |
177
|
6 |
|
} else { |
178
|
36 |
|
$this->setRazaoSocial(null); |
179
|
|
|
} |
180
|
36 |
|
if (isset($pessoa['cnpj'])) { |
181
|
5 |
|
$this->setCNPJ($pessoa['cnpj']); |
182
|
5 |
|
} else { |
183
|
36 |
|
$this->setCNPJ(null); |
184
|
|
|
} |
185
|
36 |
|
if (isset($pessoa['ie'])) { |
186
|
5 |
|
$this->setIE($pessoa['ie']); |
187
|
5 |
|
} else { |
188
|
36 |
|
$this->setIE(null); |
189
|
|
|
} |
190
|
36 |
|
if (isset($pessoa['im'])) { |
191
|
3 |
|
$this->setIM($pessoa['im']); |
192
|
3 |
|
} else { |
193
|
36 |
|
$this->setIM(null); |
194
|
|
|
} |
195
|
36 |
|
if (!array_key_exists('endereco', $pessoa)) { |
196
|
36 |
|
$this->setEndereco(new Endereco()); |
197
|
36 |
|
} else { |
198
|
7 |
|
$this->setEndereco($pessoa['endereco']); |
199
|
|
|
} |
200
|
36 |
|
if (isset($pessoa['telefone'])) { |
201
|
4 |
|
$this->setTelefone($pessoa['telefone']); |
202
|
4 |
|
} else { |
203
|
36 |
|
$this->setTelefone(null); |
204
|
|
|
} |
205
|
36 |
|
return $this; |
206
|
|
|
} |
207
|
|
|
|
208
|
7 |
|
public function loadNode($element, $name = null) |
|
|
|
|
209
|
|
|
{ |
210
|
7 |
|
$name = is_null($name)?'emit':$name; |
211
|
7 |
|
if ($element->tagName != $name) { |
212
|
|
|
$_fields = $element->getElementsByTagName($name); |
213
|
|
|
if ($_fields->length == 0) { |
214
|
|
|
throw new \Exception('Tag "'.$name.'" não encontrada', 404); |
215
|
|
|
} |
216
|
|
|
$element = $_fields->item(0); |
217
|
|
|
} |
218
|
7 |
|
$razao_social = null; |
219
|
7 |
|
$_fields = $element->getElementsByTagName('xNome'); |
220
|
7 |
|
if ($_fields->length > 0) { |
221
|
6 |
|
$razao_social = $_fields->item(0)->nodeValue; |
222
|
7 |
|
} elseif ($this instanceof Emitente) { |
223
|
|
|
throw new \Exception('Tag "xNome" do campo "RazaoSocial" não encontrada', 404); |
224
|
|
|
} |
225
|
7 |
|
$this->setRazaoSocial($razao_social); |
226
|
7 |
|
$cnpj = null; |
227
|
7 |
|
$_fields = $element->getElementsByTagName('CNPJ'); |
228
|
7 |
|
if ($_fields->length > 0) { |
229
|
5 |
|
$cnpj = $_fields->item(0)->nodeValue; |
230
|
7 |
|
} elseif ($this instanceof Emitente) { |
231
|
|
|
throw new \Exception('Tag "CNPJ" do campo "CNPJ" não encontrada', 404); |
232
|
|
|
} |
233
|
7 |
|
$this->setCNPJ($cnpj); |
234
|
7 |
|
$ie = null; |
|
|
|
|
235
|
7 |
|
$_fields = $element->getElementsByTagName('IE'); |
236
|
7 |
|
if ($_fields->length > 0) { |
237
|
5 |
|
$ie = $_fields->item(0)->nodeValue; |
238
|
7 |
|
} elseif ($this instanceof Emitente) { |
239
|
|
|
throw new \Exception('Tag "IE" do campo "IE" não encontrada', 404); |
240
|
|
|
} |
241
|
7 |
|
$this->setIE($ie); |
242
|
7 |
|
$this->setIM(Util::loadNode($element, 'IM')); |
243
|
7 |
|
if ($this instanceof Emitente) { |
244
|
4 |
|
$tag_ender = 'enderEmit'; |
245
|
4 |
|
} else { |
246
|
6 |
|
$tag_ender = 'enderDest'; |
247
|
|
|
} |
248
|
7 |
|
$endereco = null; |
249
|
7 |
|
$_fields = $element->getElementsByTagName($tag_ender); |
250
|
7 |
|
if ($_fields->length > 0) { |
251
|
6 |
|
$endereco = new Endereco(); |
252
|
6 |
|
$endereco->loadNode($_fields->item(0), $tag_ender); |
253
|
7 |
|
} elseif ($this instanceof Emitente) { |
254
|
|
|
throw new \Exception('Tag "'.$tag_ender.'" do objeto "Endereco" não encontrada', 404); |
255
|
|
|
} |
256
|
7 |
|
$this->setEndereco($endereco); |
257
|
7 |
|
$telefone = null; |
258
|
7 |
|
if ($_fields->length > 0) { |
259
|
6 |
|
$ender = $_fields->item(0); |
260
|
6 |
|
$_fields = $ender->getElementsByTagName('fone'); |
261
|
6 |
|
} |
262
|
7 |
|
if ($_fields->length > 0) { |
263
|
5 |
|
$telefone = $_fields->item(0)->nodeValue; |
264
|
5 |
|
} |
265
|
7 |
|
$this->setTelefone($telefone); |
266
|
7 |
|
return $element; |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
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.