예제 #1
0
        public tcLoteRps BuscaDadosNFes(List<string> sListaNotas)
        {
            belConnection cx = new belConnection();            
            try
            {                
                objLoteRps = new tcLoteRps();

                objLoteRps.Rps = new List<TcRps>();
                foreach (string sNota in sListaNotas)
                {
                    TcRps objTcRps = new TcRps();

                    //IdentificacaoRps - TcIdentificacaoRps
                    daotcIdentificacaoRps objdaotcIdentificacaoRps = new daotcIdentificacaoRps();
                    objTcRps.InfRps = new TcInfRps();

                    StringBuilder sQuery = new StringBuilder();
                    sQuery.Append("SELECT   coalesce (tpdoc.cd_natureza_oper_nfse,'1')cd_natureza_oper_nfse , ");
                    sQuery.Append("coalesce (empresa.st_simples,'')st_simples , ");
                    sQuery.Append("coalesce (empresa.cd_regime_trib_especial,'0')RegimeEspecialTributacao , ");
                    sQuery.Append("coalesce (empresa.st_insentivador_cultural,'N')st_insentivador_cultural from nf ");
                    sQuery.Append("inner join tpdoc on nf.cd_tipodoc = tpdoc.cd_tipodoc ");
                    sQuery.Append("inner join empresa on empresa.cd_empresa = nf.cd_empresa ");
                    sQuery.Append(" where nf.cd_nfseq = '" + sNota + "' and ");
                    sQuery.Append(" nf.cd_empresa = '" + belStatic.codEmpresaNFe + "'");

                    FbCommand Command = new FbCommand(sQuery.ToString(), cx.get_Conexao());
                    cx.Open_Conexao();
                    Command.ExecuteNonQuery();
                    FbDataReader dr = Command.ExecuteReader();
                    dr.Read();

                    
                    objTcRps.InfRps.DataEmissao = HLP.Util.Util.GetDateServidor();
                    objTcRps.InfRps.NaturezaOperacao = Convert.ToInt16(dr["cd_natureza_oper_nfse"].ToString());

                    objTcRps.InfRps.OptanteSimplesNacional = (dr["st_simples"].ToString().Equals("S") ? 1 : 2);
                    objTcRps.InfRps.IncentivadorCultural = (dr["st_insentivador_cultural"].ToString().Equals("S")?1:2);

                    objTcRps.InfRps.Status = 1;//Normal;

                    if (objTcRps.InfRps.OptanteSimplesNacional == 1)
                    {
                        objTcRps.InfRps.RegimeEspecialTributacao = Convert.ToInt16(dr["RegimeEspecialTributacao"].ToString());
                    }
                    else
                    {
                        objTcRps.InfRps.RegimeEspecialTributacao = 0;
                    }


                    objTcRps.InfRps.IdentificacaoRps = objdaotcIdentificacaoRps.BuscatcIdentificacaoRps(cx.get_Conexao(), sNota);


                    //RpsSubstituido - TcIdentificacaoRps // Método tratado na visualização da Nota;
                    //daoRpsSubstituido objdaoRpsSubstituido = new daoRpsSubstituido();
                    //objTcRps.InfRps.RpsSubstituido = objdaoRpsSubstituido.RetornaIdentificacaoRpds(Conn, sNota);

                    //Servico - TcDadosServico
                    daoServico objdaoServico = new daoServico();
                    objTcRps.InfRps.Servico = objdaoServico.RetornaDadosServico(cx.get_Conexao(), sNota, objTcRps.InfRps.NaturezaOperacao);


                    //Prestador - tcIdentificacaoPrestador
                    daoPrestador objdaoPrestador = new daoPrestador();
                    objTcRps.InfRps.Prestador = objdaoPrestador.RettcIdentificacaoPrestador(cx.get_Conexao(), sNota);
                    objLoteRps.Cnpj = objTcRps.InfRps.Prestador.Cnpj; // Tag Pai;
                    objLoteRps.InscricaoMunicipal = objTcRps.InfRps.Prestador.InscricaoMunicipal; //Tag Pai;


                    //Tomador - TcDadosTomador
                    daoTomador objdaoTomador = new daoTomador();
                    objTcRps.InfRps.Tomador = objdaoTomador.RettcDadosTomador(cx.get_Conexao(), sNota);

                    //IntermediarioServico - tcIdentificacaoIntermediarioServico  //Tratado na visualização da Nota
                    //daoIntermediarioServico objdaoIntermediarioServico = new daoIntermediarioServico();
                    //objTcRps.InfRps.IntermediarioServico = objdaoIntermediarioServico.RettcIdentificacaoIntermediarioServico(Conn, sNota);

                    //ConstrucaoCivil - TcDadosContrucaoCivil - Tratado na Visualização da Nota
                    if (belStatic.sNomeEmpresa.Equals("AENGE"))
                    {
                        daoConstrucaoCivil objdaoConstrucaoCivil = new daoConstrucaoCivil();
                        objTcRps.InfRps.ConstrucaoCivil = objdaoConstrucaoCivil.RettcDadosConstrucaoCivil(cx.get_Conexao(), sNota);                       
                    }
                    objLoteRps.Rps.Add(objTcRps);
                }
                daoUtil objdaoUtil = new daoUtil();
                objLoteRps.NumeroLote = objdaoUtil.RetornaProximoValorGenerator("GEN_LOTE_NFES", 15);
                objLoteRps.QuantidadeRps = objLoteRps.Rps.Count;

                return objLoteRps;
            }
            catch (Exception ex)
            {              
                throw ex;
            }
            finally
            {
                cx.Close_Conexao();
            }
        }
