private IEnumerable <TASK> GetP6ScheduleTasks(string shortName, out P6Data.PROJECT P6Schedule)
        {
            if (shortName != string.Empty)
            {
                var PROJECTRepository = this.P6UnitOfWork.PROJECT;
                P6Schedule = PROJECTRepository.FirstOrDefault(x => x.proj_short_name == shortName);

                if (P6Schedule != null)
                {
                    return(P6Schedule.TASK);
                }
            }
            else
            {
                P6Schedule = null;
            }

            return(null);
        }
        /// <summary>
        /// Try to generate non-cumulative data points from P6 TASKs repository
        /// </summary>
        /// <param name="progressItem">current progress item to generate against, also populate progressItem nonCumulative datapoints collection</param>
        /// <param name="p6ScheduleTasks">context P6 tasks</param>
        /// <param name="firstAlignedDataDate">universal chart first aligned data date</param>
        /// <param name="progressInterval">period iteration interval</param>
        /// <param name="this.CurrencyConversion">currency conversion factor</param>
        /// <param name="nonCumulativeP6DataPoints">current progress item non cumulative data points</param>
        /// <returns>is generation success</returns>
        private bool TryBuildP6DataPoints(P6Data.PROJECT P6PROJECT, IEnumerable <TASK> P6TASKS, ReportableObject reportableObject, DataPointsType processingType, WorkpackAssignmentLoadType assignmentLoadType, out List <ProgressInfo> nonCumulativeP6DataPoints)
        {
            nonCumulativeP6DataPoints = new List <ProgressInfo>();
            if (reportableObject.BASELINE_ITEMJoinRATE.BASELINE_ITEM.WORKPACK == null)
            {
                return(false);
            }

            IEnumerable <WORKPACK_ASSIGNMENT> FilteredWORKPACK_ASSIGNMENTS;

            if (assignmentLoadType == WorkpackAssignmentLoadType.Modified)
            {
                FilteredWORKPACK_ASSIGNMENTS = reportableObject.BASELINE_ITEMJoinRATE.BASELINE_ITEM.WORKPACK.WORKPACK_ASSIGNMENT.Where(assignment => assignment.ISMODIFIEDBASELINE == true);
                if (FilteredWORKPACK_ASSIGNMENTS.Count() == 0)
                {
                    FilteredWORKPACK_ASSIGNMENTS = reportableObject.BASELINE_ITEMJoinRATE.BASELINE_ITEM.WORKPACK.WORKPACK_ASSIGNMENT; //try to get original if modified is empty
                }
            }
            else
            {
                FilteredWORKPACK_ASSIGNMENTS = reportableObject.BASELINE_ITEMJoinRATE.BASELINE_ITEM.WORKPACK.WORKPACK_ASSIGNMENT.Where(assignment => assignment.ISMODIFIEDBASELINE == false);
            }

            if (P6PROJECT != null && FilteredWORKPACK_ASSIGNMENTS != null && P6TASKS != null && FilteredWORKPACK_ASSIGNMENTS.Count() != 0 && P6TASKS.Count() != 0)
            {
                DateTime?lastRecalcDate = P6PROJECT.last_recalc_date;
                BASELINE_ITEMProjection currentBASELINE_ITEM = reportableObject.BASELINE_ITEMJoinRATE;
                decimal progressItemTotalHours = currentBASELINE_ITEM.BASELINE_ITEM.TOTAL_HOURS;
                decimal progressItemTotalCosts = currentBASELINE_ITEM.TOTAL_COSTS;

                foreach (var WORKPACK_ASSIGNMENTS in FilteredWORKPACK_ASSIGNMENTS)
                {
                    decimal  CurrentAssignmentUnits;
                    decimal  CurrentAssignmentCosts;
                    decimal  CurrentAssignmentMaxUnits;
                    decimal  CurrentAssignmentMinUnits;
                    DateTime CurrentAssignmentStartDate;
                    TimeSpan CurrentAssignmentWorkingPeriod;
                    TASK     currentAssignmentTASK = P6TASKS.FirstOrDefault(task => task.task_code == WORKPACK_ASSIGNMENTS.P6_ACTIVITYID);

                    if (processingType == DataPointsType.Planned)
                    {
                        //routine failed so report to revert to workpack dates calculation
                        if (currentAssignmentTASK == null || currentAssignmentTASK.target_start_date == null)
                        {
                            return(false);
                        }

                        CurrentAssignmentStartDate     = (DateTime)currentAssignmentTASK.target_start_date;
                        CurrentAssignmentWorkingPeriod = (DateTime)currentAssignmentTASK.target_end_date - (DateTime)currentAssignmentTASK.target_start_date;
                        CurrentAssignmentMaxUnits      = progressItemTotalHours;
                        CurrentAssignmentMinUnits      = WORKPACK_ASSIGNMENTS.LOW_VALUE;
                    }
                    else if (processingType == DataPointsType.Earned)
                    {
                        CurrentAssignmentMaxUnits = reportableObject.TOTAL_EARNED_UNITS;
                        CurrentAssignmentMinUnits = WORKPACK_ASSIGNMENTS.LOW_VALUE;
                        if (WORKPACK_ASSIGNMENTS.LOW_VALUE > CurrentAssignmentMaxUnits)
                        {
                            continue;
                        }

                        if (currentAssignmentTASK.act_work_qty == null || currentAssignmentTASK.act_start_date == null || (currentAssignmentTASK.act_end_date == null && lastRecalcDate == null))
                        {
                            return(false);
                        }

                        CurrentAssignmentStartDate = (DateTime)currentAssignmentTASK.act_start_date;
                        if (currentAssignmentTASK.act_end_date == null)
                        {
                            CurrentAssignmentWorkingPeriod = (DateTime)lastRecalcDate - (DateTime)currentAssignmentTASK.act_start_date;
                        }
                        else
                        {
                            CurrentAssignmentWorkingPeriod = (DateTime)currentAssignmentTASK.act_end_date - (DateTime)currentAssignmentTASK.act_start_date;
                        }
                    }
                    else
                    {
                        if (currentAssignmentTASK.early_start_date == null || currentAssignmentTASK.early_end_date == null)
                        {
                            return(false);
                        }

                        if (WORKPACK_ASSIGNMENTS.HIGH_VALUE < reportableObject.TOTAL_EARNED_UNITS)
                        {
                            continue;
                        }

                        CurrentAssignmentMaxUnits = progressItemTotalHours;
                        decimal earnedUnits = reportableObject.TOTAL_EARNED_UNITS;
                        if (earnedUnits > WORKPACK_ASSIGNMENTS.LOW_VALUE)
                        {
                            CurrentAssignmentMinUnits = earnedUnits + 1;
                        }
                        else
                        {
                            CurrentAssignmentMinUnits = WORKPACK_ASSIGNMENTS.LOW_VALUE;
                        }

                        CurrentAssignmentStartDate     = (DateTime)currentAssignmentTASK.early_start_date;
                        CurrentAssignmentWorkingPeriod = (DateTime)currentAssignmentTASK.early_end_date - (DateTime)currentAssignmentTASK.early_start_date;
                    }

                    if (WORKPACK_ASSIGNMENTS.HIGH_VALUE > CurrentAssignmentMaxUnits)
                    {
                        CurrentAssignmentUnits = (CurrentAssignmentMaxUnits - CurrentAssignmentMinUnits) + 1;
                    }
                    else
                    {
                        CurrentAssignmentUnits = (WORKPACK_ASSIGNMENTS.HIGH_VALUE - CurrentAssignmentMinUnits) + 1;
                    }

                    //use assignment units instead of estimated units because we are working on a subset of total units, also, this cost will be processed by conversion later
                    CurrentAssignmentCosts = CurrentAssignmentUnits * reportableObject.BASELINE_ITEMJoinRATE.ITEMRATE;
                    nonCumulativeP6DataPoints.AddRange(ISupportProgressReportingExtensions.DataPointsGenerator(SummaryObject, CurrentAssignmentWorkingPeriod, CurrentAssignmentUnits, CurrentAssignmentCosts, CurrentAssignmentStartDate, currentBASELINE_ITEM.BASELINE_ITEM.GUID_ORIGINAL, this.CurrencyConversion, null, null, null));
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }
        private void PlannedDataPointsBuilder(bool fromOriginalBaseline)
        {
            foreach (ReportableObject ReportableObject in SummaryObject.ReportableObjects)
            {
                //Populate the progressItem variation adjustments
                ReportableObject.NonCumulative_VariationAdjustments = new ObservableCollection <VariationAdjustment>(SummaryObject.NonCumulative_VariationAdjustments.Where(adjustment => adjustment.BaselineItemGuid == ReportableObject.BASELINE_ITEMJoinRATE.BASELINE_ITEM.GUID_ORIGINAL).ToList());
                ReportableObject.Cumulative_VariationAdjustments    = ISupportProgressReportingExtensions.PopulateCumulativeVariationAdjustments(ReportableObject.NonCumulative_VariationAdjustments, SummaryObject.FirstAlignedDataDate, SummaryObject.IntervalPeriod);

                //Assign the report date for stats display
                ReportableObject.ReportingDataDate = SummaryObject.ReportingDataDate;

                BASELINE_ITEMProjection currentBASELINE_ITEM = ReportableObject.BASELINE_ITEMJoinRATE;
                WORKPACK currentWORKPACK = currentBASELINE_ITEM.BASELINE_ITEM.WORKPACK;
                if (currentWORKPACK == null)
                {
                    continue;
                }

                List <ProgressInfo>        progressItemP6DataPoints;
                WorkpackAssignmentLoadType assignmentLoadType = WorkpackAssignmentLoadType.Original;
                if (!fromOriginalBaseline)
                {
                    assignmentLoadType = WorkpackAssignmentLoadType.Modified;
                }

                IEnumerable <TASK> BASELINE_TASKS     = null;
                P6Data.PROJECT     BASELINE_P6PROJECT = null;
                if (fromOriginalBaseline)
                {
                    if (this.SummaryObject.LiveBASELINE.P6BASELINE_NAME != null && this.SummaryObject.LiveBASELINE.P6BASELINE_NAME != string.Empty)
                    {
                        BASELINE_TASKS = GetP6ScheduleTasks(SummaryObject.LiveBASELINE.P6BASELINE_NAME, out BASELINE_P6PROJECT);
                    }
                }
                else
                {
                    if (this.SummaryObject.LiveBASELINE.P6MODBASELINE_NAME != null && this.SummaryObject.LiveBASELINE.P6MODBASELINE_NAME != string.Empty)
                    {
                        BASELINE_TASKS = GetP6ScheduleTasks(SummaryObject.LiveBASELINE.P6MODBASELINE_NAME, out BASELINE_P6PROJECT);
                    }
                }

                if (SummaryObject.LivePROGRESS.P6PROGRESS_NAME != null && TryBuildP6DataPoints(BASELINE_P6PROJECT, BASELINE_TASKS, ReportableObject, DataPointsType.Planned, assignmentLoadType, out progressItemP6DataPoints))
                {
                    if (fromOriginalBaseline)
                    {
                        ReportableObject.NonCumulative_OriginalDataPoints = new ObservableCollection <ProgressInfo>(progressItemP6DataPoints);
                    }
                    else
                    {
                        ReportableObject.NonCumulative_PlannedDataPoints = new ObservableCollection <ProgressInfo>(progressItemP6DataPoints);
                    }
                }
                else
                {
                    List <Period> workpackSuspensionPeriod = new List <Period>();

                    workpackSuspensionPeriod.Add(new Period(currentWORKPACK.REVIEWSTARTDATE, currentWORKPACK.REVIEWENDDATE));

                    decimal BaselineItemBaseUnits  = currentBASELINE_ITEM.BASELINE_ITEM.ESTIMATED_HOURS;
                    decimal BaselineItemBaseCosts  = currentBASELINE_ITEM.ESTIMATED_COSTS;
                    decimal BaselineItemTotalUnits = currentBASELINE_ITEM.BASELINE_ITEM.TOTAL_HOURS;
                    decimal BaselineItemTotalCosts = currentBASELINE_ITEM.TOTAL_COSTS;

                    List <ProgressInfo> plannedDataPoints;
                    if (fromOriginalBaseline) //if it's generating from original baseline ignore variation
                    {
                        TimeSpan workingBaseTimeSpan = currentWORKPACK.ENDDATE - currentWORKPACK.STARTDATE;
                        plannedDataPoints = ISupportProgressReportingExtensions.DataPointsGenerator(SummaryObject, workingBaseTimeSpan, BaselineItemBaseUnits, BaselineItemBaseCosts, currentWORKPACK.STARTDATE, currentBASELINE_ITEM.BASELINE_ITEM.GUID_ORIGINAL, this.CurrencyConversion, workpackSuspensionPeriod, BaselineItemTotalUnits, BaselineItemTotalCosts);
                        ReportableObject.NonCumulative_OriginalDataPoints = new ObservableCollection <ProgressInfo>(plannedDataPoints);
                    }
                    else
                    {
                        DateTime modifiedEndDateToUse = currentWORKPACK.ENDDATE;
                        if (currentWORKPACK.FORECASTENDDATE != null)
                        {
                            modifiedEndDateToUse = (DateTime)currentWORKPACK.FORECASTENDDATE;
                        }

                        TimeSpan workingModifiedTimeSpan = modifiedEndDateToUse - currentWORKPACK.STARTDATE;
                        if (currentWORKPACK.FORECASTSTARTDATE != null && ((DateTime)currentWORKPACK.FORECASTSTARTDATE) > currentWORKPACK.ENDDATE)
                        {
                            workpackSuspensionPeriod.Add(new Period(currentWORKPACK.ENDDATE.AddDays(1), (DateTime)currentWORKPACK.FORECASTSTARTDATE));
                        }

                        //Used to show sharktooth on variation
                        plannedDataPoints = ISupportProgressReportingExtensions.DataPointsGenerator(SummaryObject, workingModifiedTimeSpan, BaselineItemBaseUnits, BaselineItemBaseCosts, currentWORKPACK.STARTDATE, currentBASELINE_ITEM.BASELINE_ITEM.GUID_ORIGINAL, this.CurrencyConversion, workpackSuspensionPeriod, null, null, ReportableObject.Cumulative_VariationAdjustments);

                        //Used to show normalized variation
                        //plannedDataPoints = DataPointsGenerator(WorkingPeriod, progressInterval, BaselineItemTotalUnits, BaselineItemTotalCosts, this.CurrencyConversion, baselineItem.WORKPACK.STARTDATE, firstAlignedDataDate, baselineItem.GUID_ORIGINAL);
                        ReportableObject.NonCumulative_PlannedDataPoints = new ObservableCollection <ProgressInfo>(plannedDataPoints);
                    }
                }
            }

            if (fromOriginalBaseline)
            {
                SummaryObject.NonCumulative_OriginalDataPoints = new ObservableCollection <ProgressInfo>(SummaryObject.ReportableObjects.SelectMany(x => x.NonCumulative_OriginalDataPoints));
            }
            else
            {
                SummaryObject.NonCumulative_PlannedDataPoints = new ObservableCollection <ProgressInfo>(SummaryObject.ReportableObjects.SelectMany(x => x.NonCumulative_PlannedDataPoints));
            }
        }