449 lines
19 KiB
C#
449 lines
19 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel.DataAnnotations;
|
||
using EnVisage.Code;
|
||
using System.Linq;
|
||
|
||
namespace EnVisage.Models
|
||
{
|
||
public class SingleResQtiesOrCostByTimeModel
|
||
{
|
||
#region Classes and enums
|
||
|
||
public class ScenarioListElement
|
||
{
|
||
public System.Guid Id { get; set; }
|
||
public string Name { get; set; }
|
||
public string ProjectName { get; set; }
|
||
public ScenarioType ScenarioType { get; set; }
|
||
public Nullable<decimal> CGSplit { get; set; }
|
||
public Nullable<decimal> EFXSplit { get; set; }
|
||
public Nullable<System.DateTime> StartDate { get; set; }
|
||
public Nullable<System.DateTime> EndDate { get; set; }
|
||
public decimal? Probability { get; set; }
|
||
}
|
||
|
||
public class СhartListElement
|
||
{
|
||
|
||
public Nullable<System.Guid> ScenarioId { get; set; }
|
||
public System.DateTime WeekEndingDate { get; set; }
|
||
public Nullable<decimal> Quantity { get; set; }
|
||
public Nullable<decimal> Cost { get; set; }
|
||
public Nullable<int> ScenarioType { get; set; }
|
||
public string ScenarioName { get; set; }
|
||
public string ScenarioDispalyName { get; set; }
|
||
public decimal Probability { get; set; }
|
||
public string ProjectColor { get; set; }
|
||
public string ProjectName { get; set; }
|
||
public string CGEFX { get; set; }
|
||
}
|
||
|
||
public class ExpenditureItem
|
||
{
|
||
public Guid Id { get; set; }
|
||
public string Group { get; set; }
|
||
public string Name { get; set; }
|
||
public bool Checked { get; set; }
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region Properties
|
||
|
||
public List<ScenarioListElement> Scenarios { get; set; }
|
||
public string SelectedScenarios { get; set; }
|
||
public Guid? GroupId { get; set; }
|
||
|
||
[Display(Name = "Scenario Group")]
|
||
public string GroupName { get; set; }
|
||
|
||
[Display(Name = "Scenario Type")]
|
||
public ScenarioType SelectedScenarioType { get; set; }
|
||
|
||
[Display(Name = "Quantity/Costs")]
|
||
public SeatsCostsType SeatsCosts { get; set; }
|
||
|
||
[Display(Name = "System Attribute 1")]
|
||
public string SystemAttribute1 { get; set; }
|
||
|
||
[Display(Name = "System Attribute 2")]
|
||
public string SystemAttribute2 { get; set; }
|
||
|
||
[DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:MM/dd/yy}", ApplyFormatInEditMode = true)]
|
||
[Display(Name = "Start Date")]
|
||
public DateTime StartDate { get; set; }
|
||
|
||
[DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:MM/dd/yy}", ApplyFormatInEditMode = true)]
|
||
[Display(Name = "End Date")]
|
||
public DateTime EndDate { get; set; }
|
||
|
||
public Dictionary<string, List<СhartListElement>> ChartData { get; set; }
|
||
List<СhartListElement> _chartData;
|
||
|
||
public IList<ExpenditureItem> ExpenditureItems { get; set; }
|
||
|
||
public string SelectedExpenditureItems { get; set; }
|
||
|
||
public bool? UOMSwitcherMode { get; set; }
|
||
|
||
#endregion
|
||
|
||
public SingleResQtiesOrCostByTimeModel()
|
||
{
|
||
GroupId = Guid.Empty;
|
||
GroupName = string.Empty;
|
||
SelectedScenarioType = ScenarioType.Portfolio;
|
||
StartDate = DateTime.Today.AddMonths(-3);
|
||
EndDate = DateTime.Today.AddMonths(3);
|
||
|
||
ExpenditureItems = new List<ExpenditureItem>();
|
||
}
|
||
|
||
#region Methods
|
||
|
||
public void FormScenarioReport(EnVisageEntities db)
|
||
{
|
||
Scenarios = GetScenarioData(db);
|
||
}
|
||
|
||
public void FormСhartListElements(EnVisageEntities db)
|
||
{
|
||
_chartData = GetСhartData(db);
|
||
ChartData = new Dictionary<string, List<СhartListElement>>();
|
||
if (_chartData != null)
|
||
{
|
||
foreach (var element in _chartData)
|
||
{
|
||
if (!ChartData.Keys.Contains(element.ProjectName))
|
||
ChartData.Add(element.ProjectName, new List<СhartListElement>());
|
||
|
||
ChartData[element.ProjectName].Add(element);
|
||
}
|
||
}
|
||
}
|
||
|
||
List<ScenarioListElement> GetScenarioData(EnVisageEntities db)
|
||
{
|
||
|
||
return db.VW_Scenario2Project.Where(t => t.Status.HasValue && t.Status.Value == (int?)ScenarioStatus.Active &&
|
||
(((GroupId == Guid.Empty || t.GroupId == GroupId) &&
|
||
t.Type == (int?)SelectedScenarioType)))
|
||
.Select(x =>
|
||
new ScenarioListElement()
|
||
{
|
||
Name = x.Name,
|
||
CGSplit = x.CGSplit,
|
||
EFXSplit = x.EFXSplit,
|
||
EndDate = x.EndDate,
|
||
StartDate = x.StartDate,
|
||
Id = x.Id,
|
||
ProjectName = x.ShowName + (x.ParentProjectId != null ? ": " + x.ParentProjectName : ""),
|
||
ScenarioType = (ScenarioType)x.Type,
|
||
Probability = x.Probability
|
||
})
|
||
.ToList();
|
||
}
|
||
|
||
List<СhartListElement> GetСhartData(EnVisageEntities db)
|
||
{
|
||
List<Guid> l = new List<Guid>();
|
||
List<Guid> SelectedExpenditures = new List<Guid>();
|
||
|
||
Guid SystemAttribute1Id;
|
||
if (!Guid.TryParse(SystemAttribute1, out SystemAttribute1Id))
|
||
SystemAttribute1Id = Guid.Empty;
|
||
Guid SystemAttribute2Id;
|
||
if (!Guid.TryParse(SystemAttribute2, out SystemAttribute2Id))
|
||
SystemAttribute2Id = Guid.Empty;
|
||
|
||
if (string.IsNullOrEmpty(SelectedScenarios))
|
||
{
|
||
SelectedScenarios = "[]";
|
||
}
|
||
if (string.IsNullOrEmpty(SelectedExpenditureItems))
|
||
{
|
||
SelectedExpenditureItems = "[]";
|
||
}
|
||
|
||
SelectedScenarios.TrimStart('[').TrimEnd(']').Split(',').Where(x => !string.IsNullOrEmpty(x)).ToList().ForEach(x =>
|
||
l.Add(new Guid(x.TrimStart('\"').TrimEnd('\"'))));
|
||
|
||
SelectedExpenditureItems.TrimStart('[').TrimEnd(']').Split(',').Where(x => !string.IsNullOrEmpty(x)).ToList().ForEach(x =>
|
||
SelectedExpenditures.Add(new Guid(x.TrimStart('\"').TrimEnd('\"'))));
|
||
//Get by type: Capacity LoanOut Training Vacation
|
||
var selectedScenarioType = this.SelectedScenarioType.GetHashCode();
|
||
|
||
var expCats = db.ExpenditureCategory.AsNoTracking().ToDictionary(e => e.Id);
|
||
var uoms = db.UOMs.AsNoTracking().ToDictionary(u => u.Id);
|
||
|
||
//get capacity, training and vacation data
|
||
#region capacity
|
||
List<Guid> validScenarioIds = (from t in db.Teams where t.ActualCapacityScenarioId != null select t.ActualCapacityScenarioId.Value).ToList();
|
||
validScenarioIds.AddRange((from t in db.Teams where t.PlannedCapacityScenarioId != null select t.PlannedCapacityScenarioId.Value).ToList());
|
||
|
||
var result = (from sd in db.ScenarioDetail
|
||
join sc in db.Scenarios on sd.ParentID equals sc.Id
|
||
join ec in db.ExpenditureCategory on sd.ExpenditureCategoryId equals ec.Id
|
||
where ((ScenarioType)sc.Type == ScenarioType.TeamPlannedCapacity || (ScenarioType)sc.Type == ScenarioType.TeamActualCapacity) &&
|
||
SelectedExpenditures.Contains(sd.ExpenditureCategoryId ?? Guid.Empty)
|
||
&& sd.WeekEndingDate >= StartDate && sd.WeekEndingDate <= EndDate && validScenarioIds.Contains(sc.Id)
|
||
select new
|
||
{
|
||
ScenarioId = sd.ParentID,
|
||
sd.WeekEndingDate,
|
||
sd.Quantity,
|
||
sd.Cost,
|
||
sd.ExpenditureCategoryId,
|
||
ec.CGEFX,
|
||
ScenarioType = sc.Type,
|
||
}).ToArray().Select(t => new
|
||
{
|
||
t.ScenarioId,
|
||
t.WeekEndingDate,
|
||
Quantity = t.Quantity * Utils.GetUOMMultiplier(expCats, uoms, t.ExpenditureCategoryId ?? Guid.Empty, UOMSwitcherMode),
|
||
t.Cost,
|
||
t.ExpenditureCategoryId,
|
||
t.CGEFX,
|
||
t.ScenarioType,
|
||
}).GroupBy(t => new
|
||
{
|
||
t.WeekEndingDate,
|
||
t.ScenarioType,
|
||
t.CGEFX,
|
||
t.ExpenditureCategoryId,
|
||
})
|
||
.Select(x => new СhartListElement()
|
||
{
|
||
ScenarioId = null,
|
||
WeekEndingDate = x.Key.WeekEndingDate.Value,
|
||
Quantity = x.Sum(s => s.Quantity),
|
||
Cost = x.Sum(s => s.Cost),
|
||
ScenarioType = x.Key.ScenarioType,
|
||
ScenarioName = ((ScenarioType)x.Key.ScenarioType).ToDisplayValue(),
|
||
ProjectName = ((ScenarioType)x.Key.ScenarioType).ToDisplayValue(),
|
||
ScenarioDispalyName = ((ScenarioType)x.Key.ScenarioType).ToDisplayValue(),
|
||
Probability = 1,
|
||
ProjectColor = "",
|
||
CGEFX = x.Key.CGEFX
|
||
})
|
||
.OrderBy(x => x.WeekEndingDate)
|
||
.ThenBy(x => x.ScenarioName)
|
||
.ToList();
|
||
#endregion
|
||
|
||
#region scenarios
|
||
//Get by selected scenrios
|
||
result.AddRange((from sd in db.ScenarioDetail
|
||
join sc in db.Scenarios on sd.ParentID equals sc.Id
|
||
join ec in db.ExpenditureCategory on sd.ExpenditureCategoryId equals ec.Id
|
||
join pr in db.Projects on sc.ParentId equals pr.Id
|
||
join st in db.Status on pr.StatusId equals st.Id
|
||
join gr in db.Scenario2Group on sc.Id equals gr.ScenarioId into gj
|
||
from subquery in gj.DefaultIfEmpty()
|
||
where sc.Type > 1 & sc.Type == selectedScenarioType &
|
||
sc.Status == (int?) ScenarioStatus.Active
|
||
& SelectedExpenditures.Contains(sd.ExpenditureCategoryId ?? Guid.Empty) &
|
||
sd.WeekEndingDate >= StartDate & sd.WeekEndingDate <= EndDate &
|
||
(Guid.Empty.Equals(SystemAttribute1Id) || SystemAttribute1Id == ec.SystemAttributeOne) &
|
||
(Guid.Empty.Equals(SystemAttribute2Id) || SystemAttribute2Id == ec.SystemAttributeTwo) &
|
||
sd.ParentID.HasValue && l.Contains(sd.ParentID.Value) &
|
||
((!GroupId.HasValue || GroupId.Value.Equals(Guid.Empty)) || subquery.GroupId == GroupId)
|
||
select new
|
||
{
|
||
ScenarioId = sd.ParentID,
|
||
sd.WeekEndingDate,
|
||
sd.Quantity,
|
||
sd.Cost,
|
||
sd.ExpenditureCategoryId,
|
||
ec.CGEFX,
|
||
ScenarioType = sc.Type,
|
||
ScenarioName = sc.Name,
|
||
ProjectId = pr.Id,
|
||
ProjectName = pr.Name + (pr.ParentProject != null ? ": " + pr.ParentProject.Name : ""),
|
||
ProjectTypeId = pr.TypeId,
|
||
StatusId = st.Id,
|
||
StatusName = st.Name,
|
||
pr.Probability,
|
||
ProjectColor = !string.IsNullOrEmpty(pr.Color) ? pr.Color :
|
||
(pr.ParentProject != null ? pr.ParentProject.Color : null),
|
||
ScenarioColor = sc.Color,
|
||
sc.Status,
|
||
sc.SystemAttributeObjectID,
|
||
GroupId = subquery == null ? (Guid?)null : subquery.GroupId,
|
||
ec.SystemAttributeOne,
|
||
ec.SystemAttributeTwo
|
||
}).ToArray().Select(t => new
|
||
{
|
||
t.ScenarioId,
|
||
t.WeekEndingDate,
|
||
Quantity = t.Quantity*Utils.GetUOMMultiplier(expCats, uoms, t.ExpenditureCategoryId ?? Guid.Empty, UOMSwitcherMode),
|
||
t.Cost,
|
||
t.ExpenditureCategoryId,
|
||
t.CGEFX,
|
||
t.ScenarioType,
|
||
t.ScenarioName,
|
||
t.ProjectId,
|
||
t.ProjectName,
|
||
t.ProjectTypeId,
|
||
t.StatusId,
|
||
t.StatusName,
|
||
t.Probability,
|
||
t.ProjectColor,
|
||
t.ScenarioColor,
|
||
t.Status,
|
||
t.SystemAttributeObjectID,
|
||
t.GroupId,
|
||
t.SystemAttributeOne,
|
||
t.SystemAttributeTwo
|
||
})
|
||
.GroupBy(t => new
|
||
{
|
||
t.WeekEndingDate,
|
||
t.ScenarioType,
|
||
t.Status,
|
||
t.ScenarioId,
|
||
t.ScenarioName,
|
||
t.ScenarioColor,
|
||
t.Probability,
|
||
t.SystemAttributeObjectID,
|
||
t.ProjectId,
|
||
t.ProjectName,
|
||
t.ProjectTypeId,
|
||
t.ProjectColor,
|
||
t.CGEFX,
|
||
t.ExpenditureCategoryId,
|
||
t.StatusId,
|
||
t.StatusName,
|
||
t.GroupId,
|
||
t.SystemAttributeOne,
|
||
t.SystemAttributeTwo
|
||
})
|
||
.Select(x => new СhartListElement()
|
||
{
|
||
ScenarioId = x.Key.ScenarioId,
|
||
WeekEndingDate = x.Key.WeekEndingDate.Value,
|
||
Quantity = x.Sum(s => s.Quantity),
|
||
Cost = x.Sum(s => s.Cost),
|
||
ScenarioType = x.Key.ScenarioType,
|
||
ScenarioName = x.Key.ScenarioName,
|
||
ProjectName = x.Key.ProjectName,
|
||
Probability = x.Key.Probability,
|
||
ProjectColor = x.Key.ProjectColor,
|
||
CGEFX = x.Key.CGEFX
|
||
})
|
||
.OrderBy(x => x.WeekEndingDate)
|
||
.ThenBy(x => x.ScenarioName)
|
||
.ToList());
|
||
|
||
#endregion
|
||
|
||
#region non-project times
|
||
if (result.Count > 0)
|
||
{
|
||
result.AddRange((from npTimeWeek in db.VW_NonProjectTimeAllocation
|
||
join npTime in db.NonProjectTimes on npTimeWeek.NonProjectTimeId equals npTime.Id
|
||
join p in db.VW_TeamResource on npTimeWeek.PeopleResourceId equals p.Id
|
||
join ec in db.ExpenditureCategory on p.ExpenditureCategoryId equals ec.Id
|
||
join t in db.Team2Project on p.TeamId equals t.TeamId
|
||
where SelectedExpenditures.Contains(p.ExpenditureCategoryId) && npTimeWeek.WeekEndingDate >= StartDate && npTimeWeek.WeekEndingDate <= EndDate
|
||
select new
|
||
{
|
||
npTimeWeek.WeekEndingDate,
|
||
npTimeWeek.HoursOff,
|
||
Rate = db.Rates.FirstOrDefault(r => r.ExpenditureCategoryId == p.ExpenditureCategoryId && r.StartDate <= npTimeWeek.WeekEndingDate && r.EndDate >= npTimeWeek.WeekEndingDate),
|
||
p.ExpenditureCategoryId,
|
||
ec.CGEFX
|
||
}).Distinct().ToArray().Select(t => new
|
||
{
|
||
t.WeekEndingDate,
|
||
Quantity = t.HoursOff * Utils.GetUOMMultiplier(expCats, uoms, t.ExpenditureCategoryId, UOMSwitcherMode),
|
||
Cost = t.HoursOff * (t.Rate != null ? t.Rate.Rate1 : 0),
|
||
t.ExpenditureCategoryId,
|
||
t.CGEFX,
|
||
}).GroupBy(t => new
|
||
{
|
||
t.WeekEndingDate,
|
||
t.CGEFX,
|
||
t.ExpenditureCategoryId,
|
||
})
|
||
.Select(x => new СhartListElement()
|
||
{
|
||
ScenarioId = null,
|
||
WeekEndingDate = x.Key.WeekEndingDate,
|
||
Quantity = x.Sum(s => s.Quantity),
|
||
Cost = x.Sum(s => s.Cost),
|
||
ScenarioType = ScenarioType.Training.GetHashCode(),
|
||
ScenarioName = ScenarioType.Training.ToDisplayValue(),
|
||
ProjectName = ScenarioType.Training.ToDisplayValue(),
|
||
Probability = 1,
|
||
ProjectColor = "",
|
||
CGEFX = x.Key.CGEFX
|
||
})
|
||
.OrderBy(x => x.WeekEndingDate)
|
||
.ThenBy(x => x.ScenarioName)
|
||
.ToList());
|
||
}
|
||
#endregion
|
||
|
||
return result;
|
||
}
|
||
|
||
#endregion
|
||
}
|
||
|
||
public class SingleResQtiesOrCostByTimeDataModel
|
||
{
|
||
public class SingleResQtiesOrCostByTimeDataRow
|
||
{
|
||
public Guid ExpCatId { get; set; }
|
||
public string ScenarioName { get; set; }
|
||
public string ScenarioDisplayName { get; set; }
|
||
public string ScenarioType { get; set; }
|
||
public string ProjectName { get; set; }
|
||
public decimal GrandTotalCost { get; set; }
|
||
public decimal GrandTotalQuantity { get; set; }
|
||
public int Probability { get; set; }
|
||
|
||
public bool Checked { get; set; }
|
||
public List<decimal> QuantityValues { get; set; }
|
||
public List<decimal> CostValues { get; set; }
|
||
|
||
public SingleResQtiesOrCostByTimeDataRow()
|
||
{
|
||
QuantityValues = new List<decimal>();
|
||
CostValues = new List<decimal>();
|
||
}
|
||
}
|
||
|
||
public Guid? CreditDepartment { get; set; }
|
||
[Display(Name = "Quantity Mode")]
|
||
public bool IsTableModeQuantity { get; set; }
|
||
public DateTime StartDate { get; set; }
|
||
public DateTime EndDate { get; set; }
|
||
public ScenarioType? ScenarioType { get; set; }
|
||
public Guid ScenarioId { get; set; }
|
||
public bool GrowthScenario { get; set; }
|
||
public Guid ParentId { get; set; }
|
||
[Display(Name = "Expenditure Categories")]
|
||
public Guid[] SelectedExpCats { get; set; }
|
||
public List<string> FiscalCalendarRecordHeaders { get; set; }
|
||
public List<long> FiscalCalendarRecordMilliseconds { get; set; }
|
||
public List<SingleResQtiesOrCostByTimeDataRow> SingleResQtiesOrCostByTime { get; set; }
|
||
|
||
|
||
public int ColumnsCount
|
||
{
|
||
get
|
||
{
|
||
return FiscalCalendarRecordHeaders != null ? FiscalCalendarRecordHeaders.Count : 0;
|
||
}
|
||
}
|
||
|
||
public SingleResQtiesOrCostByTimeDataModel()
|
||
{
|
||
SingleResQtiesOrCostByTime = new List<SingleResQtiesOrCostByTimeDataRow>();
|
||
|
||
}
|
||
}
|
||
} |