/// <summary> /// Method which computes the Balance points of an account /// //TODO: Modify this to work only over certain time period /// </summary> /// <param name="account"></param> /// <returns></returns> private Dictionary<DateTime, BalancePoint> ComputeBalancePoints(Account account) { Dictionary<DateTime, BalancePoint> result = new Dictionary<DateTime, BalancePoint>(); if (account.Operations.Count > 0) { var parDate = account.Operations .OrderBy(x => x.Date.Date) .GroupBy(x => x.Date.Date) .ToDictionary(x => x.Key, x => x.ToList()); decimal previousDate = 0; for (DateTime date = parDate.Keys.First(); date.CompareTo(DateTime.Now) <= 0; date = date.AddDays(1)) { BalancePoint point = null; if (parDate.ContainsKey(date)) { List<Operation> operationsToAdd = parDate[date]; point = new BalancePoint() { Date = date.Date, Balance = previousDate + operationsToAdd.Sum(x => x.SignedAmount) }; } else { point = new BalancePoint() { Date = date, Balance = previousDate }; } result.Add(date.Date, point); previousDate = result[date].Balance; _repository.SaveOrUpdate(point); } } return result; }
private BalancePointDto CreateBalancePoint(BalancePoint point) { var dto = new BalancePointDto(); dto.Balance = point.Balance; dto.Date = point.Date; dto.AccountId = point.Account.Id; dto.Id = point.Id; return dto; }
/// <summary> /// Helping method, iterates through operations and sums the values to compute the account evolution /// </summary> /// <param name="operations"></param> /// <param name="lastBalance"></param> /// <param name="account"></param> private void CreateBalancePointsForOperations(IEnumerable<Operation> operations, Decimal lastBalance,Account account) { var parDate = operations .OrderBy(x => x.Date.Date) .GroupBy(x => x.Date.Date) .ToDictionary(x => x.Key, x => x.Sum(y => y.SignedAmount)); using (TransactionScope scope = new TransactionScope()) { foreach (var item in parDate) { BalancePoint point = new BalancePoint { Date = item.Key, Balance = lastBalance + item.Value, Account = account }; // item.Value.Sum(x => x.SignedAmount) }; lastBalance = point.Balance; _repository.Save(point); account.BalancePoints.Add(point); } account.Balance = lastBalance; scope.Complete(); } }