/// <summary> /// Calculates report by tag but without percentage values. /// </summary> /// <param name="tagResult">Tag which is used to calculate the result</param> /// <returns>Tag with results for report, without percentage values.</returns> private TagResult GetTagResult(TagResult tagResult) { var tag = TagRepository.GetById(SessionVars.UserId, tagResult.TagId).Object; tagResult.SpentAmount = TagRepository.GetSpentAmountByTag(SessionVars.UserId, tagResult.TagId, startDate, endDate).Object; tagResult.TagName = tag.Name; foreach (var childTag in tag.Children) { var childTagResult = new TagResult(); childTagResult.TagId = childTag.Id; tagResult.Children.Add(GetTagResult(childTagResult)); } return tagResult; }
public ActionResult Index(IndexViewModel indexViewModel) { if (indexViewModel.StartDate > indexViewModel.EndDate) { ModelState.AddModelError("StartDate", "End date must be greater than start date"); } if (ModelState.IsValid) { startDate = indexViewModel.StartDate; endDate = indexViewModel.EndDate; var tag = TagRepository.GetParentTagByUserId(SessionVars.UserId).Object; var tagResult = new TagResult(); tagResult.TagId = tag.Id; indexViewModel.ParentTagResult = GetTagResult(tagResult); CalculatePercents(indexViewModel.ParentTagResult, indexViewModel.ParentTagResult.SpentAmount); return View(indexViewModel); } else { return View(indexViewModel); } }
/// <summary> /// Method calculated percentage values for all level in the tree of tags /// </summary> /// <param name="tagResult">Tree of tags</param> /// <param name="totalAmount">Total amount. So it is the value which we use to calculate percentage values. i.e. [Branch Result] / [Total amount]</param> /// <returns></returns> private TagResult CalculatePercents(TagResult tagResult, decimal totalAmount) { tagResult.Percentage = Math.Round(Convert.ToDouble(tagResult.SpentAmount / totalAmount) * 100.0, 2); foreach (var child in tagResult.Children) { CalculatePercents(child, totalAmount); } return tagResult; }