예제 #1
0
 /**
  * addB2B
  * Adiciona tags de comunicação B2B, especialmente ANFAVEA
  * @param string $pathNFefile
  * @param string $pathB2Bfile
  * @param string $tagB2B
  * @return string
  * @throws Exception\InvalidArgumentException
  * @throws Exception\RuntimeException
  */
 public function addB2B($pathNFefile = '', $pathB2Bfile = '', $tagB2B = '')
 {
     if (!is_file($pathNFefile) || !is_file($pathB2Bfile)) {
         $msg = "Algum dos arquivos não foi localizado no caminho indicado ! {$pathNFefile} ou {$pathB2Bfile}";
         throw new Exception\InvalidArgumentException($msg);
     }
     if ($tagB2B == '') {
         //padrão anfavea
         $tagB2B = 'NFeB2BFin';
     }
     $docnfe = new Dom();
     $docnfe->loadXMLFile($pathNFefile);
     $nodenfe = $docnfe->getNode('nfeProc', 0);
     if ($nodenfe == '') {
         $msg = "O arquivo indicado como NFe não está protocolado ou não é uma NFe!!";
         throw new Exception\RuntimeException($msg);
     }
     //carrega o arquivo B2B
     $docb2b = new Dom();
     $docb2b->loadXMLFile($pathNFefile);
     $nodeb2b = $docnfe->getNode($tagB2B, 0);
     if ($nodeb2b == '') {
         $msg = "O arquivo indicado como B2B não contên a tag requerida!!";
         throw new Exception\RuntimeException($msg);
     }
     //cria a NFe processada com a tag do protocolo
     $procb2b = new Dom();
     //cria a tag nfeProc
     $nfeProcB2B = $procb2b->createElement('nfeProcB2B');
     $procb2b->appendChild($nfeProcB2B);
     //inclui a tag NFe
     $node1 = $procb2b->importNode($nodenfe, true);
     $nfeProcB2B->appendChild($node1);
     //inclui a tag NFeB2BFin
     $node2 = $procb2b->importNode($nodeb2b, true);
     $nfeProcB2B->appendChild($node2);
     //salva o xml como string em uma variável
     $nfeb2bXML = $procb2b->saveXML();
     //remove as informações indesejadas
     $nfeb2bXMLString = str_replace(array("\n", "\r", "\\s"), '', $nfeb2bXML);
     return (string) $nfeb2bXMLString;
 }