Complex classes like Nota often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Nota, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | abstract class Nota implements Node |
||
| 47 | { |
||
| 48 | |||
| 49 | const VERSAO = '3.10'; |
||
| 50 | const APP_VERSAO = '1.0'; |
||
| 51 | const PORTAL = 'http://www.portalfiscal.inf.br/nfe'; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Código do modelo do Documento Fiscal. 55 = NF-e; 65 = NFC-e. |
||
| 55 | */ |
||
| 56 | const MODELO_NFE = 'nfe'; |
||
| 57 | const MODELO_NFCE = 'nfce'; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Tipo do Documento Fiscal (0 - entrada; 1 - saída) |
||
| 61 | */ |
||
| 62 | const TIPO_ENTRADA = 'entrada'; |
||
| 63 | const TIPO_SAIDA = 'saida'; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Identificador de Local de destino da operação |
||
| 67 | * (1-Interna;2-Interestadual;3-Exterior) |
||
| 68 | */ |
||
| 69 | const DESTINO_INTERNA = 'interna'; |
||
| 70 | const DESTINO_INTERESTADUAL = 'interestadual'; |
||
| 71 | const DESTINO_EXTERIOR = 'exterior'; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Indicador da forma de pagamento: 0 – pagamento à vista; 1 – pagamento à |
||
| 75 | * prazo; 2 – outros. |
||
| 76 | */ |
||
| 77 | const INDICADOR_AVISTA = 'avista'; |
||
| 78 | const INDICADOR_APRAZO = 'aprazo'; |
||
| 79 | const INDICADOR_OUTROS = 'outros'; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Formato de impressão do DANFE (0-sem DANFE;1-DANFe Retrato; 2-DANFe |
||
| 83 | * Paisagem;3-DANFe Simplificado;4-DANFe NFC-e;5-DANFe NFC-e em mensagem |
||
| 84 | * eletrônica) |
||
| 85 | */ |
||
| 86 | const FORMATO_NENHUMA = 'nenhuma'; |
||
| 87 | const FORMATO_RETRATO = 'retrato'; |
||
| 88 | const FORMATO_PAISAGEM = 'paisagem'; |
||
| 89 | const FORMATO_SIMPLIFICADO = 'simplificado'; |
||
| 90 | const FORMATO_CONSUMIDOR = 'consumidor'; |
||
| 91 | const FORMATO_MENSAGEM = 'mensagem'; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Forma de emissão da NF-e |
||
| 95 | */ |
||
| 96 | const EMISSAO_NORMAL = 'normal'; |
||
| 97 | const EMISSAO_CONTINGENCIA = 'contingencia'; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Identificação do Ambiente: 1 - Produção, 2 - Homologação |
||
| 101 | */ |
||
| 102 | const AMBIENTE_PRODUCAO = 'producao'; |
||
| 103 | const AMBIENTE_HOMOLOGACAO = 'homologacao'; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Finalidade da emissão da NF-e: 1 - NFe normal, 2 - NFe complementar, 3 - |
||
| 107 | * NFe de ajuste, 4 - Devolução/Retorno |
||
| 108 | */ |
||
| 109 | const FINALIDADE_NORMAL = 'normal'; |
||
| 110 | const FINALIDADE_COMPLEMENTAR = 'complementar'; |
||
| 111 | const FINALIDADE_AJUSTE = 'ajuste'; |
||
| 112 | const FINALIDADE_RETORNO = 'retorno'; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Indicador de presença do comprador no estabelecimento comercial no |
||
| 116 | * momento da oepração (0-Não se aplica, ex.: Nota Fiscal complementar ou |
||
| 117 | * de ajuste;1-Operação presencial;2-Não presencial, internet;3-Não |
||
| 118 | * presencial, teleatendimento;4-NFC-e entrega em domicílio;9-Não |
||
| 119 | * presencial, outros) |
||
| 120 | */ |
||
| 121 | const PRESENCA_NENHUM = 'nenhum'; |
||
| 122 | const PRESENCA_PRESENCIAL = 'presencial'; |
||
| 123 | const PRESENCA_INTERNET = 'internet'; |
||
| 124 | const PRESENCA_TELEATENDIMENTO = 'teleatendimento'; |
||
| 125 | const PRESENCA_ENTREGA = 'entrega'; |
||
| 126 | const PRESENCA_OUTROS = 'outros'; |
||
| 127 | |||
| 128 | private $id; |
||
| 129 | private $numero; |
||
| 130 | private $emitente; |
||
| 131 | private $destinatario; |
||
| 132 | private $produtos; |
||
| 133 | private $transporte; |
||
| 134 | private $pagamentos; |
||
| 135 | private $data_movimentacao; |
||
| 136 | private $data_contingencia; |
||
| 137 | private $justificativa; |
||
| 138 | private $modelo; |
||
| 139 | private $tipo; |
||
| 140 | private $destino; |
||
| 141 | private $natureza; |
||
| 142 | private $codigo; |
||
| 143 | private $indicador; |
||
| 144 | private $data_emissao; |
||
| 145 | private $serie; |
||
| 146 | private $formato; |
||
| 147 | private $emissao; |
||
| 148 | private $digito_verificador; |
||
| 149 | private $ambiente; |
||
| 150 | private $finalidade; |
||
| 151 | private $consumidor_final; |
||
| 152 | private $presenca; |
||
| 153 | private $protocolo; |
||
| 154 | |||
| 155 | 7 | public function __construct($nota = array()) |
|
| 159 | |||
| 160 | 5 | public function getID($normalize = false) |
|
| 167 | |||
| 168 | 7 | public function setID($id) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Número do Documento Fiscal |
||
| 176 | */ |
||
| 177 | 5 | public function getNumero($normalize = false) |
|
| 184 | |||
| 185 | 7 | public function setNumero($numero) |
|
| 190 | |||
| 191 | 5 | public function getEmitente() |
|
| 195 | |||
| 196 | 7 | public function setEmitente($emitente) |
|
| 201 | |||
| 202 | 5 | public function getDestinatario() |
|
| 206 | |||
| 207 | 7 | public function setDestinatario($destinatario) |
|
| 212 | |||
| 213 | 5 | public function getProdutos() |
|
| 217 | |||
| 218 | 7 | public function setProdutos($produtos) |
|
| 223 | |||
| 224 | 2 | public function addProduto($produto) |
|
| 229 | |||
| 230 | 5 | public function getTransporte() |
|
| 234 | |||
| 235 | 7 | public function setTransporte($transporte) |
|
| 240 | |||
| 241 | 5 | public function getPagamentos() |
|
| 245 | |||
| 246 | 7 | public function setPagamentos($pagamentos) |
|
| 251 | |||
| 252 | 2 | public function addPagamento($pagamento) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Data e Hora da saída ou de entrada da mercadoria / produto |
||
| 260 | */ |
||
| 261 | 5 | public function getDataMovimentacao($normalize = false) |
|
| 268 | |||
| 269 | 7 | public function setDataMovimentacao($data_movimentacao) |
|
| 270 | { |
||
| 271 | 7 | if (!is_null($data_movimentacao) && !is_numeric($data_movimentacao)) { |
|
| 272 | $data_movimentacao = strtotime($data_movimentacao); |
||
| 273 | } |
||
| 274 | 7 | $this->data_movimentacao = $data_movimentacao; |
|
| 275 | 7 | return $this; |
|
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Informar a data e hora de entrada em contingência |
||
| 280 | */ |
||
| 281 | 2 | public function getDataContingencia($normalize = false) |
|
| 288 | |||
| 289 | 7 | public function setDataContingencia($data_contingencia) |
|
| 290 | { |
||
| 291 | 7 | if (!is_null($data_contingencia) && !is_numeric($data_contingencia)) { |
|
| 292 | $data_contingencia = strtotime($data_contingencia); |
||
| 293 | } |
||
| 294 | 7 | $this->data_contingencia = $data_contingencia; |
|
| 295 | 7 | return $this; |
|
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Informar a Justificativa da entrada em contingência |
||
| 300 | */ |
||
| 301 | 2 | public function getJustificativa($normalize = false) |
|
| 308 | |||
| 309 | 7 | public function setJustificativa($justificativa) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Código do modelo do Documento Fiscal. 55 = NF-e; 65 = NFC-e. |
||
| 317 | */ |
||
| 318 | 5 | public function getModelo($normalize = false) |
|
| 319 | { |
||
| 320 | 5 | if (!$normalize) { |
|
| 321 | 4 | return $this->modelo; |
|
| 322 | } |
||
| 323 | 5 | switch ($this->modelo) { |
|
| 324 | 5 | case self::MODELO_NFE: |
|
| 325 | return '55'; |
||
| 326 | 5 | case self::MODELO_NFCE: |
|
| 327 | 5 | return '65'; |
|
| 328 | } |
||
| 329 | return $this->modelo; |
||
| 330 | } |
||
| 331 | |||
| 332 | 7 | public function setModelo($modelo) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Tipo do Documento Fiscal (0 - entrada; 1 - saída) |
||
| 348 | */ |
||
| 349 | 5 | public function getTipo($normalize = false) |
|
| 350 | { |
||
| 351 | 5 | if (!$normalize) { |
|
| 352 | 2 | return $this->tipo; |
|
| 353 | } |
||
| 354 | 5 | switch ($this->tipo) { |
|
| 355 | 5 | case self::TIPO_ENTRADA: |
|
| 356 | return '0'; |
||
| 357 | 5 | case self::TIPO_SAIDA: |
|
| 358 | 5 | return '1'; |
|
| 359 | } |
||
| 360 | return $this->tipo; |
||
| 361 | } |
||
| 362 | |||
| 363 | 7 | public function setTipo($tipo) |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Identificador de Local de destino da operação |
||
| 379 | * (1-Interna;2-Interestadual;3-Exterior) |
||
| 380 | */ |
||
| 381 | 5 | public function getDestino($normalize = false) |
|
| 382 | { |
||
| 383 | 5 | if (!$normalize) { |
|
| 384 | 2 | return $this->destino; |
|
| 385 | } |
||
| 386 | 5 | switch ($this->destino) { |
|
| 387 | 5 | case self::DESTINO_INTERNA: |
|
| 388 | 5 | return '1'; |
|
| 389 | case self::DESTINO_INTERESTADUAL: |
||
| 390 | return '2'; |
||
| 391 | case self::DESTINO_EXTERIOR: |
||
| 392 | return '3'; |
||
| 393 | } |
||
| 394 | return $this->destino; |
||
| 395 | } |
||
| 396 | |||
| 397 | 7 | public function setDestino($destino) |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Descrição da Natureza da Operação |
||
| 416 | */ |
||
| 417 | 5 | public function getNatureza($normalize = false) |
|
| 424 | |||
| 425 | 7 | public function setNatureza($natureza) |
|
| 430 | |||
| 431 | /** |
||
| 432 | * Código numérico que compõe a Chave de Acesso. Número aleatório gerado |
||
| 433 | * pelo emitente para cada NF-e. |
||
| 434 | */ |
||
| 435 | 5 | public function getCodigo($normalize = false) |
|
| 442 | |||
| 443 | 7 | public function setCodigo($codigo) |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Indicador da forma de pagamento: 0 – pagamento à vista; 1 – pagamento à |
||
| 451 | * prazo; 2 – outros. |
||
| 452 | */ |
||
| 453 | 5 | public function getIndicador($normalize = false) |
|
| 454 | { |
||
| 455 | 5 | if (!$normalize) { |
|
| 456 | 2 | return $this->indicador; |
|
| 457 | } |
||
| 458 | 5 | switch ($this->indicador) { |
|
| 459 | 5 | case self::INDICADOR_AVISTA: |
|
| 460 | 5 | return '0'; |
|
| 461 | case self::INDICADOR_APRAZO: |
||
| 462 | return '1'; |
||
| 463 | case self::INDICADOR_OUTROS: |
||
| 464 | return '2'; |
||
| 465 | } |
||
| 466 | return $this->indicador; |
||
| 467 | } |
||
| 468 | |||
| 469 | 7 | public function setIndicador($indicador) |
|
| 485 | |||
| 486 | /** |
||
| 487 | * Data e Hora de emissão do Documento Fiscal |
||
| 488 | */ |
||
| 489 | 5 | public function getDataEmissao($normalize = false) |
|
| 496 | |||
| 497 | 7 | public function setDataEmissao($data_emissao) |
|
| 498 | { |
||
| 499 | 7 | if (!is_numeric($data_emissao)) { |
|
| 500 | 7 | $data_emissao = strtotime($data_emissao); |
|
| 501 | 7 | } |
|
| 502 | 7 | $this->data_emissao = $data_emissao; |
|
| 503 | 7 | return $this; |
|
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Série do Documento Fiscal: série normal 0-889, Avulsa Fisco 890-899, |
||
| 508 | * SCAN 900-999 |
||
| 509 | */ |
||
| 510 | 5 | public function getSerie($normalize = false) |
|
| 517 | |||
| 518 | 7 | public function setSerie($serie) |
|
| 523 | |||
| 524 | /** |
||
| 525 | * Formato de impressão do DANFE (0-sem DANFE;1-DANFe Retrato; 2-DANFe |
||
| 526 | * Paisagem;3-DANFe Simplificado;4-DANFe NFC-e;5-DANFe NFC-e em mensagem |
||
| 527 | * eletrônica) |
||
| 528 | */ |
||
| 529 | 5 | public function getFormato($normalize = false) |
|
| 530 | { |
||
| 531 | 5 | if (!$normalize) { |
|
| 532 | 2 | return $this->formato; |
|
| 533 | } |
||
| 534 | 5 | switch ($this->formato) { |
|
| 535 | 5 | case self::FORMATO_NENHUMA: |
|
| 536 | return '0'; |
||
| 537 | 5 | case self::FORMATO_RETRATO: |
|
| 538 | return '1'; |
||
| 539 | 5 | case self::FORMATO_PAISAGEM: |
|
| 540 | return '2'; |
||
| 541 | 5 | case self::FORMATO_SIMPLIFICADO: |
|
| 542 | return '3'; |
||
| 543 | 5 | case self::FORMATO_CONSUMIDOR: |
|
| 544 | 5 | return '4'; |
|
| 545 | case self::FORMATO_MENSAGEM: |
||
| 546 | return '5'; |
||
| 547 | } |
||
| 548 | return $this->formato; |
||
| 549 | } |
||
| 550 | |||
| 551 | 7 | public function setFormato($formato) |
|
| 576 | |||
| 577 | /** |
||
| 578 | * Forma de emissão da NF-e |
||
| 579 | */ |
||
| 580 | 5 | public function getEmissao($normalize = false) |
|
| 581 | { |
||
| 582 | 5 | if (!$normalize) { |
|
| 583 | 5 | return $this->emissao; |
|
| 584 | } |
||
| 585 | 5 | switch ($this->emissao) { |
|
| 586 | 5 | case self::EMISSAO_NORMAL: |
|
| 587 | 5 | return '1'; |
|
| 588 | case self::EMISSAO_CONTINGENCIA: |
||
| 589 | return '9'; |
||
| 590 | } |
||
| 591 | return $this->emissao; |
||
| 592 | } |
||
| 593 | |||
| 594 | 7 | public function setEmissao($emissao) |
|
| 607 | |||
| 608 | /** |
||
| 609 | * Digito Verificador da Chave de Acesso da NF-e |
||
| 610 | */ |
||
| 611 | 5 | public function getDigitoVerificador($normalize = false) |
|
| 618 | |||
| 619 | 7 | public function setDigitoVerificador($digito_verificador) |
|
| 624 | |||
| 625 | /** |
||
| 626 | * Identificação do Ambiente: 1 - Produção, 2 - Homologação |
||
| 627 | */ |
||
| 628 | 5 | public function getAmbiente($normalize = false) |
|
| 629 | { |
||
| 630 | 5 | if (!$normalize) { |
|
| 631 | 5 | return $this->ambiente; |
|
| 632 | } |
||
| 633 | 5 | switch ($this->ambiente) { |
|
| 634 | 5 | case self::AMBIENTE_PRODUCAO: |
|
| 635 | return '1'; |
||
| 636 | 5 | case self::AMBIENTE_HOMOLOGACAO: |
|
| 637 | 5 | return '2'; |
|
| 638 | } |
||
| 639 | return $this->ambiente; |
||
| 640 | } |
||
| 641 | |||
| 642 | 7 | public function setAmbiente($ambiente) |
|
| 655 | |||
| 656 | /** |
||
| 657 | * Finalidade da emissão da NF-e: 1 - NFe normal, 2 - NFe complementar, 3 - |
||
| 658 | * NFe de ajuste, 4 - Devolução/Retorno |
||
| 659 | */ |
||
| 660 | 5 | public function getFinalidade($normalize = false) |
|
| 661 | { |
||
| 662 | 5 | if (!$normalize) { |
|
| 663 | 2 | return $this->finalidade; |
|
| 664 | } |
||
| 665 | 5 | switch ($this->finalidade) { |
|
| 666 | 5 | case self::FINALIDADE_NORMAL: |
|
| 667 | 5 | return '1'; |
|
| 668 | case self::FINALIDADE_COMPLEMENTAR: |
||
| 669 | return '2'; |
||
| 670 | case self::FINALIDADE_AJUSTE: |
||
| 671 | return '3'; |
||
| 672 | case self::FINALIDADE_RETORNO: |
||
| 673 | return '4'; |
||
| 674 | } |
||
| 675 | return $this->finalidade; |
||
| 676 | } |
||
| 677 | |||
| 678 | 7 | public function setFinalidade($finalidade) |
|
| 697 | |||
| 698 | /** |
||
| 699 | * Indica operação com consumidor final (0-Não;1-Consumidor Final) |
||
| 700 | */ |
||
| 701 | 5 | public function getConsumidorFinal($normalize = false) |
|
| 702 | { |
||
| 703 | 5 | if (!$normalize) { |
|
| 704 | 2 | return $this->consumidor_final; |
|
| 705 | } |
||
| 706 | 5 | switch ($this->consumidor_final) { |
|
| 707 | 5 | case 'N': |
|
| 708 | return '0'; |
||
| 709 | 5 | case 'Y': |
|
| 710 | 5 | return '1'; |
|
| 711 | } |
||
| 712 | return $this->consumidor_final; |
||
| 713 | } |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Indica operação com consumidor final (0-Não;1-Consumidor Final) |
||
| 717 | */ |
||
| 718 | public function isConsumidorFinal() |
||
| 722 | |||
| 723 | 7 | public function setConsumidorFinal($consumidor_final) |
|
| 724 | { |
||
| 725 | 7 | if (!in_array($consumidor_final, array('N', 'Y'))) { |
|
| 726 | 3 | $consumidor_final = $consumidor_final?'Y':'N'; |
|
| 727 | 3 | } |
|
| 728 | 7 | $this->consumidor_final = $consumidor_final; |
|
| 729 | 7 | return $this; |
|
| 730 | } |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Indicador de presença do comprador no estabelecimento comercial no |
||
| 734 | * momento da oepração (0-Não se aplica, ex.: Nota Fiscal complementar ou |
||
| 735 | * de ajuste;1-Operação presencial;2-Não presencial, internet;3-Não |
||
| 736 | * presencial, teleatendimento;4-NFC-e entrega em domicílio;9-Não |
||
| 737 | * presencial, outros) |
||
| 738 | */ |
||
| 739 | 5 | public function getPresenca($normalize = false) |
|
| 740 | { |
||
| 741 | 5 | if (!$normalize) { |
|
| 742 | 2 | return $this->presenca; |
|
| 743 | } |
||
| 744 | 5 | switch ($this->presenca) { |
|
| 745 | 5 | case self::PRESENCA_NENHUM: |
|
| 746 | return '0'; |
||
| 747 | 5 | case self::PRESENCA_PRESENCIAL: |
|
| 748 | 5 | return '1'; |
|
| 749 | case self::PRESENCA_INTERNET: |
||
| 750 | return '2'; |
||
| 751 | case self::PRESENCA_TELEATENDIMENTO: |
||
| 752 | return '3'; |
||
| 753 | case self::PRESENCA_ENTREGA: |
||
| 754 | return '4'; |
||
| 755 | case self::PRESENCA_OUTROS: |
||
| 756 | return '9'; |
||
| 757 | } |
||
| 758 | return $this->presenca; |
||
| 759 | } |
||
| 760 | |||
| 761 | 7 | public function setPresenca($presenca) |
|
| 786 | |||
| 787 | /** |
||
| 788 | * Protocolo de autorização da nota, informado apenas quando a nota for |
||
| 789 | * enviada e autorizada |
||
| 790 | */ |
||
| 791 | 3 | public function getProtocolo() |
|
| 795 | |||
| 796 | 7 | public function setProtocolo($protocolo) |
|
| 801 | |||
| 802 | 2 | public function toArray() |
|
| 833 | |||
| 834 | 7 | public function fromArray($nota = array()) |
|
| 835 | { |
||
| 836 | 7 | if ($nota instanceof Nota) { |
|
| 837 | $nota = $nota->toArray(); |
||
| 838 | 7 | } elseif (!is_array($nota)) { |
|
| 839 | return $this; |
||
| 840 | } |
||
| 841 | 7 | if (isset($nota['id'])) { |
|
| 842 | $this->setID($nota['id']); |
||
| 843 | } else { |
||
| 844 | 7 | $this->setID(null); |
|
| 845 | } |
||
| 846 | 7 | if (isset($nota['numero'])) { |
|
| 847 | 2 | $this->setNumero($nota['numero']); |
|
| 848 | 2 | } else { |
|
| 849 | 7 | $this->setNumero(null); |
|
| 850 | } |
||
| 851 | 7 | if (!isset($nota['emitente']) || is_null($nota['emitente'])) { |
|
| 852 | 7 | $this->setEmitente(new Emitente()); |
|
| 853 | 7 | } else { |
|
| 854 | 2 | $this->setEmitente($nota['emitente']); |
|
| 855 | } |
||
| 856 | 7 | if (!isset($nota['destinatario']) || is_null($nota['destinatario'])) { |
|
| 857 | 7 | $this->setDestinatario(new Destinatario()); |
|
| 858 | 7 | } else { |
|
| 859 | 2 | $this->setDestinatario($nota['destinatario']); |
|
| 860 | } |
||
| 861 | 7 | if (!isset($nota['produtos']) || is_null($nota['produtos'])) { |
|
| 862 | 7 | $this->setProdutos(array()); |
|
| 863 | 7 | } else { |
|
| 864 | 2 | $this->setProdutos($nota['produtos']); |
|
| 865 | } |
||
| 866 | 7 | if (!isset($nota['transporte']) || is_null($nota['transporte'])) { |
|
| 867 | 7 | $this->setTransporte(new Transporte()); |
|
| 868 | 7 | } else { |
|
| 869 | 2 | $this->setTransporte($nota['transporte']); |
|
| 870 | } |
||
| 871 | 7 | if (!isset($nota['pagamentos']) || is_null($nota['pagamentos'])) { |
|
| 872 | 7 | $this->setPagamentos(array()); |
|
| 873 | 7 | } else { |
|
| 874 | 2 | $this->setPagamentos($nota['pagamentos']); |
|
| 875 | } |
||
| 876 | 7 | if (isset($nota['data_movimentacao'])) { |
|
| 877 | $this->setDataMovimentacao($nota['data_movimentacao']); |
||
| 878 | } else { |
||
| 879 | 7 | $this->setDataMovimentacao(null); |
|
| 880 | } |
||
| 881 | 7 | if (isset($nota['data_contingencia'])) { |
|
| 882 | $this->setDataContingencia($nota['data_contingencia']); |
||
| 883 | } else { |
||
| 884 | 7 | $this->setDataContingencia(null); |
|
| 885 | } |
||
| 886 | 7 | if (isset($nota['justificativa'])) { |
|
| 887 | $this->setJustificativa($nota['justificativa']); |
||
| 888 | } else { |
||
| 889 | 7 | $this->setJustificativa(null); |
|
| 890 | } |
||
| 891 | 7 | if (isset($nota['modelo'])) { |
|
| 892 | 2 | $this->setModelo($nota['modelo']); |
|
| 893 | 2 | } else { |
|
| 894 | 7 | $this->setModelo(null); |
|
| 895 | } |
||
| 896 | 7 | if (!isset($nota['tipo']) || is_null($nota['tipo'])) { |
|
| 897 | 7 | $this->setTipo(self::TIPO_SAIDA); |
|
| 898 | 7 | } else { |
|
| 899 | 2 | $this->setTipo($nota['tipo']); |
|
| 900 | } |
||
| 901 | 7 | if (!isset($nota['destino']) || is_null($nota['destino'])) { |
|
| 902 | 7 | $this->setDestino(self::DESTINO_INTERNA); |
|
| 903 | 7 | } else { |
|
| 904 | 2 | $this->setDestino($nota['destino']); |
|
| 905 | } |
||
| 906 | 7 | if (!isset($nota['natureza']) || is_null($nota['natureza'])) { |
|
| 907 | 7 | $this->setNatureza('VENDA PARA CONSUMIDOR FINAL'); |
|
| 908 | 7 | } else { |
|
| 909 | 2 | $this->setNatureza($nota['natureza']); |
|
| 910 | } |
||
| 911 | 7 | if (isset($nota['codigo'])) { |
|
| 912 | 2 | $this->setCodigo($nota['codigo']); |
|
| 913 | 2 | } else { |
|
| 914 | 7 | $this->setCodigo(null); |
|
| 915 | } |
||
| 916 | 7 | if (!isset($nota['indicador']) || is_null($nota['indicador'])) { |
|
| 917 | 7 | $this->setIndicador(self::INDICADOR_AVISTA); |
|
| 918 | 7 | } else { |
|
| 919 | 2 | $this->setIndicador($nota['indicador']); |
|
| 920 | } |
||
| 921 | 7 | if (isset($nota['data_emissao'])) { |
|
| 922 | 2 | $this->setDataEmissao($nota['data_emissao']); |
|
| 923 | 2 | } else { |
|
| 924 | 7 | $this->setDataEmissao(null); |
|
| 925 | } |
||
| 926 | 7 | if (isset($nota['serie'])) { |
|
| 927 | 2 | $this->setSerie($nota['serie']); |
|
| 928 | 2 | } else { |
|
| 929 | 7 | $this->setSerie(null); |
|
| 930 | } |
||
| 931 | 7 | if (!isset($nota['formato']) || is_null($nota['formato'])) { |
|
| 932 | 7 | $this->setFormato(self::FORMATO_NENHUMA); |
|
| 933 | 7 | } else { |
|
| 934 | 2 | $this->setFormato($nota['formato']); |
|
| 935 | } |
||
| 936 | 7 | if (!isset($nota['emissao']) || is_null($nota['emissao'])) { |
|
| 937 | 7 | $this->setEmissao(self::EMISSAO_NORMAL); |
|
| 938 | 7 | } else { |
|
| 939 | 2 | $this->setEmissao($nota['emissao']); |
|
| 940 | } |
||
| 941 | 7 | if (isset($nota['digito_verificador'])) { |
|
| 942 | $this->setDigitoVerificador($nota['digito_verificador']); |
||
| 943 | } else { |
||
| 944 | 7 | $this->setDigitoVerificador(null); |
|
| 945 | } |
||
| 946 | 7 | if (!isset($nota['ambiente']) || is_null($nota['ambiente'])) { |
|
| 947 | 7 | $this->setAmbiente(self::AMBIENTE_HOMOLOGACAO); |
|
| 948 | 7 | } else { |
|
| 949 | 2 | $this->setAmbiente($nota['ambiente']); |
|
| 950 | } |
||
| 951 | 7 | if (!isset($nota['finalidade']) || is_null($nota['finalidade'])) { |
|
| 952 | 7 | $this->setFinalidade(self::FINALIDADE_NORMAL); |
|
| 953 | 7 | } else { |
|
| 954 | 2 | $this->setFinalidade($nota['finalidade']); |
|
| 955 | } |
||
| 956 | 7 | if (!isset($nota['consumidor_final']) || is_null($nota['consumidor_final'])) { |
|
| 957 | 7 | $this->setConsumidorFinal('Y'); |
|
| 958 | 7 | } else { |
|
| 959 | 2 | $this->setConsumidorFinal($nota['consumidor_final']); |
|
| 960 | } |
||
| 961 | 7 | if (isset($nota['presenca'])) { |
|
| 962 | 2 | $this->setPresenca($nota['presenca']); |
|
| 963 | 2 | } else { |
|
| 964 | 7 | $this->setPresenca(null); |
|
| 965 | } |
||
| 966 | 7 | if (isset($nota['protocolo'])) { |
|
| 967 | $this->setProtocolo($nota['protocolo']); |
||
| 968 | } else { |
||
| 969 | 7 | $this->setProtocolo(null); |
|
| 970 | } |
||
| 971 | 7 | return $this; |
|
| 972 | } |
||
| 973 | |||
| 974 | 5 | public function gerarID() |
|
| 975 | { |
||
| 976 | 5 | $estado = $this->getEmitente()->getEndereco()->getMunicipio()->getEstado(); |
|
| 977 | 5 | $estado->checkCodigos(); |
|
| 978 | 5 | $id = sprintf( |
|
| 979 | 5 | '%02d%02d%02d%s%02d%03d%09d%01d%08d', |
|
| 980 | 5 | $estado->getCodigo(), |
|
| 981 | 5 | date('y', $this->getDataEmissao()), // Ano 2 dígitos |
|
| 982 | 5 | date('m', $this->getDataEmissao()), // Mês 2 dígitos |
|
| 983 | 5 | $this->getEmitente()->getCNPJ(), |
|
| 984 | 5 | $this->getModelo(true), |
|
| 985 | 5 | $this->getSerie(), |
|
| 986 | 5 | $this->getNumero(), |
|
| 987 | 5 | $this->getEmissao(true), |
|
| 988 | 5 | $this->getCodigo() |
|
| 989 | 5 | ); |
|
| 990 | 5 | return $id.Util::getDAC($id, 11); |
|
| 991 | } |
||
| 992 | |||
| 993 | 5 | protected function getTotais() |
|
| 994 | { |
||
| 995 | 5 | $total = array(); |
|
| 996 | 5 | $total['produtos'] = 0.00; |
|
| 997 | 5 | $total['descontos'] = 0.00; |
|
| 998 | 5 | $total['frete'] = 0.00; |
|
| 999 | 5 | $total['seguro'] = 0.00; |
|
| 1000 | 5 | $total['outros'] = 0.00; |
|
| 1001 | 5 | $total['nota'] = 0.00; |
|
| 1002 | 5 | $total['tributos'] = 0.00; |
|
| 1003 | 5 | $total['icms'] = 0.00; |
|
| 1004 | 5 | $total['icms.st'] = 0.00; |
|
| 1005 | 5 | $total['base'] = 0.00; |
|
| 1006 | 5 | $total['base.st'] = 0.00; |
|
| 1007 | 5 | $total['ii'] = 0.00; |
|
| 1008 | 5 | $total['ipi'] = 0.00; |
|
| 1009 | 5 | $total['pis'] = 0.00; |
|
| 1010 | 5 | $total['cofins'] = 0.00; |
|
| 1011 | 5 | $total['desoneracao'] = 0.00; |
|
| 1012 | 5 | $_produtos = $this->getProdutos(); |
|
| 1013 | 5 | foreach ($_produtos as $_produto) { |
|
| 1014 | 5 | $imposto_info = $_produto->getImpostoInfo(); |
|
| 1015 | 5 | $total['produtos'] += $_produto->getPreco(); |
|
| 1016 | 5 | $total['descontos'] += $_produto->getDesconto(); |
|
| 1017 | 5 | $total['frete'] += $_produto->getFrete(); |
|
| 1018 | 5 | $total['seguro'] += $_produto->getSeguro(); |
|
| 1019 | 5 | $total['outros'] += $_produto->getDespesas(); |
|
| 1020 | 5 | $total['nota'] += $_produto->getContabilizado(); |
|
| 1021 | 5 | $total['tributos'] += $imposto_info['total']; |
|
| 1022 | 5 | $_impostos = $_produto->getImpostos(); |
|
| 1023 | 5 | foreach ($_impostos as $_imposto) { |
|
| 1024 | 5 | switch ($_imposto->getGrupo()) { |
|
| 1025 | 5 | case Imposto::GRUPO_ICMS: |
|
| 1026 | 5 | if (($_imposto instanceof \NFe\Entity\Imposto\ICMS\Cobranca) || |
|
| 1027 | 5 | ($_imposto instanceof \NFe\Entity\Imposto\ICMS\Simples\Cobranca)) { |
|
| 1028 | 5 | $total[$_imposto->getGrupo()] += round($_imposto->getNormal()->getValor(), 2); |
|
| 1029 | 5 | $total['base'] += $_imposto->getNormal()->getBase(); |
|
| 1030 | 5 | } |
|
| 1031 | 5 | if (($_imposto instanceof \NFe\Entity\Imposto\ICMS\Parcial) || |
|
| 1032 | 5 | ($_imposto instanceof \NFe\Entity\Imposto\ICMS\Simples\Parcial)) { |
|
| 1033 | 5 | $total['icms.st'] += $_imposto->getValor(); |
|
| 1034 | 5 | $total['base.st'] += $_imposto->getBase(); |
|
| 1035 | 5 | } else { |
|
| 1036 | $total[$_imposto->getGrupo()] += round($_imposto->getValor(), 2); |
||
| 1037 | $total['base'] += $_imposto->getBase(); |
||
| 1038 | } |
||
| 1039 | 5 | break; |
|
| 1040 | 5 | default: |
|
| 1041 | 5 | $total[$_imposto->getGrupo()] += round($_imposto->getValor(), 2); |
|
| 1042 | 5 | } |
|
| 1043 | 5 | } |
|
| 1044 | 5 | } |
|
| 1045 | 5 | return $total; |
|
| 1046 | } |
||
| 1047 | |||
| 1048 | 5 | private function getNodeTotal($name = null) |
|
| 1079 | |||
| 1080 | 5 | public function getNode($name = null) |
|
| 1081 | { |
||
| 1082 | 5 | $this->getEmitente()->getEndereco()->checkCodigos(); |
|
| 1083 | 5 | $this->setID($this->gerarID()); |
|
| 1084 | 5 | $this->setDigitoVerificador(substr($this->getID(), -1, 1)); |
|
| 1085 | |||
| 1086 | 5 | $dom = new \DOMDocument('1.0', 'UTF-8'); |
|
| 1087 | 5 | $element = $dom->createElement(is_null($name)?'NFe':$name); |
|
| 1088 | 5 | $element->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns', self::PORTAL); |
|
| 1089 | |||
| 1090 | 5 | $info = $dom->createElement('infNFe'); |
|
| 1091 | 5 | $id = $dom->createAttribute('Id'); |
|
| 1092 | 5 | $id->value = $this->getID(true); |
|
| 1093 | 5 | $info->appendChild($id); |
|
| 1094 | 5 | $versao = $dom->createAttribute('versao'); |
|
| 1095 | 5 | $versao->value = self::VERSAO; |
|
| 1096 | 5 | $info->appendChild($versao); |
|
| 1194 | |||
| 1195 | 3 | public function loadNode($element, $name = null) |
|
| 1401 | |||
| 1402 | /** |
||
| 1403 | * Carrega um arquivo XML e preenche a nota com as informações dele |
||
| 1404 | * @param string $filename caminho do arquivo |
||
| 1405 | * @return DOMDocument objeto do documento carregado |
||
| 1406 | */ |
||
| 1407 | 1 | public function load($filename) |
|
| 1417 | |||
| 1418 | /** |
||
| 1419 | * Assina o XML com a assinatura eletrônica do tipo A1 |
||
| 1420 | */ |
||
| 1421 | 3 | public function assinar($dom = null) |
|
| 1437 | |||
| 1438 | /** |
||
| 1439 | * Valida o documento após assinar |
||
| 1440 | */ |
||
| 1441 | 1 | public function validar($dom) |
|
| 1468 | |||
| 1469 | /** |
||
| 1470 | * Adiciona o protocolo no XML da nota |
||
| 1471 | */ |
||
| 1472 | public function addProtocolo($dom) |
||
| 1502 | } |
||
| 1503 |
Too many fields generally indicate a class which does too much and does not follow the single responsibility principle.
We suggest taking a look at the “Code” section for further suggestions on how to fix this.