From 92063a61b0b87dcff0243fa2a0fbc80ee9b1d489 Mon Sep 17 00:00:00 2001 From: Mathias Schulz Date: Wed, 11 Sep 2024 10:45:48 -0300 Subject: [PATCH 1/2] =?UTF-8?q?fix(danfe,sonar):=20ajusta=20quebra=20de=20?= =?UTF-8?q?linha=20para=20n=C3=A3o=20interferir=20nas=20valida=C3=A7=C3=B5?= =?UTF-8?q?es=20da=20info=20compl?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/NFe/Danfe.php | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/NFe/Danfe.php b/src/NFe/Danfe.php index b959f064..3ec79fc8 100644 --- a/src/NFe/Danfe.php +++ b/src/NFe/Danfe.php @@ -357,14 +357,6 @@ public function __construct( //se for passado o xml if (!empty($this->xml)) { $this->dom = new Dom(); - - // Remove caracteres não imprimíveis que podem quebrar a leitura do XML - @author Mathias Artur Schulz (20-06-2024) - // Tabela de conversão ASCII, decimal, hexadecimal, octal e binário: - // https://www.ibm.com/docs/pt-br/aix/7.3?topic=adapters-ascii-decimal-hexadecimal-octal-binary-conversion-table - // A linha abaixo ignora qualquer caractere com valor ASCII entre 0 e 31 (caracteres não imprimíveis) ou o caractere com valor ASCII 127 (DEL) - // O modificador 'u' indica que estamos tratando o xml como UTF-8 - $this->xml = preg_replace('/[\x00-\x1F\x7F]/u', '', $this->xml); - $this->dom->loadXML($this->xml); $this->nfeProc = $this->dom->getElementsByTagName("nfeProc")->item(0); $this->infNFe = $this->dom->getElementsByTagName("infNFe")->item(0); @@ -612,9 +604,9 @@ public function montaDANFE( if ($this->textoAdic != '') { $this->textoAdic .= ". \r\n"; } - $this->textoAdic .= !empty($this->getTagValue($this->infAdic, "infCpl")) ? - 'Inf. Contribuinte: ' . - $this->pAnfavea($this->getTagValue($this->infAdic, "infCpl")) : ''; + $this->textoAdic .= !empty($this->getTagValue($this->infAdic, "infCpl")) + ? 'Inf. Contribuinte: ' . $this->pAnfavea($this->getTagValue($this->infAdic, "infCpl")) + : ''; $infPedido = $this->pGeraInformacoesDaTagCompra(); if ($infPedido != "") { $this->textoAdic .= $infPedido; @@ -824,12 +816,18 @@ protected function pAnfavea($cdata = '') if ($cdata == '') { return ''; } + + // ajusta a quebra de linha para não interferir nas validações das informações complementares + $cdata = str_replace('
', "\n", $cdata); + $cdata = str_replace('
', "\n", $cdata); + //remove qualquer texto antes ou depois da tag CDATA $cdata = str_replace('', $cdata); $cdata = str_replace(']]>', '', $cdata); $cdata = preg_replace('/\s\s+/', ' ', $cdata); $cdata = str_replace("> <", "><", $cdata); $len = strlen($cdata); + $startPos = strpos($cdata, '<'); if ($startPos === false) { return $cdata; @@ -851,17 +849,20 @@ protected function pAnfavea($cdata = '') } else { $parte3 = ''; } + $texto = trim($parte1) . ' ' . trim($parte3); + if (strpos($parte2, '') === false) { $cdata = '' . $parte2 . ''; } else { $cdata = $parte2; } + //Retira a tag (caso existir) pois não é uma estrutura válida XML $cdata = str_replace('', '', $cdata); //carrega o xml CDATA em um objeto DOM $dom = new Dom(); - $dom->loadXML($cdata, LIBXML_NOBLANKS | LIBXML_NOEMPTYTAG); + @$dom->loadXML($cdata, LIBXML_NOBLANKS | LIBXML_NOEMPTYTAG); //$xml = $dom->saveXML(); //grupo CDATA infADprod $id = $dom->getElementsByTagName('id')->item(0); @@ -3085,6 +3086,7 @@ protected function pDadosAdicionaisDANFE($x, $y, $h) } $aFont = array('font' => $this->fontePadrao, 'size' => 7, 'style' => 'B'); $this->pTextBox($x, $y, $w, 8, $texto, $aFont, 'T', 'L', 0, ''); + //INFORMAÇÕES COMPLEMENTARES $texto = "INFORMAÇÕES COMPLEMENTARES"; $y += 3; @@ -3098,6 +3100,7 @@ protected function pDadosAdicionaisDANFE($x, $y, $h) $y += 1; $aFont = array('font' => $this->fontePadrao, 'size' => 7, 'style' => ''); $this->pTextBox($x, $y + 2, $w - 2, $h - 3, $this->textoAdic, $aFont, 'T', 'L', 0, '', false); + //RESERVADO AO FISCO $texto = "RESERVADO AO FISCO"; $x += $w; From 5e3742b3465790bdb0fdaa63bff91e392016c314 Mon Sep 17 00:00:00 2001 From: Mathias Schulz Date: Wed, 11 Sep 2024 10:53:51 -0300 Subject: [PATCH 2/2] =?UTF-8?q?revert:=20reverte=20c=C3=B3digo=20removido?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/NFe/Danfe.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/NFe/Danfe.php b/src/NFe/Danfe.php index 3ec79fc8..cc911964 100644 --- a/src/NFe/Danfe.php +++ b/src/NFe/Danfe.php @@ -357,6 +357,14 @@ public function __construct( //se for passado o xml if (!empty($this->xml)) { $this->dom = new Dom(); + + // Remove caracteres não imprimíveis que podem quebrar a leitura do XML - @author Mathias Artur Schulz (20-06-2024) + // Tabela de conversão ASCII, decimal, hexadecimal, octal e binário: + // https://www.ibm.com/docs/pt-br/aix/7.3?topic=adapters-ascii-decimal-hexadecimal-octal-binary-conversion-table + // A linha abaixo ignora qualquer caractere com valor ASCII entre 0 e 31 (caracteres não imprimíveis) ou o caractere com valor ASCII 127 (DEL) + // O modificador 'u' indica que estamos tratando o xml como UTF-8 + $this->xml = preg_replace('/[\x00-\x1F\x7F]/u', '', $this->xml); + $this->dom->loadXML($this->xml); $this->nfeProc = $this->dom->getElementsByTagName("nfeProc")->item(0); $this->infNFe = $this->dom->getElementsByTagName("infNFe")->item(0);