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 | 2 | 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); |
|
| 1097 | |||
| 1098 | 5 | $municipio = $this->getEmitente()->getEndereco()->getMunicipio(); |
|
| 1099 | 5 | $estado = $municipio->getEstado(); |
|
| 1100 | 5 | $ident = $dom->createElement('ide'); |
|
| 1101 | 5 | $ident->appendChild($dom->createElement('cUF', $estado->getCodigo(true))); |
|
| 1102 | 5 | $ident->appendChild($dom->createElement('cNF', $this->getCodigo(true))); |
|
| 1103 | 5 | $ident->appendChild($dom->createElement('natOp', $this->getNatureza(true))); |
|
| 1104 | 5 | $ident->appendChild($dom->createElement('indPag', $this->getIndicador(true))); |
|
| 1105 | 5 | $ident->appendChild($dom->createElement('mod', $this->getModelo(true))); |
|
| 1106 | 5 | $ident->appendChild($dom->createElement('serie', $this->getSerie(true))); |
|
| 1107 | 5 | $ident->appendChild($dom->createElement('nNF', $this->getNumero(true))); |
|
| 1108 | 5 | $ident->appendChild($dom->createElement('dhEmi', $this->getDataEmissao(true))); |
|
| 1109 | 5 | $ident->appendChild($dom->createElement('tpNF', $this->getTipo(true))); |
|
| 1110 | 5 | $ident->appendChild($dom->createElement('idDest', $this->getDestino(true))); |
|
| 1111 | 5 | $ident->appendChild($dom->createElement('cMunFG', $municipio->getCodigo(true))); |
|
| 1112 | 5 | $ident->appendChild($dom->createElement('tpImp', $this->getFormato(true))); |
|
| 1113 | 5 | $ident->appendChild($dom->createElement('tpEmis', $this->getEmissao(true))); |
|
| 1114 | 5 | $ident->appendChild($dom->createElement('cDV', $this->getDigitoVerificador(true))); |
|
| 1115 | 5 | $ident->appendChild($dom->createElement('tpAmb', $this->getAmbiente(true))); |
|
| 1116 | 5 | $ident->appendChild($dom->createElement('finNFe', $this->getFinalidade(true))); |
|
| 1117 | 5 | $ident->appendChild($dom->createElement('indFinal', $this->getConsumidorFinal(true))); |
|
| 1118 | 5 | $ident->appendChild($dom->createElement('indPres', $this->getPresenca(true))); |
|
| 1119 | 5 | $ident->appendChild($dom->createElement('procEmi', 0)); // emissão de NF-e com aplicativo do contribuinte |
|
| 1120 | 5 | $ident->appendChild($dom->createElement('verProc', self::APP_VERSAO)); |
|
| 1121 | 5 | if (!is_null($this->getDataMovimentacao())) { |
|
| 1122 | $ident->appendChild($dom->createElement('dhSaiEnt', $this->getDataMovimentacao(true))); |
||
| 1123 | } |
||
| 1124 | 5 | if ($this->getEmissao() != self::EMISSAO_NORMAL) { |
|
| 1125 | $ident->appendChild($dom->createElement('dhCont', $this->getDataContingencia(true))); |
||
| 1126 | $ident->appendChild($dom->createElement('xJust', $this->getJustificativa(true))); |
||
| 1127 | } |
||
| 1128 | 5 | $info->appendChild($ident); |
|
| 1129 | |||
| 1130 | 5 | $emitente = $this->getEmitente()->getNode(); |
|
| 1131 | 5 | $emitente = $dom->importNode($emitente, true); |
|
| 1132 | 5 | $info->appendChild($emitente); |
|
| 1133 | 5 | if ($this->getAmbiente() == self::AMBIENTE_HOMOLOGACAO && !is_null($this->getDestinatario())) { |
|
| 1134 | 5 | $this->getDestinatario()->setNome('NF-E EMITIDA EM AMBIENTE DE HOMOLOGACAO - SEM VALOR FISCAL'); |
|
| 1135 | 5 | } |
|
| 1136 | 5 | if (!is_null($this->getDestinatario())) { |
|
| 1137 | 5 | $destinatario = $this->getDestinatario()->getNode(); |
|
| 1138 | 5 | $destinatario = $dom->importNode($destinatario, true); |
|
| 1139 | 5 | $info->appendChild($destinatario); |
|
| 1140 | 5 | } |
|
| 1141 | 5 | $item = 0; |
|
| 1142 | 5 | $tributos = array(); |
|
| 1143 | 5 | $_produtos = $this->getProdutos(); |
|
| 1144 | 5 | foreach ($_produtos as $_produto) { |
|
| 1145 | 5 | if (is_null($_produto->getItem())) { |
|
| 1146 | 2 | $item += 1; |
|
| 1147 | 2 | $_produto->setItem($item); |
|
| 1148 | 2 | } else { |
|
| 1149 | 3 | $item = $_produto->getItem(); |
|
| 1150 | } |
||
| 1151 | 5 | if ($this->getAmbiente() == self::AMBIENTE_HOMOLOGACAO) { |
|
| 1152 | 5 | $_produto->setDescricao('NOTA FISCAL EMITIDA EM AMBIENTE DE HOMOLOGACAO - SEM VALOR FISCAL'); |
|
| 1153 | 5 | } |
|
| 1154 | 5 | $produto = $_produto->getNode(); |
|
| 1155 | 5 | $produto = $dom->importNode($produto, true); |
|
| 1156 | 5 | $info->appendChild($produto); |
|
| 1157 | // Soma os tributos aproximados dos produtos |
||
| 1158 | 5 | $imposto_info = $_produto->getImpostoInfo(); |
|
| 1159 | 5 | $tributos['info'] = $imposto_info['info']; |
|
| 1160 | 5 | foreach ($imposto_info as $key => $value) { |
|
| 1161 | 5 | if (!is_numeric($value)) { |
|
| 1162 | 5 | continue; |
|
| 1163 | } |
||
| 1164 | 5 | if (!isset($tributos[$key])) { |
|
| 1165 | 5 | $tributos[$key] = 0.00; |
|
| 1166 | 5 | } |
|
| 1167 | 5 | $tributos[$key] += $value; |
|
| 1168 | 5 | } |
|
| 1169 | 5 | } |
|
| 1170 | 5 | $total = $this->getNodeTotal(); |
|
| 1171 | 5 | $total = $dom->importNode($total, true); |
|
| 1172 | 5 | $info->appendChild($total); |
|
| 1173 | 5 | $transporte = $this->getTransporte()->getNode(); |
|
| 1174 | 5 | $transporte = $dom->importNode($transporte, true); |
|
| 1175 | 5 | $info->appendChild($transporte); |
|
| 1176 | // TODO: adicionar cobrança |
||
| 1177 | 5 | $_pagamentos = $this->getPagamentos(); |
|
| 1178 | 5 | foreach ($_pagamentos as $_pagamento) { |
|
| 1179 | 5 | $pagamento = $_pagamento->getNode(); |
|
| 1180 | 5 | $pagamento = $dom->importNode($pagamento, true); |
|
| 1181 | 5 | $info->appendChild($pagamento); |
|
| 1182 | 5 | } |
|
| 1183 | // TODO: adicionar informações adicionais somente na NFC-e? |
||
| 1184 | 5 | $adicional = $dom->createElement('infAdic'); |
|
| 1185 | 5 | Produto::addNodeInformacoes($tributos, $adicional, 'infCpl'); |
|
| 1186 | 5 | $info->appendChild($adicional); |
|
| 1187 | // TODO: adicionar exportação |
||
| 1188 | // TODO: adicionar compra |
||
| 1189 | // TODO: adicionar cana |
||
| 1190 | 5 | $element->appendChild($info); |
|
| 1191 | 5 | $dom->appendChild($element); |
|
| 1192 | 5 | return $element; |
|
| 1193 | } |
||
| 1194 | |||
| 1195 | 3 | public function loadNode($element, $name = null) |
|
| 1196 | { |
||
| 1197 | 3 | $root = $element; |
|
| 1198 | 3 | $name = is_null($name)?'NFe':$name; |
|
| 1199 | 3 | if ($element->tagName != $name) { |
|
| 1200 | $_fields = $element->getElementsByTagName($name); |
||
| 1201 | if ($_fields->length == 0) { |
||
| 1202 | throw new \Exception('Tag "'.$name.'" não encontrada', 404); |
||
| 1203 | } |
||
| 1204 | $element = $_fields->item(0); |
||
| 1205 | } |
||
| 1206 | 3 | $_fields = $element->getElementsByTagName('infNFe'); |
|
| 1207 | 3 | if ($_fields->length > 0) { |
|
| 1208 | 3 | $info = $_fields->item(0); |
|
| 1209 | 3 | } else { |
|
| 1210 | throw new \Exception('Tag "infNFe" não encontrada', 404); |
||
| 1211 | } |
||
| 1212 | 3 | $id = $info->getAttribute('Id'); |
|
| 1213 | 3 | if (strlen($id) != 47) { |
|
| 1214 | throw new \Exception('Atributo "Id" inválido, encontrado: "'.$id.'"', 500); |
||
| 1215 | } |
||
| 1216 | 3 | $this->setID(substr($id, 3)); |
|
| 1217 | 3 | $_fields = $info->getElementsByTagName('ide'); |
|
| 1218 | 3 | if ($_fields->length > 0) { |
|
| 1219 | 3 | $ident = $_fields->item(0); |
|
| 1220 | 3 | } else { |
|
| 1221 | throw new \Exception('Tag "ide" não encontrada', 404); |
||
| 1222 | } |
||
| 1223 | 3 | $emitente = new Emitente(); |
|
| 1224 | 3 | $_fields = $ident->getElementsByTagName('cUF'); |
|
| 1225 | 3 | if ($_fields->length > 0) { |
|
| 1226 | 3 | $codigo = $_fields->item(0)->nodeValue; |
|
| 1227 | 3 | } else { |
|
| 1228 | throw new \Exception('Tag "cUF" do campo "Codigo IBGE da UF" não encontrada', 404); |
||
| 1229 | } |
||
| 1230 | 3 | $emitente->getEndereco()->getMunicipio()->getEstado()->setCodigo($codigo); |
|
| 1231 | 3 | $_fields = $ident->getElementsByTagName('cNF'); |
|
| 1232 | 3 | if ($_fields->length > 0) { |
|
| 1233 | 3 | $codigo = $_fields->item(0)->nodeValue; |
|
| 1234 | 3 | } else { |
|
| 1235 | throw new \Exception('Tag "cNF" do campo "Codigo" não encontrada', 404); |
||
| 1236 | } |
||
| 1237 | 3 | $this->setCodigo($codigo); |
|
| 1238 | 3 | $_fields = $ident->getElementsByTagName('natOp'); |
|
| 1239 | 3 | if ($_fields->length > 0) { |
|
| 1240 | 3 | $natureza = $_fields->item(0)->nodeValue; |
|
| 1241 | 3 | } else { |
|
| 1242 | throw new \Exception('Tag "natOp" do campo "Natureza" não encontrada', 404); |
||
| 1243 | } |
||
| 1244 | 3 | $this->setNatureza($natureza); |
|
| 1245 | 3 | $_fields = $ident->getElementsByTagName('indPag'); |
|
| 1246 | 3 | if ($_fields->length > 0) { |
|
| 1247 | 3 | $indicador = $_fields->item(0)->nodeValue; |
|
| 1248 | 3 | } else { |
|
| 1249 | throw new \Exception('Tag "indPag" do campo "Indicador" não encontrada', 404); |
||
| 1250 | } |
||
| 1251 | 3 | $this->setIndicador($indicador); |
|
| 1252 | 3 | $_fields = $ident->getElementsByTagName('mod'); |
|
| 1253 | 3 | if ($_fields->length > 0) { |
|
| 1254 | 3 | $modelo = $_fields->item(0)->nodeValue; |
|
| 1255 | 3 | } else { |
|
| 1256 | throw new \Exception('Tag "mod" do campo "Modelo" não encontrada', 404); |
||
| 1257 | } |
||
| 1258 | 3 | $this->setModelo($modelo); |
|
| 1259 | 3 | $_fields = $ident->getElementsByTagName('serie'); |
|
| 1260 | 3 | if ($_fields->length > 0) { |
|
| 1261 | 3 | $serie = $_fields->item(0)->nodeValue; |
|
| 1262 | 3 | } else { |
|
| 1263 | throw new \Exception('Tag "serie" do campo "Serie" não encontrada', 404); |
||
| 1264 | } |
||
| 1265 | 3 | $this->setSerie($serie); |
|
| 1266 | 3 | $_fields = $ident->getElementsByTagName('nNF'); |
|
| 1267 | 3 | if ($_fields->length > 0) { |
|
| 1268 | 3 | $numero = $_fields->item(0)->nodeValue; |
|
| 1269 | 3 | } else { |
|
| 1270 | throw new \Exception('Tag "nNF" do campo "Numero" não encontrada', 404); |
||
| 1271 | } |
||
| 1272 | 3 | $this->setNumero($numero); |
|
| 1273 | 3 | $_fields = $ident->getElementsByTagName('dhEmi'); |
|
| 1274 | 3 | if ($_fields->length > 0) { |
|
| 1275 | 3 | $data_emissao = $_fields->item(0)->nodeValue; |
|
| 1276 | 3 | } else { |
|
| 1277 | throw new \Exception('Tag "dhEmi" do campo "DataEmissao" não encontrada', 404); |
||
| 1278 | } |
||
| 1279 | 3 | $this->setDataEmissao($data_emissao); |
|
| 1280 | 3 | $_fields = $ident->getElementsByTagName('tpNF'); |
|
| 1281 | 3 | if ($_fields->length > 0) { |
|
| 1282 | 3 | $tipo = $_fields->item(0)->nodeValue; |
|
| 1283 | 3 | } else { |
|
| 1284 | throw new \Exception('Tag "tpNF" do campo "Tipo" não encontrada', 404); |
||
| 1285 | } |
||
| 1286 | 3 | $this->setTipo($tipo); |
|
| 1287 | 3 | $_fields = $ident->getElementsByTagName('idDest'); |
|
| 1288 | 3 | if ($_fields->length > 0) { |
|
| 1289 | 3 | $destino = $_fields->item(0)->nodeValue; |
|
| 1290 | 3 | } else { |
|
| 1291 | throw new \Exception('Tag "idDest" do campo "Destino" não encontrada', 404); |
||
| 1292 | } |
||
| 1293 | 3 | $this->setDestino($destino); |
|
| 1294 | 3 | $_fields = $ident->getElementsByTagName('cMunFG'); |
|
| 1295 | 3 | if ($_fields->length > 0) { |
|
| 1296 | 3 | $codigo = $_fields->item(0)->nodeValue; |
|
| 1297 | 3 | } else { |
|
| 1298 | throw new \Exception('Tag "cMunFG" do campo "Codigo IBGE do município" não encontrada', 404); |
||
| 1299 | } |
||
| 1300 | 3 | $emitente->getEndereco()->getMunicipio()->setCodigo($codigo); |
|
| 1301 | 3 | $_fields = $ident->getElementsByTagName('dhSaiEnt'); |
|
| 1302 | 3 | $data_movimentacao = null; |
|
| 1303 | 3 | if ($_fields->length > 0) { |
|
| 1304 | $data_movimentacao = $_fields->item(0)->nodeValue; |
||
| 1305 | } |
||
| 1306 | 3 | $this->setDataMovimentacao($data_movimentacao); |
|
| 1307 | 3 | $_fields = $ident->getElementsByTagName('tpImp'); |
|
| 1308 | 3 | if ($_fields->length > 0) { |
|
| 1309 | 3 | $formato = $_fields->item(0)->nodeValue; |
|
| 1310 | 3 | } else { |
|
| 1311 | throw new \Exception('Tag "tpImp" do campo "Formato" não encontrada', 404); |
||
| 1312 | } |
||
| 1313 | 3 | $this->setFormato($formato); |
|
| 1314 | 3 | $_fields = $ident->getElementsByTagName('tpEmis'); |
|
| 1315 | 3 | if ($_fields->length > 0) { |
|
| 1316 | 3 | $emissao = $_fields->item(0)->nodeValue; |
|
| 1317 | 3 | } else { |
|
| 1318 | throw new \Exception('Tag "tpEmis" do campo "Emissao" não encontrada', 404); |
||
| 1319 | } |
||
| 1320 | 3 | $this->setEmissao($emissao); |
|
| 1321 | 3 | $_fields = $ident->getElementsByTagName('cDV'); |
|
| 1322 | 3 | if ($_fields->length > 0) { |
|
| 1323 | 3 | $digito_verificador = $_fields->item(0)->nodeValue; |
|
| 1324 | 3 | } else { |
|
| 1325 | throw new \Exception('Tag "cDV" do campo "DigitoVerificador" não encontrada', 404); |
||
| 1326 | } |
||
| 1327 | 3 | $this->setDigitoVerificador($digito_verificador); |
|
| 1328 | 3 | $_fields = $ident->getElementsByTagName('tpAmb'); |
|
| 1329 | 3 | if ($_fields->length > 0) { |
|
| 1330 | 3 | $ambiente = $_fields->item(0)->nodeValue; |
|
| 1331 | 3 | } else { |
|
| 1332 | throw new \Exception('Tag "tpAmb" do campo "Ambiente" não encontrada', 404); |
||
| 1333 | } |
||
| 1334 | 3 | $this->setAmbiente($ambiente); |
|
| 1335 | 3 | $_fields = $ident->getElementsByTagName('finNFe'); |
|
| 1336 | 3 | if ($_fields->length > 0) { |
|
| 1337 | 3 | $finalidade = $_fields->item(0)->nodeValue; |
|
| 1338 | 3 | } else { |
|
| 1339 | throw new \Exception('Tag "finNFe" do campo "Finalidade" não encontrada', 404); |
||
| 1340 | } |
||
| 1341 | 3 | $this->setFinalidade($finalidade); |
|
| 1342 | 3 | $_fields = $ident->getElementsByTagName('indFinal'); |
|
| 1343 | 3 | if ($_fields->length > 0) { |
|
| 1344 | 3 | $consumidor_final = $_fields->item(0)->nodeValue; |
|
| 1345 | 3 | } else { |
|
| 1346 | throw new \Exception('Tag "indFinal" do campo "ConsumidorFinal" não encontrada', 404); |
||
| 1347 | } |
||
| 1348 | 3 | $this->setConsumidorFinal($consumidor_final); |
|
| 1349 | 3 | $_fields = $ident->getElementsByTagName('indPres'); |
|
| 1350 | 3 | if ($_fields->length > 0) { |
|
| 1351 | 3 | $presenca = $_fields->item(0)->nodeValue; |
|
| 1352 | 3 | } else { |
|
| 1353 | throw new \Exception('Tag "indPres" do campo "Presenca" não encontrada', 404); |
||
| 1354 | } |
||
| 1355 | 3 | $this->setPresenca($presenca); |
|
| 1356 | 3 | $_fields = $ident->getElementsByTagName('dhCont'); |
|
| 1357 | 3 | $data_contingencia = null; |
|
| 1358 | 3 | if ($_fields->length > 0) { |
|
| 1359 | $data_contingencia = $_fields->item(0)->nodeValue; |
||
| 1360 | } |
||
| 1361 | 3 | $this->setDataContingencia($data_contingencia); |
|
| 1362 | 3 | $_fields = $ident->getElementsByTagName('xJust'); |
|
| 1363 | 3 | $justificativa = null; |
|
| 1364 | 3 | if ($_fields->length > 0) { |
|
| 1365 | $justificativa = $_fields->item(0)->nodeValue; |
||
| 1366 | } |
||
| 1367 | 3 | $this->setJustificativa($justificativa); |
|
| 1368 | |||
| 1369 | 3 | $_fields = $info->getElementsByTagName('emit'); |
|
| 1370 | 3 | if ($_fields->length > 0) { |
|
| 1371 | 3 | $emitente->loadNode($_fields->item(0), 'emit'); |
|
| 1372 | 3 | } else { |
|
| 1373 | throw new \Exception('Tag "emit" do objeto "Emitente" não encontrada', 404); |
||
| 1374 | } |
||
| 1375 | 3 | $this->setEmitente($emitente); |
|
| 1376 | 3 | $_fields = $info->getElementsByTagName('dest'); |
|
| 1377 | 3 | $destinatario = null; |
|
| 1378 | 3 | if ($_fields->length > 0) { |
|
| 1379 | 3 | $destinatario = new Destinatario(); |
|
| 1380 | 3 | $destinatario->loadNode($_fields->item(0), 'dest'); |
|
| 1381 | 3 | } |
|
| 1382 | 3 | $this->setDestinatario($destinatario); |
|
| 1383 | 3 | $produtos = array(); |
|
| 1384 | 3 | $_items = $info->getElementsByTagName('det'); |
|
| 1385 | 3 | foreach ($_items as $_item) { |
|
| 1386 | 3 | $produto = new Produto(); |
|
| 1387 | 3 | $produto->loadNode($_item, 'det'); |
|
| 1388 | 3 | $produtos[] = $produto; |
|
| 1389 | 3 | } |
|
| 1390 | 3 | $this->setProdutos($produtos); |
|
| 1391 | 3 | $_fields = $info->getElementsByTagName('transp'); |
|
| 1392 | 3 | $transporte = null; |
|
| 1393 | 3 | if ($_fields->length > 0) { |
|
| 1394 | 3 | $transporte = new Transporte(); |
|
| 1395 | 3 | $transporte->loadNode($_fields->item(0), 'transp'); |
|
| 1396 | 3 | } |
|
| 1397 | 3 | $this->setTransporte($transporte); |
|
| 1398 | 3 | $pagamentos = array(); |
|
| 1399 | 3 | $_items = $info->getElementsByTagName('pag'); |
|
| 1400 | 3 | foreach ($_items as $_item) { |
|
| 1401 | 3 | $pagamento = new Pagamento(); |
|
| 1402 | 3 | $pagamento->loadNode($_item, 'pag'); |
|
| 1403 | 3 | $pagamentos[] = $pagamento; |
|
| 1404 | 3 | } |
|
| 1405 | 3 | $this->setPagamentos($pagamentos); |
|
| 1406 | |||
| 1407 | 3 | $_fields = $root->getElementsByTagName('protNFe'); |
|
| 1408 | 3 | $protocolo = null; |
|
| 1409 | 3 | if ($_fields->length > 0) { |
|
| 1410 | $protocolo = new Protocolo(); |
||
| 1411 | $protocolo->loadNode($_fields->item(0), 'infProt'); |
||
| 1412 | } |
||
| 1413 | 3 | $this->setProtocolo($protocolo); |
|
| 1414 | 3 | return $element; |
|
| 1415 | } |
||
| 1416 | |||
| 1417 | /** |
||
| 1418 | * Assina o XML com a assinatura eletrônica do tipo A1 |
||
| 1419 | */ |
||
| 1420 | 3 | public function assinar($dom = null) |
|
| 1421 | { |
||
| 1422 | 3 | if (is_null($dom)) { |
|
| 1423 | $xml = $this->getNode(); |
||
| 1424 | $dom = $xml->ownerDocument; |
||
| 1425 | } |
||
| 1426 | 3 | $config = SEFAZ::getInstance()->getConfiguracao(); |
|
| 1427 | |||
| 1428 | 3 | $adapter = new XmlseclibsAdapter(); |
|
| 1429 | 3 | $adapter->setPrivateKey($config->getChavePrivada()); |
|
| 1430 | 3 | $adapter->setPublicKey($config->getChavePublica()); |
|
| 1431 | 3 | $adapter->addTransform(AdapterInterface::ENVELOPED); |
|
| 1432 | 3 | $adapter->addTransform(AdapterInterface::XML_C14N); |
|
| 1433 | 3 | $adapter->sign($dom, 'infNFe'); |
|
| 1434 | 3 | return $dom; |
|
| 1435 | } |
||
| 1436 | |||
| 1437 | /** |
||
| 1438 | * Valida o documento após assinar |
||
| 1439 | */ |
||
| 1440 | 1 | public function validar($dom) |
|
| 1441 | { |
||
| 1442 | 1 | $dom->loadXML($dom->saveXML()); |
|
| 1443 | 1 | $xsd_path = __DIR__ . '/schema'; |
|
| 1444 | 1 | if (is_null($this->getProtocolo())) { |
|
| 1445 | 1 | $xsd_file = $xsd_path . '/nfe_v3.10.xsd'; |
|
| 1446 | 1 | } else { |
|
| 1447 | $xsd_file = $xsd_path . '/procNFe_v3.10.xsd'; |
||
| 1448 | } |
||
| 1449 | 1 | if (!file_exists($xsd_file)) { |
|
| 1450 | throw new \Exception('O arquivo "'.$xsd_file.'" de esquema XSD não existe!', 404); |
||
| 1451 | } |
||
| 1452 | // Enable user error handling |
||
| 1453 | 1 | $save = libxml_use_internal_errors(true); |
|
| 1454 | 1 | if ($dom->schemaValidate($xsd_file)) { |
|
| 1455 | 1 | libxml_use_internal_errors($save); |
|
| 1456 | 1 | return $dom; |
|
| 1457 | } |
||
| 1458 | $msg = array(); |
||
| 1459 | $errors = libxml_get_errors(); |
||
| 1460 | foreach ($errors as $error) { |
||
| 1461 | $msg[] = 'Não foi possível validar o XML: '.$error->message; |
||
| 1462 | } |
||
| 1463 | libxml_clear_errors(); |
||
| 1464 | libxml_use_internal_errors($save); |
||
| 1465 | throw new ValidationException($msg); |
||
| 1466 | } |
||
| 1467 | |||
| 1468 | /** |
||
| 1469 | * Adiciona o protocolo no XML da nota |
||
| 1470 | */ |
||
| 1471 | public function addProtocolo($dom) |
||
| 1501 | } |
||
| 1502 |
Really long classes often contain too much logic and violate the single responsibility principle.
We suggest to take a look at the “Code” section for options on how to refactor this code.