using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Web; using System.Web.Mvc; using EnVisage.Code; using EnVisage.Code.Cache; using EnVisage.Models; using Microsoft.AspNet.Identity; namespace EnVisage.Controllers { public class WhatIfCalendarController : BaseController { public enum GraphType{ Gebneral = 0, Probability =1 } const string CG = "CG"; const string EFX = "EFX"; // GET: /ShowRollUp/ [AreaSecurityAttribute(area = Areas.Reports, level = AccessLevel.Read)] public ActionResult Index() { var model = new WhatIfCalendarModel(); model.FormScenarioReport(DbContext); model.FormСhartListElements(DbContext); var l = new List(); foreach (var s in model.Scenarios) { l.Add(s.Id); } model.SelectedScenarios = "[" + string.Join(",", l.Select(x=>"\"" + x.ToString() + "\"").ToArray()) + "]"; var user = new UsersCache().Value.FirstOrDefault(x => x.Id == new Guid(HttpContext.User.Identity.GetID())); if (user != null) ViewBag.IsUOMHours = !user.PreferredResourceAllocation; return View(model); } [HttpPost] [AreaSecurityAttribute(area = Areas.Reports, level = AccessLevel.Read)] public ActionResult Index(WhatIfCalendarModel model) { ModelState.Clear(); model.FormScenarioReport(DbContext); model.FormСhartListElements(DbContext); var l = new List(); foreach (var s in model.Scenarios) { l.Add(s.Id); } model.SelectedScenarios = "[" + string.Join(",", l.Select(x => "\"" + x.ToString() + "\"").ToArray()) + "]"; return View(model); } [HttpPost] [AreaSecurityAttribute(area = Areas.Reports, level = AccessLevel.Read)] public ActionResult Filter(WhatIfCalendarModel model) { model.FormScenarioReport(DbContext); model.FormСhartListElements(DbContext); return View(model); } [HttpPost] [AreaSecurityAttribute(area = Areas.Reports, level = AccessLevel.Read)] public JsonResult GetGraphData(DateTime? StartDate, DateTime? EndDate, bool cost, List selectedScenarios, GraphType graphType, ScenarioType type, Guid? groupId, bool? isUOMHours) { if (selectedScenarios == null) selectedScenarios = new List(); WhatIfCalendarDataModel result = new WhatIfCalendarDataModel(); StartDate = StartDate ?? DateTime.Now.AddMonths(-6); EndDate = EndDate ?? DateTime.Now.AddMonths(6); var model = new WhatIfCalendarModel() { StartDate = StartDate.Value, EndDate = EndDate.Value, SelectedScenarios = "[" + string.Join(",", selectedScenarios.Select(x=>x.ToString()).ToArray()) + "]", SelectedScenarioType= type, GroupId = groupId, UOMSwitcherMode = isUOMHours }; model.FormScenarioReport(DbContext); model.FormСhartListElements(DbContext); var days = EndDate.Value.Subtract(StartDate.Value).TotalDays; var periodType = (int)FiscalCalendarModel.FiscalYearType.Week; if (days > 400) periodType = (int)FiscalCalendarModel.FiscalYearType.Month; var weeks = DbContext.FiscalCalendars .Where(t => t.Type == periodType && t.EndDate <= EndDate && t.EndDate >= StartDate && t.AdjustingPeriod == false) .OrderBy(t => t.EndDate) .ToArray(); var gridHeaders = weeks.Select(t => t.EndDate).ToList(); //long _awardedCost = 0, _likelyAwardedCost = 0, _trainingCost = 0, _vacationCost = 0, _capacityCost = 0; var vacationRow = new WhatIfCalendarDataModel.WhatIfCalendarDataRow() { ScenarioName = "Vacation", QuantityValues = new List(), ScenarioType = ScenarioType.Vacation.ToDisplayValue() }; var trainingRow = new WhatIfCalendarDataModel.WhatIfCalendarDataRow() { ScenarioName = "Training", QuantityValues = new List(), ScenarioType = ScenarioType.Training.ToDisplayValue() }; foreach (var s in model.Scenarios.Where(t => selectedScenarios.Contains(t.Id))) { result.WhatIfCalendar.Add(new WhatIfCalendarDataModel.WhatIfCalendarDataRow() { ScenarioName = s.Name, QuantityValues = new List(), ScenarioType = s.Type.ToDisplayValue() }); } result.WhatIfCalendar.Add(trainingRow); result.WhatIfCalendar.Add(vacationRow); for (int i = 0; i < weeks.Count(); i++) { var endDate = weeks[i].EndDate; foreach (var s in model.Scenarios.Where(t => selectedScenarios.Contains(t.Id))) { long val = 0; if (model.ChartData.ContainsKey(s.Name)) { var scenario = model.ChartData[s.Name]; var chartVal = scenario.FirstOrDefault(t => t.WeekEndingDate.HasValue && endDate.ToShortDateString().Equals(t.WeekEndingDate.Value.ToShortDateString())); if (chartVal != null) { if (((chartVal.ScenarioType == (int?)ScenarioType.Actuals && model.IncludeActuals == YesNoType.Yes) || (chartVal.ScenarioType != (int?)ScenarioType.Actuals && model.IncludeActuals == YesNoType.No)) && (((EFX.Equals(chartVal.CGEFX.Trim()) || CG.Equals(chartVal.CGEFX.Trim())) && model.LaborMaterials == LaborMaterialsType.LaborMaterials) || (CG.Equals(chartVal.CGEFX.Trim()) && model.LaborMaterials == LaborMaterialsType.Labor) || (EFX.Equals(chartVal.CGEFX.Trim()) && model.LaborMaterials == LaborMaterialsType.Materials))) { val = cost ? (long)chartVal.Cost : (long)(chartVal.Quantity); if (graphType == GraphType.Probability && CG.Equals(chartVal.CGEFX.Trim())) { val = Convert.ToInt64(Math.Round(val * chartVal.Probability)); } } } } var item = result.WhatIfCalendar.FirstOrDefault(x => x.ScenarioName.Equals(s.Name)); item.QuantityValues.Add(val); } #region Vacations if (model.ChartData.ContainsKey("Vacation")) { var chartVal = model.ChartData["Vacation"].FirstOrDefault(t => t.WeekEndingDate.HasValue && endDate.ToShortDateString().Equals(t.WeekEndingDate.Value.ToShortDateString())); long val = 0; if (chartVal != null) { val = cost ? (long)chartVal.Cost : (long)(chartVal.Quantity); if (graphType == GraphType.Probability && CG.Equals(chartVal.CGEFX.Trim())) { val = Convert.ToInt64(Math.Round(val * chartVal.Probability)); } } vacationRow.QuantityValues.Add(val); } #endregion #region Trainings if (model.ChartData.ContainsKey("Training")) { var chartVal = model.ChartData["Training"].FirstOrDefault(t => t.WeekEndingDate.HasValue && endDate.ToShortDateString().Equals(t.WeekEndingDate.Value.ToShortDateString())); long val = 0; if (chartVal != null) { val = cost ? (long)chartVal.Cost : (long)(chartVal.Quantity); if (graphType == GraphType.Probability && CG.Equals(chartVal.CGEFX.Trim())) { val = Convert.ToInt64(Math.Round(val * chartVal.Probability)); } } trainingRow.QuantityValues.Add(val); } #endregion } result.FiscalCalendarRecordHeaders = gridHeaders.Select(t=>t.ToShortDateString()).ToList(); result.FiscalCalendarRecordMilliseconds = gridHeaders.Select(t => (long)t.Subtract(Constants.UnixEpochDate).TotalMilliseconds).ToList(); //JavaScriptSerializer js = new JavaScriptSerializer(); //string strJSON = js.Serialize(result); //return strJSON; return Json(result, JsonRequestBehavior.AllowGet); } } }