public async Task <IEnumerable <string> > SendSaleFile(FileStatusParamDto fileStatusParamDto, IFormFile formFile, int network, int user) { var extensions = Path.GetFileName(formFile.FileName).Split('.').Last().ToUpper(); if (extensions == "XLS") { return new string[1] { "favor importar arquivo com extensão .xlsx." } } ; if (extensions != "XLSX") { return new string[1] { "favor importar arquivo com extensão .xlsx." } } ; if (await _saleFileRepository.ExistFile(fileStatusParamDto.CurrentMonth, fileStatusParamDto.CurrentYear, network)) { return new string[1] { "Existe um arquivo em fase de processamento ou ja foi processado" } } ; var fileName = $"{DateTime.Now.Year}" + $"{DateTime.Now.Month.ToString().PadLeft(2, '0')}" + $"{DateTime.Now.Day.ToString().PadLeft(2, '0')}" + $"{DateTime.Now.Hour.ToString().PadLeft(2, '0')}" + $"{DateTime.Now.Minute.ToString().PadLeft(2, '0')}" + $"{DateTime.Now.Second.ToString().PadLeft(2, '0')}.{extensions}"; var path = Path.Combine(_env.WebRootPath, $"Content/SpreadSheet/Sale/{Path.GetFileName(fileName)}"); if (File.Exists(path)) { File.Delete(path); } using (var stream = new FileStream(path, FileMode.Create)) { await formFile.CopyToAsync(stream); } var spreadSheetMessages = new List <string>(); var columnsToValidate = new string[11] { "REVENDA", "COD LOJA", "CNPJ", "CPF VENDEDOR", "NOME VENDEDOR", "CATEGORIA", "DESCRIÇÃO PRODUTO", "QUANTIDADE", "DATA VENDA", "NÚMERO PEDIDO", "CÓDIGO EAN" }; var fileInfo = new FileInfo(path); using (var excelPackage = new ExcelPackage(fileInfo)) { var tab = excelPackage.Workbook.Worksheets[1]; spreadSheetMessages = FileManipulator.ValidateSpreadsheet(tab, columnsToValidate); } if (spreadSheetMessages.Count == 0) { _saleFileRepository.Save(new SaleFile { CreatedAt = DateTime.Now, CurrentMonth = fileStatusParamDto.CurrentMonth, CurrentYear = fileStatusParamDto.CurrentYear, FileName = fileName, FileStatus = await _fileStatusRepository.GetById((int)FileStatusEnum.Pending), Network = await _networkRepository.GetById(network), User = await _userRepository.GetById(user) }); await _unitOfWork.CommitAsync(); spreadSheetMessages.Add("Carga recebida com sucesso. Aguarde a validação dos seus dados."); } return(spreadSheetMessages); }
public async Task <bool> DoProcessesSale() { if (DateTime.Now.Day == 10) { var saleFile = new SaleFile { CreatedAt = DateTime.Now, CurrentMonth = DateTime.Now.AddMonths(-1).Month, CurrentYear = DateTime.Now.AddMonths(-1).Year, FileName = "", FileStatus = await _fileStatusRepository.GetById((int)FileStatusEnum.InProgress), Network = await _networkRepository.GetById((int)NetworkEnum.Havan), User = (await _userRepository.CustomFind(x => x.Cpf.Equals("rhuscaya"))).First() }; var skuPendingClassification = await _saleFileSkuStatusRepository.GetAll(); var products = await _productRepository.GetAll(); _saleFileRepository.Save(saleFile); for (var i = 1; i <= DateTime.DaysInMonth(DateTime.Now.AddMonths(-1).Year, DateTime.Now.AddMonths(-1).Month); i++) { using (var client = new HttpClient()) { client.BaseAddress = new Uri("https://cliente.havan.com.br/ClubePontuacao/Api/"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "5cb8d9a916c33a25efdf0dd1"); var result = await client.PostAsync("Venda/Lotes", new StringContent(JsonConvert.SerializeObject(new { inicio = new DateTime(DateTime.Now.AddMonths(-1).Year, DateTime.Now.AddMonths(-1).Month, i) }), Encoding.UTF8, "application/json")); if (result.IsSuccessStatusCode && result.StatusCode == System.Net.HttpStatusCode.OK) { var json = await result.Content.ReadAsStringAsync(); var havanSaleIntegration = JsonConvert.DeserializeObject <HavanSaleIntegrationDto>(json); foreach (var item in havanSaleIntegration.Lotes.First().Itens) { var productId = 0; var skuValidated = skuPendingClassification.Where(x => x.Id == (int)FileSKUStatusEnum.PendingClassification).First(); if (products.Where(x => x.Ean.Equals(item.Produto.Ean)).Any()) { skuValidated = skuPendingClassification.Where(x => x.Id == (int)FileSKUStatusEnum.AutomaticValidate).First(); productId = products.Where(x => x.Ean.Equals(item.Produto.Ean)).First().Id; } _saleFileDataRepository.Save(new SaleFileData { SaleFile = saleFile, SaleFileSkuStatus = skuValidated, Amount = item.Venda.Quantidade, Cnpj = item.Venda.CnpjLoja, CpfSalesman = item.Vendedor.Cpf, CreatedAt = DateTime.Now, NameSalesman = item.Vendedor.Nome, Product = productId, ProductDescription = item.Produto.Descricao, RequestNumber = item.Venda.NumeroCupomNota.ToString(), SaleDate = item.Venda.Data, Resale = "Havan" }); } } else { _logger.Fatal($"Atualização de hierarquia havan - ocorreu uma falha ao tentar acessar api de colaboradores da havan - {result.RequestMessage}"); } } } return(await _unitOfWork.CommitAsync()); } return(false); }