public PlanForTrackingDto Get(FilterPlanTracking filterPlanTracking)
        {
            PlanForTrackingDto planForTracking = new PlanForTrackingDto();

            planForTracking.Plan = _planRepository.GetById(filterPlanTracking.IdPlan);

            List <VPlanGlobal> vPlanGlobal = _vPlanGlobalService.Get(filterPlanTracking);

            if (vPlanGlobal.Count > 0)
            {
                //recherche de tous les postes de la base et affectation au planTrackingDto
                planForTracking.Postes = GetPostesForTracking(vPlanGlobal, filterPlanTracking.IdPlan, filterPlanTracking.MonthYear.Month.Id == (int)EnumMonth.BalanceSheetYear);

                //somme pour le plan
                PlanTrackingAmountStoreDto planTrackingAmountStore = vPlanGlobal
                                                                     .GroupBy(x => new { x.PreviewAmount, x.IdPlan })
                                                                     .Select(g => new PlanTrackingAmountStoreDto {
                    Id = g.Key.IdPlan.Value, Label = planForTracking.Plan.Label, AmountPreview = g.Key.PreviewAmount.Value, AmountReal = g.Sum(a => a.AmountOperation.Value)
                })
                                                                     .GroupBy(x => new { x.Id })
                                                                     .Select(g => new PlanTrackingAmountStoreDto {
                    Id = g.Key.Id, Label = planForTracking.Plan.Label, AmountPreview = g.Sum(ap => ap.AmountPreview), AmountReal = g.Sum(a => a.AmountReal)
                })
                                                                     .First();

                planForTracking.AmountPreview = Math.Round(planTrackingAmountStore.AmountPreview, 2);
                planForTracking.AmountReal    = Math.Round(planTrackingAmountStore.AmountReal, 2);
                planForTracking.GaugeValue    = CalculatePercentage(planTrackingAmountStore.AmountReal, planTrackingAmountStore.AmountPreview);
            }


            return(planForTracking);
        }
        private List <PosteForTrackingDto> GetPostesForTracking(List <VPlanGlobal> vPlanGlobal, int idPlan, bool isBalanceSheet)
        {
            List <Poste> postes = _posteRepository.GetAll();
            List <PosteForTrackingDto> postesForTracking = new List <PosteForTrackingDto>();

            foreach (var poste in postes)
            {
                PosteForTrackingDto posteForTracking = new PosteForTrackingDto();
                posteForTracking.Poste = _mapper.Map <SelectDto>(poste);

                var vByPoste = vPlanGlobal.Where(x => x.IdPoste == poste.Id).ToList();
                //recherche des planPoste et affectation au planTrackingDto
                posteForTracking.PlanPostes = GetPlanPostesForTracking(vByPoste, idPlan, poste.Id);

                if (vByPoste.Count > 0)
                {
                    PlanTrackingAmountStoreDto planTrackingAmountStore = GetplanTrackingAmountStore(vByPoste, isBalanceSheet);

                    posteForTracking.AmountPreview = Math.Round(planTrackingAmountStore.AmountPreview, 2);
                    posteForTracking.AmountReal    = Math.Round(planTrackingAmountStore.AmountReal, 2);
                    posteForTracking.GaugeValue    = CalculatePercentage(planTrackingAmountStore.AmountReal, planTrackingAmountStore.AmountPreview);
                }
                postesForTracking.Add(posteForTracking);
            }
            return(postesForTracking);
        }
        private PlanTrackingAmountStoreDto GetplanTrackingAmountStore(List <VPlanGlobal> vByPoste, bool isBalanceSheet)
        {
            PlanTrackingAmountStoreDto planTrackingAmountStore = new PlanTrackingAmountStoreDto();

            if (!isBalanceSheet)
            {
                //sommme pour le poste
                planTrackingAmountStore = vByPoste
                                          .GroupBy(x => new { x.PreviewAmount, x.IdPoste })
                                          .Select(g => new PlanTrackingAmountStoreDto {
                    Id = g.Key.IdPoste.Value, Label = g.Key.IdPoste.Value.ToString(), AmountPreview = g.Key.PreviewAmount.Value, AmountReal = g.Sum(a => a.AmountOperation.Value)
                })
                                          .GroupBy(x => new { x.Id })
                                          .Select(g => new PlanTrackingAmountStoreDto {
                    Id = g.Key.Id, Label = g.Key.Id.ToString(), AmountPreview = g.Sum(ap => ap.AmountPreview), AmountReal = g.Sum(a => a.AmountReal)
                })
                                          .First();
            }
            else
            {
                //sommme pour le poste, preview annuel
                PlanTrackingAmountStoreDto planTrackingAmountStoreYear = new PlanTrackingAmountStoreDto();
                if (vByPoste.Where(x => x.Month == (int)EnumMonth.BalanceSheetYear).Any())
                {
                    planTrackingAmountStoreYear = vByPoste
                                                  .Where(x => x.Month == (int)EnumMonth.BalanceSheetYear)
                                                  .GroupBy(x => new { x.PreviewAmount, x.IdPoste })
                                                  .Select(g => new PlanTrackingAmountStoreDto {
                        Id = g.Key.IdPoste.Value, Label = g.Key.IdPoste.Value.ToString(), AmountPreview = g.Key.PreviewAmount.Value, AmountReal = g.Sum(a => a.AmountOperation.Value)
                    })
                                                  .GroupBy(x => new { x.Id })
                                                  .Select(g => new PlanTrackingAmountStoreDto {
                        Id = g.Key.Id, Label = g.Key.Id.ToString(), AmountPreview = g.Sum(ap => ap.AmountPreview), AmountReal = g.Sum(a => a.AmountReal)
                    })
                                                  .First();
                }
                //sommme pour le poste, preview mensuel
                PlanTrackingAmountStoreDto planTrackingAmountStoreMonth = vByPoste
                                                                          .Where(x => x.Month != (int)EnumMonth.BalanceSheetYear)
                                                                          .GroupBy(x => new { x.IdPoste })
                                                                          .Select(g => new PlanTrackingAmountStoreDto {
                    Id = g.Key.IdPoste.Value, Label = g.Key.IdPoste.Value.ToString(), AmountPreview = g.Sum(a => a.PreviewAmount.Value), AmountReal = g.Sum(a => a.AmountOperation.Value)
                })
                                                                          .GroupBy(x => new { x.Id })
                                                                          .Select(g => new PlanTrackingAmountStoreDto {
                    Id = g.Key.Id, Label = g.Key.Id.ToString(), AmountPreview = g.Sum(ap => ap.AmountPreview), AmountReal = g.Sum(a => a.AmountReal)
                })
                                                                          .First();

                List <PlanTrackingAmountStoreDto> planTrackingAmountStores = new List <PlanTrackingAmountStoreDto>();
                planTrackingAmountStores.Add(planTrackingAmountStoreMonth);
                if (planTrackingAmountStoreYear.Id != 0)
                {
                    planTrackingAmountStores.Add(planTrackingAmountStoreYear);
                }

                planTrackingAmountStore = planTrackingAmountStores
                                          .GroupBy(x => new { x.Id })
                                          .Select(g => new PlanTrackingAmountStoreDto {
                    Id = g.Key.Id, Label = g.Key.Id.ToString(), AmountPreview = g.Sum(ap => ap.AmountPreview), AmountReal = g.Sum(a => a.AmountReal)
                })
                                          .FirstOrDefault();
            }


            return(planTrackingAmountStore);
        }