Beispiel #1
0
        public GetPmsSummaryReportResponse GetPmsSummaryReport(GetPmsSummaryReportRequest request)
        {
            var response = new GetPmsSummaryReportResponse();
            try
            {
                //var xxx = DataContext.PmsSummaries.Include(x => x.PmsSummaryScoringIndicators.Select(a => a.)).ToList();
                var pmsSummary = DataContext.PmsSummaries
                                            .Include(x => x.ScoreIndicators)
                                            .Include("PmsConfigs.Pillar")
                                            .Include("PmsConfigs.ScoreIndicators")
                                            .Include("PmsConfigs.PmsConfigDetailsList.Kpi.Measurement")
                                            .Include("PmsConfigs.PmsConfigDetailsList.Kpi.KpiAchievements")
                                            .Include("PmsConfigs.PmsConfigDetailsList.Kpi.KpiTargets")
                                            .Include("PmsConfigs.PmsConfigDetailsList.Kpi.Pillar")
                                            .Include("PmsConfigs.PmsConfigDetailsList.ScoreIndicators")
                                            .FirstOrDefault(x => x.IsActive && x.Year == request.Year);
                if (pmsSummary != null)
                {
                    response.Title = pmsSummary.Title;
                    foreach (var pmsConfig in pmsSummary.PmsConfigs)
                    {
                        foreach (var pmsConfigDetails in pmsConfig.PmsConfigDetailsList)
                        {
                            var kpiData = new GetPmsSummaryReportResponse.KpiData();
                            kpiData.Id = pmsConfigDetails.Id;
                            kpiData.Pillar = pmsConfig.Pillar.Name;
                            kpiData.PillarId = pmsConfig.Pillar.Id;
                            kpiData.Kpi = pmsConfigDetails.Kpi.Name;
                            kpiData.Unit = pmsConfigDetails.Kpi.Measurement.Name;
                            kpiData.Weight = pmsConfigDetails.Weight;
                            kpiData.PillarOrder = pmsConfigDetails.Kpi.Pillar.Order;
                            kpiData.KpiOrder = pmsConfigDetails.Kpi.Order;
                            kpiData.PillarWeight = pmsConfig.Weight;
                            kpiData.ScoringType = pmsConfigDetails.ScoringType;
                            kpiData.YtdFormula = pmsConfigDetails.Kpi.YtdFormula;
                            kpiData.ScoreIndicators = ParseScoreIndicator(pmsConfigDetails.ScoreIndicators);

                            #region KPI Achievement

                            var kpiAchievementYearly =
                                pmsConfigDetails.Kpi.KpiAchievements.FirstOrDefault(x => x.PeriodeType == PeriodeType.Yearly && x.Periode.Year == request.Year);
                            if (kpiAchievementYearly != null && kpiAchievementYearly.Value != null)
                                kpiData.ActualYearly = kpiAchievementYearly.Value.Value;

                            var kpiAchievementMonthly =
                                pmsConfigDetails.Kpi.KpiAchievements.FirstOrDefault(
                                    x => x.PeriodeType == PeriodeType.Monthly && x.Periode.Month == request.Month && x.Periode.Year == request.Year);
                            if (kpiAchievementMonthly != null && kpiAchievementMonthly.Value.HasValue)
                                kpiData.ActualMonthly = kpiAchievementMonthly.Value.Value;

                            var kpiAchievementYtd = pmsConfigDetails.Kpi.KpiAchievements.Where(
                                x =>
                                x.PeriodeType == PeriodeType.Monthly && x.Value.HasValue &&
                                (x.Periode.Month >= 1 && x.Periode.Month <= request.Month && x.Periode.Year == request.Year)).ToList();
                            if (kpiAchievementYtd.Count > 0) kpiData.ActualYtd = 0;
                            foreach (var achievementYtd in kpiAchievementYtd)
                            {
                                if (achievementYtd.Value.HasValue)
                                    kpiData.ActualYtd += achievementYtd.Value;
                            }

                            if (kpiData.YtdFormula == YtdFormula.Average)
                            {
                                if (kpiData.ActualYtd.HasValue)
                                {
                                    kpiData.ActualYtd = kpiData.ActualYtd/kpiAchievementYtd.Count;
                                }
                            }

                            #endregion

                            #region KPI Target

                            var kpiTargetYearly =
                                pmsConfigDetails.Kpi.KpiTargets.FirstOrDefault(x => x.PeriodeType == PeriodeType.Yearly && x.Periode.Year == request.Year);
                            if (kpiTargetYearly != null && kpiTargetYearly.Value != null)
                                kpiData.TargetYearly = kpiTargetYearly.Value.Value;

                            var kpiTargetMonthly =
                                pmsConfigDetails.Kpi.KpiTargets.FirstOrDefault(
                                    x => x.PeriodeType == PeriodeType.Monthly && x.Periode.Month == request.Month && x.Periode.Year == request.Year);
                            if (kpiTargetMonthly != null && kpiTargetMonthly.Value.HasValue)
                                kpiData.TargetMonthly = kpiTargetMonthly.Value.Value;

                            var kpiTargetYtd = pmsConfigDetails.Kpi.KpiTargets.Where(
                                x =>
                                x.PeriodeType == PeriodeType.Monthly && x.Value.HasValue &&
                                (x.Periode.Month >= 1 && x.Periode.Month <= request.Month && x.Periode.Year == request.Year)).ToList();
                            if (kpiTargetYtd.Count > 0) kpiData.TargetYtd = 0;
                            foreach (var targetYtd in kpiTargetYtd)
                            {
                                if (targetYtd.Value.HasValue)
                                {
                                    kpiData.TargetYtd += targetYtd.Value;
                                }
                            }

                            if (kpiData.YtdFormula == YtdFormula.Average)
                            {
                                if (kpiData.TargetYtd.HasValue)
                                {
                                    kpiData.TargetYtd = kpiData.TargetYtd/kpiTargetYtd.Count;
                                }
                            }

                            #endregion

                            #region Score

                            if (kpiData.ActualYtd.HasValue && kpiData.TargetYtd.HasValue)
                            {
                                //var indexYtd = (kpiData.ActualYtd.Value / kpiData.TargetYtd.Value);

                                switch (pmsConfigDetails.ScoringType)
                                {
                                    case ScoringType.Positive:
                                        kpiData.Score = pmsConfigDetails.Weight * kpiData.IndexYtd;
                                        break;
                                    case ScoringType.Negative:
                                        if (kpiData.IndexYtd.Equals(0))
                                        {
                                            response.IsSuccess = false;
                                            response.Message =
                                                string.Format(
                                                    @"KPI {0} memiliki nilai index YTD 0 dengan Nilai Scoring Type negative yang mengakibatkan terjadinya nilai infinity",
                                                    pmsConfigDetails.Kpi.Name);
                                            return response;
                                        }
                                        kpiData.Score = pmsConfigDetails.Weight / kpiData.IndexYtd;
                                        break;
                                    case ScoringType.Boolean:
                                        bool isMoreThanZero = false;
                                        var kpiAchievement =
                                            pmsConfigDetails.Kpi.KpiAchievements.Where(x => x.Value.HasValue && x.Periode.Year == request.Year).ToList();
                                        bool isNull = kpiAchievement.Count == 0;
                                        foreach (var achievement in kpiAchievement)
                                        {
                                            if (achievement.Value > 0)
                                            {
                                                isMoreThanZero = true;
                                                break;
                                            }

                                        }

                                        if (!isNull)
                                        {
                                            kpiData.Score = isMoreThanZero ? 0 : Double.Parse(kpiData.Weight.ToString());
                                        }

                                        break;
                                }

                            }

                            #endregion

                            kpiData.KpiColor = GetScoreColor(kpiData.ActualYtd, pmsConfigDetails.ScoreIndicators);

                            response.KpiDatas.Add(kpiData);
                        }

                        response.KpiDatas = SetPmsConfigColor(response.KpiDatas, pmsConfig.ScoreIndicators,
                                                              pmsConfig.Pillar.Id);
                    }

                    response.KpiDatas = SetPmsSummaryColor(response.KpiDatas, pmsSummary.ScoreIndicators);
                }

                response.IsSuccess = true;
            }
            catch (InvalidOperationException invalidOperationException)
            {
                response.Message = invalidOperationException.Message;
            }
            catch (NullReferenceException nullReferenceException)
            {
                response.Message = nullReferenceException.Message;
            }

            return response;
        }
