Пример #1
0
 /**
  * o objetivo desta função é ler o XML e gerar o DANFE NFC-e com auxilio de conversão HTML-PDF
  * @param boolean $ecoNFCe false = Não (NFC-e Completa); true = Sim (NFC-e Simplificada)
  * @return string
  */
 public function montaDANFCE($ecoNFCe = true)
 {
     $toolsNFe = new ToolsNFe('../../config/config.json');
     //DADOS DA NF
     $dhRecbto = $nProt = '';
     if (isset($this->nfeProc)) {
         $nProt = $this->pSimpleGetValue($this->nfeProc, "nProt");
         $dhRecbto = $this->pSimpleGetValue($this->nfeProc, "dhRecbto");
     }
     $digVal = $this->pSimpleGetValue($this->nfe, "DigestValue");
     $chNFe = str_replace('NFe', '', $this->infNFe->getAttribute("Id"));
     $chNFe = $this->pFormat($chNFe, "#### #### #### #### #### #### #### #### #### #### ####");
     $tpAmb = $this->pSimpleGetValue($this->ide, 'tpAmb');
     $tpEmis = $this->pSimpleGetValue($this->ide, 'tpEmis');
     $cUF = $this->pSimpleGetValue($this->ide, 'cUF');
     $nNF = $this->pSimpleGetValue($this->ide, 'nNF');
     $serieNF = str_pad($this->pSimpleGetValue($this->ide, "serie"), 3, "0", STR_PAD_LEFT);
     $dhEmi = $this->pSimpleGetValue($this->ide, "dhEmi");
     $vTotTrib = $this->pSimpleGetValue($this->ICMSTot, "vTotTrib");
     $vICMS = $this->pSimpleGetValue($this->ICMSTot, "vICMS");
     $vProd = $this->pSimpleGetValue($this->ICMSTot, "vProd");
     $vDesc = $this->pSimpleGetValue($this->ICMSTot, "vDesc");
     $vOutro = $this->pSimpleGetValue($this->ICMSTot, "vOutro");
     $vNF = $this->pSimpleGetValue($this->ICMSTot, "vNF");
     $qtdItens = $this->det->length;
     if ($this->urlQR == '') {
         //Busca no XML a URL da Consulta
         $urlQR = $toolsNFe->zGetUrlQR($cUF, $tpAmb);
     } else {
         $urlQR = $this->urlQR;
     }
     //DADOS DO EMITENTE
     if (empty($this->logomarca)) {
         $image = $toolsNFe->aConfig['aDocFormat']->pathLogoNFCe;
         $imageData = base64_encode(file_get_contents($image));
         $this->logomarca = "data: " . mime_content_type($image) . ";base64,{$imageData}";
     }
     $emitRazao = $this->pSimpleGetValue($this->emit, "xNome");
     $emitCnpj = $this->pSimpleGetValue($this->emit, "CNPJ");
     $emitCnpj = $this->pFormat($emitCnpj, "##.###.###/####-##");
     $emitIE = $this->pSimpleGetValue($this->emit, "IE");
     $emitFone = $this->pSimpleGetValue($this->enderEmit, "fone");
     $foneLen = strlen($emitFone);
     if ($foneLen > 0) {
         $fone2 = substr($emitFone, 0, $foneLen - 4);
         $fone1 = substr($emitFone, 0, $foneLen - 8);
         $emitFone = '(' . $fone1 . ') ' . substr($fone2, -4) . '-' . substr($emitFone, -4);
     } else {
         $emitFone = '';
     }
     $emitLgr = $this->pSimpleGetValue($this->enderEmit, "xLgr");
     $emitNro = $this->pSimpleGetValue($this->enderEmit, "nro");
     $emitCpl = $this->pSimpleGetValue($this->enderEmit, "xCpl");
     $emitBairro = $this->pSimpleGetValue($this->enderEmit, "xBairro");
     $emitCEP = $this->pFormat($this->pSimpleGetValue($this->enderEmit, "CEP"), "#####-###");
     $emitMun = $this->pSimpleGetValue($this->enderEmit, "xMun");
     $emitUF = $this->pSimpleGetValue($this->enderEmit, "UF");
     //necessário para QRCode
     $cDest = '';
     if (isset($this->dest)) {
         $considEstrangeiro = !empty($this->dest->getElementsByTagName("idEstrangeiro")->item(0)->nodeValue) ? $this->dest->getElementsByTagName("idEstrangeiro")->item(0)->nodeValue : '';
         $consCPF = !empty($this->dest->getElementsByTagName("CPF")->item(0)->nodeValue) ? $this->dest->getElementsByTagName("CPF")->item(0)->nodeValue : '';
         $consCNPJ = !empty($this->dest->getElementsByTagName("CNPJ")->item(0)->nodeValue) ? $this->dest->getElementsByTagName("CNPJ")->item(0)->nodeValue : '';
         $cDest = $consCPF . $consCNPJ . $considEstrangeiro;
         //documentos do consumidor
     }
     //DADOS PARA QRCODE
     if (!empty($this->qrCode)) {
         $this->imgQRCode = self::imgQR($this->qrCode);
     } else {
         $this->imgQRCode = self::makeQRCode($chNFe, $urlQR, $tpAmb, $cDest, $dhEmi, $vNF, $vICMS, $digVal, $this->idToken, $this->emitToken);
     }
     //FORMATAÇÃO DOS CAMPOS
     $numNF = "Número " . $this->pFormat($nNF, "###.###.###");
     $tsHora = $this->pConvertTime($dhEmi);
     if ($dhRecbto == '') {
         $dhRecbto = $dhEmi;
     }
     $tsProt = $this->pConvertTime($dhRecbto);
     //$valorProdutos = number_format($vProd, 2, ",", ".");
     //$valorTotal = number_format($vNF, 2, ",", ".");
     /* 
      * Leiaute de Impressão DANFE NFC-e em acordo com 
      * Manual Padrões Técnicos do DANFE-NFC-e e QR Code
      * Versão 3.4
      * Outubro 2015
      * Refatorado por: Chinnon Santos - 05/2016
      */
     $this->html = "";
     $this->html .= "<html>\n";
     $this->html .= "<head>\n";
     $this->html .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">";
     $this->html .= $this->css;
     $this->html .= "</head>\n";
     $this->html .= "<body>\n";
     // ***                                                    ***//
     // *** Via Única ou Via do Consumidor em Modo Contigência ***//
     // ***                                                    ***//
     // -- Divisão I - Informações do Cabeçalho
     $this->html .= "<table width=\"100%\">\n";
     $this->html .= "<tr>\n";
     $this->html .= "<td><img src=\"{$this->logomarca}\" width=\"82\" ></td>\n";
     $this->html .= "<td colspan=\"2\">" . htmlspecialchars($emitRazao) . "<br>CNPJ:{$emitCnpj} I.E.:{$emitIE}<br>" . htmlspecialchars($emitLgr . ", nº" . $emitNro) . "<br>" . htmlspecialchars($emitCpl) . "<br>" . htmlspecialchars($emitBairro . ", " . $emitMun . ", " . $emitUF) . "<br>CEP: {$emitCEP} {$emitFone}</td>\n";
     $this->html .= "</tr>\n";
     $this->html .= "</table>\n";
     // -- Divisão II – Identificação do DANFE NFC-e
     $this->html .= "<table width=\"100%\">\n";
     $this->html .= "<tr>\n";
     $this->html .= "<td colspan=\"3\" class=\"tCenter\"><strong>" . htmlspecialchars("DANFE NFC-e - DOCUMENTO AUXILIAR DA NOTA FISCAL DE CONSUMIDOR ELETRÔNICA") . "</strong></td>\n";
     $this->html .= "</tr>\n";
     $this->html .= "</table>\n";
     // -- Divisão III – Informações de detalhes de produtos/serviços
     if (!$ecoNFCe) {
         $this->html .= self::itens($this->det);
     }
     // -- Divisão IV – Informações de Totais do DANFE NFC-e
     $this->html .= "<table width=\"100%\">\n";
     $this->html .= "<tr>\n";
     $this->html .= "<td class=\"tLeft\">Qtde. Total de Itens</td>\n";
     $this->html .= "<td class=\"tRight\">{$qtdItens}</td>\n";
     $this->html .= "</tr>\n";
     $this->html .= "<tr>\n";
     $this->html .= "<td class=\"tLeft\">" . htmlspecialchars('Valor Total R$') . "</td>\n";
     $this->html .= "<td class=\"tRight\">" . number_format($vProd, 2, ',', '.') . "</td>\n";
     $this->html .= "</tr>\n";
     $this->html .= "<tr>\n";
     // Acréscimos (frete, seguro e outras despesas)/Desconto R$ (Exibe somente se houver!)
     $hasAD = false;
     if ($vDesc != '0.00') {
         $this->html .= "<tr>\n";
         $this->html .= "<td class=\"tLeft\">" . htmlspecialchars('Desconto R$') . "</td>\n";
         $this->html .= "<td class=\"tRight\">-" . number_format($vDesc, 2, ',', '.') . "</td>\n";
         $this->html .= "</tr>\n";
         $this->html .= "<tr>\n";
         $hasAD = true;
     }
     if ($vOutro != '0.00') {
         $this->html .= "<tr>\n";
         $this->html .= "<td class=\"tLeft\">" . htmlspecialchars('Acréscimos R$') . "</td>\n";
         $this->html .= "<td class=\"tRight\">" . number_format($vOutro, 2, ',', '.') . "</td>\n";
         $this->html .= "</tr>\n";
         $this->html .= "<tr>\n";
         $hasAD = true;
     }
     // (Total Itens - Descontos + Acréscimos) deve ser impresso apenas se existir acréscimo ou desconto
     if ($hasAD) {
         $this->html .= "<tr>\n";
         $this->html .= "<td class=\"tLeft\">" . htmlspecialchars('Valor a Pagar R$') . "</td>\n";
         $this->html .= "<td class=\"tRight\">" . number_format($vOutro, 2, ',', '.') . "</td>\n";
         $this->html .= "</tr>\n";
     }
     // Formas de Pagamentos
     $this->html .= "<tr>\n";
     $this->html .= "<th class=\"tLeft\">FORMA DE PAGAMENTO</th>\n";
     $this->html .= "<th class=\"tRight\">VALOR PAGO</th>\n";
     $this->html .= "</tr>\n";
     $this->html .= self::pagamento($this->pag);
     $this->html .= "</table>\n";
     // -- Divisão V – Área de Mensagem Fiscal
     $this->html .= "<table width=\"100%\">\n";
     if ($tpEmis != 1) {
         $this->html .= "<tr>\n";
         $this->html .= "<td colspan=\"3\"><strong>" . htmlspecialchars("EMITIDA EM CONTINGÊNCIA") . "</strong></td>\n";
         $this->html .= "</tr>\n";
     } elseif ($tpAmb == 2) {
         $this->html .= "<tr>\n";
         $this->html .= "<td colspan=\"3\"><strong>" . htmlspecialchars("EMITIDA EM AMBIENTE DE HOMOLOGAÇÃO – SEM VALOR FISCAL") . "<strong></td>\n";
         $this->html .= "</tr>\n";
     } elseif (!empty($this->infAdFisco)) {
         $this->html .= "<tr>\n";
         $this->html .= "<td colspan=\"3\"><strong>" . htmlspecialchars("INFORMAÇÕES ADICIONAIS DE INTERESSE DO FISCO") . "<strong></td>\n";
         $this->html .= "<td colspan=\"3\">{$this->infAdFisco}</td>\n" . ($this->html .= "</tr>\n");
     }
     $this->html .= "</table>\n";
     // -- Divisão VI – Informações de Identificação da NFC-e e do Protocolo de Autorização
     $this->html .= "<table width=\"100%\">\n";
     $this->html .= "<tr>\n";
     $this->html .= "<td colspan=\"3\">" . htmlspecialchars($numNF);
     $this->html .= " " . htmlspecialchars('Série: ') . "{$serieNF}";
     if ($tpEmis == 1) {
         $this->html .= " " . htmlspecialchars('Emissão: ') . date('d/m/y H:i:s', $tsHora) . "</td>\n";
     } else {
         $this->html .= " " . htmlspecialchars('Emissão: ') . date('d/m/y H:i:s', $tsHora);
         $this->html .= "<br><strong>Via do Consumidor</strong></td>\n";
     }
     $this->html .= "</tr>\n";
     $this->html .= "<tr>\n";
     $this->html .= "<td colspan=\"3\">Consulte pela Chave de Acesso em <a href=\"{$urlQR}\">{$urlQR}</a></td>\n";
     $this->html .= "</tr>\n";
     $this->html .= "<tr>\n";
     $this->html .= "<td colspan=\"3\">Chave de Acesso<br>{$chNFe}</td>\n";
     $this->html .= "</tr>\n";
     if ($tpEmis == 1) {
         $this->html .= "<tr>\n";
         $this->html .= "<td colspan=\"3\">Protocolo de autorização: {$nProt} - " . date('d/m/y H:i:s', $tsProt) . "</td>\n";
         $this->html .= "</tr>\n";
     }
     $this->html .= "</table>\n";
     // -- Divisão VII – Informações sobre o Consumidor
     $this->html .= self::consumidor($this->dest);
     // -- Divisão VIII – Informações da Consulta via QR Code
     $this->html .= "<table width=\"100%\">\n";
     $this->html .= "<tr>\n";
     $this->html .= "<td colspan=\"3\"><img src=\"{$this->imgQRCode}\" ></td>\n";
     $this->html .= "</tr>\n";
     $this->html .= "</table>\n";
     // -- Divisão IX – Mensagem de Interesse do Contribuinte
     $this->html .= "<table width=\"100%\" class=\"noBorder\">\n";
     $this->html .= "<tr>\n";
     $this->html .= "<td colspan=\"3\" class=\"menor tCenter\"><strong>{$this->infCpl}</strong></td>\n";
     $this->html .= "</tr>\n";
     $this->html .= "</table>\n";
     // ***                                            ***//
     // *** Via do Estabelecimento em Modo Contigência ***//
     // ***                                            ***//
     if ($tpEmis != 1) {
         $html2via = str_replace('Via do Consumidor', 'Via do Estabelecimento', $this->html);
         $this->html .= "<br><hr><br>\n";
         $this->html .= $html2via;
     }
     $this->html .= "</body>\n</html>\n";
     return $chNFe;
 }