Esempio n. 1
0
 public SavePlanningBlueprintResponse SavePlanningBlueprint(SavePlanningBlueprintRequest request)
 {
     try
     {
         if (request.Id == 0)
         {
             var planningBluePrint = request.MapTo<PlanningBlueprint>();
             var environmentsScanning = new EnvironmentsScanning();
             var businessPostureIdentification = new BusinessPostureIdentification { IsLocked = true };
             var midtermPhaseFormulation = new MidtermPhaseFormulation { IsLocked = true };
             var midtermStrategyPlanning = new MidtermStrategyPlanning { IsLocked = true };
             var constructionPosture = new Posture { Type = PostureType.Construction };
             var operationPosture = new Posture { Type = PostureType.Operation };
             var decommissioningPosture = new Posture { Type = PostureType.Decommissioning };
             businessPostureIdentification.Postures.Add(constructionPosture);
             businessPostureIdentification.Postures.Add(operationPosture);
             businessPostureIdentification.Postures.Add(decommissioningPosture);
             planningBluePrint.EnvironmentsScanning = environmentsScanning;
             planningBluePrint.BusinessPostureIdentification = businessPostureIdentification;
             planningBluePrint.MidtermPhaseFormulation = midtermPhaseFormulation;
             planningBluePrint.MidtermStragetyPlanning = midtermStrategyPlanning;
             foreach (var keyOutputId in request.KeyOutputIds)
             {
                 var keyOutputConfig = new KeyOutputConfiguration { Id = keyOutputId };
                 DataContext.KeyOutputConfigs.Attach(keyOutputConfig);
                 planningBluePrint.KeyOutput.Add(keyOutputConfig);
             }
             DataContext.PlanningBlueprints.Add(planningBluePrint);
         }
         else
         {
             var planningBlueprint = DataContext.PlanningBlueprints
                 .Include(x => x.KeyOutput).First(x => x.Id == request.Id);
             planningBlueprint.KeyOutput = new List<KeyOutputConfiguration>();
             foreach (var keyOutputId in request.KeyOutputIds)
             {
                 var keyOutputConfig = DataContext.KeyOutputConfigs.Local.FirstOrDefault(x => x.Id == keyOutputId);
                 if (keyOutputConfig == null)
                 {
                     keyOutputConfig = new KeyOutputConfiguration { Id = keyOutputId };
                     DataContext.KeyOutputConfigs.Attach(keyOutputConfig);
                 }
                 planningBlueprint.KeyOutput.Add(keyOutputConfig);
             }
             request.MapPropertiesToInstance<PlanningBlueprint>(planningBlueprint);
         }
         DataContext.SaveChanges();
         return new SavePlanningBlueprintResponse
         {
             IsSuccess = true,
             Message = "The item has been successfully saved"
         };
     }
     catch
     {
         return new SavePlanningBlueprintResponse
         {
             IsSuccess = false,
             Message = "An error occured, please contact administrator for further information"
         };
     }
 }
Esempio n. 2
0
 private OutputResult ProfitInvestmentRatio(KeyOutputConfiguration keyOutput, int scenarioId)
 {
     var fcf = this.Sum(keyOutput, scenarioId);
     var projectCost = this.Sum(keyOutput, scenarioId, 1);
     var result = new OutputResult();
     if (!string.IsNullOrEmpty(fcf.Actual) && !string.IsNullOrEmpty(projectCost.Actual))
     {
         result.Actual = String.Format("{0:0.0}", double.Parse(fcf.Actual) /
             double.Parse(projectCost.Actual));
     }
     if (!string.IsNullOrEmpty(fcf.Forecast) && !string.IsNullOrEmpty(projectCost.Forecast))
     {
         result.Forecast = String.Format("{0:0.0}", double.Parse(fcf.Forecast) /
             double.Parse(projectCost.Forecast));
     }
     return result;
 }
