Exemple #1
0
        public async Task <IActionResult> GetCustomersSold([FromQuery] FilterCurrencyCustomerDto filterDto)
        {
            if (ModelState.IsValid)
            {
                var piDetail = await _saleService.GetSoldPerCustomers(filterDto, User.GeFinancialPeriodId());

                return(JsonResponseStatus.Success(piDetail));
            }
            return(JsonResponseStatus.Error());
        }
        public async Task <FilterCurrencyCustomerDto> GetSoldPerCustomers(FilterCurrencyCustomerDto filterDto, long financialPeriodId)
        {
            var asQueryableEntity = _customerRepository
                                    .GetEntities()
                                    .Include(x => x.CurrencySale)
                                    .Where(c => c.CurrencySale.Any(x => x.CustomerId == c.Id))
                                    .AsQueryable();

            if (filterDto.SearchText != null || !(string.IsNullOrEmpty(filterDto.SearchText)))
            {
                asQueryableEntity = asQueryableEntity.Where(x => x.Title.Contains(filterDto.SearchText.Trim()) ||
                                                            x.Name.Contains(filterDto.SearchText.Trim()));
            }
            var count = (int)Math.Ceiling(asQueryableEntity.Count() / (double)filterDto.TakeEntity);
            var pager = Pager.Builder(count, filterDto.PageId, filterDto.TakeEntity);
            var list  = await asQueryableEntity.Paging(pager).ToListAsync();

            filterDto.Entities = new List <CurrencyCustomerDto>();
            foreach (var item in list)
            {
                var totalBuy = await _saleRepository.GetTotalCurrencyByCustomerId(item.Id, financialPeriodId);

                if (totalBuy > 0)
                {
                    filterDto.Entities.Add(new CurrencyCustomerDto()
                    {
                        Title      = item.Title.SanitizeText(),
                        Name       = item.Name.SanitizeText(),
                        Phone      = item.Phone.SanitizeText(),
                        Address    = item.Address.SanitizeText(),
                        Id         = item.Id,
                        SoldAmount = totalBuy
                    });
                }
            }
            return((FilterCurrencyCustomerDto)filterDto.SetEntitiesDto(filterDto.Entities).SetPaging(pager));
        }