Beispiel #2
0
        public GetPmsSummaryReportResponse GetPmsSummaryReport(GetPmsSummaryReportRequest request)
        {
            var response = new GetPmsSummaryReportResponse();
            try
            {
                //var xxx = DataContext.PmsSummaries.Include(x => x.PmsSummaryScoringIndicators.Select(a => a.)).ToList();
                var pmsSummary = DataContext.PmsSummaries
                                            .Include(x => x.ScoreIndicators)
                                            .Include("PmsConfigs.Pillar")
                                            .Include("PmsConfigs.ScoreIndicators")
                                            .Include("PmsConfigs.PmsConfigDetailsList.Kpi.Measurement")
                                            .Include("PmsConfigs.PmsConfigDetailsList.Kpi.KpiAchievements")
                                            .Include("PmsConfigs.PmsConfigDetailsList.Kpi.KpiTargets")
                                            .Include("PmsConfigs.PmsConfigDetailsList.Kpi.Pillar")
                                            .Include("PmsConfigs.PmsConfigDetailsList.ScoreIndicators")
                                            .FirstOrDefault(x => x.Year == request.Year);
                if (pmsSummary != null)
                {
                    response.Title = pmsSummary.Title;
                    foreach (var pmsConfig in pmsSummary.PmsConfigs.OrderBy(x => x.Pillar.Order))
                    {
                        foreach (var pmsConfigDetails in pmsConfig.PmsConfigDetailsList.OrderBy(x => x.Kpi.Order))
                        {
                            var kpiData = new GetPmsSummaryReportResponse.KpiData();
                            kpiData.PmsSummaryId = pmsSummary.Id;
                            kpiData.PmsConfigId = pmsConfig.Id;
                            kpiData.PmsConfigDetailId = pmsConfigDetails.Id;
                            kpiData.Pillar = pmsConfig.Pillar.Name;
                            kpiData.PillarId = pmsConfig.Pillar.Id;
                            kpiData.Kpi = pmsConfigDetails.Kpi.Name;
                            kpiData.Unit = pmsConfigDetails.Kpi.Measurement.Name;
                            kpiData.Weight = pmsConfigDetails.Weight;
                            kpiData.PillarOrder = pmsConfigDetails.Kpi.Pillar.Order;
                            kpiData.KpiOrder = pmsConfigDetails.Kpi.Order;
                            kpiData.PillarWeight = pmsConfig.Weight;
                            kpiData.ScoringType = pmsConfigDetails.ScoringType;
                            kpiData.YtdFormula = pmsConfigDetails.Kpi.YtdFormula;
                            kpiData.ScoreIndicators = ParseScoreIndicator(pmsConfigDetails.ScoreIndicators);
                            kpiData.KpiIcon = pmsConfigDetails.Kpi.Icon;
                            kpiData.PillarIcon = pmsConfig.Pillar.Icon;
                            kpiData.PmsConfigColor = pmsConfig.Pillar.Color;
                            #region KPI Achievement

                            var kpiAchievementYearly =
                                pmsConfigDetails.Kpi.KpiAchievements.FirstOrDefault(x => x.PeriodeType == PeriodeType.Yearly && x.Periode.Year == request.Year);
                            if (kpiAchievementYearly != null && kpiAchievementYearly.Value != null)
                                kpiData.ActualYearly = kpiAchievementYearly.Value.Value;

                            var kpiAchievementMonthly =
                                pmsConfigDetails.Kpi.KpiAchievements.FirstOrDefault(
                                    x => x.PeriodeType == PeriodeType.Monthly && x.Periode.Month == request.Month && x.Periode.Year == request.Year);
                            if (kpiAchievementMonthly != null && kpiAchievementMonthly.Value.HasValue)
                                kpiData.ActualMonthly = kpiAchievementMonthly.Value.Value;

                            var kpiAchievementYtd = pmsConfigDetails.Kpi.KpiAchievements.Where(
                                x =>
                                x.PeriodeType == PeriodeType.Monthly && x.Value.HasValue &&
                                (x.Periode.Month >= 1 && x.Periode.Month <= request.Month && x.Periode.Year == request.Year)).ToList();
                            if (kpiAchievementYtd.Count > 0) kpiData.ActualYtd = 0;
                            foreach (var achievementYtd in kpiAchievementYtd)
                            {
                                if (achievementYtd.Value.HasValue)
                                    kpiData.ActualYtd += achievementYtd.Value;
                            }

                            if (kpiData.YtdFormula == YtdFormula.Average)
                            {
                                if (kpiData.ActualYtd.HasValue)
                                {
                                    kpiData.ActualYtd = kpiData.ActualYtd / kpiAchievementYtd.Count;
                                }
                            }

                            #endregion

                            #region KPI Target

                            var kpiTargetYearly =
                                pmsConfigDetails.Kpi.KpiTargets.FirstOrDefault(x => x.PeriodeType == PeriodeType.Yearly && x.Periode.Year == request.Year);
                            if (kpiTargetYearly != null && kpiTargetYearly.Value != null)
                                kpiData.TargetYearly = kpiTargetYearly.Value.Value;

                            var kpiTargetMonthly =
                                pmsConfigDetails.Kpi.KpiTargets.FirstOrDefault(
                                    x => x.PeriodeType == PeriodeType.Monthly && x.Periode.Month == request.Month && x.Periode.Year == request.Year);
                            if (kpiTargetMonthly != null && kpiTargetMonthly.Value.HasValue)
                                kpiData.TargetMonthly = kpiTargetMonthly.Value.Value;

                            var kpiTargetYtd = pmsConfigDetails.Kpi.KpiTargets.Where(
                                x =>
                                x.PeriodeType == PeriodeType.Monthly && x.Value.HasValue &&
                                (x.Periode.Month >= 1 && x.Periode.Month <= request.Month && x.Periode.Year == request.Year)).ToList();
                            if (kpiTargetYtd.Count > 0) kpiData.TargetYtd = 0;
                            foreach (var targetYtd in kpiTargetYtd)
                            {
                                if (targetYtd.Value.HasValue)
                                {
                                    kpiData.TargetYtd += targetYtd.Value;
                                }
                            }

                            if (kpiData.YtdFormula == YtdFormula.Average)
                            {
                                if (kpiData.TargetYtd.HasValue)
                                {
                                    kpiData.TargetYtd = kpiData.TargetYtd / kpiTargetYtd.Count;
                                }
                            }

                            #endregion

                            #region Score

                            switch (pmsConfigDetails.ScoringType)
                            {
                                case ScoringType.Positive:
                                    {
                                        kpiData.Score = pmsConfigDetails.Weight * kpiData.IndexYearly;
                                        var maxScore = pmsConfigDetails.Weight * 1.05;
                                        if (kpiData.Score >= maxScore)
                                            kpiData.Score = maxScore;
                                        break;
                                    }

                                case ScoringType.Negative:
                                    {
                                        if (kpiData.IndexYearly.Equals(0))
                                        {
                                            response.IsSuccess = false;
                                            response.Message =
                                                string.Format(
                                                    @"KPI {0} memiliki nilai index Yearly 0 dengan Nilai Scoring Type negative yang mengakibatkan terjadinya nilai infinity",
                                                    pmsConfigDetails.Kpi.Name);
                                            return response;
                                        }

                                        kpiData.Score = (kpiData.IndexYearly <= _maxNegativeScore)
                                                            ? pmsConfigDetails.Weight * 1.05
                                                            : pmsConfigDetails.Weight / kpiData.IndexYearly;
                                        break;
                                    }

                                case ScoringType.Boolean:
                                    kpiData.Score = 0;
                                    if (kpiAchievementYearly.Value.HasValue && !string.IsNullOrEmpty(pmsConfigDetails.Target) && pmsConfigDetails.Target.Contains("x"))
                                    {
                                        Expression e = new Expression(pmsConfigDetails.Target.Replace("x", kpiAchievementYearly.Value.Value.ToString("f2", CultureInfo.InvariantCulture)));
                                        bool isPassed = (bool)e.Evaluate();
                                        if (isPassed)
                                        {
                                            kpiData.Score = kpiData.Weight;
                                        }
                                    }

                                    break;
                            }

                            #endregion

                            kpiData.KpiColor = GetScoreColor(kpiData.ActualYearly, pmsConfigDetails.ScoreIndicators);

                            response.KpiDatas.Add(kpiData);
                        }

                        response.KpiDatas = SetPmsConfigColor(response.KpiDatas, pmsConfig.ScoreIndicators,
                                                              pmsConfig.Pillar.Id);
                    }

                    response.KpiDatas = SetPmsSummaryColor(response.KpiDatas, pmsSummary.ScoreIndicators);
                }

                response.IsSuccess = true;
            }
            catch (InvalidOperationException invalidOperationException)
            {
                response.Message = invalidOperationException.Message;
            }
            catch (NullReferenceException nullReferenceException)
            {
                response.Message = nullReferenceException.Message;
            }

            return response;
        }