Esempio n. 3
0
        private OutputResult Sum(KeyOutputConfiguration keyOutput, int scenarioId, int kpiIdIndex = 0)
        {
            var currentYear = DateTime.Now.Year;
            var currentMonth = DateTime.Now.Month;
            var result = new OutputResult();
            var kpiIds = keyOutput.KpiIds.Split(',').Select(x => int.Parse(x)).ToList();
            var kpiId = keyOutput.Kpis.First(x => x.Id == kpiIds[kpiIdIndex]).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 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 forecastValue = 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)
                    .Sum(x => x.Value);
                if (forecastValue.HasValue) result.Forecast = forecastValue.ToString();
            }

            DateTime startActual;
            DateTime endActual;
            if (IsStartAndEndValid(startAssumption.ActualValue, endAssumption.ActualValue, out startActual, out endActual))
            {
                var pastValue = DataContext.KpiAchievements.Where(x => x.Kpi.Id == kpiId
                    && x.Periode.Year >= startActual.Year && x.Periode.Year < currentYear && x.PeriodeType == PeriodeType.Yearly
                    && x.Value.HasValue).Sum(x => x.Value);
                var futureValue = 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).Sum(x => x.Value);
                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 actualValues =
                    new List<double?> { pastValue, futureValue, untilNowThisYearValue, thisYearForecastValue };
                if (actualValues.Any(x => x.HasValue))
                {
                    result.Actual = actualValues.Sum().ToString();
                }
            }
            return result;
        }
Esempio n. 4
0
        private OutputResult MinDate(KeyOutputConfiguration keyOutput, int scenarioId)
        {
            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 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 minForecast = DataContext.KeyOperationDatas.Where(x => x.Kpi.Id == kpiId && x.Scenario.Id == scenarioId
                    && x.Periode >= startForecast && x.Periode <= endForecast && x.PeriodeType == PeriodeType.Monthly
                    && x.Value != keyOutput.ExcludeValue
                    && x.Value.HasValue)
                    .OrderBy(x => x.Value).FirstOrDefault();
                if (minForecast != null) result.Forecast = minForecast.Periode.AddMonths(1).AddDays(-1).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 >= startActual && x.PeriodeType == PeriodeType.Monthly
                    && x.Value != keyOutput.ExcludeValue
                    && x.Value.HasValue).OrderBy(x => x.Periode).Select(x => new
                    {
                        Periode = x.Periode,
                        Value = x.Value
                    });
                var startingDateForecast = startActual;
                if (pastValues.Count() > 0) {
                    startingDateForecast = pastValues.Last().Periode.AddMonths(1);
                }
                var futureValues = DataContext.KeyOperationDatas.Where(x => x.Kpi.Id == kpiId
                    && x.Periode >= startingDateForecast && x.Periode <= endForecast && x.PeriodeType == PeriodeType.Monthly && x.Scenario.Id == scenarioId
                    && x.Value != keyOutput.ExcludeValue
                    && x.Value.HasValue).Select(x => new
                    {
                        Periode = x.Periode,
                        Value = x.Value
                    });

                var minActual = pastValues.Concat(futureValues).OrderBy(x => x.Value).FirstOrDefault();
                if (minActual != null) result.Actual = minActual.Periode.AddMonths(1).AddDays(-1).ToString();
            }
            return result;
        }
Esempio n. 5
0
        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;
        }
