public async Task <MyTeamSaleDto> GetTeamSales(int shop, int currentMonth, int currentYear) { var sales = await _saleRepository.CustomFind( x => x.Shop.Id == shop && x.CurrentMonth == currentMonth && x.CurrentYear == currentYear, x => x.Product, x => x.Product.CategoryProduct, x => x.User); var products = sales.Select(x => x.Product.Id).ToList(); if (products.Count() == 0 || sales.Count() == 0) { return(new MyTeamSaleDto()); } var superTop = await _focusProductRepository.CustomFind(x => x.CurrentMonth == currentMonth && x.CurrentYear == currentYear); var participant = await _participantProductRepository.CustomFind(x => x.CurrentMonth == currentMonth && x.CurrentYear == currentYear); var salesGroup = sales.GroupBy(x => new { x.User, x.Product.Id }) .Select( x => new MyTeamSaleReturnListDto { Name = x.Key.User.Name, IdProduct = x.Key.Id, SuperTop = 0, Participant = 0 }).ToList(); foreach (var item in salesGroup) { item.SuperTop = superTop.Where(p => p.Product.Id == item.IdProduct).Count(); item.Participant = participant.Where(p => p.Product.Id == item.IdProduct).Count(); } salesGroup = salesGroup.GroupBy(x => new { x.Name, x.SuperTop, x.Participant }) .Select( x => new MyTeamSaleReturnListDto { Name = x.Key.Name, SuperTop = x.Sum(p => p.SuperTop), Participant = x.Sum(p => p.Participant) }).ToList(); var superTopCount = superTop.Count(x => products.Contains(x.Product.Id) && x.CurrentMonth == currentMonth && x.CurrentYear == currentYear); var participantCount = participant.Count(x => products.Contains(x.Product.Id) && x.CurrentMonth == currentMonth && x.CurrentYear == currentYear); return(new MyTeamSaleDto { ListSale = salesGroup, SuperTop = (superTopCount * 100) / products.Count(), Participant = (participantCount * 100) / products.Count() }); }
public async Task <FocusProduct> GetFocusProductById(int id) => (await _focusProductRepository.CustomFind(x => x.Id == id, x => x.Network, x => x.GroupProduct, x => x.Product)).FirstOrDefault();
public async Task <(bool saved, IEnumerable <Sale> sales)> ProcessesSaleFile() { var saleFileDatas = await _saleFileDataRepository.CustomFind(x => x.SaleFile.FileStatus.Id == (int)FileStatusEnum.SuccessfullyCompleted && (x.SaleFileSkuStatus.Id == (int)FileSKUStatusEnum.AutomaticValidate || x.SaleFileSkuStatus.Id == (int)FileSKUStatusEnum.Validated), x => x.SaleFile, x => x.SaleFile.Network); if (saleFileDatas.Any()) { var saleFile = saleFileDatas.GroupBy(x => x.SaleFile).Select(x => x.Key).ToList(); var monthsFiltered = saleFile.GroupBy(g => g.CurrentMonth).Select(g => g.Key).ToList(); var networksFiltered = saleFile.GroupBy(g => g.Network.Id).Select(g => g.Key).ToList(); var productFiltered = saleFileDatas.GroupBy(g => g.Product).Select(g => g.Key).ToList(); var shopFiltered = saleFileDatas.GroupBy(g => g.Cnpj).Select(g => g.Key).ToList(); var userFiltered = saleFileDatas.GroupBy(g => g.CpfSalesman).Select(g => g.Key).ToList(); var sales = new List <Sale>(); var participantProduct = await _participantProductRepository.CustomFind(x => monthsFiltered.Contains(x.CurrentMonth) && x.CurrentYear == saleFile.First().CurrentYear&& productFiltered.Contains(x.Product.Id) && networksFiltered.Contains(x.Network.Id) && x.Activated); var focusProduct = await _focusProductRepository.CustomFind(x => monthsFiltered.Contains(x.CurrentMonth) && x.CurrentYear == saleFile.First().CurrentYear&& productFiltered.Contains(x.Product.Id) && networksFiltered.Contains(x.Network.Id) && x.Activated); var fileStatus = await _fileStatusRepository.GetById((int)FileStatusEnum.ProcessedSales); var products = await _productRepository.CustomFind(x => productFiltered.Contains(x.Id)); var shops = await _shopRepository.CustomFind(x => shopFiltered.Contains(x.Cnpj)); var users = await _userRepository.CustomFind(x => userFiltered.Contains(x.Cpf), x => x.UserStatus); foreach (var item in saleFileDatas) { var product = products.Where(x => x.Id == item.Product).FirstOrDefault(); var shop = shops.Where(x => x.Cnpj.Equals(item.Cnpj)).FirstOrDefault(); var participantProductFiltered = participantProduct.Where(x => item.SaleFile.CurrentMonth == x.CurrentMonth && item.SaleFile.CurrentYear == x.CurrentYear && item.Product == x.Product.Id && item.SaleFile.Network.Id == x.Network.Id).FirstOrDefault(); var focusProductFiltered = focusProduct.Where(x => item.SaleFile.CurrentMonth == x.CurrentMonth && item.SaleFile.CurrentYear == x.CurrentYear && item.Product == x.Product.Id && item.SaleFile.Network.Id == x.Network.Id).FirstOrDefault(); var unitvalue = (focusProductFiltered != null || participantProductFiltered != null ? (focusProductFiltered != null ? focusProductFiltered.Punctuation : participantProductFiltered.Punctuation) : 0); var user = users.Where(x => x.Cpf.Equals(item.CpfSalesman)).FirstOrDefault(); if (unitvalue > 0 && user != null && shop != null && product != null && user.UserStatus.Id != (int)UserStatusEnum.PreRegistration && user.UserStatus.Id != (int)UserStatusEnum.Inactive) { var sale = new Sale { Activated = false, Processed = false, Amout = item.Amount, CreatedAt = DateTime.Now, Network = item.SaleFile.Network, Product = product, SaleDate = item.SaleDate, CurrentMonth = item.SaleFile.CurrentMonth, CurrentYear = item.SaleFile.CurrentYear, Shop = shop, UnitValue = unitvalue, User = user, }; sale.TotalValue = sale.Amout * sale.UnitValue; sale.Punctuation = sale.TotalValue; sales.Add(sale); } } if (sales.Any()) { saleFile.ForEach(sale => sale.FileStatus = fileStatus); _saleRepository.SaveMany(sales); return(await _unitOfWork.CommitAsync(), sales); } return(false, sales); } return(false, new List <Sale>()); }