Пример #1
0
 public long Importar(TNfeProc nfe)
 {
     try
     {
         string numeroNF                = nfe.NFe.infNFe.ide.nNF;
         string cpf_cnpjFornecedor      = nfe.NFe.infNFe.emit.Item;
         IEnumerable <Entrada> entradas = ObterPorNumeroNotaFiscalFornecedor(numeroNF, cpf_cnpjFornecedor);
         if (entradas.Count() > 0)
         {
             Entrada entrada = entradas.ElementAtOrDefault(0);
             RecuperarDadosEntrada(nfe, entrada);
             GerenciadorEntrada.GetInstance().Atualizar(entrada);
             return(entrada.CodEntrada);
         }
         else
         {
             Entrada entrada = new Entrada();
             RecuperarDadosEntrada(nfe, entrada);
             long codEntrada = GerenciadorEntrada.GetInstance().Inserir(entrada);
             return(codEntrada);
         }
     }
     catch (Exception e)
     {
         throw new NegocioException("Problema durante a importação dos dados da Entrada da NF-e. Favor contactar administrador.", e);
     }
 }
Пример #2
0
 public static GerenciadorEntrada GetInstance()
 {
     if (gEntrada == null)
     {
         gEntrada = new GerenciadorEntrada();
     }
     return(gEntrada);
 }
        /// <summary>
        /// Insere um novo pagamento numa entrada
        /// </summary>
        /// <param name="entradaPagamento"></param>
        /// <param name="entrada"></param>
        /// <returns></returns>
        public long Inserir(EntradaPagamento entradaPagamento)
        {
            try
            {
                if (entradaPagamento.Valor <= 0)
                {
                    throw new NegocioException("O valor do pagamento deve ser maior que zero e não deve ultrapassar o valor total da entrada.");
                }

                decimal?totalPagamentos = ObterPorEntrada(entradaPagamento.CodEntrada).Sum(ep => ep.Valor);

                Entrada entrada = GerenciadorEntrada.GetInstance().Obter(entradaPagamento.CodEntrada).ElementAt(0);

                decimal totalRegistrado = 0;
                if (totalPagamentos != null)
                {
                    totalRegistrado = (decimal)totalPagamentos;
                }

                if ((totalRegistrado + entradaPagamento.Valor) > (entrada.TotalNota + entrada.ValorFrete))
                {
                    throw new NegocioException("O valor dos pagamentos não pode ultrapassar o valor da nota mais o valor do frete");
                }

                tb_entrada_forma_pagamento _entradaFormaPagamentoE = new tb_entrada_forma_pagamento();
                Atribuir(entradaPagamento, _entradaFormaPagamentoE);

                var repEntradaPagamento = new RepositorioGenerico <tb_entrada_forma_pagamento>();

                repEntradaPagamento.Inserir(_entradaFormaPagamentoE);
                repEntradaPagamento.SaveChanges();

                return(_entradaFormaPagamentoE.codEntradaFormaPagamento);
            }
            catch (Exception e)
            {
                throw new DadosException("Pagamentos", e.Message, e);
            }
        }