예제 #2
0
        private void btnBuscaRetorno_Click(object sender, EventArgs e)
        {
            frmStatusEnvioNfs objfrmStatus = null;
            try
            {
                #region Busca Notas Selecionadas na Grid
                string sNfCancelada = string.Empty;
                sListaNotas = new List<string>();
                for (int i = 0; i < dgvNF.RowCount; i++)
                {
                    if (((dgvNF["ASSINANF", i].Value != null) && (dgvNF["ASSINANF", i].Value.ToString().Equals("True"))) && ((dgvNF["CANCELADA", i].Value == null) || (dgvNF["CANCELADA", i].Value.ToString() == "0"))) //Danner - o.s. SEM - 17/12/2009
                    {
                        sListaNotas.Add((string)dgvNF["CD_NFSEQ", i].Value);
                    }
                    if ((dgvNF["CANCELADA", i].Value != null) && (dgvNF["CANCELADA", i].Value.ToString() == "1"))
                    {
                        sNfCancelada += "Nota Fiscal " + dgvNF["CD_NOTAFIS", i].Value.ToString() + " - Esta Cancelada e não é Permitido o Reenvio da mesma Nota!" + Environment.NewLine + Environment.NewLine;
                    }
                }

                if (sListaNotas.Count == 0)
                {
                    KryptonMessageBox.Show("Nenhuma nota Valida foi Selecionada!", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    if (sNfCancelada != "")
                    {
                        KryptonMessageBox.Show(sNfCancelada, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    return;
                }

                #endregion

                if (sListaNotas.Count == 1)
                {
                    belRecepcao objBelRecepcao = new belRecepcao();
                    daoPrestador objdaoPrestador = new daoPrestador();
                    daoRecepcao objdaoRecepcao = new daoRecepcao();
                    //Buscar Protocolo no banco
                    objBelRecepcao.Protocolo = objdaoRecepcao.BuscaNumProtocolo(sListaNotas[0]);

                    //Busca Retorno do lote
                    AssinaNFeXml Assinatura = new AssinaNFeXml();
                    X509Certificate2 cert = Assinatura.BuscaNome("");
                    if (!ValidaCertificado(cert))
                    {
                        throw new Exception("Certificado não Selecionado!!");
                    }
                    else
                    {
                        objBelRecepcao.cert = cert;
                    }

                    objfrmStatus = new frmStatusEnvioNfs();
                    objfrmStatus.Show();
                    objfrmStatus.Refresh();
                    string sMsgErro = objBelRecepcao.BuscaRetorno(objdaoPrestador.RettcIdentificacaoPrestador(cx.get_Conexao(), sListaNotas[0]), objfrmStatus.lblMsg, objfrmStatus.progressBarStatus);

                    if (objBelRecepcao.sCodigoRetorno.Equals("E4"))
                    {
                        objfrmStatus.Close();
                        KryptonMessageBox.Show(null, sMsgErro + Environment.NewLine + Environment.NewLine + "IMPORTANTE: Tente Buscar Retorno da NFse pois o serviço do Web service está demorando para responder; ", "MENSAGEM DE RETORNO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else if (objBelRecepcao.objListaNfseRetorno.Count > 0) //Lote Enviado
                    {
                        objfrmStatus.lblMsg.Text = "Alterando Status da Nota para Enviada!!";
                        objfrmStatus.lblMsg.Refresh();
                        objdaoRecepcao.AlteraStatusDaNota(objBelRecepcao.objListaNfseRetorno);
                        objdaoRecepcao.VerificaNotasParaCancelar(objBelRecepcao.objListaNfseRetorno);
                        for (int i = 0; i < dgvNF.RowCount; i++)
                        {
                            //CD_NFSEQ
                            int ienviado = objBelRecepcao.objListaNfseRetorno.Count(lote => lote.IdentificacaoRps.Nfseq == dgvNF["CD_NFSEQ", i].Value.ToString());
                            if (ienviado > 0)
                            {
                                dgvNF["ST_NFE", i].Value = true;
                            }
                        }
                        KryptonMessageBox.Show(null, objBelRecepcao.MontaMsgDeRetornoParaCliente(), "MENSAGEM DE RETORNO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        EnviaEmail(objBelRecepcao.objListaNfseRetorno);
                    }
                    else
                    {
                        objdaoRecepcao.LimpaRecibo();
                        objfrmStatus.Close();
                        KryptonMessageBox.Show(null, sMsgErro + Environment.NewLine, "MENSAGEM DE RETORNO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    objfrmStatus.Close();
                }
                else
                {
                    throw new Exception("Selecione apenas uma Nota e o lote ref. a éssa nota será consultado!!");
                }
                VerificaGeneratorLote();
                PopulaDataGridView();
            }
            catch (Exception ex)
            {
                if (objfrmStatus != null)
                {
                    objfrmStatus.Close();
                }
                KryptonMessageBox.Show(null, string.Format(Msg_Padrao.CException, Environment.NewLine) + (ex.InnerException != null ? ex.InnerException.Message : ex.Message).ToString(), "AVISO ", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally { cx.Close_Conexao(); }
        }