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
|
|
|
* Dados dos totais da NF-e e do produto |
35
|
|
|
*/ |
36
|
|
|
class Total implements Node |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Valor Total dos produtos e serviços |
41
|
|
|
*/ |
42
|
|
|
private $produtos; |
43
|
|
|
/** |
44
|
|
|
* Valor do Desconto |
45
|
|
|
*/ |
46
|
|
|
private $desconto; |
47
|
|
|
/** |
48
|
|
|
* informar o valor do Seguro, o Seguro deve ser rateado entre os itens de |
49
|
|
|
* produto |
50
|
|
|
*/ |
51
|
|
|
private $seguro; |
52
|
|
|
/** |
53
|
|
|
* informar o valor do Frete, o Frete deve ser rateado entre os itens de |
54
|
|
|
* produto. |
55
|
|
|
*/ |
56
|
|
|
private $frete; |
57
|
|
|
/** |
58
|
|
|
* informar o valor de outras despesas acessórias do item de produto ou |
59
|
|
|
* serviço |
60
|
|
|
*/ |
61
|
|
|
private $despesas; |
62
|
|
|
/** |
63
|
|
|
* Valor estimado total de impostos federais, estaduais e municipais |
64
|
|
|
*/ |
65
|
|
|
private $tributos; |
66
|
|
|
/** |
67
|
|
|
* Informações complementares de interesse do Contribuinte |
68
|
|
|
*/ |
69
|
|
|
private $complemento; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Constroi uma instância de Total vazia |
73
|
|
|
* @param array $total Array contendo dados do Total |
74
|
|
|
*/ |
75
|
29 |
|
public function __construct($total = array()) |
76
|
|
|
{ |
77
|
29 |
|
$this->fromArray($total); |
78
|
29 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Valor Total dos produtos e serviços |
82
|
|
|
* @param boolean $normalize informa se o produtos deve estar no formato do XML |
83
|
|
|
* @return mixed produtos do Total |
84
|
|
|
*/ |
85
|
30 |
|
public function getProdutos($normalize = false) |
86
|
|
|
{ |
87
|
30 |
|
if (!$normalize) { |
88
|
30 |
|
return $this->produtos; |
89
|
|
|
} |
90
|
29 |
|
return Util::toCurrency($this->produtos); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Altera o valor do Produtos para o informado no parâmetro |
95
|
|
|
* @param mixed $produtos novo valor para Produtos |
96
|
|
|
* @return Total A própria instância da classe |
97
|
|
|
*/ |
98
|
33 |
|
public function setProdutos($produtos) |
99
|
|
|
{ |
100
|
33 |
|
if (trim($produtos) != '') { |
101
|
30 |
|
$produtos = floatval($produtos); |
102
|
|
|
} |
103
|
33 |
|
$this->produtos = $produtos; |
104
|
33 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Valor do Desconto |
109
|
|
|
* @param boolean $normalize informa se o desconto deve estar no formato do XML |
110
|
|
|
* @return mixed desconto do Total |
111
|
|
|
*/ |
112
|
30 |
|
public function getDesconto($normalize = false) |
113
|
|
|
{ |
114
|
30 |
|
if (!$normalize) { |
115
|
30 |
|
return $this->desconto; |
116
|
|
|
} |
117
|
29 |
|
return Util::toCurrency($this->desconto); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Altera o valor do Desconto para o informado no parâmetro |
122
|
|
|
* @param mixed $desconto novo valor para Desconto |
123
|
|
|
* @return Total A própria instância da classe |
124
|
|
|
*/ |
125
|
33 |
|
public function setDesconto($desconto) |
126
|
|
|
{ |
127
|
33 |
|
if (trim($desconto) != '') { |
128
|
30 |
|
$desconto = floatval($desconto); |
129
|
|
|
} |
130
|
33 |
|
$this->desconto = $desconto; |
131
|
33 |
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* informar o valor do Seguro, o Seguro deve ser rateado entre os itens de |
136
|
|
|
* produto |
137
|
|
|
* @param boolean $normalize informa se o seguro deve estar no formato do XML |
138
|
|
|
* @return mixed seguro do Total |
139
|
|
|
*/ |
140
|
29 |
|
public function getSeguro($normalize = false) |
141
|
|
|
{ |
142
|
29 |
|
if (!$normalize) { |
143
|
29 |
|
return $this->seguro; |
144
|
|
|
} |
145
|
2 |
|
return Util::toCurrency($this->seguro); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Altera o valor do Seguro para o informado no parâmetro |
150
|
|
|
* @param mixed $seguro novo valor para Seguro |
151
|
|
|
* @return Total A própria instância da classe |
152
|
|
|
*/ |
153
|
33 |
|
public function setSeguro($seguro) |
154
|
|
|
{ |
155
|
33 |
|
if (trim($seguro) != '') { |
156
|
30 |
|
$seguro = floatval($seguro); |
157
|
|
|
} |
158
|
33 |
|
$this->seguro = $seguro; |
159
|
33 |
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* informar o valor do Frete, o Frete deve ser rateado entre os itens de |
164
|
|
|
* produto. |
165
|
|
|
* @param boolean $normalize informa se o frete deve estar no formato do XML |
166
|
|
|
* @return mixed frete do Total |
167
|
|
|
*/ |
168
|
29 |
|
public function getFrete($normalize = false) |
169
|
|
|
{ |
170
|
29 |
|
if (!$normalize) { |
171
|
29 |
|
return $this->frete; |
172
|
|
|
} |
173
|
2 |
|
return Util::toCurrency($this->frete); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Altera o valor do Frete para o informado no parâmetro |
178
|
|
|
* @param mixed $frete novo valor para Frete |
179
|
|
|
* @return Total A própria instância da classe |
180
|
|
|
*/ |
181
|
33 |
|
public function setFrete($frete) |
182
|
|
|
{ |
183
|
33 |
|
if (trim($frete) != '') { |
184
|
30 |
|
$frete = floatval($frete); |
185
|
|
|
} |
186
|
33 |
|
$this->frete = $frete; |
187
|
33 |
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* informar o valor de outras despesas acessórias do item de produto ou |
192
|
|
|
* serviço |
193
|
|
|
* @param boolean $normalize informa se a despesas deve estar no formato do XML |
194
|
|
|
* @return mixed despesas do Total |
195
|
|
|
*/ |
196
|
29 |
|
public function getDespesas($normalize = false) |
197
|
|
|
{ |
198
|
29 |
|
if (!$normalize) { |
199
|
29 |
|
return $this->despesas; |
200
|
|
|
} |
201
|
2 |
|
return Util::toCurrency($this->despesas); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Altera o valor da Despesas para o informado no parâmetro |
206
|
|
|
* @param mixed $despesas novo valor para Despesas |
207
|
|
|
* @return Total A própria instância da classe |
208
|
|
|
*/ |
209
|
33 |
|
public function setDespesas($despesas) |
210
|
|
|
{ |
211
|
33 |
|
if (trim($despesas) != '') { |
212
|
30 |
|
$despesas = floatval($despesas); |
213
|
|
|
} |
214
|
33 |
|
$this->despesas = $despesas; |
215
|
33 |
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Valor estimado total de impostos federais, estaduais e municipais |
220
|
|
|
* @param boolean $normalize informa se o tributos deve estar no formato do XML |
221
|
|
|
* @return mixed tributos do Total |
222
|
|
|
*/ |
223
|
4 |
|
public function getTributos($normalize = false) |
224
|
|
|
{ |
225
|
4 |
|
if (!$normalize) { |
226
|
4 |
|
return $this->tributos; |
227
|
|
|
} |
228
|
|
|
return Util::toCurrency($this->tributos); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Altera o valor do Tributos para o informado no parâmetro |
233
|
|
|
* @param mixed $tributos novo valor para Tributos |
234
|
|
|
* @return Total A própria instância da classe |
235
|
|
|
*/ |
236
|
33 |
|
public function setTributos($tributos) |
237
|
|
|
{ |
238
|
33 |
|
if (trim($tributos) != '') { |
239
|
30 |
|
$tributos = floatval($tributos); |
240
|
|
|
} |
241
|
33 |
|
$this->tributos = $tributos; |
242
|
33 |
|
return $this; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Informações complementares de interesse do Contribuinte |
247
|
|
|
* @param boolean $normalize informa se o complemento deve estar no formato do XML |
248
|
|
|
* @return mixed complemento do Total |
249
|
|
|
*/ |
250
|
4 |
|
public function getComplemento($normalize = false) |
251
|
|
|
{ |
252
|
4 |
|
if (!$normalize) { |
253
|
4 |
|
return $this->complemento; |
254
|
|
|
} |
255
|
|
|
return $this->complemento; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Altera o valor do Complemento para o informado no parâmetro |
260
|
|
|
* @param mixed $complemento novo valor para Complemento |
261
|
|
|
* @return Total A própria instância da classe |
262
|
|
|
*/ |
263
|
33 |
|
public function setComplemento($complemento) |
264
|
|
|
{ |
265
|
33 |
|
$this->complemento = $complemento; |
266
|
33 |
|
return $this; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Converte a instância da classe para um array de campos com valores |
271
|
|
|
* @return array Array contendo todos os campos e valores da instância |
272
|
|
|
*/ |
273
|
4 |
|
public function toArray($recursive = false) |
274
|
|
|
{ |
275
|
4 |
|
$total = array(); |
276
|
4 |
|
$total['produtos'] = $this->getProdutos(); |
277
|
4 |
|
$total['desconto'] = $this->getDesconto(); |
278
|
4 |
|
$total['seguro'] = $this->getSeguro(); |
279
|
4 |
|
$total['frete'] = $this->getFrete(); |
280
|
4 |
|
$total['despesas'] = $this->getDespesas(); |
281
|
4 |
|
$total['tributos'] = $this->getTributos(); |
282
|
4 |
|
$total['complemento'] = $this->getComplemento(); |
283
|
4 |
|
return $total; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Atribui os valores do array para a instância atual |
288
|
|
|
* @param mixed $total Array ou instância de Total, para copiar os valores |
289
|
|
|
* @return Total A própria instância da classe |
290
|
|
|
*/ |
291
|
33 |
|
public function fromArray($total = array()) |
|
|
|
|
292
|
|
|
{ |
293
|
33 |
|
if ($total instanceof Total) { |
294
|
|
|
$total = $total->toArray(); |
295
|
33 |
|
} elseif (!is_array($total)) { |
296
|
|
|
return $this; |
297
|
|
|
} |
298
|
33 |
|
if (!isset($total['produtos'])) { |
299
|
33 |
|
$this->setProdutos(null); |
300
|
|
|
} else { |
301
|
27 |
|
$this->setProdutos($total['produtos']); |
302
|
|
|
} |
303
|
33 |
|
if (!array_key_exists('desconto', $total)) { |
304
|
33 |
|
$this->setDesconto(null); |
305
|
|
|
} else { |
306
|
28 |
|
$this->setDesconto($total['desconto']); |
307
|
|
|
} |
308
|
33 |
|
if (!array_key_exists('seguro', $total)) { |
309
|
33 |
|
$this->setSeguro(null); |
310
|
|
|
} else { |
311
|
28 |
|
$this->setSeguro($total['seguro']); |
312
|
|
|
} |
313
|
33 |
|
if (!array_key_exists('frete', $total)) { |
314
|
33 |
|
$this->setFrete(null); |
315
|
|
|
} else { |
316
|
28 |
|
$this->setFrete($total['frete']); |
317
|
|
|
} |
318
|
33 |
|
if (!array_key_exists('despesas', $total)) { |
319
|
33 |
|
$this->setDespesas(null); |
320
|
|
|
} else { |
321
|
28 |
|
$this->setDespesas($total['despesas']); |
322
|
|
|
} |
323
|
33 |
|
if (!array_key_exists('tributos', $total)) { |
324
|
33 |
|
$this->setTributos(null); |
325
|
|
|
} else { |
326
|
28 |
|
$this->setTributos($total['tributos']); |
327
|
|
|
} |
328
|
33 |
|
if (!array_key_exists('complemento', $total)) { |
329
|
33 |
|
$this->setComplemento(null); |
330
|
|
|
} else { |
331
|
3 |
|
$this->setComplemento($total['complemento']); |
332
|
|
|
} |
333
|
33 |
|
return $this; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Cria um nó XML do total de acordo com o leiaute da NFe |
338
|
|
|
* @param string $name Nome do nó que será criado |
339
|
|
|
* @return DOMElement Nó que contém todos os campos da classe |
340
|
|
|
*/ |
341
|
|
|
public function getNode($name = null) |
|
|
|
|
342
|
|
|
{ |
343
|
|
|
$dom = new \DOMDocument('1.0', 'UTF-8'); |
344
|
|
|
$element = $dom->createElement(is_null($name)?'prod':$name); |
345
|
|
|
Util::appendNode($element, 'vProd', $this->getProdutos(true)); |
346
|
|
|
if (!is_null($this->getDesconto())) { |
347
|
|
|
Util::appendNode($element, 'vDesc', $this->getDesconto(true)); |
348
|
|
|
} |
349
|
|
|
if (!is_null($this->getSeguro())) { |
350
|
|
|
Util::appendNode($element, 'vSeg', $this->getSeguro(true)); |
351
|
|
|
} |
352
|
|
|
if (!is_null($this->getFrete())) { |
353
|
|
|
Util::appendNode($element, 'vFrete', $this->getFrete(true)); |
354
|
|
|
} |
355
|
|
|
if (!is_null($this->getDespesas())) { |
356
|
|
|
Util::appendNode($element, 'vOutro', $this->getDespesas(true)); |
357
|
|
|
} |
358
|
|
|
if (!is_null($this->getTributos())) { |
359
|
|
|
Util::appendNode($element, 'vTotTrib', $this->getTributos(true)); |
360
|
|
|
} |
361
|
|
|
if (!is_null($this->getComplemento())) { |
362
|
|
|
Util::appendNode($element, 'infCpl', $this->getComplemento(true)); |
363
|
|
|
} |
364
|
|
|
return $element; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Carrega as informações do nó e preenche a instância da classe |
369
|
|
|
* @param DOMElement $element Nó do xml com todos as tags dos campos |
370
|
|
|
* @param string $name Nome do nó que será carregado |
371
|
|
|
* @return DOMElement Instância do nó que foi carregado |
372
|
|
|
*/ |
373
|
27 |
|
public function loadNode($element, $name = null) |
374
|
|
|
{ |
375
|
27 |
|
$name = is_null($name)?'prod':$name; |
376
|
27 |
|
if ($element->nodeName != $name) { |
377
|
|
|
$_fields = $element->getElementsByTagName($name); |
378
|
|
|
if ($_fields->length == 0) { |
379
|
|
|
throw new \Exception('Tag "'.$name.'" do Total ou Produto não encontrada', 404); |
380
|
|
|
} |
381
|
|
|
$element = $_fields->item(0); |
382
|
|
|
} |
383
|
27 |
|
$this->setProdutos( |
384
|
27 |
|
Util::loadNode( |
385
|
27 |
|
$element, |
386
|
27 |
|
'vProd', |
387
|
27 |
|
'Tag "vProd" não encontrada no Total ou Produto' |
388
|
|
|
) |
389
|
|
|
); |
390
|
27 |
|
$this->setDesconto(Util::loadNode($element, 'vDesc')); |
391
|
27 |
|
$this->setSeguro(Util::loadNode($element, 'vSeg')); |
392
|
27 |
|
$this->setFrete(Util::loadNode($element, 'vFrete')); |
393
|
27 |
|
$this->setDespesas(Util::loadNode($element, 'vOutro')); |
394
|
27 |
|
$this->setTributos(Util::loadNode($element, 'vTotTrib')); |
395
|
27 |
|
$this->setComplemento(Util::loadNode($element, 'infCpl')); |
396
|
27 |
|
return $element; |
397
|
|
|
} |
398
|
|
|
} |
399
|
|
|
|
The class complexity is the sum of the complexity of all methods. A very high value is usually an indication that your class does not follow the single reponsibility principle and does more than one job.
Some resources for further reading:
You can also find more detailed suggestions for refactoring in the “Code” section of your repository.