internal void UpdateBy(SupplierPriceItem newItem) { if (newItem != null) { this.ProducerName = newItem.ProducerName; this.Status = newItem.Status; this.ProducerCodeTrimmed = newItem.ProducerCodeTrimmed; this.Name = newItem.Name; this.Count = newItem.Count; this.Price = newItem.Price; this.PriceEu = newItem.PriceEu; this.PriceUsd = newItem.PriceUsd; this.IsDeleted = newItem.IsAvailable; this.SetUploadedAt(); } }
public async Task <IActionResult> DownloadItemsViessmann() { var files = new List <FileModel> { new FileModel("Viessmann", "viessmann/Viessmann.xlsx"), }; try { string sWebRootFolder = _hostingEnvironment.WebRootPath + "./../files/offers_of_suppliers/"; foreach (var fileModel in files) { var prices = new Collection <SupplierPriceItem> (); var fileName = fileModel.FileName; FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, fileName)); var supplierModel = new PartsSupplier() { Name = fileModel.ProducerName, SearchName = fileModel.ProducerName, Logo = new ModelFile { Name = "logo.png" } }; var supplier = await _partSupplierRepository.CreateSupplierIfNotExist(supplierModel); using (ExcelPackage package = new ExcelPackage(file)) { if (package.Workbook != null && package.Workbook.Worksheets != null) { foreach (var worksheet in package.Workbook.Worksheets) { if (worksheet != null) { int rowCount = worksheet.Dimension != null ? worksheet.Dimension.Rows : 0; int ColCount = worksheet.Dimension != null ? worksheet.Dimension.Columns : 0; // return Ok ($"{package.Workbook.Worksheets.Count} - {rowCount} - {ColCount}"); for (int row = 2; row <= rowCount; row++) { //if (!string.IsNullOrEmpty (worksheet.Cells[row, 1].Value.ToString ())) { var price = new SupplierPriceItem(); price.PartsSupplier = supplier; for (int col = 1; col <= ColCount; col++) { var value = worksheet.Cells[row, col]?.Value?.ToString().Trim();; switch (col) { case 1: { price.ProducerName = value; break; } case 2: { price.ProducerCode = value; break; } case 3: { price.Name = value; break; } case 4: { int count = 0; int.TryParse(value, result: out count); price.Count = count; break; } case 5: { decimal priceValie = 0; decimal.TryParse(value, result: out priceValie); price.Price = priceValie; break; } case 6: { price.Description = value; break; } } } prices.Add(price); //} } } } } } var groups = prices.GroupBy(x => x.ProducerName); foreach (IGrouping <string, SupplierPriceItem> group in groups) { var producerName = group.Key; if (!string.IsNullOrEmpty(producerName)) { var producer = await this._partProducerRepository.CreateProducerIfNotExist(producerName); foreach (var priceItem in group) { priceItem.Producer = producer; } await this._supplierPriceItemRepository.AddRangeForSupplier(group, supplier); } } } } catch (Exception ex) { return(Ok(ex.Message + "\n" + ex.StackTrace)); } return(Ok(true)); }
public override async Task Process(IEnumerable <SupplierOfferFile> files, PartsSupplier supplier) { // OFFERS #1 var offerFile = files.First(x => x.Name == "offers0_1.xml"); var filePath = GetPathToFile(_hostingEnvironment, offerFile, supplier.SearchName); var xmlDoc = new XmlDocument(); xmlDoc.Load(filePath); var offersTag = "Предложение"; var offersNodes = xmlDoc.GetElementsByTagName(offersTag); var offers = new List <EnbsvOffers> (); foreach (XmlNode node in offersNodes) { var offer = new EnbsvOffers(); offer.Id = node["Ид"].InnerText; offer.ProducerCode = node["Артикул"].InnerText; foreach (XmlNode infoNode in node.ChildNodes) { string value = infoNode.InnerText; switch (infoNode.Name) { case "Цены": { var priceNodes = infoNode.ChildNodes; foreach (XmlNode priceNode in priceNodes) { var price = decimal.Parse(priceNode["ЦенаЗаЕдиницу"].InnerText); switch (priceNode["Валюта"].InnerText) { case "EUR": { offer.PriceEu = price; break; } case "USD": { offer.PriceUsd = price; break; } case "руб": { offer.PriceRu = price; break; } } } break; } } } var warehouseNodes = node.ChildNodes.Cast <XmlNode> (); foreach (var warehouseNode in warehouseNodes.Where(x => x.Name == "Склад")) { var resultCount = 0; var countAttr = warehouseNode.Attributes["КоличествоНаСкладе"]; int.TryParse(countAttr.Value, out resultCount); offer.Count += resultCount; } offers.Add(offer); } // var producersDictionary = GetProducersDictionary(_partProducerRepository); // OFFERS #2 filePath = GetPathToFile(_hostingEnvironment, files.First(x => x.Name == "import0_1.xml"), supplier.SearchName); xmlDoc = new XmlDocument(); xmlDoc.Load(filePath); var importTag = "Товар"; var importNodes = xmlDoc.GetElementsByTagName(importTag); var imports = new List <EnbsvImport> (); foreach (XmlNode node in importNodes) { var import = new EnbsvImport { }; import.Id = node["Ид"].InnerText; import.ProducerCode = node["Артикул"].InnerText; try { import.ProducerName = this.CheckProducerNameErrors(node["Изготовитель"]["Наименование"].InnerText); } catch (System.Exception e) { var v = e.Message; } var requisiteNodes = node["ЗначенияРеквизитов"].ChildNodes; foreach (XmlNode requisiteNode in requisiteNodes) { switch (requisiteNode["Наименование"].InnerText) { case "Полное наименование": { import.Name = requisiteNode["Значение"].InnerText; break; } } } if (!string.IsNullOrEmpty(import.ProducerName) && !producersDictionary.ContainsKey(import.ProducerName)) { var newProducer = await this._partProducerRepository.CreateProducerIfNotExist(import.ProducerName); producersDictionary.Add(newProducer.Name, newProducer); } imports.Add(import); } // Merge in new supplier items var offersDictionary = offers.ToDictionary(x => x.Id); var supplierItems = new List <SupplierPriceItem> (); foreach (var import in imports) { var supplierItem = new SupplierPriceItem(); if (offersDictionary.ContainsKey(import.Id)) { var offer = offersDictionary[import.Id]; supplierItem.Price = offer.PriceRu; supplierItem.PriceUsd = offer.PriceUsd; supplierItem.PriceEu = offer.PriceEu; supplierItem.Count = offer.Count; supplierItem.ProducerCode = offer.ProducerCode; } supplierItem.PartsSupplier = supplier; supplierItem.Name = import.Name; if (!string.IsNullOrEmpty(import.ProducerName)) { var producer = producersDictionary[import.ProducerName]; supplierItem.Producer = producer; supplierItem.ProducerName = producer.Name; } if (string.IsNullOrEmpty(supplierItem.ProducerCode)) { supplierItem.ProducerCode = import.ProducerCode; } supplierItems.Add(supplierItem); } await this._supplierPriceItemRepository.AddRangeForSupplier(supplierItems, supplier); }