public List <T> GetPeriodsForRegularAmount(RMDataSourceGL dsGL, Dictionary <string, T> periodsForKey) { if (dsGL.StartPeriod != null) { var periods = new List <T>(); string per = RMReportWildcard.EnsureWildcard(dsGL.StartPeriod, _perWildcard); if (!per.Contains(_perWildcard) && dsGL.StartPeriodOffset != null && dsGL.StartPeriodOffset != 0 || dsGL.StartPeriodYearOffset != 0) { per = GetFinPeriod(per, dsGL.StartPeriodYearOffset, dsGL.StartPeriodOffset); } if (dsGL.EndPeriod == null) { if (!per.Contains(_perWildcard)) { T v; if (periodsForKey.TryGetValue(per, out v)) { periods.Add(v); } } else { foreach (var p in periodsForKey) { if (RMReportWildcard.IsLike(per, p.Key)) { periods.Add(p.Value); } } } } else { string toper = RMReportWildcard.EnsureWildcard(dsGL.EndPeriod, _perWildcard); if (!toper.Contains(RMReportConstants.DefaultWildcardChar) && dsGL.EndPeriodOffset != null && dsGL.EndPeriodOffset != 0 || dsGL.EndPeriodYearOffset != 0) { toper = GetFinPeriod(toper, dsGL.EndPeriodYearOffset, dsGL.EndPeriodOffset); } toper = toper.Replace(RMReportConstants.DefaultWildcardChar, '9'); foreach (var p in periodsForKey) { if (RMReportWildcard.IsBetween(per, toper, p.Key)) { periods.Add(p.Value); } } } return(periods); } else { return(periodsForKey.Values.ToList <T>()); } }
public List <T> GetPeriodsForBeginningBalanceAmount(RMDataSourceGL dsGL, Dictionary <string, T> periodsForKey, bool limitToStartYear, out bool takeLast) { takeLast = false; if (dsGL.StartPeriod != null) { string per = RMReportWildcard.EnsureWildcard(dsGL.StartPeriod, _perWildcard); if (!per.Contains(_perWildcard)) { per = GetFinPeriod(per, dsGL.StartPeriodYearOffset, dsGL.StartPeriodOffset); string mostRecentPeriod; if (limitToStartYear) { // For profit and loss accounts, we only look into same fiscal year as the end period. mostRecentPeriod = GetMostRecentPeriodInList(periodsForKey, per.Substring(0, 4), per); } else { // For other type of accounts, we retrive the most recent row, no matter what is the year mostRecentPeriod = GetMostRecentPeriodInList(periodsForKey, per); } if (String.IsNullOrEmpty(mostRecentPeriod)) { return(null); } else { takeLast = per != mostRecentPeriod; return(new List <T> { periodsForKey[mostRecentPeriod] }); } } else { return(periodsForKey.Values.ToList <T>()); } } else { return(periodsForKey.Values.ToList <T>()); } }
private object CalculateAndExpandValue(bool drilldown, RMDataSource ds, RMDataSourceGL dsGL, RMDataSourcePM dsPM, ARmDataSet dataSet, List <PMAccountGroup> accountGroups, List <PMProject> projects, List <PMTask> tasks, List <InventoryItem> items, List <object[]> splitret) { decimal totalAmount = 0; object locker = new object(); Dictionary <PMHistoryKeyTuple, PXResult <PMHistory, PMProject, PMTask, PMAccountGroup, InventoryItem> > drilldownData = null; if (drilldown) { drilldownData = new Dictionary <PMHistoryKeyTuple, PXResult <PMHistory, PMProject, PMTask, PMAccountGroup, InventoryItem> >(); } Parallel.For(0, accountGroups.Count, accountGroup => { PMAccountGroup currentAccountGroup = accountGroups[accountGroup]; if (!_historySegments.Contains(new PMHistoryKeyTuple(0, String.Empty, currentAccountGroup.GroupID.Value, 0))) { return; } Parallel.For(0, projects.Count, project => { PMProject currentProject = projects[project]; if (!_historySegments.Contains(new PMHistoryKeyTuple(currentProject.ContractID.Value, String.Empty, currentAccountGroup.GroupID.Value, 0))) { return; } Parallel.For(0, tasks.Count, task => { PMTask currentTask = tasks[task]; if (!_historySegments.Contains(new PMHistoryKeyTuple(currentProject.ContractID.Value, currentTask.TaskCD, currentAccountGroup.GroupID.Value, 0))) { return; } if (ds.Expand != ExpandType.ProjectTask && currentTask.ProjectID != currentProject.ContractID) { return; } Parallel.For(0, items.Count, item => { InventoryItem currentItem = items[item]; List <PMHistory> periods = GetPeriodsToCalculate(currentProject, currentTask, currentAccountGroup, currentItem, ds, dsGL); if (periods == null) { return; } foreach (var hist in periods) { decimal amount = GetAmountFromPMHistory(ds, hist); lock (locker) { totalAmount += amount; } if (drilldown) { var key = new PMHistoryKeyTuple(currentProject.ContractID.Value, currentTask.TaskCD, currentAccountGroup.GroupID.Value, currentItem.InventoryID.Value); PXResult <PMHistory, PMProject, PMTask, PMAccountGroup, InventoryItem> drilldownRow = null; lock (drilldownData) { if (!drilldownData.TryGetValue(key, out drilldownRow)) { drilldownRow = new PXResult <PMHistory, PMProject, PMTask, PMAccountGroup, InventoryItem>(new PMHistory(), currentProject, currentTask, currentAccountGroup, currentItem); drilldownData.Add(key, drilldownRow); } } lock (drilldownRow) { AggregatePMHistoryForDrilldown(drilldownRow, hist); } } if (ds.Expand == ExpandType.AccountGroup) { lock (currentAccountGroup) { splitret[accountGroup][2] = (decimal)splitret[accountGroup][2] + amount; if (splitret[accountGroup][3] == null) { var dataSetCopy = new ARmDataSet(dataSet); dataSetCopy[PX.Objects.CS.RMReportReaderPM.Keys.StartAccountGroup] = dataSetCopy[PX.Objects.CS.RMReportReaderPM.Keys.EndAccountGroup] = currentAccountGroup.GroupCD; splitret[accountGroup][3] = dataSetCopy; } } } else if (ds.Expand == ExpandType.Project) { lock (currentProject) { splitret[project][2] = (decimal)splitret[project][2] + amount; if (splitret[project][3] == null) { var dataSetCopy = new ARmDataSet(dataSet); dataSetCopy[PX.Objects.CS.RMReportReaderPM.Keys.StartProject] = dataSetCopy[PX.Objects.CS.RMReportReaderPM.Keys.EndProject] = currentProject.ContractCD; splitret[project][3] = dataSetCopy; } } } else if (ds.Expand == ExpandType.ProjectTask) { lock (currentItem) { splitret[task][2] = (decimal)splitret[task][2] + amount; if (splitret[task][3] == null) { var dataSetCopy = new ARmDataSet(dataSet); dataSetCopy[PX.Objects.CS.RMReportReaderPM.Keys.StartProjectTask] = dataSetCopy[PX.Objects.CS.RMReportReaderPM.Keys.EndProjectTask] = currentTask.TaskCD; splitret[task][3] = dataSetCopy; } } } else if (ds.Expand == ExpandType.Inventory) { lock (currentItem) { splitret[item][2] = (decimal)splitret[item][2] + amount; if (splitret[item][3] == null) { var dataSetCopy = new ARmDataSet(dataSet); dataSetCopy[PX.Objects.CS.RMReportReaderPM.Keys.StartInventory] = dataSetCopy[PX.Objects.CS.RMReportReaderPM.Keys.EndInventory] = currentItem.InventoryCD; splitret[item][3] = dataSetCopy; } } } } }); }); }); }); if (drilldown) { var resultset = new PXResultset <PMHistory, PMProject, PMTask, PMAccountGroup, InventoryItem>(); foreach (var r in from row in drilldownData.Values let projectCD = ((PMProject)row[typeof(PMProject)]).With(_ => _.ContractCD) let taskCD = ((PMTask)row[typeof(Sub)]).With(_ => _.TaskCD) let accGroupCD = ((PMAccountGroup)row[typeof(PMAccountGroup)]).With(_ => _.GroupCD) let inventoryCD = ((InventoryItem)row[typeof(InventoryItem)]).With(_ => _.InventoryCD) orderby projectCD, taskCD, accGroupCD, inventoryCD select row) { resultset.Add(r); } return(resultset); } else if (ds.Expand != ExpandType.Nothing) { return(splitret); } else { return(totalAmount); } }
private List <PMHistory> GetPeriodsToCalculate(PMProject project, PMTask task, PMAccountGroup accountGroup, InventoryItem item, RMDataSource ds, RMDataSourceGL dsGL) { Dictionary <string, PMHistory> periodsForKey = null; var key = new PMHistoryKeyTuple(project.ContractID.Value, task.TaskCD, accountGroup.GroupID.Value, item.InventoryID.Value); if (!_pmhistoryPeriodsByKey.TryGetValue(key, out periodsForKey)) { return(null); } if (ds.AmountType == BalanceType.Amount || ds.AmountType == BalanceType.Quantity || ds.AmountType == BalanceType.BudgetAmount || ds.AmountType == BalanceType.BudgetQuantity || ds.AmountType == BalanceType.RevisedAmount || ds.AmountType == BalanceType.RevisedQuantity) { //These amounts are calculated against start of project dsGL.StartPeriod = _reportPeriods.PerWildcard; dsGL.StartPeriodOffset = 0; dsGL.StartPeriodYearOffset = 0; } return(_reportPeriods.GetPeriodsForRegularAmount(dsGL, periodsForKey)); }
public virtual object GetHistoryValue(ARmDataSet dataSet, bool drilldown, Func <ARmDataSet, bool, object> del) { string rmType = Base.Report.Current.Type; if (rmType == ARmReport.PM) { RMDataSource ds = Base.DataSourceByID.Current; RMDataSourcePM dsPM = Base.Caches[typeof(RMDataSource)].GetExtension <RMDataSourcePM>(ds); ds.AmountType = (short?)dataSet[RMReportReaderGL.Keys.AmountType]; dsPM.StartAccountGroup = dataSet[Keys.StartAccountGroup] as string ?? ""; dsPM.EndAccountGroup = dataSet[Keys.EndAccountGroup] as string ?? ""; dsPM.StartProject = dataSet[Keys.StartProject] as string ?? ""; dsPM.EndProject = dataSet[Keys.EndProject] as string ?? ""; dsPM.StartProjectTask = dataSet[Keys.StartProjectTask] as string ?? ""; dsPM.EndProjectTask = dataSet[Keys.EndProjectTask] as string ?? ""; dsPM.StartInventory = dataSet[Keys.StartInventory] as string ?? ""; dsPM.EndInventory = dataSet[Keys.EndInventory] as string ?? ""; RMDataSourceGL dsGL = Base.Caches[typeof(RMDataSource)].GetExtension <RMDataSourceGL>(ds); dsGL.StartBranch = dataSet[RMReportReaderGL.Keys.StartBranch] as string ?? ""; dsGL.EndBranch = dataSet[RMReportReaderGL.Keys.EndBranch] as string ?? ""; dsGL.EndPeriod = ((dataSet[RMReportReaderGL.Keys.EndPeriod] as string ?? "").Length > 2 ? ((dataSet[RMReportReaderGL.Keys.EndPeriod] as string ?? "").Substring(2) + " ").Substring(0, 4) : " ") + ((dataSet[RMReportReaderGL.Keys.EndPeriod] as string ?? "").Length > 2 ? (dataSet[RMReportReaderGL.Keys.EndPeriod] as string ?? "").Substring(0, 2) : dataSet[RMReportReaderGL.Keys.EndPeriod] as string ?? ""); dsGL.EndPeriodOffset = (short?)(int?)dataSet[RMReportReaderGL.Keys.EndOffset]; dsGL.EndPeriodYearOffset = (short?)(int?)dataSet[RMReportReaderGL.Keys.EndYearOffset]; dsGL.StartPeriod = ((dataSet[RMReportReaderGL.Keys.StartPeriod] as string ?? "").Length > 2 ? ((dataSet[RMReportReaderGL.Keys.StartPeriod] as string ?? "").Substring(2) + " ").Substring(0, 4) : " ") + ((dataSet[RMReportReaderGL.Keys.StartPeriod] as string ?? "").Length > 2 ? (dataSet[RMReportReaderGL.Keys.StartPeriod] as string ?? "").Substring(0, 2) : dataSet[RMReportReaderGL.Keys.StartPeriod] as string ?? ""); dsGL.StartPeriodOffset = (short?)(int?)dataSet[RMReportReaderGL.Keys.StartOffset]; dsGL.StartPeriodYearOffset = (short?)(int?)dataSet[RMReportReaderGL.Keys.StartYearOffset]; List <object[]> splitret = null; if (ds.Expand != ExpandType.Nothing) { splitret = new List <object[]>(); } if (ds.AmountType == null || ds.AmountType == 0) { return(0m); } PMEnsureInitialized(); EnsureHistoryLoaded(dsPM); NormalizeDataSource(dsPM); List <PMAccountGroup> accountGroups = _accountGroupsRangeCache.GetItemsInRange(dataSet[Keys.StartAccountGroup] as string, group => group.GroupCD, (group, code) => group.GroupCD = code); List <PMProject> projects = _projectsRangeCache.GetItemsInRange(dataSet[Keys.StartProject] as string, project => project.ContractCD, (project, code) => project.ContractCD = code); List <PMTask> tasks = _tasksRangeCache.GetItemsInRange(dataSet[Keys.StartProjectTask] as string, task => task.TaskCD, (task, code) => task.TaskCD = code); List <InventoryItem> items = _itemRangeCache.GetItemsInRange(dataSet[Keys.StartInventory] as string, item => item.InventoryCD, (item, code) => item.InventoryCD = code); if (ds.Expand == ExpandType.AccountGroup) { accountGroups.ForEach(a => splitret.Add(new object[] { a.GroupCD, a.Description, 0m, null, string.Empty })); } else if (ds.Expand == ExpandType.Project) { projects.ForEach(p => splitret.Add(new object[] { p.ContractCD, p.Description, 0m, null, string.Empty })); } else if (ds.Expand == ExpandType.ProjectTask) { //To avoid useless expansion, first restrict list of tasks to those tasks which point to current projects tasks = tasks.Where(t => projects.Any(p => t.ProjectID == p.ContractID)).ToList <PMTask>(); tasks = tasks.GroupBy(t => t.TaskCD).Select(g => new PMTask() { TaskCD = g.Key, TaskID = g.Min(t => t.TaskID), Description = g.Min(t => t.Description) }).ToList <PMTask>(); tasks.ForEach(pt => splitret.Add(new object[] { pt.TaskCD, pt.Description, 0m, null, string.Empty })); } else if (ds.Expand == ExpandType.Inventory) { items.ForEach(i => splitret.Add(new object[] { i.InventoryCD, i.Descr, 0m, null, string.Empty })); } return(CalculateAndExpandValue(drilldown, ds, dsGL, dsPM, dataSet, accountGroups, projects, tasks, items, splitret)); } else { return(del(dataSet, drilldown)); } }