public async Task <OperationDataResult <Paged <StatByYearModel> > > GetProducersStatsByYear([FromBody] ProducersYearRequestModel requestModel)
 {
     return(await _producerService.GetProducersStatsByYear(requestModel));
 }
        public async Task <OperationDataResult <Paged <StatByYearModel> > > GetProducersStatsByYear(ProducersYearRequestModel pnRequestModel)
        {
            try
            {
                var trashInspectionsQuery = _dbContext.TrashInspections
                                            .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed)
                                            .Where(x => x.Date.Year == pnRequestModel.Year)
                                            .AsQueryable();

                var producersStatsByYearModel = new Paged <StatByYearModel>();

                var producerQuery = _dbContext.Producers.AsQueryable();

                //if (!pnRequestModel.NameFilter.IsNullOrEmpty() && pnRequestModel.NameFilter != "")
                //{
                //    producerQuery = producerQuery.Where(x =>
                //        x.Name.Contains(pnRequestModel.NameFilter) ||
                //        x.Description.Contains(pnRequestModel.NameFilter));
                //}
                var exludeSort = new List <string>
                {
                    "Weighings",
                    "ControlPercentage",
                    "AmountOfWeighingsControlled",
                    "ApprovedPercentage",
                    "NotApprovedPercentage",
                    "ConditionalApprovedPercentage"
                };

                QueryHelper.AddSortToQuery(producerQuery,
                                           pnRequestModel.Sort,
                                           pnRequestModel.IsSortDsc,
                                           exludeSort);

                producersStatsByYearModel.Total =
                    producerQuery.Count(x => x.WorkflowState != Constants.WorkflowStates.Removed);

                var producersStatByYear = await producerQuery
                                          .Select(x => new StatByYearModel
                {
                    Id                            = x.Id,
                    Name                          = x.Name,
                    Weighings                     = trashInspectionsQuery.Count(t => t.ProducerId == x.Id),
                    ControlPercentage             = 0,
                    AmountOfWeighingsControlled   = trashInspectionsQuery.Count(t => t.ProducerId == x.Id && t.Status == 100),
                    ApprovedPercentage            = trashInspectionsQuery.Count(t => t.ProducerId == x.Id && t.IsApproved && t.Status == 100),
                    NotApprovedPercentage         = trashInspectionsQuery.Count(t => t.ProducerId == x.Id && t.ApprovedValue == "3" && t.Status == 100),
                    ConditionalApprovedPercentage = trashInspectionsQuery.Count(t => t.ProducerId == x.Id && t.ApprovedValue == "2" && t.Status == 100)
                })
                                          .ToListAsync();

                foreach (var statByYearModel in producersStatByYear)
                {
                    if (statByYearModel.AmountOfWeighingsControlled > 0 && statByYearModel.Weighings > 0)
                    {
                        statByYearModel.ControlPercentage = Math.Round((statByYearModel.AmountOfWeighingsControlled / statByYearModel.Weighings) * 100, 1);
                    }
                    else
                    {
                        statByYearModel.ControlPercentage = 0;
                    }

                    if (statByYearModel.ApprovedPercentage > 0 && statByYearModel.AmountOfWeighingsControlled > 0)
                    {
                        statByYearModel.ApprovedPercentage =
                            Math.Round((statByYearModel.ApprovedPercentage / statByYearModel.AmountOfWeighingsControlled) * 100, 1);
                    }
                    else
                    {
                        statByYearModel.ApprovedPercentage = 0;
                    }

                    if (statByYearModel.ConditionalApprovedPercentage > 0 && statByYearModel.AmountOfWeighingsControlled > 0)
                    {
                        statByYearModel.ConditionalApprovedPercentage =
                            Math.Round((statByYearModel.ConditionalApprovedPercentage / statByYearModel.AmountOfWeighingsControlled) * 100, 1);
                    }
                    else
                    {
                        statByYearModel.ConditionalApprovedPercentage = 0;
                    }

                    if (statByYearModel.NotApprovedPercentage > 0 && statByYearModel.AmountOfWeighingsControlled > 0)
                    {
                        statByYearModel.NotApprovedPercentage =
                            Math.Round((statByYearModel.NotApprovedPercentage / statByYearModel.AmountOfWeighingsControlled) * 100, 1);
                    }
                    else
                    {
                        statByYearModel.NotApprovedPercentage = 0;
                    }
                }

                if (!string.IsNullOrEmpty(pnRequestModel.Sort) && exludeSort.Contains(pnRequestModel.Sort))
                {
                    producersStatByYear = pnRequestModel.IsSortDsc
                        ? producersStatByYear.OrderByDescending(x => x.GetPropValue(pnRequestModel.Sort)).ToList()
                        : producersStatByYear.OrderBy(x => x.GetPropValue(pnRequestModel.Sort)).ToList();
                }

                producersStatsByYearModel.Entities = producersStatByYear;

                return(new OperationDataResult <Paged <StatByYearModel> >(true, producersStatsByYearModel));
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                _coreHelper.LogException(e.Message);
                return(new OperationDataResult <Paged <StatByYearModel> >(false,
                                                                          _trashInspectionLocalizationService.GetString("ErrorObtainingProducers")));
            }
        }