public IHttpActionResult GetAllNpvResults() { NpvResponse response = new NpvResponse(); List <NPVRecord> npvRecords = new List <NPVRecord>(); using (var dbContext = new NpvContext()) { npvRecords = dbContext.NPVRecords.OrderByDescending(x => x.Id).Take(100).ToList(); } response.NPVs = npvRecords; return(Ok(response)); }
public List <NPVRecord> CalculatePV(decimal lbDiscountRate, decimal ubDiscountRate, decimal discountRateIncrement, decimal initialValue, List <Decimal> cashFlows) { List <NPVRecord> npvRecords = new List <NPVRecord>(); List <decimal> discountRates = this.GetDiscountRate(lbDiscountRate, ubDiscountRate, discountRateIncrement); foreach (var discountRatePercentage in discountRates) { int timePeriods = 0; decimal discountRate = discountRatePercentage / 100; //if discountRate is not Percentage, replace line above with this line. //decimal discountRate = discountRatePercentage; List <decimal> netPresentValue = new List <decimal>(); netPresentValue.Add(-initialValue); string cashFlowsTxt = string.Empty; foreach (var cashFlow in cashFlows) { timePeriods++; decimal presentValue = cashFlow / (Convert.ToDecimal(Math.Pow(Convert.ToDouble(1 + discountRate), timePeriods))); netPresentValue.Add(presentValue); if (timePeriods == 1) { cashFlowsTxt = cashFlow.ToString(); } else { cashFlowsTxt = cashFlowsTxt + "," + cashFlow.ToString(); } } npvRecords.Add(new NPVRecord { CashFlows = cashFlowsTxt, DiscountRate = discountRate * 100, InitialValue = initialValue, NetPresentValue = netPresentValue.Sum() }); using (var dbContext = new NpvContext()) { //dbContext.NPVRecords.Add(new NPVRecord { CashFlows = cashFlowsTxt, DiscountRate = discountRate * 100, InitialValue = initialValue, NetPresentValue = netPresentValue.Sum() }); //dbContext.SaveChanges(); } } return(npvRecords); }