public override async Task ImportParameters(string SERVICE_SEARCH_NAME)
        {
            var energobyt = new PartsSupplier {
                Name       = "Энергобыт Сервис, ООО",
                SearchName = SERVICE_SEARCH_NAME,
                Site       = "enbsv.ru",
                Email      = "*****@*****.**",
                Contacts   = new List <SupplierContact> {
                    new SupplierContact {
                        Phone = "74953745351"
                    },
                },
                SalePoints = new List <SalePoint> {
                    new SalePoint {
                        Name    = "Главный офис",
                        Phone   = "74953745351",
                        Address = new Address {
                            Locality = "г.Химки",
                            Location = "ул. Бабакина, 5а, оф. 104"
                        },
                        TimeWorks = new List <TimeWork> {
                            new TimeWork {
                                Description = "Пн-Пт: с 9-00 до 21-00"
                            }
                        },
                        DeliveryMethod = "Cамовывоз, курьер, ночная доставка"
                    }
                },
                Logo = new ModelFile {
                    Name = "logo.png"
                }
            };

            energobyt = await _partSupplierRepository.CreateSupplierIfNotExist(energobyt);
        }
示例#2
0
 public IQueryable <object> AllFor(PartsSupplier partsSupplier) => this._dbContext.SupplierOfferFile
 .Where(x => !x.IsDeleted && x.PartsSupplier == partsSupplier)
 .OrderByDescending(x => x.GroupIdentifier)
 .GroupBy(x => x.GroupIdentifier).Select(i => new {
     groupIdentifier = i.Key,
     files           = i.Select(f => new {
         Id              = f.ID,
         CreatedAt       = f.CreatedAt,
         Name            = f.Name,
         Status          = f.Status,
         ErrorMessage    = f.ErrorMessage,
         GroupIdentifier = f.GroupIdentifier
     })
 });
        public async Task <PartsSupplier> CreateSupplierIfNotExist(PartsSupplier model)
        {
            var findByName = this._dbContext
                             .PartsSuppliers
                             .Include(x => x.SalePoints)
                             .ThenInclude(x => x.TimeWorks)
                             .FirstOrDefault(x => x.Name == model.Name);

            if (findByName == null)
            {
                findByName = model;

                await this._dbContext.AddAsync(findByName);
            }
            else
            {
                foreach (var sp in model.SalePoints)
                {
                    var oldSalePoint = findByName.SalePoints.FirstOrDefault(x => x.Name == sp.Name);

                    if (oldSalePoint == null)
                    {
                        findByName.SalePoints.Add(sp);
                    }
                    else
                    {
                        oldSalePoint.UpdateTimeWorks(sp);
                        this._dbContext.UpdateRange(oldSalePoint.TimeWorks);
                    }
                }

                this._dbContext.UpdateRange(findByName.SalePoints);
                this._dbContext.Update(findByName);
            }

            await this._dbContext.SaveChangesAsync();

            return(findByName);
        }
示例#4
0
        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));
        }
示例#5
0
 public IQueryable <SupplierOfferFile> NeedProcessingFor(PartsSupplier partsSupplier) => this._dbContext.SupplierOfferFile.Where(x => !x.IsDeleted && x.Status == OfferFileStatusEnum.Downloaded && x.PartsSupplier == partsSupplier);
        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);
        }
 public abstract Task Process(IEnumerable <SupplierOfferFile> files, PartsSupplier supplier);
        public async Task <bool> AddRangeForSupplier(IEnumerable <SupplierPriceItem> items, PartsSupplier supplier)
        {
            items = items.Where(x => !string.IsNullOrEmpty(x.ProducerCode));

            SetTrimmedForItems(items);

            items = items.Where(x => !string.IsNullOrEmpty(x.ProducerCodeTrimmed)).GroupBy(x => x.ProducerCodeTrimmed).Select(x => x.FirstOrDefault());

            var articlesTrimmed = items.Select(x => x.ProducerCodeTrimmed).ToList();

            // UPDATE
            var oldItemsForUpdate = await this.GetItemsForUpdate(supplier.ID, articlesTrimmed, items);

            if (oldItemsForUpdate.Any())
            {
                this._dbContext.SupplierPriceItems.UpdateRange(oldItemsForUpdate);
                await this._dbContext.SaveChangesAsync();
            }

            // REMOVE
            var oldItemsForDelete = await this.GetItemsForDelete(supplier.ID, articlesTrimmed);

            if (oldItemsForDelete.Any())
            {
                foreach (var item in oldItemsForDelete)
                {
                    item.IsDeleted = true;
                }
                if (oldItemsForDelete.Any(x => x.ID == 62341))
                {
                    var a = 1;
                }
                this._dbContext.SupplierPriceItems.UpdateRange(oldItemsForDelete);
                await this._dbContext.SaveChangesAsync();
            }

            // INSERT
            var insertItems = this.GetItemsForInsert(items, oldItemsForUpdate)
                              .Where(x => x.Producer != null && x.PartsSupplier != null);

            if (insertItems.Any())
            {
                foreach (var item in insertItems)
                {
                    item.SetUploadedAt();

                    item.PartsSupplierId = item.PartsSupplier.ID;
                    item.ProducerId      = item.Producer.ID;
                }

                await this._dbContext.SupplierPriceItems.AddRangeAsync(insertItems);

                await this._dbContext.SaveChangesAsync();
            }

            return(true);
        }