예제 #1
0
 protected void btnLoad_Click(object sender, EventArgs e)
 {
     using (var dataAccess = new DemandGapDataAccess())
     {
         var data = dataAccess.GetFinanceEntities();
         SessionStoredRanking = data;
         agLimits.GridData    = data.ToList();
     }
     ucFaoParameter.Visible = true;
     btnFilter.Visible      = true;
 }
        public void CalcualteLimits()
        {
            try
            {
                var parameters = ucParameters.GetParameters();
                List <WeeklyMaxMinValues> demandGapData;
                var      sb = new StringBuilder();
                var      stepToCalculateTo = int.Parse(ddlStage.SelectedValue);
                DateTime currentDay;

                using (var dataAccess = new DemandGapDataAccess(parameters))
                {
                    currentDay = dataAccess.GetLastHistoryTimestamp();
                    var weeksToCalculate = int.Parse(tbWeeks.Text);

                    //Constant 12 weeks for history calculations
                    parameters[DictionaryParameter.StartDate]         = (currentDay.AddDays(-84)).ToShortDateString();
                    parameters[DictionaryParameter.EndDate]           = (currentDay).ToShortDateString();
                    parameters[DictionaryParameter.ForecastStartDate] = (currentDay).ToShortDateString();

                    var endDate = currentDay.AddDays(weeksToCalculate * 7).Next(DayOfWeek.Sunday);
                    parameters[DictionaryParameter.ForecastEndDate] = (endDate).ToShortDateString();

                    var minComSegScenarioId      = int.Parse(ddlMinCommSeg.SelectedValue);
                    var maxFleetFactorScenarioId = int.Parse(ddlMaxFleetFactor.SelectedValue);

                    demandGapData = dataAccess.CalculateMinMax(minComSegScenarioId, maxFleetFactorScenarioId, 0);


                    ucDgcProgress.CurrentProgress = DemandGapCalculationStep.CalculateMinAndMax;
                    var sw = new Stopwatch();
                    sw.Start();
                    DemandGapOneCalculations.CalculateGap(demandGapData);

                    demandGapData = demandGapData.ToList();
                    sw.Stop();
                    var xx = sw.Elapsed;

                    sw.Reset();
                    sw.Start();
                    var rankingData = dataAccess.GetFinanceEntities();
                    sw.Stop();
                    var yy = sw.Elapsed;
                    sw.Reset();
                    sw.Start();
                    DemandGapOneCalculations.AttachRankingsToDemandGaps(demandGapData, rankingData);

                    sw.Stop();
                    var zz = sw.Elapsed;
                    sw.Reset();
                    sw.Start();

                    demandGapData = demandGapData.OrderBy(d => d.RankFromRevenue).ToList();
                    sw.Stop();
                    var qq = sw.Elapsed;
                    ucDemanGapStageOneGrid.GridData = demandGapData;
                }

                List <MonthlyLimitRow> monthlyData;
                List <WeeklyLimitRow>  weeklyData;

                int?carSegmentId = null;
                if (parameters.ContainsValueAndIsntEmpty(DictionaryParameter.CarSegment))
                {
                    carSegmentId = int.Parse(parameters[DictionaryParameter.CarSegment]);
                }

                using (var dataAccess = new MonthlyAddLimitDataAccess())
                {
                    monthlyData = dataAccess.GetMonthlyLimitRows(carSegmentId, currentDay);
                    weeklyData  = dataAccess.GetWeekLyLimitRows(carSegmentId, currentDay);
                }

                using (var dataAccess = new VehicleDistributionDataAccess())
                {
                    if (parameters[DictionaryParameter.CarGroup] != string.Empty &&
                        (!parameters[DictionaryParameter.CarClass].Contains(LocationQueryable.Separator)))
                    {
                        var carClassId = int.Parse(parameters[DictionaryParameter.CarClass]);
                        using (var lookup = new LookupDataAccess())
                        {
                            var splitCarGroupIds = lookup.FindCarGroupsInCarClass(carClassId);
                            monthlyData = monthlyData.Where(d => splitCarGroupIds.Contains(d.GetCarGroupId())).ToList();
                        }
                    }

                    ucDgcProgress.CurrentProgress = DemandGapCalculationStep.StepOneCarGroup;
                    var calculatedAdds = DemandGapOneCalculations.FillGapOneCarGroup(demandGapData, monthlyData,
                                                                                     weeklyData);

                    sb.AppendLine("Additions Step 1 Generated:" + calculatedAdds.Sum(d => d.Amount));

                    if (stepToCalculateTo > 1)
                    {
                        ucDgcProgress.CurrentProgress = DemandGapCalculationStep.StepOneCarClass;
                        DemandGapOneCalculations.FillGapOneCarClass(demandGapData, monthlyData, weeklyData,
                                                                    calculatedAdds);
                        sb.AppendLine("Additions Step 1.1 Generated:" + calculatedAdds.Sum(d => d.Amount));
                    }

                    if (stepToCalculateTo > 2)
                    {
                        ucDgcProgress.CurrentProgress = DemandGapCalculationStep.StepTwoCarGroup;
                        DemandGapOneCalculations.FillGapTwoCarGroup(demandGapData, monthlyData, weeklyData,
                                                                    calculatedAdds);
                        sb.AppendLine("Additions Step 2 Generated:" + calculatedAdds.Sum(d => d.Amount));
                    }

                    if (stepToCalculateTo > 3)
                    {
                        ucDgcProgress.CurrentProgress = DemandGapCalculationStep.StepTwoCarClass;
                        DemandGapOneCalculations.FillGapTwoCarClass(demandGapData, monthlyData, weeklyData,
                                                                    calculatedAdds);
                        sb.AppendLine("Additions Step 2.1 Generated:" + calculatedAdds.Sum(d => d.Amount));
                    }


                    dataAccess.FillNames(calculatedAdds);


                    var distinctContributionData = from dgd in demandGapData
                                                   group dgd by new { CarGroupId = dgd.GetCarGroupId(), LocationId = dgd.GetLocationId() }
                    into groupedData
                        select new
                    {
                        groupedData.Key.CarGroupId,
                        groupedData.Key.LocationId,
                        groupedData.First().Contribution
                    };
                    var additionEntities = from ca in calculatedAdds
                                           join dgd in distinctContributionData on new { ca.CarGroupId, ca.LocationId }
                    equals new { dgd.CarGroupId, dgd.LocationId }
                    into jdgd
                    from joinedDcd in jdgd.DefaultIfEmpty()
                    select new AdditionEntity
                    {
                        Year         = ca.Year,
                        IsoWeek      = ca.IsoWeek,
                        CarGroupId   = ca.CarGroupId,
                        LocationId   = ca.LocationId,
                        CarGroup     = ca.CarGroup,
                        Location     = ca.Location,
                        Amount       = ca.Amount,
                        Contribution = joinedDcd == null ? 0 : (double)joinedDcd.Contribution
                    };

                    ucResult.GridData = additionEntities.ToList();
                }
                ucMonthlyLimit.LoadData(monthlyData);
                ucWeeklyLimit.LoadData(weeklyData);

                sb.AppendLine("Total Monthly assigned: " + monthlyData.Sum(d => d.Assigned));
                sb.AppendLine("Total Weekly assigned: " + weeklyData.Sum(d => d.Assigned));
                SummaryText = sb.ToString();

                ucDgcProgress.CurrentProgress = DemandGapCalculationStep.Complete;
            }
            catch (Exception e)
            {
                SummaryText = e.ToString();
                ucDgcProgress.CurrentProgress = DemandGapCalculationStep.Error;
            }
        }