/// <summary>
 /// Creates a new instance of the DMeasurement class
 /// </summary>
 /// <param name="parent">DDepartment hosting this DMeasurement</param>
 /// <param name="model">Model providng data for this DMeasurement</param>
 public DMeasurement(DDepartment parent, Measurement model)
 {
     this.Parent = parent;
     this.db = parent.db;
     this.Model = model;
     ViewModel = new BasicMeasurementData();
     ViewModel.DeptId = model.Department_ID;
     ViewModel.HasGoal = model.Has_Goal_IN;
     ViewModel.MeasurementID = model.Measurement_ID;
     ViewModel.NameMeasurement = model.NM;
 }
 /// <summary>
 /// Creates a new instance of the DashboardDataRepository class
 /// </summary>
 /// <param name="db">Existing database</param>
 public DashboardRepository(IPASEntities db)
 {
     this.db = db;
 }
 /// <summary>
 /// Creates a new instance of the DGoal class
 /// </summary>
 /// <param name="parent">DMeasurement hosting this DGoal</param>
 /// <param name="model">Model providing data for this DGoal</param>
 public DGoal(DMeasurement parent, Goal model)
 {
     this.Parent = parent;
     this.db = parent.db;
     this.Model = model;
     ViewModel = new GoalDataViewModel();
     ViewModel.AppliesAfter = model.Applies_After_Tmstp;
     ViewModel.ExceedsVal = model.Exceeds_Val;
     ViewModel.GoalID = model.Goal_ID;
     ViewModel.MeetsPlusVal = model.Meets_Plus_Val;
     ViewModel.MeetsVal = model.Meets_Val;
     ViewModel.Operation = model.Typ;
 }
 /// <summary>
 /// Creates a new instance of the DashboardDataRepository class
 /// </summary>
 /// <param name="r">Existing repository</param>
 public DashboardRepository(Repository r)
 {
     db = r.db;
 }
 /// <summary>
 /// Creates a new instance of the DDepartment class
 /// </summary>
 /// <param name="parent">DashboardRepository hosting this DDepartment</param>
 /// <param name="model">Model providing data for this DDepartment</param>
 public DDepartment(DashboardRepository parent, Department model)
 {
     this.Parent = parent;
     this.db = parent.db;
     this.Model = model;
     this.ViewModel = new DepartmentViewModel();
     ViewModel.DepartmentId = model.Department_ID;
     ViewModel.DepartmentName = model.NM;
     ViewModel.AdGroupId = model.ADGroup_ID;
 }
 /// <summary>
 /// Creates a new instance of the DDatapoint class
 /// </summary>
 /// <param name="parent">DMeasurement hosting this DDatapoint</param>
 /// <param name="model">Model providng data for this DDatapoint</param>
 public DDatapoint(DMeasurement parent, Datapoint model)
 {
     this.Parent = parent;
     this.db = parent.db;
     this.Model = model;
     ViewModel = new DataPointViewModel();
     ViewModel.MeasurementID = Parent.Model.Measurement_ID;
     ViewModel.NameMeasurement = Parent.Model.NM;
     ViewModel.YTDCalc = Parent.Model.YTD_Calc;
     ViewModel.DataPointID = model.Datapoint_ID;
     ViewModel.Applicable = model.Applicable_DT;
     ViewModel.Value = model.Value_AMT;
     ViewModel.NumType = Parent.Model.Type_ID;
     ViewModel.IsCalculated = Parent.Model.Is_Calculated;
     ViewModel.HasSubmitted = model.HasSubmitted_IN;
     string roundingString = "";
     var decimalPlaces = (from m in db.Measurements
                          where Parent.Model.Measurement_ID == m.Measurement_ID
                          select m.Decimal_Points_SZ).ToList();
     var roundingInt = (int)(decimalPlaces.Count > 0 ? decimalPlaces.First() : 0);
     if (roundingInt != 0)
     {
         roundingString = ".";
         for (var i = 0; i < roundingInt; i++)
             roundingString += "0";
     }
     ViewModel.RoudingString = roundingString;
 }
        //This generates all of the rows and cells for the report
        public ReportGenerator(int id, DateTime startDate, DateTime endDate, IPASEntities db)
        {
            reportId = id;
            startDateTime = startDate;
            endDateTime = endDate;
            repository = new Repository(db);
            columnHeaders = new List<string>();
            rowHeaders = new List<string>();
            finalReportRows = new List<FinalReportRow>();
            initialReportRowsList = repository.GetReportRowsForReportId(id);
            reportType = repository.GetReportType(reportId);
            measurementList = repository.GetAllAvaliableMeasurementsManageMeasurement();
            reportMeasurementList = new List<BaseFieldsViewModel>();

            // Build headers
            title = repository.GetReportTitle(reportId);
            GetReportColumnList();

            //Build data for report type 1(PAS Report)
            if (reportType == 1)
            {
                //populate reportMeasurementList with only measures that are in the report
                foreach (var measure in measurementList)
                {
                    int? measureIdInReport = null;
                    foreach (var row in initialReportRowsList)
                        if (row.Type == 1 && measure.MeasurementID == Convert.ToInt32(row.Row_Item))
                            measureIdInReport = Convert.ToInt32(row.Row_Item);

                    if (measureIdInReport != null)
                        reportMeasurementList.Add(measure);
                }

                //build Rows
                foreach (var row in initialReportRowsList)
                {
                    // Make new FinalReportrow
                    FinalReportRow reportRow = new FinalReportRow();

                    switch (row.Type)
                    {
                        case 1: //row is a measure
                            int measureId = Convert.ToInt32(row.Row_Item);
                            reportRow.rowName = reportMeasurementList.Find(m => m.MeasurementID == measureId).NameMeasurement;
                            rowHeaders.Add(reportRow.rowName);
                            //TODO optimize call so it only makes one database call per row, instead of once per column in each row
                            foreach (var col in columnList)
                            {
                                // Go through each column and get Datapoint
                                var list = repository.GetReportDatapoint(measureId, (ReportColumnsEnum)col.Type, startDate, endDate);

                                // Add DP to RR
                                reportRow.measurementId = measureId;
                                if (list == null)
                                    reportRow.rowItems.Add(new RowItemWithMetadata("", ""));
                                else
                                    foreach (var item in list)
                                        reportRow.rowItems.Add(item.cellData == null ? new RowItemWithMetadata("", "") : new RowItemWithMetadata(item.cellData, item.cellMetadata));
                            }
                            break;
                        case 2: //row is a Display Text
                            reportRow.rowName = row.Row_Item;
                            rowHeaders.Add(row.Row_Item);
                            reportRow.isHeader = true;
                            break;
                        case 3: //row is a Category Score
                            switch (row.Row_Item)
                            {
                                case "1":
                                    GetCategoryScoreData("Key Performance", startDate, endDate, ref reportRow);
                                    break;
                                case "2":
                                    GetCategoryScoreData("Financial", startDate, endDate, ref reportRow);
                                    break;
                                case "3":
                                    GetCategoryScoreData("HR", startDate, endDate, ref reportRow);
                                    break;
                                default:
                                    //error
                                    break;
                            }
                            break;
                        default:
                            //error
                            break;
                    }

                    // Add RR to table
                    finalReportRows.Add(reportRow);
                }
            }
        }