297 lines
14 KiB
C#
297 lines
14 KiB
C#
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 SingleResQtiesOrCostByTimeController : BaseController
|
||
{
|
||
public enum GraphType{
|
||
General = 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 SingleResQtiesOrCostByTimeModel();
|
||
model.ExpenditureItems = GetExpenditureCategories();
|
||
model.SelectedExpenditureItems = "[" + string.Join(",", model.ExpenditureItems.Select(x => "\"" + x.Id.ToString() + "\"").ToArray()) + "]";
|
||
|
||
model.FormScenarioReport(DbContext);
|
||
|
||
var l = new List<Guid>();
|
||
foreach (var s in model.Scenarios)
|
||
{
|
||
l.Add(s.Id);
|
||
}
|
||
model.SelectedScenarios = "[" + string.Join(",", l.Select(x=>"\"" + x.ToString() + "\"").ToArray()) + "]";
|
||
|
||
//model.SelectedCapacityId = Guid.Parse("{B04238F5-32A6-4EA2-A4E7-22D64439229A}");
|
||
|
||
model.FormСhartListElements(DbContext);
|
||
var user = new UsersCache().Value.FirstOrDefault(x => x.Id == new Guid(HttpContext.User.Identity.GetUserId()));
|
||
if (user != null)
|
||
ViewBag.IsUOMHours = !user.PreferredResourceAllocation;
|
||
return View(model);
|
||
}
|
||
|
||
[HttpPost]
|
||
[AreaSecurityAttribute(area = Areas.Reports, level = AccessLevel.Read)]
|
||
public ActionResult Index(SingleResQtiesOrCostByTimeModel model)
|
||
{
|
||
ModelState.Clear();
|
||
//model.SelectedCapacityId = Guid.Parse("{B04238F5-32A6-4EA2-A4E7-22D64439229A}");
|
||
model.FormScenarioReport(DbContext);
|
||
model.FormСhartListElements(DbContext);
|
||
model.ExpenditureItems = GetExpenditureCategories();
|
||
|
||
//var l = new List<Guid>();
|
||
|
||
//foreach (var s in model.Scenarios)
|
||
//{
|
||
// l.Add(s.Id);
|
||
//}
|
||
//model.SelectedScenarios = "[" + string.Join(",", l.Select(x => "\"" + x.ToString() + "\"").ToArray()) + "]";
|
||
//return View(model);
|
||
return PartialView("_filter", model);
|
||
}
|
||
|
||
[HttpPost]
|
||
[AreaSecurityAttribute(area = Areas.Reports, level = AccessLevel.Read)]
|
||
public ActionResult Filter(SingleResQtiesOrCostByTimeModel model)
|
||
{
|
||
//model.SelectedCapacityId = Guid.Parse("{B04238F5-32A6-4EA2-A4E7-22D64439229A}");
|
||
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<Guid> selectedScenarios, List<Guid> selectedExpenditureItems,
|
||
GraphType graphType, ScenarioType type, Guid? groupId, string SystemAttributeOne, string SystemAttributeTwo, Guid? selectedCapacity, bool? isUOMHours)
|
||
{
|
||
if (selectedScenarios == null)
|
||
selectedScenarios = new List<Guid>();
|
||
|
||
if (selectedExpenditureItems == null)
|
||
selectedExpenditureItems = new List<Guid>();
|
||
|
||
SingleResQtiesOrCostByTimeDataModel result = new SingleResQtiesOrCostByTimeDataModel();
|
||
StartDate = StartDate ?? DateTime.Now.AddMonths(-6);
|
||
EndDate = EndDate ?? DateTime.Now.AddMonths(6);
|
||
|
||
var model = new SingleResQtiesOrCostByTimeModel()
|
||
{
|
||
StartDate = StartDate.Value,
|
||
EndDate = EndDate.Value,
|
||
|
||
SelectedExpenditureItems = "[" + string.Join(",", selectedExpenditureItems.Select(x => x.ToString()).ToArray()) + "]",
|
||
SelectedScenarioType= type,
|
||
GroupId = groupId,
|
||
SystemAttribute1 = SystemAttributeOne,
|
||
SystemAttribute2 = SystemAttributeTwo,
|
||
SelectedCapacity = selectedCapacity.Value,
|
||
UOMSwitcherMode = isUOMHours
|
||
};
|
||
model.FormScenarioReport(DbContext);
|
||
|
||
selectedScenarios.RemoveAll(x => !model.Scenarios.Select(s => s.Id).Contains(x));
|
||
model.SelectedScenarios = "[" + string.Join(",", selectedScenarios.Select(x => x.ToString()).ToArray()) + "]";
|
||
|
||
//model.SelectedCapacityId = Guid.Parse("{B04238F5-32A6-4EA2-A4E7-22D64439229A}");
|
||
|
||
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)
|
||
.OrderBy(t => t.EndDate)
|
||
.ToArray();
|
||
|
||
var gridHeaders = weeks.Select(t => t.EndDate).ToList();
|
||
|
||
List<ScenarioType> ScenarioTypes = new List<ScenarioType>(){
|
||
ScenarioType.Capacity,
|
||
ScenarioType.LoanOut,
|
||
ScenarioType.Training,
|
||
ScenarioType.Vacation
|
||
};
|
||
|
||
foreach (var s in model.Scenarios.Where(t => selectedScenarios.Contains(t.Id)))
|
||
{
|
||
result.SingleResQtiesOrCostByTime.Add(new SingleResQtiesOrCostByTimeDataModel.SingleResQtiesOrCostByTimeDataRow()
|
||
{
|
||
ScenarioName = s.Name,
|
||
QuantityValues = new List<decimal>(),
|
||
CostValues = new List<decimal>()
|
||
});
|
||
}
|
||
|
||
foreach (var s in ScenarioTypes)
|
||
{
|
||
result.SingleResQtiesOrCostByTime.Add(new SingleResQtiesOrCostByTimeDataModel.SingleResQtiesOrCostByTimeDataRow()
|
||
{
|
||
ScenarioName = s.ToDisplayValue(),
|
||
QuantityValues = new List<decimal>(),
|
||
CostValues = new List<decimal>()
|
||
});
|
||
}
|
||
|
||
for (int i = 0; i < weeks.Count(); i++)
|
||
{
|
||
var endDate = weeks[i].EndDate;
|
||
var scenarioName = string.Empty;
|
||
|
||
foreach (var s in ScenarioTypes)
|
||
{
|
||
decimal qty_val = 0;
|
||
decimal cost_val = 0;
|
||
if (model.ChartData.ContainsKey(s.ToDisplayValue()))
|
||
{
|
||
var scenario = model.ChartData[s.ToDisplayValue()];
|
||
var chartVals = new List<SingleResQtiesOrCostByTimeModel.СhartListElement>();
|
||
if (ScenarioType.Vacation.GetHashCode().Equals(scenario.FirstOrDefault().ScenarioType) ||
|
||
ScenarioType.Training.GetHashCode().Equals(scenario.FirstOrDefault().ScenarioType))
|
||
chartVals = scenario.Where(t => endDate >= t.WeekEndingDate && t.WeekEndingDate >= endDate.AddDays(-6)).ToList();
|
||
else
|
||
chartVals = scenario.Where(t => endDate.ToShortDateString().Equals(t.WeekEndingDate.ToShortDateString())).ToList();
|
||
|
||
qty_val = (decimal)chartVals.Select(x=>x.Quantity).Sum();
|
||
if (graphType == GraphType.Probability) // && CG.Equals(chartVal.CGEFX.Trim()))
|
||
{
|
||
//qty_val = Convert.ToDecimal(qty_val * chartVal.Probability);
|
||
qty_val = Convert.ToDecimal(Math.Round((decimal)chartVals.Where(chartVal => CG.Equals(chartVal.CGEFX.Trim())).
|
||
Select(chartVal => chartVal.Quantity * chartVal.Probability).Sum(), 2));
|
||
}
|
||
|
||
cost_val = Math.Round((decimal)chartVals.Select(x => x.Cost).Sum(), 2);
|
||
if (graphType == GraphType.Probability) // && CG.Equals(chartVal.CGEFX.Trim()))
|
||
{
|
||
cost_val = Convert.ToDecimal(Math.Round((decimal)chartVals.Where(chartVal=>CG.Equals(chartVal.CGEFX.Trim())).
|
||
Select(chartVal => chartVal.Cost * chartVal.Probability).Sum(), 2));
|
||
}
|
||
scenarioName = scenario.FirstOrDefault().ScenarioDispalyName;
|
||
}
|
||
var item = result.SingleResQtiesOrCostByTime.FirstOrDefault(x => x.ScenarioName.Equals(s.ToDisplayValue()));
|
||
item.QuantityValues.Add(qty_val);
|
||
//if (s == ScenarioType.Capacity)
|
||
item.ScenarioDisplayName = scenarioName;
|
||
item.ScenarioType = s.ToDisplayValue();
|
||
item.CostValues.Add(cost_val);
|
||
}
|
||
|
||
foreach (var s in model.Scenarios.Where(t => selectedScenarios.Contains(t.Id)))
|
||
{
|
||
decimal qty_val = 0;
|
||
decimal cost_val = 0;
|
||
if (model.ChartData.ContainsKey(s.Name))
|
||
{
|
||
var scenario = model.ChartData[s.Name];
|
||
var chartVals = scenario.Where(t => endDate.ToShortDateString().Equals(t.WeekEndingDate.ToShortDateString())).ToList();
|
||
qty_val = (decimal)chartVals.Select(x => x.Quantity).Sum();
|
||
if (graphType == GraphType.Probability) // && CG.Equals(chartVal.CGEFX.Trim()))
|
||
{
|
||
//qty_val = Convert.ToDecimal(qty_val * chartVal.Probability);
|
||
qty_val = Convert.ToDecimal(Math.Round((decimal)chartVals.Where(chartVal => CG.Equals(chartVal.CGEFX.Trim())).
|
||
Select(chartVal => chartVal.Quantity * chartVal.Probability).Sum(), 2));
|
||
}
|
||
|
||
cost_val = Math.Round((decimal)chartVals.Select(x => x.Cost).Sum(), 2);
|
||
if (graphType == GraphType.Probability) // && CG.Equals(chartVal.CGEFX.Trim()))
|
||
{
|
||
cost_val = Convert.ToDecimal(Math.Round((decimal)chartVals.Where(chartVal => CG.Equals(chartVal.CGEFX.Trim())).
|
||
Select(chartVal => chartVal.Cost * chartVal.Probability).Sum(), 2));
|
||
}
|
||
}
|
||
var items = result.SingleResQtiesOrCostByTime.Where(x => x.ScenarioName.Equals(s.Name)).ToList();
|
||
items.ForEach(x => x.QuantityValues.Add(qty_val));
|
||
items.ForEach(x => x.CostValues.Add(cost_val));
|
||
items.ForEach(x => x.ProjectName = s.ProjectName);
|
||
|
||
//item.QuantityValues.Add(qty_val);
|
||
//item.CostValues.Add(cost_val);
|
||
//item.ProjectName = s.ProjectName;
|
||
}
|
||
}
|
||
|
||
result.FiscalCalendarRecordHeaders = gridHeaders.Select(t=>t.ToShortDateString()).ToList();
|
||
result.FiscalCalendarRecordMilliseconds = gridHeaders.Select(t => (long)t.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds).ToList();
|
||
|
||
//JavaScriptSerializer js = new JavaScriptSerializer();
|
||
//string strJSON = js.Serialize(result);
|
||
//return strJSON;
|
||
|
||
return Json(result, JsonRequestBehavior.AllowGet);
|
||
}
|
||
|
||
public IList<SingleResQtiesOrCostByTimeModel.ExpenditureItem> GetExpenditureCategories()
|
||
{
|
||
//if (templateId == null || templateId == Guid.Empty)
|
||
// return new ScenarioModel.ExpenditureItem[0];
|
||
|
||
var list = new List<SingleResQtiesOrCostByTimeModel.ExpenditureItem>();
|
||
var categories = DbContext.VW_ExpCategoriesInScenario.ToArray();
|
||
var lstIds = categories.Select(t => t.Id).ToList();
|
||
var arrCalculatedIds = categories.Where(t => t.UseType == (int)ExpenditureCategoryModel.UseTypes.Calculated).Select(t => t.Id).ToArray();
|
||
|
||
if (arrCalculatedIds.Length > 0)
|
||
{
|
||
foreach (var item in DbContext.VW_Expenditure2Calculation.Where(t => arrCalculatedIds.Contains(t.ParentId)))
|
||
{
|
||
if (!item.ExpenditureCategoryID.HasValue)
|
||
continue;
|
||
if (!lstIds.Contains(item.ExpenditureCategoryID.Value))
|
||
lstIds.Add(item.ExpenditureCategoryID.Value);
|
||
}
|
||
}
|
||
// add not checked expenditure categories for Labor/Materials/Usage types
|
||
list.AddRange(DbContext.VW_Expenditure2Category.Where(t => !lstIds.Contains(t.Id))
|
||
.OrderBy(t => t.Type)
|
||
.ThenBy(t => t.ExpenditureName)
|
||
.Select(t => new
|
||
{
|
||
t.Id,
|
||
t.ExpenditureName,
|
||
t.Type
|
||
}).ToArray()
|
||
.Select(t => new SingleResQtiesOrCostByTimeModel.ExpenditureItem
|
||
{
|
||
Id = t.Id,
|
||
Group = ((ExpenditureCategoryModel.CategoryTypes)(t.Type ?? 0)).ToDisplayValue(),
|
||
Name = t.ExpenditureName,
|
||
Checked = false
|
||
}));
|
||
// add checked expenditure categories from template
|
||
list.AddRange(DbContext.VW_Expenditure2Category.Where(t => lstIds.Contains(t.Id))
|
||
.OrderBy(t => t.ExpenditureName)
|
||
.Select(t => new SingleResQtiesOrCostByTimeModel.ExpenditureItem
|
||
{
|
||
Id = t.Id,
|
||
Group = Constants.EXPENDITURE_INTEMPLATE_TITLE,
|
||
Name = t.ExpenditureName,
|
||
Checked = true
|
||
}).ToArray());
|
||
return list;
|
||
}
|
||
}
|
||
}
|