예제 #1
0
 public GetKpiAchievementResponse GetKpiAchievementByValue(GetKpiAchievementRequestByValue request)
 {
     PeriodeType periodeType = (PeriodeType)Enum.Parse(typeof(PeriodeType), request.PeriodeType);
     var response = new GetKpiAchievementResponse();
     response.PeriodeType = periodeType;
     try
     {
         var kpiAchievement = DataContext.KpiAchievements.Include(x => x.Kpi).Single(x => x.Kpi.Id == request.Kpi_Id && x.PeriodeType == periodeType && x.Periode == request.periode);
         response = kpiAchievement.MapTo<GetKpiAchievementResponse>();
         response.IsSuccess = true;
     }
     catch (InvalidOperationException invalidOperationException)
     {
         response.IsSuccess = false;
         response.Message = invalidOperationException.Message;
     }
     catch (ArgumentNullException argumentNullException)
     {
         response.IsSuccess = false;
         response.Message = argumentNullException.Message;
     }
     return response;
 }
예제 #2
0
        public GetKpiAchievementResponse GetKpiAchievement(int kpiId, DateTime date, RangeFilter rangeFilter, YtdFormula ytdFormula)
        {
            var response = new GetKpiAchievementResponse();
            try
            {
                switch (rangeFilter)
                {
                    case RangeFilter.CurrentDay:
                        {
                            var kpi = DataContext.Kpis.Include(x => x.Measurement).Single(x => x.Id == kpiId);
                            var data = DataContext.KpiAchievements.Include(x => x.Kpi).FirstOrDefault(x => x.Kpi.Id == kpiId && x.PeriodeType == PeriodeType.Daily && x.Periode == date);
                            var kpiResponse = new GetKpiAchievementResponse.KpiResponse
                            {
                                Id = kpi.Id,
                                Measurement = kpi.Measurement.Name,
                                Name = kpi.Name,
                                Remark = kpi.Remark,
                            };

                            return new GetKpiAchievementResponse
                            {
                                Value = (data != null) ? data.Value : null,
                                Kpi = kpiResponse,
                                IsSuccess = true
                            };
                        }

                    case RangeFilter.MTD:
                        {
                            var kpi = DataContext.Kpis.Include(x => x.Measurement).Single(x => x.Id == kpiId);
                            var data = DataContext.KpiAchievements.Include(x => x.Kpi)
                                .Where(x => x.Kpi.Id == kpiId && x.PeriodeType == PeriodeType.Daily &&
                                    (x.Periode.Day >= 1 && x.Periode.Day <= date.Day && x.Periode.Month == date.Month && x.Periode.Year == date.Year)).AsQueryable();
                            double? kpiAchievement = ytdFormula == YtdFormula.Average ? data.Average(x => x.Value) : data.Sum(x => x.Value);
                            var kpiResponse = new GetKpiAchievementResponse.KpiResponse
                            {
                                Id = kpi.Id,
                                Measurement = kpi.Measurement.Name,
                                Name = kpi.Name,
                                Remark = kpi.Remark,
                            };

                            return new GetKpiAchievementResponse
                            {
                                Value = kpiAchievement,
                                Kpi = kpiResponse,
                                IsSuccess = true
                            };
                        }

                    case RangeFilter.YTD:
                        {
                            var kpi = DataContext.Kpis.Include(x => x.Measurement).Single(x => x.Id == kpiId);
                            var data = DataContext.KpiAchievements.Include(x => x.Kpi)
                                    .Where(x => x.Kpi.Id == kpiId && x.PeriodeType == PeriodeType.Monthly && x.Value.HasValue &&
                                    (x.Periode.Month >= 1 && x.Periode.Month <= date.Month && x.Periode.Year == date.Year)).AsQueryable();
                            double? kpiAchievement = ytdFormula == YtdFormula.Average ? data.Average(x => x.Value) : data.Sum(x => x.Value);
                            var kpiResponse = new GetKpiAchievementResponse.KpiResponse
                                {
                                    Id = kpi.Id,
                                    Measurement = kpi.Measurement.Name,
                                    Name = kpi.Name,
                                    Remark = kpi.Remark,
                                };

                            return new GetKpiAchievementResponse
                            {
                                Value = kpiAchievement,
                                Kpi = kpiResponse,
                                IsSuccess = true
                            };
                        }

                    case RangeFilter.AllExistingYears:
                        {
                            var kpi = DataContext.Kpis.Include(x => x.Measurement).Single(x => x.Id == kpiId);
                            var data = DataContext.KpiAchievements.Include(x => x.Kpi)
                                    .Where(x => x.Kpi.Id == kpiId && x.PeriodeType == PeriodeType.Yearly && x.Value.HasValue &&
                                    (x.Periode.Year >= 2011 && x.Periode.Year <= date.Year)).AsQueryable();
                            double? kpiAchievement = ytdFormula == YtdFormula.Average ? data.Average(x => x.Value) : data.Sum(x => x.Value);
                            var kpiResponse = new GetKpiAchievementResponse.KpiResponse
                            {
                                Id = kpi.Id,
                                Measurement = kpi.Measurement.Name,
                                Name = kpi.Name,
                                Remark = kpi.Remark,
                            };

                            return new GetKpiAchievementResponse
                            {
                                Value = kpiAchievement,
                                Kpi = kpiResponse,
                                IsSuccess = true
                            };
                        }
                    case RangeFilter.CurrentWeek:
                        {
                            DateTime lastWednesday = date;
                            while (lastWednesday.DayOfWeek != DayOfWeek.Wednesday)
                                lastWednesday = lastWednesday.AddDays(-1);
                            var kpi = DataContext.Kpis.Include(x => x.Measurement).Single(x => x.Id == kpiId);
                            var data = DataContext.KpiAchievements.Include(x => x.Kpi)
                                    .Where(x => x.Kpi.Id == kpiId && x.PeriodeType == PeriodeType.Daily && x.Value.HasValue &&
                                        ((x.Periode.Year == lastWednesday.Year && x.Periode.Month == lastWednesday.Month && x.Periode.Day >= lastWednesday.Day) &&
                                        (x.Periode.Year == date.Year && x.Periode.Month == date.Month && x.Periode.Day <= date.Day))).AsQueryable();

                            double? kpiAchievement = ytdFormula == YtdFormula.Average ? data.Average(x => x.Value) : data.Sum(x => x.Value);
                            var kpiResponse = new GetKpiAchievementResponse.KpiResponse
                            {
                                Id = kpi.Id,
                                Measurement = kpi.Measurement.Name,
                                Name = kpi.Name,
                                Remark = kpi.Remark,
                            };

                            return new GetKpiAchievementResponse
                            {
                                Value = kpiAchievement,
                                Kpi = kpiResponse,
                                IsSuccess = true
                            };
                        }
                }

            }
            catch (Exception exception)
            {
                response.Message = exception.Message;
            }

            return response;
        }
예제 #3
0
        public GetKpiAchievementResponse GetKpiAchievement(int kpiId, DateTime date, RangeFilter rangeFilter)
        {
            var response = new GetKpiAchievementResponse();
            try
            {
                switch (rangeFilter)
                {
                    case RangeFilter.CurrentDay:
                    case RangeFilter.MTD:
                    case RangeFilter.YTD:
                    case RangeFilter.AllExistingYears:
                    case RangeFilter.CurrentWeek:
                        {
                            var kpi = DataContext.Kpis
                                .Include(x => x.Measurement)
                                .Single(x => x.Id == kpiId);

                            return GetKpiAchievement(kpi.Id, date, rangeFilter, kpi.YtdFormula);
                        }
                }

            }
            catch (Exception exception)
            {
                response.Message = exception.Message;
            }

            return response;
        }