/** * Inutiza sequencia de numeracao * * @param type $nAno * @param type $nSerie * @param type $nIni * @param type $nFin * @param type $xJust * @param type $tpAmb * @param type $aRetorno * @return boolean * @throws Exception\RuntimeException * @throws Exception\InvalidArgumentException */ public function sefazInutiliza($nSerie = '1', $nIni = '', $nFin = '', $xJust = '', $tpAmb = '2', &$aRetorno = array(), $salvarMensagens = true) { $nSerie = (int) $nSerie; $nIni = (int) $nIni; $nFin = (int) $nFin; $xJust = Strings::cleanString($xJust); $this->zValidParamInut($xJust, $nSerie, $nIni, $nFin); if ($tpAmb == '') { $tpAmb = $this->aConfig['tpAmb']; } // Identificação do serviço $servico = 'CteInutilizacao'; //monta serviço $siglaUF = $this->aConfig['siglaUF']; //carrega serviço $servico = 'CteInutilizacao'; $this->zLoadServico('cte', $servico, $siglaUF, $tpAmb); if ($this->urlService == '') { $msg = "A inutilização não está disponível na SEFAZ {$siglaUF}!!!"; throw new Exception\RuntimeException($msg); } //montagem dos dados da mensagem SOAP $cnpj = $this->aConfig['cnpj']; $sAno = (string) date('y'); $sSerie = str_pad($nSerie, 3, '0', STR_PAD_LEFT); $sInicio = str_pad($nIni, 9, '0', STR_PAD_LEFT); $sFinal = str_pad($nFin, 9, '0', STR_PAD_LEFT); //limpa os caracteres indesejados da justificativa $xJust = Strings::cleanString($xJust); // Identificador da TAG a ser assinada formada com Código da UF + // precedida do literal “ID” // 41 posições $id = 'ID' . $this->urlcUF . $cnpj . '57' . $sSerie . $sInicio . $sFinal; // Montagem do corpo da mensagem $dXML = "<inutCTe xmlns=\"{$this->urlPortal}\" versao=\"{$this->urlVersion}\">" . "<infInut Id=\"{$id}\">" . "<tpAmb>{$tpAmb}</tpAmb>" . "<xServ>INUTILIZAR</xServ>" . "<cUF>{$this->urlcUF}</cUF>" . "<ano>{$sAno}</ano>" . "<CNPJ>{$cnpj}</CNPJ>" . "<mod>57</mod>" . "<serie>{$nSerie}</serie>" . "<nCTIni>{$nIni}</nCTIni>" . "<nCTFin>{$nFin}</nCTFin>" . "<xJust>{$xJust}</xJust>" . "</infInut></inutCTe>"; //assina a solicitação de inutilização $signedMsg = $this->oCertificate->signXML($dXML, 'infInut'); $signedMsg = Strings::clearXml($signedMsg, true); $body = "<cteDadosMsg xmlns=\"{$this->urlNamespace}\">{$signedMsg}</cteDadosMsg>"; //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 if ($salvarMensagens) { $filename = "{$sAno}-{$this->modelo}-{$sSerie}-" . $sInicio . "_" . $sFinal . "-inutCTe.xml"; $this->zGravaFile('cte', $tpAmb, $filename, $lastMsg); $filename = "{$sAno}-{$this->modelo}-{$sSerie}-" . $sInicio . "_" . $sFinal . "-retInutCTe.xml"; $this->zGravaFile('cte', $tpAmb, $filename, $retorno); } //tratar dados de retorno $aRetorno = Response::readReturnSefaz($servico, $retorno); if ($aRetorno['cStat'] == '102') { $retorno = $this->zAddProtMsg('ProcInutCTe', 'inutCTe', $signedMsg, 'retInutCTe', $retorno); if ($salvarMensagens) { $filename = "{$sAno}-{$this->modelo}-{$sSerie}-" . $sInicio . "_" . $sFinal . "-procInutCTe.xml"; $this->zGravaFile('cte', $tpAmb, $filename, $retorno, 'inutilizadas'); } } 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; }
/** * zSefazEvento * * @param string $siglaUF * @param string $chave * @param string $cOrgao * @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 = '', $chave = '', $cOrgao = '', $tpAmb = '2', $tpEvento = '', $nSeqEvento = '1', $tagAdic = '') { if ($tpAmb == '') { $tpAmb = $this->aConfig['tpAmb']; } //carrega serviço $servico = 'MDFeRecepcaoEvento'; $this->zLoadServico('mdfe', $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']; $cnpj = $this->aConfig['cnpj']; $dhEvento = (string) str_replace(' ', 'T', date('Y-m-d H:i:s')); $sSeqEvento = str_pad($nSeqEvento, 2, "0", STR_PAD_LEFT); $eventId = "ID" . $tpEvento . $chave . $sSeqEvento; if ($cOrgao == '') { $cOrgao = $this->urlcUF; } $mensagem = "<eventoMDFe xmlns=\"{$this->urlPortal}\" versao=\"{$this->urlVersion}\">" . "<infEvento Id=\"{$eventId}\">" . "<cOrgao>{$cOrgao}</cOrgao>" . "<tpAmb>{$tpAmb}</tpAmb>" . "<CNPJ>{$cnpj}</CNPJ>" . "<chMDFe>{$chave}</chMDFe>" . "<dhEvento>{$dhEvento}</dhEvento>" . "<tpEvento>{$tpEvento}</tpEvento>" . "<nSeqEvento>{$nSeqEvento}</nSeqEvento>" . "<detEvento versaoEvento=\"{$this->urlVersion}\">" . "{$tagAdic}" . "</detEvento>" . "</infEvento>" . "</eventoMDFe>"; //assinatura dos dados $signedMsg = $this->oCertificate->signXML($mensagem, 'infEvento'); $cons = Strings::clearXml($signedMsg, true); //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, 'mdfe', 'eventoMDFe', $version)) { // $msg = 'Falha na validação. '.$this->error; // throw new Exception\RuntimeException($msg); //} $body = "<mdfeDadosMsg xmlns=\"{$this->urlNamespace}\">{$cons}</mdfeDadosMsg>"; //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 = "{$chave}-{$aliasEvento}-eventoMDFe.xml"; $this->zGravaFile('mdfe', $tpAmb, $filename, $lastMsg); $filename = "{$chave}-{$aliasEvento}-retEventoMDFe.xml"; $this->zGravaFile('mdfe', $tpAmb, $filename, $retorno); //tratar dados de retorno $this->aLastRetEvent = Response::readReturnSefaz($servico, $retorno); return (string) $retorno; }