Esempio n. 6
0
        private OutputResult Irr(KeyOutputConfiguration keyOutput, int scenarioId)
        {
            var currentYear = DateTime.Now.Year;
            var currentMonth = DateTime.Now.Month;
            var currentDate = DateTime.Now;
            var result = new OutputResult();
            var kpiId = keyOutput.Kpis[0].Id;
            var keyAssumptionIds = keyOutput.KeyAssumptionIds.Split(',');
            var startId = int.Parse(keyAssumptionIds[0]);
            var endId = int.Parse(keyAssumptionIds[1]);

            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 forecast = DataContext.KeyOperationDatas.Where(x => x.Scenario.Id == scenarioId && x.Kpi.Id == kpiId
                        && x.Periode.Year >= startForecast.Year && x.Periode.Year <= endForecast.Year
                    && x.PeriodeType == PeriodeType.Yearly
                    && x.Value.HasValue).OrderBy(x => x.Periode).Select(x => x.Value.Value).ToList().ToArray();
                if (forecast.Length >= 2)
                {
                    var forecastIrrCalculator = new NewtonRaphsonIRRCalculator(forecast);
                    result.Forecast = forecastIrrCalculator.ComputeIRR().ToString();
                }
            }
            DateTime startActual;
            DateTime endActual;
            if (IsStartAndEndValid(startAssumption.ActualValue, endAssumption.ActualValue, out startActual, out endActual))
            {
                var past = DataContext.KpiAchievements.Where(x => x.Kpi.Id == kpiId
                    && startActual.Year <= x.Periode.Year && x.Periode.Year < currentYear
                    && x.PeriodeType == PeriodeType.Yearly &&  x.Value.HasValue).OrderBy(x => x.Periode).Select(x => x.Value).ToList();
                var future = DataContext.KeyOperationDatas.Where(x => x.Kpi.Id == kpiId && x.Scenario.Id == scenarioId
                    && x.Periode.Year <= endActual.Year && x.Periode.Year > currentYear
                    && x.PeriodeType == PeriodeType.Yearly && x.Value.HasValue).OrderBy(x => x.Periode).Select(x => x.Value).ToList();
                var untilNowThisYear = DataContext.KpiAchievements.Where(x => x.Kpi.Id == kpiId
                  && x.PeriodeType == PeriodeType.Monthly
                  && x.Periode.Year == currentYear && x.Value.HasValue).OrderBy(x => x.Periode).ToList();
                var startingMonthForecast = 1;
                if (untilNowThisYear.Count > 0)
                {
                    startingMonthForecast = untilNowThisYear.Last().Periode.Month + 1;
                }

                var currentPastMonths = untilNowThisYear.Sum(x => x.Value);
                var currentNextMonths = DataContext.KeyOperationDatas.Where(x => x.Kpi.Id == kpiId && x.Scenario.Id == scenarioId
                    && x.Periode.Year == currentYear && x.Periode.Month >= startingMonthForecast
                    && x.PeriodeType == PeriodeType.Monthly && x.Value.HasValue).OrderBy(x => x.Periode).Sum(x => x.Value);
                past.Add(currentPastMonths + currentNextMonths);
                var actualArray = past.Concat(future).ToArray();
                if (actualArray.Length >= 2)
                {
                    var actualIrrCalculator = new NewtonRaphsonIRRCalculator(actualArray.Select(x => x.HasValue ? x.Value : 0).ToArray());
                    result.Actual = actualIrrCalculator.ComputeIRR().ToString();
                }
            }
            return result;
        }
Esempio n. 7
0
        //by default it will use sum as aggregator
        private OutputResult HeaderSubstruction(KeyOutputConfiguration keyOutput, int scenarioId, bool useAverage = false)
        {
            var currentYear = DateTime.Now.Year;
            var currentMonth = DateTime.Now.Month;
            var currentDate = DateTime.Now;
            var kpiIds = keyOutput.KpiIds.Split(',').Select(x => int.Parse(x)).ToList();
            var headId = kpiIds[0];
            var result = new OutputResult();
            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 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();
            }

            var headKeyOutpu = new KeyOutputConfiguration();
            headKeyOutpu.KeyAssumptions = keyOutput.KeyAssumptions;
            headKeyOutpu.KeyAssumptionIds = keyOutput.KeyAssumptionIds;
            headKeyOutpu.Kpis = new List<Kpi> { keyOutput.Kpis.First(x => x.Id == headId) };
            headKeyOutpu.KpiIds = headId.ToString();
            var headResult = new OutputResult();
            if (useAverage)
            {
                headResult = Average(headKeyOutpu, scenarioId);
            }
            else
            {
                headResult = Sum(headKeyOutpu, scenarioId);
            }
            var restResults = new List<OutputResult>();
            foreach (var kpiId in kpiIds)
            {
                if (kpiId != headId)
                {
                    var newKeyOutput = new KeyOutputConfiguration();
                    newKeyOutput.KeyAssumptions = keyOutput.KeyAssumptions;
                    newKeyOutput.KeyAssumptionIds = keyOutput.KeyAssumptionIds;
                    newKeyOutput.Kpis = new List<Kpi> { keyOutput.Kpis.First(x => x.Id == kpiId) };
                    newKeyOutput.KpiIds = kpiId.ToString();
                    if (useAverage)
                    {
                        restResults.Add(Average(newKeyOutput, scenarioId));
                    }
                    else
                    {
                        restResults.Add(Sum(newKeyOutput, scenarioId));
                    }
                }

            }
            if (!string.IsNullOrEmpty(headResult.Forecast))
            {
                result.Forecast = (double.Parse(headResult.Forecast) - restResults.Select(x => x.Forecast).Sum(x => string.IsNullOrEmpty(x) ? 0.00 : double.Parse(x))).ToString();
            }
            if (!string.IsNullOrEmpty(headResult.Actual))
            {
                result.Actual = (double.Parse(headResult.Actual) - restResults.Select(x => x.Actual).Sum(x => string.IsNullOrEmpty(x) ? 0.00 : double.Parse(x))).ToString();
            }
            return result;
        }
