NFePHP\NFe\ToolsNFe::sefazInutiliza PHP Method

sefazInutiliza() public method

sefazInutiliza Solicita a inutilização de uma ou uma sequencia de NFe de uma determinada série
public sefazInutiliza ( integer $nSerie = 1, integer $nIni, integer $nFin, string $xJust = '', string $tpAmb = '2', array &$aRetorno = [], $salvarMensagens = true ) : string
$nSerie integer
$nIni integer
$nFin integer
$xJust string
$tpAmb string
$aRetorno array
return string
    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;
    }

Usage Example

Ejemplo n.º 1
0
                //EFETUA O DOWNLOAD
                header('Content-Disposition: attachment; filename="' . $nome_arquivo . '"');
                echo $string;
            } else {
                echo "Falha na inserção da nota no banco de dados. Contate o programador";
            }
        }
    }
    ######################################### FIM TRANSMISSÃO #########################################################################################
    ##################################################### INUTILIZAÇÃO ##############################################################################333
    if (isset($_POST['function']) && $_POST['function'] == "inutilizacao") {
        //SE FOR inutilização DE NOTA FISCAL
        $aResposta = array();
        $nSerie = $_POST['serie'];
        $nIni = $_POST['ininmr'];
        $nFin = $_POST['fimnmr'];
        $xJust = $_POST['justificativa'];
        $tpAmb = '2';
        $xml = $nfe->sefazInutiliza($nSerie, $nIni, $nFin, $xJust, $tpAmb, $aResposta);
        echo '<br><br><PRE>';
        echo htmlspecialchars($nfe->soapDebug);
        echo '</PRE><BR>';
        print_r($aResposta);
        echo "<br>";
    }
    ##########################################3# FIM INUTILIZAÇÃO ###############################################################################
    ############################################ CANCELAMENTO ####################################################################################
    ############################################ FIM CANCELAMENTO #############################################################################
} else {
    echo "Possível ataque de upload de arquivo!\n";
}