예제 #1
0
 private DerValuesViewModel GetDerValuesPerSection(string date, int[] actualKpiIds, int[] targetKpiIds, int[] highlightTypeIds)
 {
     var theDate = DateTime.ParseExact(date, "MM/dd/yyyy", CultureInfo.InvariantCulture);
     var kpiInformationValuesRequest = new GetKpiInformationValuesRequest
     {
         Date = theDate,
         ActualKpiIds = actualKpiIds,
         TargetKpiIds = targetKpiIds
     };
     var kpiInformationValuesResponse = _derTransactionService.GetKpiInformationValues(kpiInformationValuesRequest);
     var highlightValuesRequest = new GetHighlightValuesRequest
     {
         Date = theDate,
         HighlightTypeIds = highlightTypeIds
     };
     var highlightValuesResponse = _derTransactionService.GetHighlightValues(highlightValuesRequest);
     var viewModel = new DerValuesViewModel();
     viewModel.Highlights = highlightValuesResponse.Highlights.MapTo<DerValuesViewModel.DerHighlightValuesViewModel>();
     viewModel.KpiInformations = kpiInformationValuesResponse.KpiInformations.MapTo<DerValuesViewModel.KpiInformationValuesViewModel>();
     return viewModel;
 }
예제 #2
0
        public GetKpiInformationValuesResponse GetKpiInformationValues(GetKpiInformationValuesRequest request)
        {
            //var kpiIds =
            //achievement section
            var kpiIdsForActual = request.ActualKpiIds;
            var previousDate = request.Date.AddDays(-1);
            var previousMonth = request.Date.AddMonths(-1);
            var previous2Month = request.Date.AddMonths(-2);
            var previousYear = request.Date.AddYears(-1);
            //var achievements = GetAchievements(kpiIdsForActual, request.Date);
            var achievements = DataContext.KpiAchievements.OrderByDescending(x=>x.Periode).Include(x => x.Kpi)
                .Where(x => kpiIdsForActual.Contains(x.Kpi.Id) &&
                (((x.Periode == request.Date || x.Periode == previousDate) && x.PeriodeType == PeriodeType.Daily) ||
                (x.PeriodeType == PeriodeType.Yearly && (x.Periode.Year == request.Date.Year || x.Periode.Year == previousYear.Year)) ||
                (x.PeriodeType == PeriodeType.Monthly && (x.Periode.Month == request.Date.Month && x.Periode.Year == request.Date.Year || x.Periode.Month == previousMonth.Month && x.Periode.Year == previousMonth.Year || x.Periode.Month == previous2Month.Month && x.Periode.Year == previousMonth.Year)))).ToList();
            var kpiIdsForTarget = request.TargetKpiIds;
            //var targets = GetTargets(kpiIdsForTarget, request.Date);
            var targets = DataContext.KpiTargets.Include(x => x.Kpi)
               .Where(x => kpiIdsForTarget.Contains(x.Kpi.Id) &&
               (((x.Periode == request.Date || x.Periode == previousDate) && x.PeriodeType == PeriodeType.Daily) ||
               (x.PeriodeType == PeriodeType.Yearly && x.Periode.Year == request.Date.Year) ||
               (x.PeriodeType == PeriodeType.Monthly && x.Periode.Month == request.Date.Month && x.Periode.Year == request.Date.Year))).ToList();

            var response = new GetKpiInformationValuesResponse();
            foreach (var kpiId in kpiIdsForActual)
            {
                var kpiInformation = response.KpiInformations.FirstOrDefault(x => x.KpiId == kpiId);
                if (kpiInformation == null)
                {
                    kpiInformation = new GetKpiInformationValuesResponse.KpiInformation { KpiId = kpiId };
                    response.KpiInformations.Add(kpiInformation);
                }
            }
            #region actual
            if (achievements != null)
            {
                foreach (var actual in achievements)
                {
                    var kpiInformation = response.KpiInformations.FirstOrDefault(x => x.KpiId == actual.Kpi.Id);
                    //if (kpiInformation == null) {
                    //    kpiInformation = new GetKpiInformationValuesResponse.KpiInformation { KpiId = actual.Kpi.Id };
                    //    response.KpiInformations.Add(kpiInformation);
                    //}
                    //var actual = achievements.FirstOrDefault(x => x.Kpi.Id == achievement.Kpi.Id);
                    if (actual == null)
                    {
                        continue;
                    }
                    #region daily
                    if (actual.PeriodeType == PeriodeType.Daily)
                    {
                        if (kpiInformation.DailyActual == null)
                        {
                            var isTodayValue = actual.Periode == request.Date;
                            if (isTodayValue)
                            {
                                kpiInformation.DailyActual = new GetKpiInformationValuesResponse.KpiValue
                                {
                                    Date = actual.Periode,
                                    Value = actual.Value.HasValue ? actual.Value : null,
                                    Remark = actual.Remark,
                                    Id = actual.Id,
                                    Type = "now"
                                };
                            }
                            else
                            {
                                //var todayValue = achievements.OrderByDescending(x => x.Periode).FirstOrDefault(x => x.Kpi.Id == actual.Kpi.Id && x.Periode == request.Date && x.PeriodeType == PeriodeType.Daily);
                                var todayValue = achievements.FirstOrDefault(x => x.Kpi.Id == actual.Kpi.Id && x.Periode == request.Date && x.PeriodeType == PeriodeType.Daily);
                                if (todayValue != null)
                                {
                                    kpiInformation.DailyActual = new GetKpiInformationValuesResponse.KpiValue
                                    {
                                        Date = todayValue.Periode,
                                        Value = todayValue.Value ?? null,
                                        Remark = todayValue.Remark,
                                        Id = todayValue.Id,
                                        Type = "now"
                                    };

                                }
                                else
                                {
                                    //yesterday value selected
                                    kpiInformation.DailyActual = new GetKpiInformationValuesResponse.KpiValue
                                    {
                                        Date = actual.Periode,
                                        Value = actual.Value ?? null,
                                        Remark = actual.Remark,
                                        Type = "prev"
                                    };
                                    // check last value of this kpi
                                    //var lastValue = achievements.OrderByDescending(x => x.Periode).FirstOrDefault(x => x.Kpi.Id == actual.Kpi.Id && x.Periode == request.Date && x.PeriodeType == PeriodeType.Daily);
                                    //if (lastValue != null)
                                    //{
                                    //    kpiInformation.DailyActual = new GetKpiInformationValuesResponse.KpiValue
                                    //    {
                                    //        Date = lastValue.Periode,
                                    //        Value = lastValue.Value.HasValue ? lastValue.Value : null,
                                    //        Remark = lastValue.Remark,
                                    //        Id = lastValue.Id,
                                    //        Type = "prev"
                                    //    };

                                    //}
                                    //else
                                    //{
                                    //    //yesterday value selected
                                    //    kpiInformation.DailyActual = new GetKpiInformationValuesResponse.KpiValue
                                    //    {
                                    //        Date = actual.Periode,
                                    //        Value = actual.Value.HasValue ? actual.Value : null,
                                    //        Remark = actual.Remark,
                                    //        Type = "prev"
                                    //    };
                                    //}
                                }
                            }
                        }
                    }
                    #endregion
                    #region if monthly
                    if (actual.PeriodeType == PeriodeType.Monthly)
                    {
                        if (kpiInformation.MonthlyActual == null)
                        {
                            bool isCurrentMonthValue = false;
                            if (actual.Kpi.Id == 385)
                            {
                                isCurrentMonthValue = actual.Periode.Month == request.Date.AddMonths(-1).Month && actual.Periode.Year == request.Date.Year && actual.PeriodeType == PeriodeType.Monthly;
                            }
                            else
                            {
                                isCurrentMonthValue = actual.Periode.Month == request.Date.Month && actual.Periode.Year == request.Date.Year && actual.PeriodeType == PeriodeType.Monthly;
                            }
                            //var isCurrentMonthValue = actual.Periode.Month == request.Date.Month && actual.Periode.Year == request.Date.Year && actual.PeriodeType == PeriodeType.Monthly;
                            if (isCurrentMonthValue)
                            {
                                kpiInformation.MonthlyActual = new GetKpiInformationValuesResponse.KpiValue
                                {
                                    Date = actual.Periode,
                                    Value = actual.Value.HasValue ? actual.Value : null,
                                    Remark = actual.Remark,
                                    Type = "now",
                                    Id = actual.Id
                                };
                            }
                            else
                            {
                                var currentMonthValue = achievements.FirstOrDefault(x => x.Kpi.Id == actual.Kpi.Id && x.Periode.Month == request.Date.Month && x.Periode.Year == request.Date.Year && x.PeriodeType == PeriodeType.Monthly);
                                if (currentMonthValue != null)
                                {
                                    kpiInformation.MonthlyActual = new GetKpiInformationValuesResponse.KpiValue
                                    {
                                        Date = currentMonthValue.Periode,
                                        Value = currentMonthValue.Value.HasValue ? currentMonthValue.Value : null,
                                        Remark = currentMonthValue.Remark,
                                        Id = currentMonthValue.Id,
                                        Type = "now"
                                    };
                                }
                                else
                                {
                                    kpiInformation.MonthlyActual = new GetKpiInformationValuesResponse.KpiValue
                                    {
                                        Date = actual.Periode,
                                        Value = actual.Value.HasValue ? actual.Value : null,
                                        Remark = actual.Remark,
                                        Type = "prev"
                                    };

                                    //var previousMonthValue = achievements.OrderByDescending(x => x.Periode).FirstOrDefault(x => x.Kpi.Id == actual.Kpi.Id && x.Periode.Month <= request.Date.Month && x.Periode.Year == request.Date.Year && x.PeriodeType == PeriodeType.Monthly);
                                    //if (previousMonthValue != null)
                                    //{
                                    //    kpiInformation.MonthlyActual = new GetKpiInformationValuesResponse.KpiValue
                                    //    {
                                    //        Date = previousMonthValue.Periode,
                                    //        Value = previousMonthValue.Value.HasValue ? previousMonthValue.Value : null,
                                    //        Remark = previousMonthValue.Remark,
                                    //        Id = previousMonthValue.Id,
                                    //        Type = "prev"
                                    //    };
                                    //}
                                    //else
                                    //{
                                    //    kpiInformation.MonthlyActual = new GetKpiInformationValuesResponse.KpiValue
                                    //    {
                                    //        Date = actual.Periode,
                                    //        Value = actual.Value.HasValue ? actual.Value : null,
                                    //        Remark = actual.Remark,
                                    //        Type = "prev"
                                    //    };
                                    //}
                                }

                            }
                        }
                    }

                    #endregion
                    #region if yearly
                    if (actual.PeriodeType == PeriodeType.Yearly)
                    {
                        if (kpiInformation.YearlyActual == null)
                        {
                            var isCurrentYearValue = actual.Periode.Year == request.Date.Year && actual.PeriodeType == PeriodeType.Yearly;
                            if (isCurrentYearValue)
                            {
                                kpiInformation.YearlyActual = new GetKpiInformationValuesResponse.KpiValue
                                {
                                    Date = actual.Periode,
                                    Value = actual.Value.HasValue ? actual.Value : null,
                                    Remark = actual.Remark,
                                    Type = "now",
                                    Id = actual.Id
                                };
                            }
                            else
                            {
                                var currentYearValue = achievements.FirstOrDefault(x => x.Kpi.Id == actual.Kpi.Id && x.Periode.Year == request.Date.Year && x.PeriodeType == PeriodeType.Yearly);
                                if (currentYearValue != null)
                                {
                                    kpiInformation.YearlyActual = new GetKpiInformationValuesResponse.KpiValue
                                    {
                                        Date = currentYearValue.Periode,
                                        Value = currentYearValue.Value.HasValue ? currentYearValue.Value : null,
                                        Remark = currentYearValue.Remark,
                                        Id = currentYearValue.Id,
                                        Type = "now"
                                    };
                                }
                                else
                                {
                                    kpiInformation.YearlyActual = new GetKpiInformationValuesResponse.KpiValue
                                    {
                                        Date = actual.Periode,
                                        Value = actual.Value.HasValue ? actual.Value : null,
                                        Remark = actual.Remark,
                                        Type = "prev"
                                    };
                                }
                            }
                        }

                    }
                    #endregion
                }
            }
            #endregion
            #region target
            foreach (var kpiId in kpiIdsForTarget)
            {
                var kpiInformation = response.KpiInformations.FirstOrDefault(x => x.KpiId == kpiId);
                if (kpiInformation == null)
                {
                    kpiInformation = new GetKpiInformationValuesResponse.KpiInformation { KpiId = kpiId };
                    response.KpiInformations.Add(kpiInformation);
                }
                //kpiInformation = new GetKpiInformationValuesResponse.KpiInformation { KpiId = kpiId };
                //response.KpiInformations.Add(kpiInformation);
            }
            if (targets != null)
            {
                foreach (var target in targets)
                {
                    var kpiInformation = response.KpiInformations.FirstOrDefault(x => x.KpiId == target.Kpi.Id);
                    //if (kpiInformation == null)
                    //{
                    //    kpiInformation = new GetKpiInformationValuesResponse.KpiInformation { KpiId = target.Kpi.Id };
                    //    response.KpiInformations.Add(kpiInformation);
                    //}
                    //var target = targets.FirstOrDefault(x => x.Kpi.Id == kpiId);
                    if (target == null)
                    {
                        continue;
                    }
                    #region target-daily
                    if (target.PeriodeType == PeriodeType.Daily)
                    {
                        if (kpiInformation.DailyTarget == null)
                        {
                            var isTodayValue = target.Periode == request.Date;
                            if (isTodayValue)
                            {
                                kpiInformation.DailyTarget = new GetKpiInformationValuesResponse.KpiValue
                                {
                                    Date = target.Periode,
                                    Value = target.Value.HasValue ? target.Value : null,
                                    Remark = target.Remark,
                                    Type = "now",
                                    Id = target.Id
                                };
                            }
                            else
                            {
                                var todayValue = targets.FirstOrDefault(x => x.Kpi.Id == target.Kpi.Id && x.Periode == request.Date && x.PeriodeType == PeriodeType.Daily);
                                if (todayValue != null)
                                {
                                    kpiInformation.DailyTarget = new GetKpiInformationValuesResponse.KpiValue
                                    {
                                        Date = todayValue.Periode,
                                        Value = todayValue.Value.HasValue ? todayValue.Value : null,
                                        Remark = todayValue.Remark,
                                        Type = "now",
                                        Id = todayValue.Id
                                    };

                                }
                                else
                                {
                                    //var prevDayValue = targets.OrderByDescending(x => x.Periode).FirstOrDefault(x => x.Kpi.Id == target.Kpi.Id && x.Periode <= request.Date && x.PeriodeType == PeriodeType.Daily);
                                    //yesterday value selected
                                    kpiInformation.DailyTarget = new GetKpiInformationValuesResponse.KpiValue
                                    {
                                        Date = target.Periode,
                                        Value = target.Value.HasValue ? target.Value : null,
                                        Remark = target.Remark,
                                        Type = "prev",
                                        Id = target.Id
                                    };
                                }
                            }
                        }
                    }
                    #endregion
                    #region target-monthly
                    if (target.PeriodeType == PeriodeType.Monthly)
                    {
                        if (kpiInformation.MonthlyTarget == null)
                        {
                            bool currentMonthTarget = false;
                            if (target.Kpi.Id == 385)
                            {
                                DateTime prevMonth = request.Date.AddMonths(-1);
                                currentMonthTarget = target.Periode.Year == prevMonth.Year && target.Periode.Month == prevMonth.Month && target.PeriodeType == PeriodeType.Monthly;
                            }
                            else {
                                currentMonthTarget = target.Periode.Year == request.Date.Year && target.Periode.Month == request.Date.Month && target.PeriodeType == PeriodeType.Monthly;
                            }

                            //var currentMonthTarget = target.Periode.Year  == request.Date.Year && target.Periode.Month == request.Date.Month && target.PeriodeType == PeriodeType.Monthly;
                            if (currentMonthTarget)
                            {
                                kpiInformation.MonthlyTarget = new GetKpiInformationValuesResponse.KpiValue
                                {
                                    Date = target.Periode,
                                    Value = target.Value ?? null,
                                    Remark = target.Remark,
                                    Type = "now",
                                    Id = target.Id
                                };
                            }
                            else
                            {
                                kpiInformation.MonthlyTarget = new GetKpiInformationValuesResponse.KpiValue
                                {
                                    Date = target.Periode,
                                    Value = target.Value ?? null,
                                    Remark = target.Remark,
                                    Type = "prev",
                                    Id = target.Id
                                };
                            }
                        }
                    }
                    #endregion
                    #region target-yearly
                    if (target.PeriodeType == PeriodeType.Yearly)
                    {
                        if (kpiInformation.YearlyTarget == null)
                        {
                            var currentYearValue = target.Periode.Year == request.Date.Year && target.PeriodeType == PeriodeType.Yearly;
                            if (currentYearValue)
                            {
                                kpiInformation.YearlyTarget = new GetKpiInformationValuesResponse.KpiValue
                                {
                                    Date = target.Periode,
                                    Value = target.Value ?? null,
                                    Remark = target.Remark,
                                    Type = "now",
                                    Id = target.Id
                                };
                            }
                            else
                            {
                                kpiInformation.YearlyTarget = new GetKpiInformationValuesResponse.KpiValue
                                {
                                    Date = target.Periode,
                                    Value = target.Value ?? null,
                                    Remark = target.Remark,
                                    Type = "prev",
                                    Id = target.Id
                                };
                            }
                        }
                    }
                    #endregion
                }
            }
            #endregion
            return response;
        }
예제 #3
0
 private IList<DerValuesViewModel.KpiInformationValuesViewModel> GetKpisValue(DateTime date, int[] actualKpiIds, int[] targetKpiIds)
 {
     var kpiInformationValuesRequest = new GetKpiInformationValuesRequest
     {
         Date = date,
         ActualKpiIds = actualKpiIds,
         TargetKpiIds = targetKpiIds
     };
     var kpiInformationValuesResponse = _derTransactionService.GetKpiInformationValues(kpiInformationValuesRequest);
     return kpiInformationValuesResponse.KpiInformations.MapTo<DerValuesViewModel.KpiInformationValuesViewModel>();
 }