Esempio n. 8
0
        private OutputResult CompletionDate(KeyOutputConfiguration keyOutput, int scenarioId)
        {
            var currentYear = DateTime.Now.Year;
            var currentMonth = DateTime.Now.Month;
            var currentDate = DateTime.Now;
            var result = new OutputResult();
            var kpiId = keyOutput.Kpis[0].Id;
            var completionId = keyOutput.KeyAssumptions[0].Id;
            var completionAssumption = DataContext.KeyAssumptionDatas.FirstOrDefault(x => x.KeyAssumptionConfig.Id == completionId);
            if (completionAssumption == null)
            {
                return new OutputResult();
            }

            DateTime completionForecast;
            DateTime completionActual;
            if (IsStartAndEndValid(completionAssumption.ForecastValue, completionAssumption.ActualValue, out completionForecast, out completionActual))
            {
                var forecast = DataContext.KeyOperationDatas.FirstOrDefault(x => x.Kpi.Id == kpiId && x.Scenario.Id == scenarioId
                    && x.PeriodeType == PeriodeType.Monthly
                    && x.Periode.Year == completionForecast.Year && x.Periode.Month == completionForecast.Month
                    && x.Value.HasValue);
                if (forecast != null) result.Forecast = forecast.Value.ToString();

                if (currentYear == completionActual.Year)
                {
                    var untilNowThisYear = DataContext.KpiAchievements.Where(x => x.Kpi.Id == kpiId
                    && x.PeriodeType == PeriodeType.Monthly
                    && x.Periode.Year == completionForecast.Year && x.Value.HasValue).OrderBy(x => x.Periode).ToList();
                    var startingMonthForecast = 1;
                    if (untilNowThisYear.Count > 0)
                    {
                        startingMonthForecast = untilNowThisYear.Last().Periode.Month + 1;
                    }

                    var pastMonthsThisYear = untilNowThisYear.Sum(x => x.Value);

                    var nextMonthThisYear = DataContext.KeyOperationDatas.Where(x => x.Kpi.Id == kpiId && scenarioId == x.Scenario.Id
                    && x.PeriodeType == PeriodeType.Monthly
                    && x.Periode.Year == completionForecast.Year && x.Periode.Month >= startingMonthForecast
                    && x.Value.HasValue).Sum(x => x.Value);

                    if (pastMonthsThisYear.HasValue || nextMonthThisYear.HasValue)
                    {
                        result.Actual = (pastMonthsThisYear + nextMonthThisYear).ToString();
                    }
                }
                else
                {
                    var actual = DataContext.KpiAchievements.FirstOrDefault(x => x.Kpi.Id == kpiId
                    && x.PeriodeType == PeriodeType.Monthly
                    && x.Periode.Year == completionForecast.Year && x.Periode.Month == completionForecast.Month
                    && x.Value.HasValue);
                    if (actual != null) result.Actual = actual.ToString();
                }
            }
            return result;
        }
