private ComparisonChartViewModel CreatePrintModel(ComparisonChartRequestModel request)
 {
     var model = service.CreateModel(request);
     model.request = request;
     foreach (var series in model.series.ToArray())
     {
         var seriesRequest = new ComparisonSeriesRequestModel
             {
                 activityId = series.activityId,
                 costCode = series.costCode,
                 endDate = request.endDate,
                 startDate = request.startDate,
                 target = series.target,
                 targetType = request.targetType
             };
         series.data = service.ComparisonData(seriesRequest).ToArray();
     }
     return model;
 }
 public IEnumerable<decimal> Target(ComparisonSeriesRequestModel request)
 {
     var indexDate = new DateTime(request.startDate.Year, request.startDate.Month, 1);
     var target = context
         .EmissionTargets
         .FirstOrDefault(emissionTarget =>
                         (emissionTarget.CostCentreCostCode == request.costCode) &&
                         (emissionTarget.ActivityGroupId == request.activityId) &&
                         (emissionTarget.TargetType == request.targetType));
     if (target == null) yield break;
     var initialAmount = target.InitialAmount;
     var initialDate = new DateTime(target.InitialDate.Year, target.InitialDate.Month, 1);
     var targetDate = new DateTime(target.TargetDate.Year, target.TargetDate.Month, 1);
     var totalDays = (targetDate - initialDate).TotalDays;
     var factor = (target.TargetAmount - initialAmount) / (decimal)totalDays;
     while (indexDate <= request.endDate)
     {
         var nextDate = indexDate.AddMonths(1);
         var daysDifference = (decimal)(nextDate - initialDate).TotalDays;
         var nextAmount = initialAmount + (daysDifference * factor);
         yield return ((nextDate < initialDate) || (indexDate > targetDate)) ? 0M : nextAmount;
         indexDate = nextDate;
     }
 }
 public ActionResult ComparisonData(ComparisonSeriesRequestModel request)
 {
     var model = service.ComparisonData(request).Select(value => new { value });
     return Json(model, JsonRequestBehavior.AllowGet);
 }
        public IEnumerable<decimal> ComparisonData(ComparisonSeriesRequestModel request)
        {
            if (request.target) return Target(request);
            IEnumerable<AverageData> averages;
            var activityNode = (request.activityId == null)
                ? new HierarchyId("/")
                : context
                    .ActivityGroups
                    .Find(request.activityId)
                    .Node;
            var costCentreNode = context
                .CostCentres
                .Find(request.costCode)
                .Node;
            switch (request.targetType)
            {
                case TargetType.CarbonEmissions:
                    averages = AverageCo2(
                        request.startDate,
                        request.endDate,
                        activityNode,
                        costCentreNode);
                    break;
                case TargetType.Money:
                    averages = AverageMoney(
                        request.startDate,
                        request.endDate,
                        activityNode,
                        costCentreNode);
                    break;
                case TargetType.Units:
                    averages = AverageUnits(
                        request.startDate,
                        request.endDate,
                        activityNode,
                        costCentreNode);
                    break;
                default:
                    throw new ArgumentOutOfRangeException("request");
            }
            var averageDictionary = averages
                .ToArray()
                .ToDictionary(data => data.YearMonth, data => data.Average);

            var returnValue = new List<decimal>();
            var indexDate = new DateTime(request.startDate.Year, request.startDate.Month, 1);
            var endRange = new DateTime(request.endDate.Year, request.endDate.Month, 1);
            
            while (indexDate <= endRange)
            {
                var lookup = (indexDate.Year*100) + indexDate.Month;
                var value = averageDictionary.ContainsKey(lookup) ? averageDictionary[lookup] : 0M;
                returnValue.Add(value);
                indexDate = indexDate.AddMonths(1);
            }
            
            return returnValue;
        }