public ActionResult RendaFixa() { RendaFixaModel model = new RendaFixaModel(); model.Investimentos = new List <RendaFixaModel.InvestimentosModel>(); using (MyInvestsDataContext context = new MyInvestsDataContext()) { foreach (var investEntDB in context.InvestimentoRendaFixa.ToList()) { RendaFixaModel.InvestimentosModel novoModelInvestimento = new RendaFixaModel.InvestimentosModel(); novoModelInvestimento.Nome = investEntDB.Nome; novoModelInvestimento.DataCompra = investEntDB.DataCompra; novoModelInvestimento.DataVencimento = investEntDB.DataVencimento.Value; novoModelInvestimento.ValorCompra = investEntDB.ValorCompra; novoModelInvestimento.PosicaoInvestimento = context.InvestimentoRendaFixaPosicao.Where(x => x.IdInvestimento == investEntDB.Id) .Select(x => new RendaFixaModel.PosicaoInvestimentoModel() { DataReferencia = x.DataReferencia, TaxaIR = x.TaxaIR, ValorBrutoAtual = x.ValorAtual, ValorIR = x.ValorIR, ValorLiquidoAtual = x.ValorAtual - x.ValorIR, LucroLiquido = x.ValorAtual - investEntDB.ValorCompra }) .OrderByDescending(x => x.DataReferencia) .ToList(); for (int i = 0; i < novoModelInvestimento.PosicaoInvestimento.Count(); i++) { RendaFixaModel.VariacaoDataAnterior variacao = new RendaFixaModel.VariacaoDataAnterior(); var posicaoAtual = novoModelInvestimento.PosicaoInvestimento.ElementAtOrDefault(i); var posicaoAnterior = novoModelInvestimento.PosicaoInvestimento.ElementAtOrDefault(i + 1); if (posicaoAnterior != null) { variacao.VariacaoValorBruto = posicaoAtual.ValorBrutoAtual - posicaoAnterior.ValorBrutoAtual; variacao.VariacaoValorLiquido = posicaoAtual.ValorBrutoAtual - posicaoAnterior.ValorBrutoAtual; variacao.PercentualVariacaoBruta = variacao.VariacaoValorBruto / posicaoAnterior.ValorBrutoAtual; variacao.PercentualVariacaoLiquida = variacao.VariacaoValorLiquido / posicaoAnterior.ValorLiquidoAtual; } novoModelInvestimento.PosicaoInvestimento[i].Variacao = variacao; } model.Investimentos.Add(novoModelInvestimento); } } return(PartialView(model)); }
public List <FundosModel.PosicaoFundoModel> CalcularHistoricoPosicao(MyInvestsDataContext context, EntityFundos.Investimento meuInvestimento) { List <FundosModel.PosicaoFundoModel> retorno = new List <FundosModel.PosicaoFundoModel>(); var posicoesFundo = context.FundosPosicao.Where(i => i.IdFundo == meuInvestimento.IdFundo).OrderByDescending(i => i.DataReferencia).ToList(); for (int i = 0; i < posicoesFundo.Count(); i++) { EntityFundos.PosicaoFundo posicaoAtual = posicoesFundo.ElementAtOrDefault(i); EntityFundos.PosicaoFundo posicaoAnterior = posicoesFundo.ElementAtOrDefault(i + 1); FundosModel.PosicaoFundoModel model = new FundosModel.PosicaoFundoModel(); model.DataReferencia = posicaoAtual.DataReferencia; model.ValorCota = posicaoAtual.ValorPorCota; model.ValorTotalBruto = meuInvestimento.QuantidadeCotas * posicaoAtual.ValorPorCota; if (posicaoAnterior != null) { model.TaxaVariacao = (posicaoAtual.ValorPorCota / posicaoAnterior.ValorPorCota) - 1M; model.ValorVariacaoBruto = model.ValorTotalBruto - (meuInvestimento.QuantidadeCotas * posicaoAnterior.ValorPorCota); } retorno.Add(model); } return(retorno); }
public EntityFundos.PosicaoFundo ObterPosicaoAtualFundo(MyInvestsDataContext context, int idfundo) { DateTime ultimaDataReferencia = context.FundosPosicao.Where(i => i.IdFundo == idfundo).Max(i => i.DataReferencia); var retorno = context.FundosPosicao.Where(i => i.IdFundo == idfundo && i.DataReferencia == ultimaDataReferencia).FirstOrDefault(); return(retorno); }
public static void Importar(FixedIncomePosition jsonData) { List <DataEntities.MyInvests.InvestimentoRendaFixaPosicao> baseRendaFixa = LerDadosRendaFixa(jsonData); using (MyInvestsDataContext context = new MyInvestsDataContext()) { /////ATUALIZAR OS INVESTIMENTOS //foreach (var registroAdicionar in baseRendaFixa.Select(x => x.Investimento)) //{ // var registroDB = context.InvestimentoRendaFixa.Where(x => x.Id == registroAdicionar.Id).FirstOrDefault(); // if (registroDB == null) // { // registroDB = context.InvestimentoRendaFixa.Add(registroAdicionar); // context.SaveChanges(); // } //} //ATUALIZAR AS POSICOES foreach (var posicaoRF in baseRendaFixa) { var registroDB = context.InvestimentoRendaFixaPosicao .Where(x => x.IdInvestimento == posicaoRF.IdInvestimento && x.DataReferencia == posicaoRF.DataReferencia) .FirstOrDefault(); if (registroDB == null) { var investDB = context.InvestimentoRendaFixa.FirstOrDefault(x => x.Id == posicaoRF.IdInvestimento); if (investDB != null) { posicaoRF.Investimento = investDB; } registroDB = context.InvestimentoRendaFixaPosicao.Add(posicaoRF); context.SaveChanges(); } } } }
public ActionResult Fundos() { FundosModel model = new FundosModel(); using (MyInvestsDataContext context = new MyInvestsDataContext()) { foreach (var itemFundoDB in context.Fundos.ToList()) { foreach (var itemMeuInvestimentoDB in context.FundosInvestimento.Where(i => i.IdFundo == itemFundoDB.Id).ToList()) { FundosModel.InvestimentosModel investModel = new FundosModel.InvestimentosModel(); var posicaoFundo = ObterPosicaoAtualFundo(context, itemFundoDB.Id); investModel.Nome = itemFundoDB.Nome; investModel.Tipo = itemFundoDB.DescricaoTipo; investModel.DataCompra = itemMeuInvestimentoDB.DataCompra; investModel.ValorIR = itemMeuInvestimentoDB.ValorIR; investModel.ValorCompra = itemMeuInvestimentoDB.QuantidadeCotas * itemMeuInvestimentoDB.ValorCompraCota; investModel.ValorAtualBruto = itemMeuInvestimentoDB.QuantidadeCotas * posicaoFundo.ValorPorCota; investModel.ValorAtualLiquido = investModel.ValorAtualBruto - investModel.ValorIR; investModel.RendimentoBruto = investModel.ValorAtualBruto - investModel.ValorCompra; investModel.RendimentoLiquido = investModel.ValorAtualLiquido - investModel.ValorCompra; investModel.TaxaRendimentoBruto = investModel.RendimentoBruto / investModel.ValorCompra; investModel.TaxaRendimentoLiquido = investModel.RendimentoLiquido / investModel.ValorCompra; investModel.TaxaIR = investModel.ValorIR / investModel.RendimentoBruto; investModel.DataReferenciaPosicao = posicaoFundo.DataReferencia; investModel.HistoricoPosicoes = CalcularHistoricoPosicao(context, itemMeuInvestimentoDB); model.Investimentos.Add(investModel); } } } return(PartialView(model)); }
public void Importar(FundsPosition jsonDataFunds, List <Offer> jsonDataOffer) { ListaOfertasFundo = jsonDataOffer; PosicaoFundos = jsonDataFunds; List <Investimento> investimentosRico = ObterInformacaoInvestimentos(); using (MyInvestsDataContext context = new MyInvestsDataContext()) { //ATUALIZAR AS POSICOES foreach (var investimento in investimentosRico) { investimento.Fundo = ObterInformacoesFundo(investimento.IdFundo); var registroDB = context.FundosInvestimento .Where(x => x.IdFundo == investimento.IdFundo && x.DataCompra == investimento.DataCompra) .FirstOrDefault(); //SALVAR O INVESTIMENTO if (registroDB == null) { var fundoDB = context.Fundos.FirstOrDefault(x => x.Id == investimento.IdFundo); //se o fundo ja existir usar o que tem na base if (fundoDB != null) { investimento.Fundo = fundoDB; } registroDB = context.FundosInvestimento.Add(investimento); context.SaveChanges(); } else { registroDB.ValorIR = investimento.ValorIR; context.SaveChanges(); } //SALVAR A POSICAO DO FUNDO var posicaoFundo = ObterInformacoesPosicao(investimento.IdFundo); var posicaoDB = context.FundosPosicao .Where(x => x.IdFundo == posicaoFundo.IdFundo && x.DataReferencia == posicaoFundo.DataReferencia) .FirstOrDefault(); if (posicaoDB == null) { var fundoDB = context.Fundos.FirstOrDefault(x => x.Id == investimento.IdFundo); //se o fundo ja existir usar o que tem na base if (fundoDB != null) { posicaoFundo.Fundo = fundoDB; } context.FundosPosicao.Add(posicaoFundo); context.SaveChanges(); } } } }