コード例 #1
0
        public BaseResponse KpiEconomicInput(KpiEconomicInputRequest request)
        {
            try
            {
                var scenario = DataContext.Scenarios.FirstOrDefault(x => x.IsActive && x.IsDashboard);
                if (scenario == null)
                {
                    return new BaseResponse
                    {
                        IsSuccess = false,
                        Message = "An error occured, please contact adminstrator for further information"
                    };
                }

                //yearly
                var year = request.Start.Year;
                var yarlyEconomic = DataContext.KeyOperationDatas.FirstOrDefault(x => x.Kpi.Id == request.KpiId && x.PeriodeType == PeriodeType.Yearly
                    && x.Periode.Year == year && x.Scenario.Id == scenario.Id);
                var kpi = new Kpi { Id = request.KpiId };
                DataContext.Kpis.Attach(kpi);
                if (yarlyEconomic != null)
                {
                    yarlyEconomic.Value = request.Value;
                }
                else
                {
                    var newYearlyEconomic = new KeyOperationData
                    {
                        Value = request.Value,
                        PeriodeType = PeriodeType.Yearly,
                        Periode = request.Start,
                        Kpi = kpi,
                        Scenario = scenario
                    };
                    DataContext.KeyOperationDatas.Add(newYearlyEconomic);
                }

                //monthly
                //var monthlyEconomics = DataContext.KeyOperationDatas.Where(x => x.Kpi.Id == request.KpiId
                //    && x.PeriodeType == PeriodeType.Monthly
                //    && x.Periode >= request.Start && x.Periode <= request.End
                //    && x.Scenario.Id == scenario.Id).ToList();

                //for (var i = request.Start.Month; i <= request.End.Month; i++)
                //{
                //    var monthlyEconomic = monthlyEconomics.FirstOrDefault(x => x.Periode.Month == i);
                //    if (monthlyEconomic != null)
                //    {
                //        monthlyEconomic.Value = request.Value;
                //    }
                //    else
                //    {
                //        var newMonthlyEconomic = new KeyOperationData
                //        {
                //            Periode = new DateTime(year, i, 1),
                //            PeriodeType = PeriodeType.Monthly,
                //            Value = request.Value,
                //            Kpi = kpi,
                //            Scenario = scenario
                //        };
                //        DataContext.KeyOperationDatas.Add(newMonthlyEconomic);
                //    }
                //}

                DataContext.SaveChanges();
                return new BaseResponse
                {
                    IsSuccess = true,
                    Message = "Kpi Valu has been saved successfully"
                };
            }
            catch
            {
                return new BaseResponse
                {
                    IsSuccess = false,
                    Message = "An error occured, please contact adminstrator for further information"
                };
            }
        }
