0
I’m using Laravel to return the Xmls issued against my client’s CNPJ. It happens that every time I access the webservice of Sefaz, returns xmls of dates I have already downloaded, and I would like you to return from the Ultimo NSU, from what I understand, we keep downloading the xmls that are in the sequence of NSU. Every day comes the same xmls from 3 months ago and I have to stay giving Reload until I get the ones I want. Please someone can tell me what I’m doing wrong. Follow the code:
public function DownloadNFE()
{
$this->tools->model(55);
$this->tools->setEnvironment(1);
//este numero deverá vir do banco de dados nas proximas buscas para reduzir
//a quantidade de documentos, e para não baixar várias vezes as mesmas coisas.
$ult_nsu_banco = nfe_ultimonsu::findOrFail('1');
$ultNSU = $ult_nsu_banco->ultimo_nsu;
$maxNSU = $ultNSU;
$loopLimit = 50;
$iCount = 0;
$aResposta = array();
while ($ultNSU <= $maxNSU) {
$iCount++;
if ($iCount >= $loopLimit) {
break;
}
try {
//executa a busca pelos documentos
$resp = $this->tools->sefazDistDFe($ultNSU);
} catch (\Exception $e) {
echo $e->getMessage();
//tratar o erro
}
//extrair e salvar os retornos
$dom = new \DOMDocument();
$dom->loadXML($resp);
$node = $dom->getElementsByTagName('retDistDFeInt')->item(0);
$tpAmb = $node->getElementsByTagName('tpAmb')->item(0)->nodeValue;
$verAplic = $node->getElementsByTagName('verAplic')->item(0)->nodeValue;
$cStat = $node->getElementsByTagName('cStat')->item(0)->nodeValue;
$xMotivo = $node->getElementsByTagName('xMotivo')->item(0)->nodeValue;
$dhResp = $node->getElementsByTagName('dhResp')->item(0)->nodeValue;
$ultNSU = $node->getElementsByTagName('ultNSU')->item(0)->nodeValue;
$maxNSU = $node->getElementsByTagName('maxNSU')->item(0)->nodeValue;
$lote = $node->getElementsByTagName('loteDistDFeInt')->item(0);
$nfe_ultimonsu = nfe_ultimonsu::findOrFail('1');
$nfe_ultimonsu->ultimo_nsu = $ultNSU;
$nfe_ultimonsu->save();
if (empty($lote)) {
//lote vazio
continue;
}
//essas tags irão conter os documentos zipados
$docs = $lote->getElementsByTagName('docZip');
foreach ($docs as $doc) {
$numnsu = $doc->getAttribute('NSU');
$schema = $doc->getAttribute('schema');
$arr_esquema = explode("_", $schema);
//descompacta o documento e recupera o XML original
$content = gzdecode(base64_decode($doc->nodeValue));
//identifica o tipo de documento
$tipo = substr($schema, 0, 6);
//processar o conteudo do NSU, da forma que melhor lhe interessar
//esse processamento depende do seu aplicativo
$xml_string = simplexml_load_string($content);
$CNPJ = null;
$IE = null;
$dhRecbto = null;
$dhEmi = null;
$vNF = null;
$xNome = null;
$CFOP = null;
$tipo = 0;
if ($arr_esquema[0] == "resEvento") {
$chNFe = $xml_string->chNFe;
$CNPJ = $xml_string->CNPJ;
$arr_dhRecbto = explode('T', $xml_string->dhRecbto);
$dhRecbto = $arr_dhRecbto[0];
$response = $this->tools->sefazManifesta($chNFe, $tpEvento = '210210', $xJust = '', $nSeqEvento = 1);
$tipo = 1;
}
if ($arr_esquema[0] == "procEventoNFe") {
$CNPJ = $xml_string->evento->infEvento->CNPJ;
$chNFe = $xml_string->evento->infEvento->chNFe;
$response = $this->tools->sefazManifesta($chNFe, $tpEvento = '210210', $xJust = '', $nSeqEvento = 1);
$tipo = 2;
}
if ($arr_esquema[0] == "resNFe") {
$chNFe = $xml_string->chNFe;
$CNPJ = $xml_string->CNPJ;
$xNome = $xml_string->xNome;
$IE = $xml_string->IE;
$arr_dhEmi = explode('T', $xml_string->dhEmi);
$arr_dhRecbto = explode('T', $xml_string->dhRecbto);
$dhRecbto = $arr_dhRecbto[0];
$dhEmi = $arr_dhEmi[0];
$response = $this->tools->sefazManifesta($chNFe, $tpEvento = '210210', $xJust = '', $nSeqEvento = 1);
$tipo = 3;
}
if ($arr_esquema[0] == "procNFe") {
$chNFe = $xml_string->protNFe->infProt->chNFe;
$xNome = $xml_string->NFe->infNFe->emit->xNome;
$CNPJ = $xml_string->NFe->infNFe->emit->CNPJ;
$IE = $xml_string->NFe->infNFe->emit->IE;
$vNF = $xml_string->NFe->infNFe->total->ICMSTot->vNF;
$CFOP = $xml_string->NFe->infNFe->det->prod->CFOP;
$arr_dhRecbto = explode('T', $xml_string->protNFe->infProt->dhRecbto);
$dhRecbto = $arr_dhRecbto[0];
$arr_dhEmi = explode('T', $xml_string->NFe->infNFe->ide->dhEmi);
$dhEmi = $arr_dhEmi[0];
$tipo = 4;
$caminho_xml = base_path() . "/public/dist/xml/" . $chNFe . ".xml";
$fp = fopen($caminho_xml, 'w+');
$result = fwrite($fp, $content);
fclose($fp);
}
$nota_fiscal = substr($chNFe, 25, 9);
$exist_nfe = nfe::where('chnfe', $chNFe)->first();
if (isset($exist_nfe->chnfe)) {
$insert_nfe = nfe::where('chnfe', $chNFe)->first();
} else {
$insert_nfe = new nfe;
}
$insert_nfe->chnfe = $chNFe;
$insert_nfe->cnpj = $CNPJ;
$insert_nfe->ie = $IE;
$insert_nfe->xnome = $xNome;
$insert_nfe->digval = $xml_string->digval;
$insert_nfe->schema = $schema;
$insert_nfe->cfop = $CFOP;
$insert_nfe->dhemi = $dhEmi;
$insert_nfe->dhrecbto = $dhRecbto;
$insert_nfe->vnf = $vNF;
$insert_nfe->nota_fiscal = $nota_fiscal;
$insert_nfe->tipo_xml = $tipo;
$insert_nfe->data_insere = date('Y-m-d');
$insert_nfe->save();
}
if ($ultNSU == $maxNSU) {
//break; //CUIDADO para não deixar seu loop infinito !!
}
sleep(2);
}