예제 #1
0
        /// <summary>
        /// Envia os dados da venda para o Sat.
        /// </summary>
        /// <param name="cfe">The cfe.</param>
        /// <returns>VendaSatResposta.</returns>
        public VendaSatResposta EnviarDadosVenda(CFe cfe)
        {
            Guard.Against <ACBrException>(!Ativo, "Componente não está ativo.");
            Guard.Against <ArgumentNullException>(cfe.IsNull(), nameof(cfe));

            var dadosVenda = GetXml(cfe, false, false);

            IniciaComando($"EnviarDadosVenda({dadosVenda})");

            if (Arquivos.SalvarEnvio)
            {
                var envioPath = Arquivos.PastaEnvio;
                var fullName  = Path.Combine(envioPath, $"{Arquivos.PrefixoArqCFe}{DateTime.Now:yyyyMMddHHmmss}-{Sessao.ZeroFill(6)}-env.xml");
                if (!Directory.Exists(envioPath))
                {
                    Directory.CreateDirectory(envioPath);
                }

                File.WriteAllText(fullName, dadosVenda);
            }

            var e = new EventoDadosEventArgs {
                Dados = dadosVenda
            };

            OnEnviarDadosVenda.Raise(this, e);

            var ret  = sat.EnviarDadosVenda(Sessao, CodigoAtivacao, e.Dados);
            var resp = FinalizaComando <VendaSatResposta>(ret);

            if (!Arquivos.SalvarCFe || resp.CodigoDeRetorno != 6000)
            {
                return(resp);
            }

            var cnpj = Arquivos.SepararPorCNPJ ? resp.Venda.InfCFe.Emit.CNPJ : "";
            var data = Arquivos.SepararPorMes ? $"{resp.Venda.InfCFe.Ide.DEmi:yyyy}\\{resp.Venda.InfCFe.Ide.DEmi:MM}" : "";
            var path = Path.Combine(Arquivos.PastaCFeVenda, cnpj, data);
            var calcPathEventEventArgs = new CalcPathEventEventArgs
            {
                CNPJ = resp.Venda.InfCFe.Emit.CNPJ,
                Data = resp.Venda.InfCFe.Ide.DEmi,
                Path = path
            };

            OnCalcPath.Raise(this, calcPathEventEventArgs);

            if (!Directory.Exists(calcPathEventEventArgs.Path))
            {
                Directory.CreateDirectory(calcPathEventEventArgs.Path);
            }

            var nomeArquivo = $"{Arquivos.PrefixoArqCFe}{resp.Venda.InfCFe.Id.OnlyNumbers()}.xml";
            var fullPath    = Path.Combine(calcPathEventEventArgs.Path, nomeArquivo);

            Salvar(resp.Venda, fullPath);

            return(resp);
        }
예제 #2
0
        /// <summary>
        /// Cancela a venda
        /// </summary>
        /// <param name="chave">A chave da CFe pra cancelar.</param>
        /// <param name="dadosCancelamento">XML de cancelamento.</param>
        /// <returns>CancelamentoSatResposta.</returns>
        public CancelamentoSatResposta CancelarUltimaVenda(string chave, string dadosCancelamento)
        {
            Guard.Against <ACBrException>(!Ativo, "Componente não está ativo.");
            Guard.Against <ArgumentException>(chave.IsEmpty(), "Chave não informada.");
            Guard.Against <ArgumentException>(dadosCancelamento.IsEmpty(), "Dados de cancelamento não informado.");

            IniciaComando($"CancelarUltimaVenda({chave}, {dadosCancelamento})");

            if (Arquivos.SalvarEnvio)
            {
                var envioPath = Arquivos.PastaEnvio;
                var fullName  = Path.Combine(envioPath, $"{Arquivos.PrefixoArqCFeCanc}{DateTime.Now:yyyyMMddHHmmss}-{Sessao.ZeroFill(6)}-env.xml");
                if (!Directory.Exists(envioPath))
                {
                    Directory.CreateDirectory(envioPath);
                }

                File.WriteAllText(fullName, dadosCancelamento);
            }

            var e = new EventoDadosEventArgs {
                Dados = dadosCancelamento
            };

            OnCancelarUltimaVenda.Raise(this, e);

            var ret  = sat.CancelarUltimaVenda(Sessao, CodigoAtivacao, chave, dadosCancelamento);
            var resp = FinalizaComando <CancelamentoSatResposta>(ret);

            if (!Arquivos.SalvarCFeCanc || resp.CodigoDeRetorno != 7000)
            {
                return(resp);
            }

            var cnpj = Arquivos.SepararPorCNPJ ? resp.Cancelamento.InfCFe.Emit.CNPJ : "";
            var data = Arquivos.SepararPorMes ? $"{resp.Cancelamento.InfCFe.Ide.DEmi:yyyy}\\{resp.Cancelamento.InfCFe.Ide.DEmi:MM}" : "";
            var path = Path.Combine(Arquivos.PastaCFeCancelamento, cnpj, data);
            var calcPathEventEventArgs = new CalcPathEventEventArgs
            {
                CNPJ = resp.Cancelamento.InfCFe.Emit.CNPJ,
                Data = resp.Cancelamento.InfCFe.Ide.DEmi,
                Path = path
            };

            OnCalcPath.Raise(this, calcPathEventEventArgs);

            if (!Directory.Exists(calcPathEventEventArgs.Path))
            {
                Directory.CreateDirectory(calcPathEventEventArgs.Path);
            }

            var nomeArquivo = $"{Arquivos.PrefixoArqCFeCanc}{resp.Cancelamento.InfCFe.Id.OnlyNumbers()}.xml";
            var fullPath    = Path.Combine(calcPathEventEventArgs.Path, nomeArquivo);

            resp.Cancelamento.Save(fullPath);
            return(resp);
        }
예제 #3
0
        /// <summary>
        /// Consulta os dados da sessão informada.
        /// </summary>
        /// <param name="numeroSessao">The numero sessao.</param>
        /// <returns>SatResposta.</returns>
        public ConsultaSessaoResposta ConsultarNumeroSessao(int numeroSessao)
        {
            Guard.Against <ACBrException>(!Ativo, "Componente não está ativo.");

            IniciaComando($"ConsultarNumeroSessao({numeroSessao})");
            var e = new EventoDadosEventArgs
            {
                Dados   = numeroSessao.ToString(),
                Retorno = string.Empty
            };

            OnConsultarNumeroSessao.Raise(this, e);

            if (e.Retorno.IsEmpty())
            {
                e.Retorno = sat.ConsultarNumeroSessao(Sessao, CodigoAtivacao, numeroSessao);
            }

            return(FinalizaComando <ConsultaSessaoResposta>(e.Retorno));
        }