public function sefazInutiliza($nSerie = 1, $nIni = 0, $nFin = 0, $xJust = '', $tpAmb = '2', &$aRetorno = array(), $salvarMensagens = true)
{
$xJust = Strings::cleanString($xJust);
$nSerie = (int) $nSerie;
$nIni = (int) $nIni;
$nFin = (int) $nFin;
$this->zValidParamInut($xJust, $nSerie, $nIni, $nFin);
if ($tpAmb == '') {
$tpAmb = $this->aConfig['tpAmb'];
}
//monta serviço
$siglaUF = $this->aConfig['siglaUF'];
//carrega serviço
$servico = 'NfeInutilizacao';
$this->zLoadServico('nfe', $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);
$idInut = "ID" . $this->urlcUF . $sAno . $cnpj . $this->modelo . $sSerie . $sInicio . $sFinal;
//limpa os caracteres indesejados da justificativa
$xJust = Strings::cleanString($xJust);
//montagem do corpo da mensagem
$cons = "<inutNFe xmlns=\"{$this->urlPortal}\" versao=\"{$this->urlVersion}\">" . "<infInut Id=\"{$idInut}\">" . "<tpAmb>{$tpAmb}</tpAmb>" . "<xServ>INUTILIZAR</xServ>" . "<cUF>{$this->urlcUF}</cUF>" . "<ano>{$sAno}</ano>" . "<CNPJ>{$cnpj}</CNPJ>" . "<mod>{$this->modelo}</mod>" . "<serie>{$nSerie}</serie>" . "<nNFIni>{$nIni}</nNFIni>" . "<nNFFin>{$nFin}</nNFFin>" . "<xJust>{$xJust}</xJust>" . "</infInut></inutNFe>";
//assina a lsolicitação de inutilização
$signedMsg = $this->oCertificate->signXML($cons, 'infInut');
$signedMsg = Strings::clearXml($signedMsg, true);
//valida a mensagem com o xsd
//if (! $this->zValidMessage($cons, 'nfe', 'inutNFe', $version)) {
// $msg = 'Falha na validação. '.$this->error;
// throw new Exception\RuntimeException($msg);
//}
$body = "<nfeDadosMsg xmlns=\"{$this->urlNamespace}\">{$signedMsg}</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
if ($salvarMensagens) {
$filename = "{$sAno}-{$this->modelo}-{$sSerie}-" . $sInicio . "_" . $sFinal . "-inutNFe.xml";
$this->zGravaFile('nfe', $tpAmb, $filename, $lastMsg);
$filename = "{$sAno}-{$this->modelo}-{$sSerie}-" . $sInicio . "_" . $sFinal . "-retInutNFe.xml";
$this->zGravaFile('nfe', $tpAmb, $filename, $retorno);
}
//tratar dados de retorno
$aRetorno = ReturnNFe::readReturnSefaz($servico, $retorno);
if ($aRetorno['cStat'] == '102') {
$retorno = $this->zAddProtMsg('ProcInutNFe', 'inutNFe', $signedMsg, 'retInutNFe', $retorno);
if ($salvarMensagens) {
$filename = "{$sAno}-{$this->modelo}-{$sSerie}-" . $sInicio . "_" . $sFinal . "-procInutNFe.xml";
$this->zGravaFile('nfe', $tpAmb, $filename, $retorno, 'inutilizadas');
}
}
return (string) $retorno;
}