using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;
using System.Data.Entity;
using System.Data.Entity.Core.Objects;
using Microsoft.AspNet.Identity;
using jQuery.DataTables.Mvc;
using EnVisage.Code.HtmlHelpers;
using EnVisage.Models;
using EnVisage.Code;
using EnVisage.Code.BLL;
using EnVisage.Code.Cache;
using System.Net;
using EnVisage.Code.Charts;
namespace EnVisage.Controllers
{
[Authorize]
public class ForecastDashboardController : BaseController
{
///
/// Direct GET to the page - returns main view
///
public ActionResult Index(string menuId, string additionalFilters, PagePreferencesList.PagePreferencesSource src)
{
return PartialView("_forecastDashboard",
new ForecastDashboardOptionsModel {MenuId = menuId, AdditionalFilterParams = additionalFilters, Source = src});
}
///
/// Direct POST request to the page - returns main part of dashboard table - active scenarios
///
[HttpPost]
public JsonResult Index(JQueryDataTablesModel jQueryDataTablesModel, DateTime? filterStartDate, DateTime? startDate, DateTime? filterEndDate, DateTime? endDate, bool filterGroupByTeam)
{
int totalRecordCount;
int searchRecordCount;
var scenarios = GetScenarios(jQueryDataTablesModel.iDisplayStart,
jQueryDataTablesModel.iDisplayLength, jQueryDataTablesModel.GetSortedColumns(),
out totalRecordCount, out searchRecordCount, jQueryDataTablesModel.sSearch,
filterStartDate, startDate, filterEndDate, endDate, filterGroupByTeam);
return this.DataTablesJson(scenarios,
totalRecordCount,
searchRecordCount,
jQueryDataTablesModel.sEcho);
}
///
/// Gets expanded part of the dashboard table - inactive scenarios.
///
/// Scenario Id.
/// Start date to filter received from dashboard control.
///
/// End date to filter received from dashboard control.
///
///
[HttpGet]
[AreaSecurityAttribute(area = Areas.Scenarios, level = AccessLevel.Read)]
public ActionResult Inactive(Guid? id, DateTime? filterStartDate, DateTime? startDate, DateTime? filterEndDate, DateTime? endDate)
{
var dtStart = (filterStartDate ?? startDate) ?? DateTime.Today;
var dtEnd = (filterEndDate ?? endDate) ?? DateTime.Today;
string type = Request.QueryString["type"];
string status = Request.QueryString["status"];
string classification = Request.QueryString["classification"];
string group = Request.QueryString["filterGroup"];
Guid scenarioGroup = Guid.Empty;
Guid.TryParse(group, out scenarioGroup);
Guid shown = new Guid(Request.QueryString["shown"].ToString());
var scenariotype = ((type == "Portfolio") ? ScenarioType.Portfolio : ScenarioType.Scheduling);
if (id == null || id == Guid.Empty)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
if (!HtmlHelpers.CheckProjectPermission(null, id.Value, AccessLevel.Read))
return new HttpStatusCodeResult(HttpStatusCode.Unauthorized);
List list = new List();
try
{
if (id == Guid.Empty)
return HttpNotFound();
else
{
IQueryable scenarios = from s in DbContext.Scenarios
where s.ParentId == id && s.Type == (int)scenariotype && s.StartDate <= dtEnd
&& s.EndDate >= dtStart && s.Status == (int?)ScenarioStatus.Inactive
select s;
if (scenarioGroup != Guid.Empty)
{
scenarios = scenarios.Where(s => s.Scenario2Group.Any(g => g.GroupId == scenarioGroup));
}
if (shown != null)
{
scenarios = scenarios.Where(s => s.Id != shown);
}
foreach (Scenario s in scenarios)
{
var tempscenario = (ScenarioDetailModel)s;
list.Add(new ForecastDashboardModel()
{
Id = s.Id,
ProjectId = (Guid)s.ParentId,
ProjectName = s.Project.Name,
Name = s.Name,
Type = (ScenarioType?)s.Type,
ProjectedRevenue = s.ProjectedRevenue,
ExpectedGrossMargin = s.ExpectedGrossMargin,
ExpectedGrossMargin_LM = s.ExpectedGrossMargin_LM,
CalculatedGrossMargin = s.CalculatedGrossMargin,
CalculatedGrossMargin_LM = s.CalculatedGrossMargin_LM,
UseLMMargin = s.UseLMMargin,
CGSplit = s.CGSplit,
EFXSplit = s.EFXSplit,
StartDate = s.StartDate,
EndDate = s.EndDate,
Priority = s.Project.Priority,
Probability = (decimal)s.Project.Probability,
TDDirectCosts = s.TDDirectCosts,
TDDirectCosts_LM = s.TDDirectCosts_LM,
BUDirectCosts = s.BUDirectCosts,
BUDirectCosts_LM = s.BUDirectCosts_LM,
ScenarioStatus = (ScenarioStatus?)s.Status,
Color = s.Color,
CostSavings = tempscenario.CostSavings,
CostSavingsDuration = tempscenario.CostSavingsDuration,
ROIDate = tempscenario.ROIDate,
HardSoftSavings = tempscenario.HardSoftSavings,
Groups = DbContext.Scenario2Group.Where(x => x.ScenarioId == s.Id).Select(x => x.GroupId).ToList(),
InactiveCount = 0
});
}
}
}
catch (BLLException blEx)
{
if (blEx.DisplayError)
SetErrorScript(message: blEx.Message);
else
{
LogException(blEx);
SetErrorScript();
}
}
catch (Exception exception)
{
LogException(exception);
SetErrorScript();
}
return Json(list, JsonRequestBehavior.AllowGet);
}
///
/// Adds Scenario to the given Scenario Group
///
[HttpPost]
public JsonResult CopyScenarioToGroup(Guid? scenarioId, Guid? groupId)
{
try
{
if (!scenarioId.HasValue || !groupId.HasValue)
throw new Exception("ScenarioId and GroupId can't be null.");
if (scenarioId == Guid.Empty || groupId == Guid.Empty)
throw new Exception("ScenarioId and GroupId can't be empty.");
CopyToGroup(scenarioId.Value, groupId.Value);
return Json(new { Status = "Ok" });
}
catch (Exception ex)
{
return Json(new { Status = "Error", ErrorMsg = ex.Message, ErrorDetails = ex.ToString() });
}
}
///
/// Removes Scenario from the given Scenario Group
///
[HttpPost]
public JsonResult ExtractFromGroup(Guid? scenarioId, Guid? groupId)
{
try
{
if (!scenarioId.HasValue || !groupId.HasValue)
throw new Exception("ScenarioId and GroupId can't be null.");
if (scenarioId == Guid.Empty || groupId == Guid.Empty)
throw new Exception("ScenarioId and GroupId can't be empty.");
DeleteFromGroup(scenarioId.Value, groupId.Value);
return Json(new { Status = "Ok" });
}
catch (Exception ex)
{
return Json(new { Status = "Error", ErrorMsg = ex.Message, ErrorDetails = ex.ToString() });
}
}
///
/// Returns data for main chart
///
public JsonResult GetGraphData(DateTime? StartDate, DateTime? EndDate, string type, string status, string classification, string additionalParams, bool? isUOMHours, bool isLaborMode, string filterGroup)
{
if (StartDate == null) StartDate = new DateTime(DateTime.Today.Year, 1, 1);
if (EndDate == null) EndDate = new DateTime(DateTime.Today.Year, 12, 31);
Dictionary additionalParamsDict = null;
if (!string.IsNullOrEmpty(additionalParams))
{
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
try
{
additionalParamsDict = (Dictionary)serializer.DeserializeObject(additionalParams);
}
catch (Exception exception)
{
LogException(exception);
SetErrorScript();
}
}
List scenarios = new List();
var principals = GetUserPrincipals();
var data = new Dictionary>();
Guid teamId = Guid.Empty;
Guid viewId = Guid.Empty;
ForecastDashboardMode mode = ForecastDashboardMode.MainDashboard;
if (additionalParamsDict != null && additionalParamsDict.ContainsKey("mode") && (string)additionalParamsDict["mode"] == "team" && additionalParamsDict.ContainsKey("teamId"))
{
mode = ForecastDashboardMode.TeamForecast;
Guid.TryParse((string)additionalParamsDict["teamId"], out teamId);
}
if (additionalParamsDict != null && additionalParamsDict.ContainsKey("mode") && (string)additionalParamsDict["mode"] == "view" && additionalParamsDict.ContainsKey("viewId"))
{
mode = ForecastDashboardMode.ViewForecast;
Guid.TryParse((string)additionalParamsDict["viewId"], out viewId);
}
data = new DashboardCharthManager().GetData(StartDate.Value, EndDate.Value, principals, Guid.Parse(User.Identity.GetUserId()), type, status, classification, mode, teamId, viewId, isUOMHours, isLaborMode, filterGroup);
var headers = new List