/** * checkForFault * Verifica se a mensagem de retorno é uma FAULT * Normalmente essas falhas ocorrem devido a falhas internas * nos servidores da SEFAZ * @param NFePHP\Common\Dom\Dom $dom * @return string */ protected static function checkForFault($dom) { $fault = $dom->getElementsByTagName('Fault')->item(0); $reason = ''; if (isset($fault)) { $reason = $fault->getElementsByTagName('Text')->item(0)->nodeValue; } return $reason; }
/** * zPutQRTag * Monta a URI para o QRCode e coloca a tag * no xml já assinado * @param Dom $dom * @return string * NOTA: O Campo QRCode está habilitado para uso a partir de * 01/10/2015 homologação * 03/11/2015 Produção */ protected function zPutQRTag(Dom $dom, $saveFile) { //pega os dados necessários para a montagem da URI a partir do xml $nfe = $dom->getNode('NFe'); $ide = $dom->getNode('ide'); $dest = $dom->getNode('dest'); $icmsTot = $dom->getNode('ICMSTot'); $signedInfo = $dom->getNode('SignedInfo'); $chNFe = $dom->getChave('infNFe'); $cUF = $dom->getValue($ide, 'cUF'); $tpAmb = $dom->getValue($ide, 'tpAmb'); $dhEmi = $dom->getValue($ide, 'dhEmi'); $cDest = ''; if (!empty($dest)) { //pode ser CNPJ , CPF ou idEstrageiro $cDest = $dom->getValue($dest, 'CNPJ'); if ($cDest == '') { $cDest = $dom->getValue($dest, 'CPF'); if ($cDest == '') { $cDest = $dom->getValue($dest, 'idEstrangeiro'); } } } $vNF = $dom->getValue($icmsTot, 'vNF'); $vICMS = $dom->getValue($icmsTot, 'vICMS'); $digVal = $dom->getValue($signedInfo, 'DigestValue'); $token = $this->aConfig['tokenNFCe']; $idToken = $this->aConfig['tokenNFCeId']; $versao = '100'; //pega a URL para consulta do QRCode do estado emissor //essa url está em nfe_ws3_mode65.xml //carrega serviço $servico = 'NfeConsultaQR'; $siglaUF = $this->zGetSigla($cUF); $this->zLoadServico('nfe', $servico, $siglaUF, $tpAmb); if ($this->urlService == '') { $this->errors[] = "A consulta por QRCode não está disponível na SEFAZ {$siglaUF}!!!"; return $dom->saveXML(); } $url = $this->urlService; //usa a função zMakeQRCode para gerar a string da URI $qrcode = $this->zMakeQRCode($chNFe, $url, $tpAmb, $dhEmi, $vNF, $vICMS, $digVal, $token, $cDest, $idToken, $versao); if ($qrcode == '') { return $dom->saveXML(); } //inclui a TAG NFe/infNFeSupl com o qrcode $infNFeSupl = $dom->createElement("infNFeSupl"); $nodeqr = $infNFeSupl->appendChild($dom->createElement('qrCode')); $nodeqr->appendChild($dom->createCDATASection($qrcode)); $signature = $dom->getElementsByTagName('Signature')->item(0); $nfe->insertBefore($infNFeSupl, $signature); $dom->formatOutput = false; $xmlSigned = $dom->saveXML(); //salva novamente o xml assinado e agora com o QRCode if ($saveFile) { $anomes = date('Ym', DateTime::convertSefazTimeToTimestamp($dhEmi)); $filename = "{$chNFe}-nfe.xml"; $this->zGravaFile('nfe', $tpAmb, $filename, $xmlSigned, 'assinadas', $anomes); } //retorna a string com o xml assinado e com o QRCode return $xmlSigned; }
/** * signXML * @param string $docxml * @param string $tagid * @return string xml assinado * @throws Exception\InvalidArgumentException * @throws Exception\RuntimeException */ public function signXML($docxml, $tagid = '') { //caso não tenha as chaves cai fora if ($this->pubKey == '' || $this->priKey == '') { $msg = "As chaves não estão disponíveis."; throw new Exception\InvalidArgumentException($msg); } //caso não seja informada a taga a ser assinada cai fora if ($tagid == '') { $msg = "A tag a ser assinada deve ser indicada."; throw new Exception\InvalidArgumentException($msg); } //carrega a chave privada no openssl $objSSLPriKey = openssl_get_privatekey($this->priKey); if ($objSSLPriKey === false) { $msg = "Houve erro no carregamento da chave privada."; $this->zGetOpenSSLError($msg); //while ($erro = openssl_error_string()) { // $msg .= $erro . "\n"; //} //throw new Exception\RuntimeException($msg); } $xml = $docxml; if (is_file($docxml)) { $xml = file_get_contents($docxml); } //remove sujeiras do xml $order = array("\r\n", "\n", "\r", "\t"); $xml = str_replace($order, '', $xml); $xmldoc = new Dom(); $xmldoc->loadXMLString($xml); //coloca o node raiz em uma variável $root = $xmldoc->documentElement; //extrair a tag com os dados a serem assinados $node = $xmldoc->getElementsByTagName($tagid)->item(0); if (!isset($node)) { throw new Exception\RuntimeException("A tag < {$tagid} > não existe no XML!!"); } $this->docId = $node->getAttribute('Id'); $xmlResp = $xml; if (!$this->zSignatureExists($xmldoc)) { //executa a assinatura $xmlResp = $this->zSignXML($xmldoc, $root, $node, $objSSLPriKey); } //libera a chave privada openssl_free_key($objSSLPriKey); return $xmlResp; }
/** * assinaDoc * @param string $xml * @param string $tipo nfe, cte, ou mdfe * @param string $tag Nome da tag a ser assinada * @param boolean $saveFile APENAS para salvar NFe, CTe ou MDFe * @return string * @throws Exception\InvalidArgumentException * @throws Exception\RuntimeException */ public function assinaDoc($xml = '', $tipo = '', $tag = '', $saveFile = false) { if ($tag == '') { $msg = 'Deve ser indicada uma tag a ser assinada'; throw new Exception\InvalidArgumentException($msg); } if (is_file($xml)) { $xml = Files\FilesFolders::readFile($xml); } $sxml = $this->oCertificate->signXML($xml, $tag); $dom = new Dom(); $dom->loadXMLString($sxml); //$versao = $dom->getElementsByTagName($tag)->item(0)->getAttribute('versao'); //if (! $this->zValidMessage($sxml, $tipo, $versao)) { //$msg = "Falha na validação do $tipo. ".$this->error; // throw new Exception\RuntimeException($msg); //} if ($saveFile && $tipo != '') { $dom = new Dom(); $dom->loadXMLString($sxml); $tpAmb = $dom->getElementsByTagName('tpAmb')->item(0)->nodeValue; $anomes = date('Ym', DateTime::convertSefazTimeToTimestamp($dom->getElementsByTagName('dhEmi')->item(0)->nodeValue)); $chave = $dom->getChave($tag); $filename = "{$chave}-{$tipo}.xml"; $this->zGravaFile($tipo, $tpAmb, $filename, $sxml, 'assinadas', $anomes); } return $sxml; }
/** * addCancelamento * Adiciona a tga de cancelamento a uma MDFe já autorizada * NOTA: não é requisito da SEFAZ, mas auxilia na identificação das MDFe que foram canceladas * @param string $pathMDFefile * @param string $pathCancfile * @param bool $saveFile * @return string * @throws Exception\RuntimeException */ public function addCancelamento($pathMDFefile = '', $pathCancfile = '', $saveFile = false) { $procXML = ''; //carrega a NFe $docmdfe = new Dom(); $docmdfe->loadXMLFile($pathMDFefile); $nodemdfe = $docmdfe->getNode('MDFe', 0); if ($nodemdfe == '') { $msg = "O arquivo indicado como MDFe não é um xml de MDFe!"; throw new Exception\RuntimeException($msg); } $proMDFe = $docmdfe->getNode('protMDFe'); if ($proMDFe == '') { $msg = "O MDFe não está protocolado ainda!!"; throw new Exception\RuntimeException($msg); } $chaveMDFe = $proMDFe->getElementsByTagName('chMDFe')->item(0)->nodeValue; //$nProtNFe = $proNFe->getElementsByTagName('nProt')->item(0)->nodeValue; $tpAmb = $docmdfe->getNodeValue('tpAmb'); $anomes = date('Ym', DateTime::convertSefazTimeToTimestamp($docmdfe->getNodeValue('dhEmi'))); //carrega o cancelamento //pode ser um evento ou resultado de uma consulta com multiplos eventos $doccanc = new Dom(); $doccanc->loadXMLFile($pathCancfile); $eventos = $doccanc->getElementsByTagName('infEvento'); foreach ($eventos as $evento) { //evento $cStat = $evento->getElementsByTagName('cStat')->item(0)->nodeValue; $tpAmb = $evento->getElementsByTagName('tpAmb')->item(0)->nodeValue; $chaveEvento = $evento->getElementsByTagName('chNFe')->item(0)->nodeValue; $tpEvento = $evento->getElementsByTagName('tpEvento')->item(0)->nodeValue; //$nProtEvento = $evento->getElementsByTagName('nProt')->item(0)->nodeValue; //verifica se conferem os dados //cStat = 135 ==> evento homologado //tpEvento = 110111 ==> Cancelamento //chave do evento == chave da NFe //protocolo do evneto == protocolo da NFe if ($cStat == '135' && $tpEvento == '110111' && $chaveEvento == $chaveMDFe) { $proMDFe->getElementsByTagName('cStat')->item(0)->nodeValue = '101'; $proMDFe->getElementsByTagName('xMotivo')->item(0)->nodeValue = 'Cancelamento de NF-e homologado'; $procXML = $docmdfe->saveXML(); //remove as informações indesejadas $procXML = Strings::clearProt($procXML); if ($saveFile) { $filename = "{$chaveMDFe}-protMDFe.xml"; $this->zGravaFile('mdfe', $tpAmb, $filename, $procXML, 'enviadas' . DIRECTORY_SEPARATOR . 'aprovadas', $anomes); } break; } } return (string) $procXML; }
public static function extrai($aList, $cnpj = '') { $aResp = array(); $totFat = 0; $totPeso = 0; $totIcms = 0; foreach ($aList as $file) { $dom = null; $ide = null; $emit = null; $dest = null; try { $dom = new Dom(); $dom->loadXMLFile($file); $ide = $dom->getNode('ide'); $emit = $dom->getNode('emit'); $dest = $dom->getNode('dest'); $icmsTot = $dom->getNode('ICMSTot'); $vol = $dom->getNode('vol'); $cStat = $dom->getNodeValue('cStat'); if ($cStat != '100') { self::$nCanc++; } $dhEmi = $dom->getValue($ide, 'dhEmi'); if (empty($dhEmi)) { $dhEmi = $dom->getValue($ide, 'dEmi'); } //echo $file.'___'.$dhEmi.'<br>'; $tsEmi = DateTime::convertSefazTimeToTimestamp($dhEmi); $data = ''; if (is_numeric($tsEmi)) { $data = date('d/m/Y', $tsEmi); } $emitCNPJ = $dom->getValue($emit, 'CNPJ'); $emitRazao = $dom->getValue($emit, 'xNome'); $destRazao = $dom->getValue($dest, 'xNome'); $vNF = $dom->getValue($icmsTot, 'vNF'); $vNFtext = $vNF; if (is_numeric($vNF)) { $vNFtext = 'R$ ' . number_format($vNF, '2', ',', '.'); } $serie = $dom->getNodeValue('serie'); $nProt = $dom->getNodeValue('nProt'); $nome = $emitRazao; if ($emitCNPJ == $cnpj) { $nome = $destRazao; } $email = $dom->getValue($dest, 'email'); $aObscont = $dom->getElementsByTagName('obsCont'); if (count($aObscont) > 0) { foreach ($aObscont as $obsCont) { $xCampo = $obsCont->getAttribute('xCampo'); if ($xCampo == 'email') { $email .= ";" . $dom->getValue($obsCont, 'xTexto'); } } } if (substr($email, 0, 1) == ';') { $email = substr($email, 1, strlen($email) - 1); } $vICMS = $dom->getValue($icmsTot, 'vICMS'); $totIcms += $vICMS; $valorFat = 0; if ($vICMS != 0 && $cStat == '100') { $valorFat = $vNF; } $totFat += $valorFat; $pesoL = $dom->getValue($vol, 'pesoL'); if ($pesoL != '') { $totPeso += $pesoL; } $aResp[] = array('nNF' => $dom->getValue($ide, 'nNF'), 'serie' => $serie, 'data' => $data, 'nome' => $nome, 'natureza' => $dom->getValue($ide, 'natOp'), 'cStat' => $cStat, 'vNF' => $vNFtext, 'nProt' => $nProt, 'email' => $email); } catch (RuntimeException $e) { $aResp[] = array('nNF' => '000000', 'serie' => '000', 'data' => '000', 'nome' => 'FALHA', 'natureza' => "{$file}", 'cStat' => '', 'vNF' => 0, 'nProt' => '', 'email' => ''); } } return array('totFat' => $totFat, 'totPeso' => $totPeso, 'totIcms' => $totIcms, 'aNF' => $aResp); }
/** * zTestaChaveXML * Remonta a chave da NFe de 44 digitos com base em seus dados * Isso é útil no caso da chave informada estar errada * se a chave estiver errada a mesma é substituida * * @param Dom $dom */ private function zTestaChaveXML($dom) { $infNFe = $dom->getElementsByTagName("infNFe")->item(0); $ide = $dom->getElementsByTagName("ide")->item(0); $emit = $dom->getElementsByTagName("emit")->item(0); $cUF = $ide->getElementsByTagName('cUF')->item(0)->nodeValue; $dhEmi = $ide->getElementsByTagName('dhEmi')->item(0)->nodeValue; $cnpj = $emit->getElementsByTagName('CNPJ')->item(0)->nodeValue; $mod = $ide->getElementsByTagName('mod')->item(0)->nodeValue; $serie = $ide->getElementsByTagName('serie')->item(0)->nodeValue; $nNF = $ide->getElementsByTagName('nNF')->item(0)->nodeValue; $tpEmis = $ide->getElementsByTagName('tpEmis')->item(0)->nodeValue; $cNF = $ide->getElementsByTagName('cNF')->item(0)->nodeValue; $chave = str_replace('NFe', '', $infNFe->getAttribute("Id")); $tempData = explode("-", $dhEmi); $chaveMontada = $this->montaChave($cUF, $tempData[0] - 2000, $tempData[1], $cnpj, $mod, $serie, $nNF, $tpEmis, $cNF); //caso a chave contida na NFe esteja errada //substituir a chave if ($chaveMontada != $chave) { $ide->getElementsByTagName('cDV')->item(0)->nodeValue = substr($chaveMontada, -1); $infNFe = $dom->getElementsByTagName("infNFe")->item(0); $infNFe->setAttribute("Id", "NFe" . $chaveMontada); $this->chNFe = $chaveMontada; } }
/** * anfavea * Função para transformar o campo cdata do padrão ANFAVEA para * texto imprimível * * @param string $cdata campo CDATA * @return string conteúdo do campo CDATA como string */ private function pAnfavea($cdata = '') { if ($cdata == '') { return ''; } //remove qualquer texto antes ou depois da tag CDATA $cdata = str_replace('<![CDATA[', '<CDATA>', $cdata); $cdata = str_replace(']]>', '</CDATA>', $cdata); $cdata = preg_replace('/\\s\\s+/', ' ', $cdata); $cdata = str_replace("> <", "><", $cdata); $len = strlen($cdata); $startPos = strpos($cdata, '<'); if ($startPos === false) { return $cdata; } for ($x = $len; $x > 0; $x--) { if (substr($cdata, $x, 1) == '>') { $endPos = $x; break; } } if ($startPos > 0) { $parte1 = substr($cdata, 0, $startPos); } else { $parte1 = ''; } $parte2 = substr($cdata, $startPos, $endPos - $startPos + 1); if ($endPos < $len) { $parte3 = substr($cdata, $endPos + 1, $len - $endPos - 1); } else { $parte3 = ''; } $texto = trim($parte1) . ' ' . trim($parte3); if (strpos($parte2, '<CDATA>') === false) { $cdata = '<CDATA>' . $parte2 . '</CDATA>'; } else { $cdata = $parte2; } //carrega o xml CDATA em um objeto DOM $dom = new Dom(); $dom->loadXML($cdata, LIBXML_NOBLANKS | LIBXML_NOEMPTYTAG); //$xml = $dom->saveXML(); //grupo CDATA infADprod $id = $dom->getElementsByTagName('id')->item(0); $div = $dom->getElementsByTagName('div')->item(0); $entg = $dom->getElementsByTagName('entg')->item(0); $dest = $dom->getElementsByTagName('dest')->item(0); $ctl = $dom->getElementsByTagName('ctl')->item(0); $ref = $dom->getElementsByTagName('ref')->item(0); if (isset($id)) { if ($id->hasAttributes()) { foreach ($id->attributes as $attr) { $name = $attr->nodeName; $value = $attr->nodeValue; $texto .= " {$name} : {$value}"; } } } if (isset($div)) { if ($div->hasAttributes()) { foreach ($div->attributes as $attr) { $name = $attr->nodeName; $value = $attr->nodeValue; $texto .= " {$name} : {$value}"; } } } if (isset($entg)) { if ($entg->hasAttributes()) { foreach ($entg->attributes as $attr) { $name = $attr->nodeName; $value = $attr->nodeValue; $texto .= " {$name} : {$value}"; } } } if (isset($dest)) { if ($dest->hasAttributes()) { foreach ($dest->attributes as $attr) { $name = $attr->nodeName; $value = $attr->nodeValue; $texto .= " {$name} : {$value}"; } } } if (isset($ctl)) { if ($ctl->hasAttributes()) { foreach ($ctl->attributes as $attr) { $name = $attr->nodeName; $value = $attr->nodeValue; $texto .= " {$name} : {$value}"; } } } if (isset($ref)) { if ($ref->hasAttributes()) { foreach ($ref->attributes as $attr) { $name = $attr->nodeName; $value = $attr->nodeValue; $texto .= " {$name} : {$value}"; } } } //grupo CADATA infCpl $t = $dom->getElementsByTagName('transmissor')->item(0); $r = $dom->getElementsByTagName('receptor')->item(0); $versao = !empty($dom->getElementsByTagName('versao')->item(0)->nodeValue) ? 'Versao:' . $dom->getElementsByTagName('versao')->item(0)->nodeValue . ' ' : ''; $especieNF = !empty($dom->getElementsByTagName('especieNF')->item(0)->nodeValue) ? 'Especie:' . $dom->getElementsByTagName('especieNF')->item(0)->nodeValue . ' ' : ''; $fabEntrega = !empty($dom->getElementsByTagName('fabEntrega')->item(0)->nodeValue) ? 'Entrega:' . $dom->getElementsByTagName('fabEntrega')->item(0)->nodeValue . ' ' : ''; $dca = !empty($dom->getElementsByTagName('dca')->item(0)->nodeValue) ? 'dca:' . $dom->getElementsByTagName('dca')->item(0)->nodeValue . ' ' : ''; $texto .= "" . $versao . $especieNF . $fabEntrega . $dca; if (isset($t)) { if ($t->hasAttributes()) { $texto .= " Transmissor "; foreach ($t->attributes as $attr) { $name = $attr->nodeName; $value = $attr->nodeValue; $texto .= " {$name} : {$value}"; } } } if (isset($r)) { if ($r->hasAttributes()) { $texto .= " Receptor "; foreach ($r->attributes as $attr) { $name = $attr->nodeName; $value = $attr->nodeValue; $texto .= " {$name} : {$value}"; } } } return $texto; }
/** * addCancelamento * Adiciona a tga de cancelamento a uma CTe já autorizada * NOTA: não é requisito da SEFAZ, mas auxilia na identificação das CTe que foram canceladas * @param string $pathCTefile * @param string $pathCancfile * @param bool $saveFile * @return string * @throws Exception\RuntimeException */ public function addCancelamento($pathCTefile = '', $pathCancfile = '', $saveFile = false) { $procXML = ''; //carrega a CTe $docCTe = new Dom(); if (file_exists($pathCTefile)) { //carrega o XML pelo caminho do arquivo informado $docCTe->loadXMLFile($pathCTefile); } else { //carrega o XML pelo conteúdo $docCTe->loadXMLString($pathCTefile); } $nodeCTe = $docCTe->getNode('CTe', 0); if ($nodeCTe == '') { $msg = "O arquivo indicado como CTe não é um xml de CTe!"; throw new Exception\RuntimeException($msg); } $proCTe = $docCTe->getNode('protCTe'); if ($proCTe == '') { $msg = "A CTe não está protocolada ainda!!"; throw new Exception\RuntimeException($msg); } $chaveCTe = $proCTe->getElementsByTagName('chCTe')->item(0)->nodeValue; //$nProtCTe = $proCTe->getElementsByTagName('nProt')->item(0)->nodeValue; $tpAmb = $docCTe->getNodeValue('tpAmb'); $anomes = date('Ym', DateTime::convertSefazTimeToTimestamp($docCTe->getNodeValue('dhEmi'))); //carrega o cancelamento //pode ser um evento ou resultado de uma consulta com multiplos eventos $doccanc = new Dom(); if (file_exists($pathCancfile)) { //carrega o XML pelo caminho do arquivo informado $doccanc->loadXMLFile($pathCancfile); } else { //carrega o XML pelo conteúdo $doccanc->loadXMLString($pathCancfile); } $retEvento = $doccanc->getElementsByTagName('retEventoCTe')->item(0); $eventos = $retEvento->getElementsByTagName('infEvento'); foreach ($eventos as $evento) { //evento $cStat = $evento->getElementsByTagName('cStat')->item(0)->nodeValue; $tpAmb = $evento->getElementsByTagName('tpAmb')->item(0)->nodeValue; $chaveEvento = $evento->getElementsByTagName('chCTe')->item(0)->nodeValue; $tpEvento = $evento->getElementsByTagName('tpEvento')->item(0)->nodeValue; //$nProtEvento = $evento->getElementsByTagName('nProt')->item(0)->nodeValue; //verifica se conferem os dados //cStat = 135 ==> evento homologado //cStat = 136 ==> vinculação do evento à respectiva NF-e prejudicada //cStat = 155 ==> Cancelamento homologado fora de prazo //tpEvento = 110111 ==> Cancelamento //chave do evento == chave da CTe //protocolo do evneto == protocolo da CTe if (($cStat == '135' || $cStat == '136' || $cStat == '155') && $tpEvento == '110111' && $chaveEvento == $chaveCTe) { $proCTe->getElementsByTagName('cStat')->item(0)->nodeValue = '101'; $proCTe->getElementsByTagName('xMotivo')->item(0)->nodeValue = 'Cancelamento de CT-e homologado'; $procXML = $docCTe->saveXML(); //remove as informações indesejadas $procXML = Strings::clearProt($procXML); if ($saveFile) { $filename = "{$chaveCTe}-protCTe.xml"; $this->zGravaFile('cte', $tpAmb, $filename, $procXML, 'canceladas', $anomes); } break; } } return (string) $procXML; }