public DashboardSummary CostCentre(DashboardRequest request) { var activity = (request.ActivityGroupId == null) ? null : context.ActivityGroups.Find(request.ActivityGroupId); var costCentreRoot = context.CostCentres.Find(request.CostCode); var slices = costCentreRoot .ChildrenCostCentres .Select(centre => { var returnModel = new SliceDataModel { CentreNode = centre.Node, Color = centre.Color, CostCode = centre.CostCode, Description = centre.Description, OrderId = centre.OrderId, Title = centre.Name }; if (activity == null) return returnModel; returnModel.ActivityGroupId = activity.Id; returnModel.ActivityGroupNode = activity.Node; returnModel.UomLong = activity.UOMLong; returnModel.UomShort = activity.UOMShort; return returnModel; }).ToArray(); if (!slices.Any()) { slices = new[] { new SliceDataModel { CentreNode = costCentreRoot.Node, Color = costCentreRoot.Color, CostCode = costCentreRoot.CostCode, Description = costCentreRoot.Description, OrderId = costCentreRoot.OrderId, Title = costCentreRoot.Name } }; if (activity != null) { slices[0].ActivityGroupId = activity.Id; slices[0].ActivityGroupNode = activity.Node; slices[0].UomLong = activity.UOMLong; slices[0].UomShort = activity.UOMShort; } } if (activity != null) return CreateSummary(request, slices, new[] {activity}, costCentreRoot); var configuration = Configurations[request.Section]; var activities = configuration.ActivityIds.Select(guid => context.ActivityGroups.Find(guid)).ToArray(); return CreateSummary(request, slices, activities, costCentreRoot); }
public DashboardSummary CreateSummary( DashboardRequest request, SliceDataModel[] sliceData, ActivityGroup[] activities, CostCentre centre) { var configuration = Configurations[request.Section]; var uomLong = string.Empty; var showCo2 = false; var groupNodes = activities.Select(a => a.Node).ToArray(); var summary = new DashboardSummary { costCentre = centre.Name, activityGroup = (request.ActivityGroupId == null) ? configuration.DisplayName : activities[0].Name, displayTotal = true }; var slices = sliceData .OrderBy(s => s.OrderId) .Select(s => { var slice = new SliceModel { activityGroupId = s.ActivityGroupId, color = s.Color, costCode = s.CostCode, description = s.Description, title = s.Title, sliceId = CreateSliceId(s.ActivityGroupId, s.CostCode), }; var noUnits = (string.IsNullOrEmpty(s.UomShort)) || (string.IsNullOrEmpty(s.UomLong)); if (configuration.ShowCo2 || noUnits) { showCo2 = true; slice.co2label = Constants.Constants.Co2LabelShort; slice.amount = GetTotal( TotalEmissions, request.StartDate, request.EndDate, s.ActivityGroupNode, s.CentreNode, groupNodes)/1000; } if (configuration.ShowCo2 && !noUnits) { slice.uom = s.UomShort; slice.units = GetTotal( TotalUnits, request.StartDate, request.EndDate, s.ActivityGroupNode, s.CentreNode, groupNodes); } if (!configuration.ShowCo2 && !noUnits) { slice.co2label = s.UomShort; uomLong = s.UomLong; slice.amount = GetTotal( TotalUnits, request.StartDate, request.EndDate, s.ActivityGroupNode, s.CentreNode, groupNodes); } return slice; }).ToArray(); decimal lastYearTotal; decimal totalAmount; if (showCo2) { totalAmount = GetTotal( TotalEmissions, request.StartDate, request.EndDate, null, centre.Node, groupNodes)/1000; lastYearTotal = GetTotal( TotalEmissions, request.StartDate.AddYears(-1), request.EndDate.AddYears(-1), null, centre.Node, groupNodes)/1000; } else { totalAmount = GetTotal( TotalUnits, request.StartDate, request.EndDate, null, centre.Node, groupNodes); lastYearTotal = GetTotal( TotalUnits, request.StartDate.AddYears(-1), request.EndDate.AddYears(-1), null, centre.Node, groupNodes); } var yoy = (lastYearTotal == 0) ? 0 : ((totalAmount - lastYearTotal)/lastYearTotal)*100; var currencies = GetCurrencies( request.StartDate, request.EndDate, groupNodes, centre.Node); summary.co2label = (showCo2) ? Constants.Constants.Co2LabelLong : uomLong; summary.currencies = currencies; summary.slices = slices; summary.total = totalAmount; summary.yoy = yoy; return summary; }