/** * verificaValidade * Verifica a validade de uma NFe recebida * @param string $pathXmlFile * @param array $aRetorno * @return boolean * @throws Exception\InvalidArgumentException */ public function verificaValidade($pathXmlFile = '', &$aRetorno = array()) { $aRetorno = array(); if (!file_exists($pathXmlFile)) { $msg = "Arquivo não localizado!!"; throw new Exception\InvalidArgumentException($msg); } //carrega a NFe $xml = Files\FilesFolders::readFile($pathXmlFile); $this->oCertificate->verifySignature($xml, 'infNFe'); //obtem o chave da NFe $docnfe = new Dom(); $docnfe->loadXMLFile($pathXmlFile); $tpAmb = $docnfe->getNodeValue('tpAmb'); $chNFe = $docnfe->getChave('infNFe'); $this->sefazConsultaChave($chNFe, $tpAmb, $aRetorno); if ($aRetorno['cStat'] != '100' && $aRetorno['cStat'] != '150') { return false; } return true; }
/** * zTrataProcEvent * @param array $resp */ private static function zTrataProcEvent($resp = array()) { $aResp = array(); $content = $resp['doc']; $dom = new Dom(); $dom->loadXMLString($content); $xmldata = $dom->saveXML(); $xmldata = str_replace('<?xml version="1.0"?>', '<?xml version="1.0" encoding="utf-8"?>', $xmldata); $data = $dom->getNodeValue('dhEvento'); $tsdhevento = DateTime::convertSefazTimeToTimestamp($data); $anomes = date('Ym', $tsdhevento); $tpEvento = $dom->getNodeValue('tpEvento'); $chave = $dom->getNodeValue('chNFe'); if ($tpEvento == '110111') { //confirmado cancelamento, localizar o xml da NFe recebida //na pasta anomes $path = $this->pathNFe . DIRECTORY_SEPARATOR . $this->ambiente . DIRECTORY_SEPARATOR . "recebidas" . DIRECTORY_SEPARATOR . $anomes; $pathFile = $path . DIRECTORY_SEPARATOR . $chave . '-nfe.xml'; self::zCancela($pathFile); $aResp[] = array('tipo' => '-cancNFe.xml', 'chave' => $chave, 'anomes' => $anomes, 'xml' => $xmldata); } elseif ($tpEvento == '110110') { //evento Carta de Correção $aResp[] = array('tipo' => '-cce.xml', 'chave' => $chave, 'anomes' => $anomes, 'xml' => $xmldata); } return $aResp; }
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); }
/** * 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; }