Пример #4
0
        /// <summary>
        /// Importa os produtos de uma Nfe
        /// </summary>
        /// <param name="nfe"></param>
        public List <EntradaProduto> Importar(TNfeProc nfe)
        {
            const string VERSAO2 = "2.00";
            const string VERSAO3 = "3.10";
            const string VERSAO4 = "4.00";

            try
            {
                CultureInfo           ci                 = new CultureInfo("en-US"); // usado para connversão dos números do xml
                string                numeroNF           = nfe.NFe.infNFe.ide.nNF;
                string                versaoNF           = nfe.NFe.infNFe.versao;
                string                cpf_cnpjFornecedor = nfe.NFe.infNFe.emit.Item;
                IEnumerable <Entrada> entradas           = GerenciadorEntrada.GetInstance().ObterPorNumeroNotaFiscalFornecedor(numeroNF, cpf_cnpjFornecedor);
                if (entradas.Count() == 0)
                {
                    throw new NegocioException("A entrada não foi encontrada para realizar o cadastro de produtos");
                }
                Entrada entrada = entradas.ElementAtOrDefault(0);
                List <EntradaProduto> listaProtutos = new List <EntradaProduto>();
                foreach (TNFeInfNFeDet produto in nfe.NFe.infNFe.det)
                {
                    EntradaProduto entradaProduto = new EntradaProduto();
                    entradaProduto.Cfop          = Convert.ToInt32(produto.prod.CFOP);
                    entradaProduto.CodEntrada    = entrada.CodEntrada;
                    entradaProduto.CodFornecedor = entrada.CodFornecedor;
                    ProdutoPesquisa produtoPesquisa = null;
                    if (!string.IsNullOrEmpty(produto.prod.cEAN))
                    {
                        produtoPesquisa = GerenciadorProduto.GetInstance().ObterPorCodigoBarraExato(produto.prod.cEAN).ElementAtOrDefault(0);
                    }

                    entradaProduto.CodProduto             = (produtoPesquisa != null) ? produtoPesquisa.CodProduto : 1;
                    entradaProduto.DataEntrada            = entrada.DataEntrada;
                    entradaProduto.FornecedorEhFabricante = entrada.FornecedorEhFabricante;
                    if (entrada.ValorFrete > 0)
                    {
                        entradaProduto.Frete = ((entrada.ValorFrete / entrada.TotalProdutos) * 100);
                    }
                    else
                    {
                        entradaProduto.Frete = 0;
                    }
                    entradaProduto.Ncmsh       = produto.prod.NCM;
                    entradaProduto.NomeProduto = produto.prod.xProd.Length > 50 ? produto.prod.xProd.Substring(0, 50).ToUpper() : produto.prod.xProd.ToUpper();

                    entradaProduto.Quantidade          = Convert.ToDecimal(produto.prod.qCom, ci);
                    entradaProduto.QuantidadeEmbalagem = (produtoPesquisa == null) ? 1 : produtoPesquisa.QuantidadeEmbalagem;
                    entradaProduto.Simples             = (produtoPesquisa == null) ? 8 : produtoPesquisa.Simples;
                    entradaProduto.UnidadeCompra       = produto.prod.uCom;
                    entradaProduto.ValorDesconto       = Convert.ToDecimal(produto.prod.vDesc, ci);
                    entradaProduto.ValorUnitario       = Convert.ToDecimal(produto.prod.vUnCom, ci);
                    entradaProduto.Desconto            = Convert.ToDecimal(produto.prod.vDesc, ci) / entradaProduto.ValorTotal * 100;
                    entradaProduto.CodigoBarra         = produto.prod.cEAN;



                    entradaProduto.ReferenciaFabricante = produto.prod.cProd;

                    for (int i = 0; i < produto.imposto.Items.Length; i++)
                    {
                        if (produto.imposto.Items[i] is TNFeInfNFeDetImpostoICMS)
                        {
                            var icms = ((TNFeInfNFeDetImpostoICMS)produto.imposto.Items[i]).Item;
                            if (icms is TNFeInfNFeDetImpostoICMSICMS00)
                            {
                                TNFeInfNFeDetImpostoICMSICMS00 icms00 = ((TNFeInfNFeDetImpostoICMSICMS00)icms);;
                                entradaProduto.BaseCalculoICMS   = Convert.ToDecimal(icms00.vBC, ci);
                                entradaProduto.BaseCalculoICMSST = 0;
                                entradaProduto.CodCST            = icms00.orig.ToString().Substring(4) + icms00.CST.ToString().Substring(4);
                                entradaProduto.CodCSTNFe         = icms00.orig.ToString().Substring(4) + icms00.CST.ToString().Substring(4);
                                entradaProduto.Icms           = Convert.ToDecimal(icms00.pICMS, ci);
                                entradaProduto.IcmsSubstituto = 0;
                            }
                            else if (icms is TNFeInfNFeDetImpostoICMSICMS10)
                            {
                                TNFeInfNFeDetImpostoICMSICMS10 icms10 = ((TNFeInfNFeDetImpostoICMSICMS10)icms);;
                                entradaProduto.BaseCalculoICMS   = Convert.ToDecimal(icms10.vBC, ci);
                                entradaProduto.BaseCalculoICMSST = Convert.ToDecimal(icms10.vBCST, ci);
                                entradaProduto.CodCST            = icms10.orig.ToString().Substring(4) + icms10.CST.ToString().Substring(4);
                                entradaProduto.CodCSTNFe         = icms10.orig.ToString().Substring(4) + icms10.CST.ToString().Substring(4);
                                entradaProduto.Icms = Convert.ToDecimal(icms10.pICMS, ci);
                                if (entrada.TotalProdutosST > 0)
                                {
                                    entradaProduto.IcmsSubstituto = entrada.TotalSubstituicao / entrada.TotalProdutosST * 100; //Convert.ToDecimal(icms10.pICMSST, ci);
                                }
                            }
                            else if (icms is TNFeInfNFeDetImpostoICMSICMS20)
                            {
                                TNFeInfNFeDetImpostoICMSICMS20 icms20 = ((TNFeInfNFeDetImpostoICMSICMS20)icms);;
                                entradaProduto.BaseCalculoICMS   = Convert.ToDecimal(icms20.vBC, ci);
                                entradaProduto.BaseCalculoICMSST = 0;
                                entradaProduto.CodCST            = icms20.orig.ToString().Substring(4) + icms20.CST.ToString().Substring(4);
                                entradaProduto.CodCSTNFe         = icms20.orig.ToString().Substring(4) + icms20.CST.ToString().Substring(4);
                                entradaProduto.Icms           = Convert.ToDecimal(icms20.pICMS, ci);
                                entradaProduto.IcmsSubstituto = 0; //Convert.ToDecimal(icms10.pICMSST, ci);
                            }
                            else if (icms is TNFeInfNFeDetImpostoICMSICMS30)
                            {
                                TNFeInfNFeDetImpostoICMSICMS30 icms30 = ((TNFeInfNFeDetImpostoICMSICMS30)icms);;
                                entradaProduto.BaseCalculoICMS   = 0;
                                entradaProduto.BaseCalculoICMSST = Convert.ToDecimal(icms30.vBCST, ci);
                                entradaProduto.CodCST            = icms30.orig.ToString().Substring(4) + icms30.CST.ToString().Substring(4);
                                entradaProduto.CodCSTNFe         = icms30.orig.ToString().Substring(4) + icms30.CST.ToString().Substring(4);
                                entradaProduto.Icms = 0;
                                if (entrada.TotalProdutosST > 0)
                                {
                                    entradaProduto.IcmsSubstituto = entrada.TotalSubstituicao / entrada.TotalProdutosST * 100; //Convert.ToDecimal(icms10.pICMSST, ci);
                                }
                            }
                            else if (icms is TNFeInfNFeDetImpostoICMSICMS40)
                            {
                                TNFeInfNFeDetImpostoICMSICMS40 icms40 = ((TNFeInfNFeDetImpostoICMSICMS40)icms);;
                                entradaProduto.BaseCalculoICMS   = 0;
                                entradaProduto.BaseCalculoICMSST = 0;
                                entradaProduto.CodCST            = icms40.orig.ToString().Substring(4) + icms40.CST.ToString().Substring(4);
                                entradaProduto.CodCSTNFe         = icms40.orig.ToString().Substring(4) + icms40.CST.ToString().Substring(4);
                                entradaProduto.Icms = 0;
                                if (entrada.TotalProdutosST > 0)
                                {
                                    entradaProduto.IcmsSubstituto = entrada.TotalSubstituicao / entrada.TotalProdutosST * 100; //Convert.ToDecimal(icms10.pICMSST, ci);
                                }
                            }
                            else if (icms is TNFeInfNFeDetImpostoICMSICMS60)
                            {
                                TNFeInfNFeDetImpostoICMSICMS60 icms60 = ((TNFeInfNFeDetImpostoICMSICMS60)icms);;
                                entradaProduto.BaseCalculoICMS   = 0;
                                entradaProduto.BaseCalculoICMSST = Convert.ToDecimal(icms60.vBCSTRet, ci);
                                entradaProduto.CodCST            = icms60.orig.ToString().Substring(4) + icms60.CST.ToString().Substring(4);
                                entradaProduto.CodCSTNFe         = icms60.orig.ToString().Substring(4) + icms60.CST.ToString().Substring(4);
                                entradaProduto.Icms = Convert.ToDecimal(icms60.vICMSSTRet, ci);
                                if (entrada.TotalProdutosST > 0)
                                {
                                    entradaProduto.IcmsSubstituto = entrada.TotalSubstituicao / entrada.TotalProdutosST * 100; //Convert.ToDecimal(icms10.pICMSST, ci);
                                }
                            }
                            else if (icms is TNFeInfNFeDetImpostoICMSICMS70)
                            {
                                TNFeInfNFeDetImpostoICMSICMS70 icms70 = ((TNFeInfNFeDetImpostoICMSICMS70)icms);;
                                entradaProduto.BaseCalculoICMS   = Convert.ToDecimal(icms70.vBC, ci);
                                entradaProduto.BaseCalculoICMSST = Convert.ToDecimal(icms70.vBCST, ci);
                                entradaProduto.CodCST            = icms70.orig.ToString().Substring(4) + icms70.CST.ToString().Substring(4);
                                entradaProduto.CodCSTNFe         = icms70.orig.ToString().Substring(4) + icms70.CST.ToString().Substring(4);
                                entradaProduto.Icms = Convert.ToDecimal(icms70.pICMS, ci);
                                if (entrada.TotalProdutosST > 0)
                                {
                                    entradaProduto.IcmsSubstituto = entrada.TotalSubstituicao / entrada.TotalProdutosST * 100; //Convert.ToDecimal(icms10.pICMSST, ci);
                                }
                            }
                            else if (icms is TNFeInfNFeDetImpostoICMSICMSSN101)
                            {
                                TNFeInfNFeDetImpostoICMSICMSSN101 icms101 = ((TNFeInfNFeDetImpostoICMSICMSSN101)icms);;
                                entradaProduto.BaseCalculoICMS   = 0;
                                entradaProduto.BaseCalculoICMSST = 0;
                                entradaProduto.CodCST            = icms101.orig.ToString().Substring(4) + icms101.CSOSN.ToString().Substring(4);
                                entradaProduto.CodCSTNFe         = icms101.orig.ToString().Substring(4) + icms101.CSOSN.ToString().Substring(4);
                                entradaProduto.Icms           = Convert.ToDecimal(icms101.pCredSN, ci);
                                entradaProduto.IcmsSubstituto = 0;
                            }
                            else if (icms is TNFeInfNFeDetImpostoICMSICMSSN102)
                            {
                                TNFeInfNFeDetImpostoICMSICMSSN102 icms102 = ((TNFeInfNFeDetImpostoICMSICMSSN102)icms);;
                                entradaProduto.BaseCalculoICMS   = 0;
                                entradaProduto.BaseCalculoICMSST = 0;
                                entradaProduto.CodCST            = icms102.orig.ToString().Substring(4) + icms102.CSOSN.ToString().Substring(4);
                                entradaProduto.CodCSTNFe         = icms102.orig.ToString().Substring(4) + icms102.CSOSN.ToString().Substring(4);
                                entradaProduto.Icms           = 0;
                                entradaProduto.IcmsSubstituto = 0;
                            }
                            else if (icms is TNFeInfNFeDetImpostoICMSICMSSN900)
                            {
                                TNFeInfNFeDetImpostoICMSICMSSN900 icms900 = ((TNFeInfNFeDetImpostoICMSICMSSN900)icms);;
                                entradaProduto.BaseCalculoICMS   = Convert.ToDecimal(icms900.vBC, ci);
                                entradaProduto.BaseCalculoICMSST = Convert.ToDecimal(icms900.vBCST, ci);
                                entradaProduto.CodCST            = icms900.orig.ToString().Substring(4) + icms900.CSOSN.ToString().Substring(4);
                                entradaProduto.CodCSTNFe         = icms900.orig.ToString().Substring(4) + icms900.CSOSN.ToString().Substring(4);
                                entradaProduto.Icms           = Convert.ToDecimal(icms900.pICMS, ci);
                                entradaProduto.IcmsSubstituto = 0;
                                if (entrada.TotalProdutosST > 0)
                                {
                                    entradaProduto.IcmsSubstituto = entrada.TotalSubstituicao / entrada.TotalProdutosST * 100;
                                }
                            }
                            else if (icms is TNFeInfNFeDetImpostoICMSICMSSN201)
                            {
                                TNFeInfNFeDetImpostoICMSICMSSN201 icms201 = ((TNFeInfNFeDetImpostoICMSICMSSN201)icms);;
                                entradaProduto.BaseCalculoICMS   = 0;
                                entradaProduto.BaseCalculoICMSST = Convert.ToDecimal(icms201.vBCST, ci);
                                entradaProduto.CodCST            = icms201.orig.ToString().Substring(4) + icms201.CSOSN.ToString().Substring(4);
                                entradaProduto.CodCSTNFe         = icms201.orig.ToString().Substring(4) + icms201.CSOSN.ToString().Substring(4);
                                entradaProduto.Icms = 0;
                                if (entrada.TotalProdutosST > 0)
                                {
                                    entradaProduto.IcmsSubstituto = entrada.TotalSubstituicao / entrada.TotalProdutosST * 100;
                                }
                            }
                            else if (icms is TNFeInfNFeDetImpostoICMSICMSSN202)
                            {
                                TNFeInfNFeDetImpostoICMSICMSSN202 icms202 = ((TNFeInfNFeDetImpostoICMSICMSSN202)icms);;
                                entradaProduto.BaseCalculoICMS   = 0;
                                entradaProduto.BaseCalculoICMSST = Convert.ToDecimal(icms202.vBCST, ci);
                                entradaProduto.CodCST            = icms202.orig.ToString().Substring(4) + icms202.CSOSN.ToString().Substring(4);
                                entradaProduto.CodCSTNFe         = icms202.orig.ToString().Substring(4) + icms202.CSOSN.ToString().Substring(4);
                                entradaProduto.Icms = 0;
                                if (entrada.TotalProdutosST > 0)
                                {
                                    entradaProduto.IcmsSubstituto = entrada.TotalSubstituicao / entrada.TotalProdutosST * 100;
                                }
                            }
                            else if (icms is TNFeInfNFeDetImpostoICMSICMSSN500)
                            {
                                TNFeInfNFeDetImpostoICMSICMSSN500 icms500 = ((TNFeInfNFeDetImpostoICMSICMSSN500)icms);;
                                entradaProduto.BaseCalculoICMS   = 0;
                                entradaProduto.BaseCalculoICMSST = Convert.ToDecimal(icms500.vBCSTRet, ci);
                                entradaProduto.CodCST            = icms500.orig.ToString().Substring(4) + icms500.CSOSN.ToString().Substring(4);
                                entradaProduto.CodCSTNFe         = icms500.orig.ToString().Substring(4) + icms500.CSOSN.ToString().Substring(4);
                                entradaProduto.Icms = 0;
                                if (entrada.TotalProdutosST > 0)
                                {
                                    entradaProduto.IcmsSubstituto = entrada.TotalSubstituicao / entrada.TotalProdutosST * 100;
                                }
                            }

                            else
                            {
                                throw new NegocioException("Existe um imposto ICMS não tratado pela importação. Avise ao administrador.");
                            }
                        }
                        else if (versaoNF.Equals(VERSAO2))
                        {
                            if (produto.imposto.Items[i] is Dominio.NFE2.TNFeInfNFeDetImpostoIPI)
                            {
                                Dominio.NFE2.TNFeInfNFeDetImpostoIPI impostoIPI = (Dominio.NFE2.TNFeInfNFeDetImpostoIPI)produto.imposto.Items[i];

                                if (impostoIPI.Item is Dominio.NFE2.TNFeInfNFeDetImpostoIPIIPITrib)
                                {
                                    Dominio.NFE2.TNFeInfNFeDetImpostoIPIIPITrib ipiTrib = ((Dominio.NFE2.TNFeInfNFeDetImpostoIPIIPITrib)impostoIPI.Item);
                                    entradaProduto.Ipi = Convert.ToDecimal(ipiTrib.vIPI, ci) / entradaProduto.ValorTotal * 100;
                                }
                            }
                        }
                        else if (versaoNF.Equals(VERSAO3) || versaoNF.Equals(VERSAO4))
                        {
                            if (produto.imposto.Items[i] is TIpi)
                            {
                                TIpi ipi = (TIpi)produto.imposto.Items[i];
                                if (ipi.Item is TIpiIPITrib)
                                {
                                    TIpiIPITrib impostoIPI = (TIpiIPITrib)ipi.Item;
                                    entradaProduto.Ipi = Convert.ToDecimal(impostoIPI.vIPI, ci) / entradaProduto.ValorTotal * 100;
                                }
                                else
                                {
                                    entradaProduto.Ipi = 0;
                                }
                            }
                        }
                    }

                    Produto produtoCalculo = new Produto()
                    {
                        Desconto          = entradaProduto.Desconto,
                        Icms              = entradaProduto.Icms,
                        IcmsSubstituto    = entradaProduto.IcmsSubstituto,
                        Ipi               = entradaProduto.Ipi,
                        Frete             = entradaProduto.Frete,
                        Simples           = entradaProduto.Simples,
                        UltimoPrecoCompra = (entradaProduto.ValorUnitario / entradaProduto.QuantidadeEmbalagem)
                    };

                    entradaProduto.PrecoCusto = produtoCalculo.PrecoCusto;

                    if (produtoPesquisa == null)
                    {
                        entradaProduto.LucroPrecoRevenda      = 15;
                        entradaProduto.LucroPrecoVendaAtacado = 30;
                        entradaProduto.LucroPrecoVendaVarejo  = 35;
                        entradaProduto.QtdProdutoAtacado      = 0;
                    }
                    else
                    {
                        entradaProduto.LucroPrecoRevenda      = produtoPesquisa.LucroPrecoRevenda;
                        entradaProduto.LucroPrecoVendaAtacado = produtoPesquisa.LucroPrecoVendaAtacado;
                        entradaProduto.LucroPrecoVendaVarejo  = produtoPesquisa.LucroPrecoVendaVarejo;
                        entradaProduto.QtdProdutoAtacado      = produtoPesquisa.QtdProdutoAtacado;
                    }

                    produtoCalculo.LucroPrecoRevenda      = entradaProduto.LucroPrecoRevenda;
                    produtoCalculo.LucroPrecoVendaAtacado = entradaProduto.LucroPrecoVendaAtacado;
                    produtoCalculo.LucroPrecoVendaVarejo  = entradaProduto.LucroPrecoVendaVarejo;

                    entradaProduto.PrecoRevendaSugestao      = produtoCalculo.PrecoRevendaSugestao;
                    entradaProduto.PrecoVendaAtacadoSugestao = produtoCalculo.PrecoVendaAtacadoSugestao;
                    entradaProduto.PrecoVendaVarejoSugestao  = produtoCalculo.PrecoVendaVarejoSugestao;

                    if (produtoPesquisa == null)
                    {
                        entradaProduto.PrecoRevenda      = entradaProduto.PrecoRevendaSugestao;
                        entradaProduto.PrecoVendaAtacado = entradaProduto.PrecoVendaAtacadoSugestao;
                        entradaProduto.PrecoVendaVarejo  = entradaProduto.PrecoVendaVarejoSugestao;
                    }
                    else
                    {
                        entradaProduto.PrecoRevenda      = produtoPesquisa.PrecoRevenda;
                        entradaProduto.PrecoVendaAtacado = produtoPesquisa.PrecoVendaAtacado;
                        entradaProduto.PrecoVendaVarejo  = produtoPesquisa.PrecoVendaVarejo;
                    }

                    listaProtutos.Add(entradaProduto);
                }

                return(listaProtutos);
            }
            catch (Exception e)
            {
                throw new NegocioException("Problema durante a importação dos dados dos Produtos da NF-e. Favor contactar administrador.", e);
            }
        }