public function addProtocolo($pathNFefile = '', $pathProtfile = '', $saveFile = false)
{
//carrega a NFe
$docnfe = new Dom();
if (file_exists($pathNFefile)) {
//carrega o XML pelo caminho do arquivo informado
$docnfe->loadXMLFile($pathNFefile);
} else {
//carrega o XML pelo conteúdo
$docnfe->loadXMLString($pathNFefile);
}
$nodenfe = $docnfe->getNode('NFe', 0);
if ($nodenfe == '') {
$msg = "O arquivo indicado como NFe não é um xml de NFe!";
throw new Exception\RuntimeException($msg);
}
if ($docnfe->getNode('Signature') == '') {
$msg = "A NFe não está assinada!";
throw new Exception\RuntimeException($msg);
}
//carrega o protocolo
$docprot = new Dom();
if (file_exists($pathProtfile)) {
//carrega o XML pelo caminho do arquivo informado
$docprot->loadXMLFile($pathProtfile);
} else {
//carrega o XML pelo conteúdo
$docprot->loadXMLString($pathProtfile);
}
$nodeprots = $docprot->getElementsByTagName('protNFe');
if ($nodeprots->length == 0) {
$msg = "O arquivo indicado não contem um protocolo de autorização!";
throw new Exception\RuntimeException($msg);
}
//carrega dados da NFe
$tpAmb = $docnfe->getNodeValue('tpAmb');
$anomes = date('Ym', DateTime::convertSefazTimeToTimestamp($docnfe->getNodeValue('dhEmi')));
$infNFe = $docnfe->getNode("infNFe", 0);
$versao = $infNFe->getAttribute("versao");
$chaveId = $infNFe->getAttribute("Id");
$chaveNFe = preg_replace('/[^0-9]/', '', $chaveId);
$digValueNFe = $docnfe->getNodeValue('DigestValue');
//carrega os dados do protocolo
for ($i = 0; $i < $nodeprots->length; $i++) {
$nodeprot = $nodeprots->item($i);
$protver = $nodeprot->getAttribute("versao");
$chaveProt = $nodeprot->getElementsByTagName("chNFe")->item(0)->nodeValue;
$digValueProt = $nodeprot->getElementsByTagName("digVal")->length ? $nodeprot->getElementsByTagName("digVal")->item(0)->nodeValue : '';
$infProt = $nodeprot->getElementsByTagName("infProt")->item(0);
if ($digValueNFe == $digValueProt && $chaveNFe == $chaveProt) {
break;
}
}
if ($digValueNFe != $digValueProt) {
$msg = "Inconsistência! O DigestValue da NFe não combina com o do digVal do protocolo indicado!";
throw new Exception\RuntimeException($msg);
}
if ($chaveNFe != $chaveProt) {
$msg = "O protocolo indicado pertence a outra NFe. Os números das chaves não combinam !";
throw new Exception\RuntimeException($msg);
}
//cria a NFe processada com a tag do protocolo
$procnfe = new \DOMDocument('1.0', 'utf-8');
$procnfe->formatOutput = false;
$procnfe->preserveWhiteSpace = false;
//cria a tag nfeProc
$nfeProc = $procnfe->createElement('nfeProc');
$procnfe->appendChild($nfeProc);
//estabele o atributo de versão
$nfeProcAtt1 = $nfeProc->appendChild($procnfe->createAttribute('versao'));
$nfeProcAtt1->appendChild($procnfe->createTextNode($protver));
//estabelece o atributo xmlns
$nfeProcAtt2 = $nfeProc->appendChild($procnfe->createAttribute('xmlns'));
$nfeProcAtt2->appendChild($procnfe->createTextNode($this->urlPortal));
//inclui a tag NFe
$node = $procnfe->importNode($nodenfe, true);
$nfeProc->appendChild($node);
//cria tag protNFe
$protNFe = $procnfe->createElement('protNFe');
$nfeProc->appendChild($protNFe);
//estabele o atributo de versão
$protNFeAtt1 = $protNFe->appendChild($procnfe->createAttribute('versao'));
$protNFeAtt1->appendChild($procnfe->createTextNode($versao));
//cria tag infProt
$nodep = $procnfe->importNode($infProt, true);
$protNFe->appendChild($nodep);
//salva o xml como string em uma variável
$procXML = $procnfe->saveXML();
//remove as informações indesejadas
$procXML = Strings::clearProt($procXML);
if ($saveFile) {
$filename = "{$chaveNFe}-protNFe.xml";
$this->zGravaFile('nfe', $tpAmb, $filename, $procXML, 'enviadas' . DIRECTORY_SEPARATOR . 'aprovadas', $anomes);
}
return $procXML;
}