public sefazDistDFe ( string $fonte = 'AN', string $tpAmb = '2', string $cnpj = '', integer $ultNSU, integer $numNSU, array &$aRetorno = [] ) : string | ||
$fonte | string | sigla da fonte dos dados 'AN' e para alguns casos pode ser 'RS' e para alguns casos pode ser 'RS' e para alguns casos pode ser 'RS' |
$tpAmb | string | tiupo de ambiente |
$cnpj | string | |
$ultNSU | integer | ultimo numero NSU que foi consultado |
$numNSU | integer | numero de NSU que se quer consultar |
$aRetorno | array | array com os dados do retorno |
return | string | contento o xml retornado pela SEFAZ |
public function sefazDistDFe($fonte = 'AN', $tpAmb = '2', $cnpj = '', $ultNSU = 0, $numNSU = 0, &$aRetorno = array())
{
if ($tpAmb == '') {
$tpAmb = $this->aConfig['tpAmb'];
}
$siglaUF = $this->aConfig['siglaUF'];
if ($cnpj == '') {
$cnpj = $this->aConfig['cnpj'];
}
//carrega serviço
$servico = 'NfeDistribuicaoDFe';
$this->zLoadServico('nfe', $servico, $fonte, $tpAmb);
if ($this->urlService == '') {
$msg = "A distribuição de documento DFe não está disponível na SEFAZ {$fonte}!!!";
throw new Exception\RuntimeException($msg);
}
$cUF = self::getcUF($siglaUF);
$ultNSU = str_pad($ultNSU, 15, '0', STR_PAD_LEFT);
$tagNSU = "<distNSU><ultNSU>{$ultNSU}</ultNSU></distNSU>";
if ($numNSU != 0) {
$numNSU = str_pad($numNSU, 15, '0', STR_PAD_LEFT);
$tagNSU = "<consNSU><NSU>{$numNSU}</NSU></consNSU>";
}
//monta a consulta
$cons = "<distDFeInt xmlns=\"{$this->urlPortal}\" versao=\"{$this->urlVersion}\">" . "<tpAmb>{$tpAmb}</tpAmb>" . "<cUFAutor>{$cUF}</cUFAutor>" . "<CNPJ>{$cnpj}</CNPJ>{$tagNSU}</distDFeInt>";
//validar mensagem com xsd
//if (! $this->validarXml($cons)) {
// $msg = 'Falha na validação. '.$this->error;
// throw new Exception\RuntimeException($msg);
//}
//montagem dos dados da mensagem SOAP
$body = "<nfeDistDFeInteresse xmlns=\"{$this->urlNamespace}\">" . "<nfeDadosMsg xmlns=\"{$this->urlNamespace}\">{$cons}</nfeDadosMsg>" . "</nfeDistDFeInteresse>";
//envia dados via SOAP e verifica o retorno este webservice não requer cabeçalho
$this->urlHeader = '';
$retorno = $this->oSoap->send($this->urlService, $this->urlNamespace, $this->urlHeader, $body, $this->urlMethod);
$lastMsg = $this->oSoap->lastMsg;
$this->soapDebug = $this->oSoap->soapDebug;
//salva mensagens
$tipoNSU = (int) ($numNSU != 0 ? $numNSU : $ultNSU);
$datahora = date('Ymd_His');
$filename = "{$tipoNSU}-{$datahora}-distDFeInt.xml";
$this->zGravaFile('nfe', $tpAmb, $filename, $lastMsg);
$filename = "{$tipoNSU}-{$datahora}-retDistDFeInt.xml";
$this->zGravaFile('nfe', $tpAmb, $filename, $retorno);
//tratar dados de retorno
$aRetorno = ReturnNFe::readReturnSefaz($servico, $retorno);
return (string) $retorno;
}
/** * Faz a consulta no SEFAZ para cada uma das empresas cadastradas * * - Para primeira carga de uma nova empresa o campo config_nfe.nfeconfig_ultnsu deve ser igual a 0 (zero), essa primeira carga vai buscar todas as notas * dos ultimos 15 dias para cada empresa. Na primeira carga deve-se chamar esse metodo quantas vezes forem necessarias ate que tenha como retorno * a seguinte mensagem "Dados atualizados. Nao ha mais NFe para buscar." * * - Para cargas diarias o script se auto executara ate buscar todas as notas de todas as empresas. * Ao final será retornado a seguinte mensagem "Dados atualizados. Nao ha mais NFe para buscar." * * @name getCustomerNfe * @access public * @author Roberson Faria */ public function getCustomerNFe() { try { $result = Customer::all()->toArray(); if (count($result) > 0) { foreach ($result as $config) { $operation = new Operation(); $ultnsu = $operation->where('id_customer', $config['id'])->max('resnfe_nsu'); $nfe = new ToolsNFe($this->setConfig($config)); $nfe->sefazDistDFe('AN', $this->dadosConfig["tpAmb"], $this->dadosConfig["cnpj"], $ultnsu, 0, $this->retorno, false); // dd($this->retorno); foreach ($this->retorno['aDoc'] as $dados) { if ($dados["schema"] == "resNFe_v1.00.xsd") { $xml = simplexml_load_string($dados['doc']); if (strlen($xml->CNPJ) > 0) { $doc = $xml->CNPJ; } else { $doc = $xml->CPF; } Operation::create(['id_customer' => $config['id'], 'resnfe_nsu' => $dados['NSU'], 'resnfe_tpdoc' => "1", 'resnfe_dhrecbtolocal' => date('Y-m-d H:i:s'), 'resnfe_cnpj_cpf' => $doc, 'resnfe_ie' => $xml->IE, 'resnfe_xnome' => $xml->xNome, 'resnfe_chnfe' => $xml->chNFe, 'resnfe_demi' => date('Y-m-d H:i:s', strtotime($xml->dhEmi)), 'resnfe_tpnf' => $xml->tpNF, 'resnfe_csitnfe' => $xml->cSitNFe, 'resnfe_dhrecbto' => date('Y-m-d H:i:s', strtotime($xml->dhRecbto)), 'resnfe_vnf' => $xml->vNF, 'resnfe_xml' => addslashes($dados['doc'])]); if ($xml->cSitNFe == 4 or $xml->cSitNFe == 1) { $this->downloadXml($config['id'], $xml->chNFe); } // echo "res = ".$xml->chNFe."<br>"; } elseif ($dados["schema"] == "procNFe_v1.00.xsd") { // dd($dados); // $xml = simplexml_load_string ($dados['doc']); // dd($xml); // echo "proc = ".$xml->protNFe->infProt->chNFe."<br>"; } else { // print_r($dados); // echo "<br>"; } } } } // dd("acabou"); } catch (Exception $e) { Log::warning("Erro LibNfe Exception: " . $e->getMessage()); exit; } }