Esempio n. 9
0
 private CalculateOutputResponse.KeyOutputResponse CalculateFormula(KeyOutputConfiguration keyOutput, int scenarioId)
 {
     var resp = keyOutput.MapTo<CalculateOutputResponse.KeyOutputResponse>();
     switch (keyOutput.Formula)
     {
         case Formula.SUM:
             {
                 var result = this.Sum(keyOutput, scenarioId);
                 resp.Actual = result.Actual;
                 resp.Forecast = result.Forecast;
             }
             break;
         case Formula.AVERAGE:
             {
                 var result = this.Average(keyOutput, scenarioId);
                 resp.Actual = result.Actual;
                 resp.Forecast = result.Forecast;
             }
             break;
         case Formula.MIN:
             {
                 var result = this.Min(keyOutput, scenarioId);
                 resp.Actual = result.Actual;
                 resp.Forecast = result.Forecast;
             }
             break;
         //is it daily
         case Formula.MINDATE:
             {
                 var result = this.MinDate(keyOutput, scenarioId);
                 resp.Actual = result.Actual;
                 resp.Forecast = result.Forecast;
             }
             break;
         case Formula.BREAKEVENTYEAR:
             {
                 var result = this.BreakEventYear(keyOutput, scenarioId);
                 resp.Actual = result.Actual;
                 resp.Forecast = result.Forecast;
             }
             break;
         case Formula.PAYBACK:
             {
                 var result = this.Payback(keyOutput, scenarioId);
                 resp.Actual = result.Actual;
                 resp.Forecast = result.Forecast;
             }
             break;
         case Formula.PROFITINVESTMENTRATIO:
             {
                 var result = this.ProfitInvestmentRatio(keyOutput, scenarioId);
                 resp.Actual = result.Actual;
                 resp.Forecast = result.Forecast;
             }
             break;
         case Formula.COMPLETIONDATE:
             {
                 var result = this.CompletionDate(keyOutput, scenarioId);
                 resp.Actual = result.Actual;
                 resp.Forecast = result.Forecast;
             }
             break;
         case Formula.GROSSPROFIT:
             {
                 var result = this.HeaderSubstruction(keyOutput, scenarioId);
                 resp.Actual = result.Actual;
                 resp.Forecast = result.Forecast;
             }
             break;
         case Formula.NETBACKVALUE:
             {
                 var result = this.HeaderSubstruction(keyOutput, scenarioId, true);
                 resp.Actual = result.Actual;
                 resp.Forecast = result.Forecast;
             }
             break;
         case Formula.PROJECTIRR:
         case Formula.EQUITYIRR:
             {
                 var result = this.Irr(keyOutput, scenarioId);
                 resp.Actual = result.Actual;
                 resp.Forecast = result.Forecast;
             }
             break;
     }
     if (keyOutput.ConversionType.HasValue && keyOutput.ConversionValue.HasValue)
     {
         if (!string.IsNullOrEmpty(resp.Actual))
         {
             switch (keyOutput.ConversionType)
             {
                 case ConversionType.Division:
                     resp.Actual = (double.Parse(resp.Actual) / keyOutput.ConversionValue.Value).ToString();
                     break;
                 default:
                     resp.Actual = (double.Parse(resp.Actual) * keyOutput.ConversionValue.Value).ToString();
                     break;
             }
         }
         if (!string.IsNullOrEmpty(resp.Forecast))
         {
             switch (keyOutput.ConversionType)
             {
                 case ConversionType.Division:
                     resp.Forecast = (double.Parse(resp.Forecast) / keyOutput.ConversionValue.Value).ToString();
                     break;
                 default:
                     resp.Forecast = (double.Parse(resp.Forecast) * keyOutput.ConversionValue.Value).ToString();
                     break;
             }
         }
     }
     return resp;
 }
Esempio n. 10
0
        private OutputResult BreakEventYear(KeyOutputConfiguration keyOutput, int scenarioId)
        {
            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 startAssumption = DataContext.KeyAssumptionDatas.FirstOrDefault(x => x.KeyAssumptionConfig.Id == startId);
            var endAssumption = DataContext.KeyAssumptionDatas.FirstOrDefault(x => x.KeyAssumptionConfig.Id == endId);
            var payback = Payback(keyOutput, scenarioId, false);
            if (startAssumption == null || endAssumption == null)
            {
                return new OutputResult();
            }
            DateTime startForecast;
            DateTime endForecast;
            if (!string.IsNullOrEmpty(payback.Forecast) && IsStartAndEndValid(startAssumption.ForecastValue, endAssumption.ForecastValue, out startForecast, out endForecast))
            {
                var breakEventYear = startForecast.AddYears((int)Math.Floor(decimal.Parse(payback.Forecast)));
                var month = Math.Round((decimal.Parse(payback.Forecast) - Math.Floor(decimal.Parse(payback.Forecast))) * 12, 0);
                breakEventYear = breakEventYear.AddMonths((int)month);
                result.Forecast = breakEventYear.ToString();
            }

            DateTime startActual;
            DateTime endActual;
            if (!string.IsNullOrEmpty(payback.Actual) && IsStartAndEndValid(startAssumption.ActualValue, endAssumption.ActualValue, out startActual, out endActual))
            {
                var breakEventYear = startActual.AddYears((int)Math.Floor(decimal.Parse(payback.Actual)));
                var month = Math.Round((decimal.Parse(payback.Actual) - Math.Floor(decimal.Parse(payback.Actual))) * 12, 0);
                breakEventYear = breakEventYear.AddMonths((int)month);
                result.Actual = breakEventYear.ToString();
            }
            return result;
        }