/** * sefazEnviaLote * @param string $xml * @param string $tpAmb * @param string $idLote * @param array $aRetorno * @return string * @throws Exception\InvalidArgumentException * @throws Exception\RuntimeException * @internal function zLoadServico (Common\Base\BaseTools) */ public function sefazEnviaLote($xml, $tpAmb = '2', $idLote = '', &$aRetorno = array()) { if (empty($xml)) { $msg = "Pelo menos uma MDFe deve ser informada."; throw new Exception\InvalidArgumentException($msg); } $sxml = preg_replace("/<\\?xml.*\\?>/", "", $xml); $siglaUF = $this->aConfig['siglaUF']; if ($tpAmb == '') { $tpAmb = $this->aConfig['tpAmb']; } if ($idLote == '') { $idLote = LotNumber::geraNumLote(15); } //carrega serviço $this->zLoadServico('mdfe', 'MDFeRecepcao', $siglaUF, $tpAmb); if ($this->urlService == '') { $msg = "O envio de lote não está disponível na SEFAZ {$siglaUF}!!!"; throw new Exception\RuntimeException($msg); } //montagem dos dados da mensagem SOAP $cons = "<enviMDFe xmlns=\"{$this->urlPortal}\" versao=\"{$this->urlVersion}\">" . "<idLote>{$idLote}</idLote>{$sxml}</enviMDFe>"; //valida a mensagem com o xsd //if (! $this->zValidMessage($cons, 'mdfe', 'enviMDFe', $version)) { // $msg = 'Falha na validação. '.$this->error; // throw new Exception\RuntimeException($msg); //} //montagem dos dados da mensagem SOAP $body = "<mdfeDadosMsg xmlns=\"{$this->urlNamespace}\">{$cons}</mdfeDadosMsg>"; $method = $this->urlMethod; //envia a solicitação via SOAP $retorno = $this->oSoap->send($this->urlService, $this->urlNamespace, $this->urlHeader, $body, $method); $lastMsg = $this->oSoap->lastMsg; $this->soapDebug = $this->oSoap->soapDebug; //salva mensagens $filename = "{$idLote}-enviMDFe.xml"; $this->zGravaFile('mdfe', $tpAmb, $filename, $lastMsg); $filename = "{$idLote}-retEnviMDFe.xml"; $this->zGravaFile('mdfe', $tpAmb, $filename, $retorno); //tratar dados de retorno $aRetorno = ReturnMDFe::readReturnSefaz($this->urlMethod, $retorno); return (string) $retorno; }
/** * zSefazEvento * @param string $siglaUF * @param string $chNFe * @param string $tpAmb * @param string $tpEvento * @param string $nSeqEvento * @param string $tagAdic * @return string * @throws Exception\RuntimeException * @internal function zLoadServico (Common\Base\BaseTools) */ protected function zSefazEvento($siglaUF = '', $chNFe = '', $tpAmb = '2', $tpEvento = '', $nSeqEvento = '1', $tagAdic = '') { if ($tpAmb == '') { $tpAmb = $this->aConfig['tpAmb']; } //carrega serviço $servico = 'RecepcaoEvento'; $this->zLoadServico('nfe', $servico, $siglaUF, $tpAmb); if ($this->urlService == '') { $msg = "A recepção de eventos não está disponível na SEFAZ {$siglaUF}!!!"; throw new Exception\RuntimeException($msg); } $aRet = $this->zTpEv($tpEvento); $aliasEvento = $aRet['alias']; $descEvento = $aRet['desc']; $cnpj = $this->aConfig['cnpj']; $dhEvento = (string) str_replace(' ', 'T', date('Y-m-d H:i:sP')); $sSeqEvento = str_pad($nSeqEvento, 2, "0", STR_PAD_LEFT); $eventId = "ID" . $tpEvento . $chNFe . $sSeqEvento; $cOrgao = $this->urlcUF; if ($siglaUF == 'AN') { $cOrgao = '91'; } $mensagem = "<evento xmlns=\"{$this->urlPortal}\" versao=\"{$this->urlVersion}\">" . "<infEvento Id=\"{$eventId}\">" . "<cOrgao>{$cOrgao}</cOrgao>" . "<tpAmb>{$tpAmb}</tpAmb>" . "<CNPJ>{$cnpj}</CNPJ>" . "<chNFe>{$chNFe}</chNFe>" . "<dhEvento>{$dhEvento}</dhEvento>" . "<tpEvento>{$tpEvento}</tpEvento>" . "<nSeqEvento>{$nSeqEvento}</nSeqEvento>" . "<verEvento>{$this->urlVersion}</verEvento>" . "<detEvento versao=\"{$this->urlVersion}\">" . "<descEvento>{$descEvento}</descEvento>" . "{$tagAdic}" . "</detEvento>" . "</infEvento>" . "</evento>"; //assinatura dos dados $signedMsg = $this->oCertificate->signXML($mensagem, 'infEvento'); $signedMsg = Strings::clearXml($signedMsg, true); $numLote = LotNumber::geraNumLote(); $cons = "<envEvento xmlns=\"{$this->urlPortal}\" versao=\"{$this->urlVersion}\">" . "<idLote>{$numLote}</idLote>" . "{$signedMsg}" . "</envEvento>"; //valida mensagem com xsd //no caso do evento nao tem xsd organizado, esta fragmentado //e por vezes incorreto por isso essa validação está desabilitada //if (! $this->zValidMessage($cons, 'nfe', 'envEvento', $version)) { // $msg = 'Falha na validação. '.$this->error; // throw new Exception\RuntimeException($msg); //} $body = "<nfeDadosMsg xmlns=\"{$this->urlNamespace}\">{$cons}</nfeDadosMsg>"; //envia a solicitação via SOAP $retorno = $this->oSoap->send($this->urlService, $this->urlNamespace, $this->urlHeader, $body, $this->urlMethod); $lastMsg = $this->oSoap->lastMsg; $this->soapDebug = $this->oSoap->soapDebug; //salva mensagens $filename = "{$chNFe}-{$aliasEvento}-envEvento.xml"; $this->zGravaFile('nfe', $tpAmb, $filename, $lastMsg); $filename = "{$chNFe}-{$aliasEvento}-retEnvEvento.xml"; $this->zGravaFile('nfe', $tpAmb, $filename, $retorno); //tratar dados de retorno $this->aLastRetEvent = ReturnNFe::readReturnSefaz($servico, $retorno); if ($this->aLastRetEvent['cStat'] == '128') { if ($this->aLastRetEvent['evento'][0]['cStat'] == '135' || $this->aLastRetEvent['evento'][0]['cStat'] == '136' || $this->aLastRetEvent['evento'][0]['cStat'] == '155') { $pasta = 'eventos'; //default if ($aliasEvento == 'CancNFe') { $pasta = 'canceladas'; $filename = "{$chNFe}-{$aliasEvento}-procEvento.xml"; } elseif ($aliasEvento == 'CCe') { $pasta = 'cartacorrecao'; $filename = "{$chNFe}-{$aliasEvento}-{$nSeqEvento}-procEvento.xml"; } $retorno = $this->zAddProtMsg('procEventoNFe', 'evento', $signedMsg, 'retEvento', $retorno); $this->zGravaFile('nfe', $tpAmb, $filename, $retorno, $pasta); } } return (string) $retorno; }
/** * Transmite o xml para a sefaz * * @param type $aXml * @param type $tpAmb * @param type $idLote * @param type $aRetorno * @param int $indSinc * @param type $compactarZip * @return type * @throws Exception\InvalidArgumentException * @throws Exception\RuntimeException */ public function sefazEnvia($aXml, $tpAmb = '2', $idLote = '', &$aRetorno = array(), $indSinc = 0, $compactarZip = false) { $sxml = $aXml; if (empty($aXml)) { $msg = "Pelo menos uma CTe deve ser informada."; throw new Exception\InvalidArgumentException($msg); } if (is_array($aXml)) { if (count($aXml) > 1) { //multiplas cte, não pode ser sincrono $indSinc = 0; } $sxml = implode("", $sxml); } $sxml = preg_replace("/<\\?xml.*\\?>/", "", $sxml); $siglaUF = $this->aConfig['siglaUF']; if ($tpAmb == '') { $tpAmb = $this->aConfig['tpAmb']; } if ($idLote == '') { $idLote = LotNumber::geraNumLote(15); } //carrega serviço $servico = 'CteRecepcao'; $this->zLoadServico('cte', $servico, $siglaUF, $tpAmb); if ($this->urlService == '') { $msg = "O envio de lote não está disponível na SEFAZ {$siglaUF}!!!"; throw new Exception\RuntimeException($msg); } // Montagem dos dados da mensagem SOAP $dados = "<cteDadosMsg xmlns=\"{$this->urlNamespace}\">" . "<enviCTe xmlns=\"{$this->urlPortal}\" versao=\"{$this->urlVersion}\">" . "<idLote>{$idLote}</idLote>" . "{$sxml}" . "</enviCTe>" . "</cteDadosMsg>"; // Envia dados via SOAP $retorno = $this->oSoap->send($this->urlService, $this->urlNamespace, $this->urlHeader, $dados, $this->urlMethod); $lastMsg = $this->oSoap->lastMsg; $this->soapDebug = $this->oSoap->soapDebug; //salva mensagens $filename = "{$idLote}-enviCTe.xml"; $this->zGravaFile('cte', $tpAmb, $filename, $lastMsg); $filename = "{$idLote}-retEnviCTe.xml"; $this->zGravaFile('cte', $tpAmb, $filename, $retorno); //tratar dados de retorno $aRetorno = Response::readReturnSefaz($servico, $retorno); //caso o envio seja recebido com sucesso mover a CTe da pasta //das assinadas para a pasta das enviadas return (string) $retorno; }