public async Task <object> GetFileStatus(FileStatusParamDto fileStatusParamDto, int network) { var saleFile = (await _saleFileRepository.CustomFind( x => x.CurrentMonth == fileStatusParamDto.CurrentMonth && x.Network.Id == network && x.CurrentYear == fileStatusParamDto.CurrentYear, x => x.FileStatus)); if (!saleFile.Any()) { return(null); } var saleFileObj = saleFile.OrderBy(x => x.Id).Last(); if (saleFileObj.FileStatus.Id == (int)FileStatusEnum.InProgress) { var saleFileData = await _saleFileDataRepository.CustomFind(x => x.SaleFile.Id == saleFileObj.Id, x => x.SaleFileSkuStatus); return(new { saleFileData, saleFileObj.FileStatus }); } else if (saleFileObj.FileStatus.Id == (int)FileStatusEnum.EndedError) { var saleFileDataError = await _saleFileDataErrorRepository.CustomFind(x => x.SaleFile.Id == saleFileObj.Id); return(new { saleFileDataError, saleFileObj.FileStatus }); } else { return(new { saleFileObj.FileStatus }); } }
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>()); }