コード例 #2
0
ファイル: OutputConfigService.cs プロジェクト: fazar/Pear
        private OutputResult Payback(KeyOutputConfiguration keyOutput, int scenarioId, bool fromCOD = true)
        {
            var currentYear = DateTime.Now.Year;
            var currentMonth = DateTime.Now.Month;
            var currentDate = DateTime.Now;
            var result = new OutputResult();
            var kpiIds = keyOutput.KpiIds.Split(',').Select(x => int.Parse(x)).ToList();
            var kpiId = keyOutput.Kpis.First(x => x.Id == kpiIds[0]).Id;
            var assumptionIds = keyOutput.KeyAssumptionIds.Split(',').Select(x => int.Parse(x)).ToList();
            var startId = keyOutput.KeyAssumptions.First(x => x.Id == assumptionIds[0]).Id;
            var endId = keyOutput.KeyAssumptions.First(x => x.Id == assumptionIds[1]).Id;
            var commercialId = 0;
            KeyAssumptionData commercialAssumption = null;
            if (fromCOD)
            {
                commercialId = keyOutput.KeyAssumptions.First(x => x.Id == assumptionIds[2]).Id;
                commercialAssumption = DataContext.KeyAssumptionDatas.FirstOrDefault(x => x.KeyAssumptionConfig.Id == commercialId);
            }
            var startAssumption = DataContext.KeyAssumptionDatas.FirstOrDefault(x => x.KeyAssumptionConfig.Id == startId);
            var endAssumption = DataContext.KeyAssumptionDatas.FirstOrDefault(x => x.KeyAssumptionConfig.Id == endId);

            if (startAssumption == null || endAssumption == null)
            {
                return new OutputResult();
            }
            DateTime startForecast;
            DateTime endForecast;
            if (IsStartAndEndValid(startAssumption.ForecastValue, endAssumption.ForecastValue, out startForecast, out endForecast))
            {
                var forecastList = DataContext.KeyOperationDatas.Where(x => x.Kpi.Id == kpiId && x.Scenario.Id == scenarioId
                   && x.Periode.Year >= startForecast.Year && x.Periode.Year <= endForecast.Year
                   && x.PeriodeType == PeriodeType.Yearly
                   && x.Value.HasValue).ToList();
                var accumulationForecastList = new List<KeyOperationData>();
                foreach (var fc in forecastList.OrderBy(x => x.Periode).ToList())
                {
                    var accForecast = new KeyOperationData
                    {
                        Periode = fc.Periode,
                        PeriodeType = fc.PeriodeType,
                        Value = forecastList.Where(x => x.Periode <= fc.Periode).Sum(x => x.Value)
                    };
                    accumulationForecastList.Add(accForecast);
                }
                var forecast = accumulationForecastList.OrderBy(x => x.Periode).FirstOrDefault(x => x.Value > 0);
                double breakEventYearWeight = 1;
                if (forecast != null && forecast.Periode.Year != startForecast.Year)
                {
                    var prev = accumulationForecastList.FirstOrDefault(x => x.Periode.Year == (forecast.Periode.Year - 1));
                    if (prev != null && prev.Value - forecast.Value != 0)
                    {
                        breakEventYearWeight = double.Parse(string.Format("{0:0.0}", (prev.Value / (prev.Value - forecast.Value))));
                    }
                    if (prev != null)
                    {
                        result.Forecast = (forecast.Periode.Year - startForecast.Year + breakEventYearWeight).ToString();
                        DateTime constForecast;
                        DateTime commercialForecast;
                        if (fromCOD && IsStartAndEndValid(startAssumption.ForecastValue, commercialAssumption.ForecastValue, out constForecast, out commercialForecast))
                        {
                            result.Forecast = (double.Parse(result.Forecast) - ((commercialForecast - constForecast).Days / 365.00)).ToString();
                        }
                    }

                }
            }
            DateTime startActual;
            DateTime endActual;
            if (IsStartAndEndValid(startAssumption.ActualValue, endAssumption.ActualValue, out startActual, out endActual))
            {
                var pastValues = DataContext.KpiAchievements.Where(x => x.Kpi.Id == kpiId
                   && x.Periode.Year >= startActual.Year && x.Periode.Year < currentYear
                   && x.PeriodeType == PeriodeType.Yearly
                   && x.Value.HasValue).Select(x => new { Value = x.Value, Periode = x.Periode, PeriodeType = x.PeriodeType }).ToList();
                var futureValues = DataContext.KeyOperationDatas.Where(x => x.Kpi.Id == kpiId
                    && x.Periode.Year <= endActual.Year && x.Periode.Year > currentYear
                    && x.PeriodeType == PeriodeType.Yearly && x.Scenario.Id == scenarioId
                    && x.Value.HasValue).Select(x => new { Value = x.Value, Periode = x.Periode, PeriodeType = x.PeriodeType }).ToList();
                var untilNowThisYear = DataContext.KpiAchievements.Where(x => x.Kpi.Id == kpiId && x.Periode.Year == currentYear
                    && x.PeriodeType == PeriodeType.Monthly && x.Value.HasValue).OrderBy(x => x.Periode).ToList();
                var startingForecastMonthCurrentYear = 1;
                if (untilNowThisYear.Count > 0)
                {
                    startingForecastMonthCurrentYear = untilNowThisYear.Last().Periode.Month + 1;
                }
                var untilNowThisYearValue = untilNowThisYear.Sum(x => x.Value);
                var thisYearForecastValue = DataContext.KeyOperationDatas.Where(x => x.Kpi.Id == kpiId
                    && x.Periode.Year == currentYear && x.Periode.Month >= startingForecastMonthCurrentYear
                    && x.PeriodeType == PeriodeType.Monthly && x.Scenario.Id == scenarioId
                    && x.Value.HasValue).Sum(x => x.Value);
                var currentYearValue = new
                {
                    Value = new List<double?> { thisYearForecastValue, untilNowThisYearValue }.Sum(),
                    Periode = DateTime.Now,
                    PeriodeType = PeriodeType.Yearly
                };
                pastValues.Add(currentYearValue);
                var actualList = pastValues.Concat(futureValues);
                var accActualList = new List<KeyOperationData>();

                foreach (var ac in actualList.OrderBy(x => x.Periode).ToList())
                {
                    var accActual = new KeyOperationData
                    {
                        Periode = ac.Periode,
                        PeriodeType = ac.PeriodeType,
                        Value = actualList.Where(x => x.Periode <= ac.Periode).Sum(x => x.Value)
                    };
                    accActualList.Add(accActual);
                }

                var actual = accActualList.OrderBy(x => x.Periode).FirstOrDefault(x => x.Value > 0);

                double actualBreakEventYearWeight = 1;
                if (actual != null && actual.Periode.Year != startForecast.Year)
                {
                    var prev = accActualList.FirstOrDefault(x => x.Periode.Year == (actual.Periode.Year - 1));
                    if (prev != null && prev.Value - actual.Value != 0)
                    {
                        actualBreakEventYearWeight = double.Parse(string.Format("{0:0.0}", (prev.Value / (prev.Value - actual.Value))));
                    }
                    if (prev != null)
                    {
                        result.Actual = (actual.Periode.Year - startForecast.Year + actualBreakEventYearWeight).ToString();
                        DateTime constActual;
                        DateTime commercialActual;
                        if (fromCOD && IsStartAndEndValid(startAssumption.ActualValue, commercialAssumption.ActualValue, out constActual, out commercialActual))
                        {
                            result.Actual = (double.Parse(result.Actual) - ((commercialActual - constActual).Days / 365.00)).ToString();
                        }
                    }

                }
            }

            return result;
        }