Example #1
0
        public List <TitleMapRule> ReadTitleMapRules()
        {
            List <TitleMapRule> rules = new List <TitleMapRule>();

            using (SpreadsheetDocument document = SpreadsheetDocument.Open(TITLEMAP_PATH, false))
            {
                SharedStringTablePart stringTablePart = document.WorkbookPart.GetPartsOfType <SharedStringTablePart>().FirstOrDefault();

                Sheet sheet = document.WorkbookPart.Workbook.Sheets.Descendants <Sheet>().First();

                if (sheet != null)
                {
                    WorksheetPart worksheetPart = document.WorkbookPart.GetPartById(sheet.Id) as WorksheetPart;

                    var headerRow = worksheetPart.Worksheet.Descendants <Row>().Single(p => p.RowIndex.Value == 1);

                    RowMapper mapper = new RowMapper(typeof(TitleMapRule), headerRow, stringTablePart.SharedStringTable);

                    foreach (Row row in worksheetPart.Worksheet.Descendants <Row>().Where(p => p.RowIndex.Value != 1))
                    {
                        TitleMapRule rule = mapper.MapRow(row) as TitleMapRule;

                        rules.Add(rule);
                    }
                }

                document.Close();
            }

            return(rules);
        }
        public void UpdateMainEntries(IEnumerable <MainEntry> entries)
        {
            var                  titleMaps     = _workbook.ReadTitleMapRules();
            TextInfo             textInfo      = Thread.CurrentThread.CurrentCulture.TextInfo;
            PictureSetRepository picRepository = new PictureSetRepository();

            using (berkeleyEntities dataContext = new berkeleyEntities())
            {
                dataContext.MaterializeAttributes = true;

                foreach (MainEntry entry in entries)
                {
                    try
                    {
                        Item item = dataContext.Items.Include("EbayListingItems").Include("AmznListingItems").SingleOrDefault(p => p.ItemLookupCode.Equals(entry.Sku));

                        if (item == null)
                        {
                            entry.Message = "sku not found";
                            continue;
                        }

                        entry.Brand     = item.SubDescription1;
                        entry.ClassName = item.ClassName;
                        entry.Qty       = item.QtyAvailable;
                        entry.Cost      = item.Cost;

                        entry.Department  = item.DepartmentName;
                        entry.Category    = item.CategoryName;
                        entry.Gender      = item.SubDescription3;
                        entry.Color       = item.SubDescription2;
                        entry.Notes       = item.Notes;
                        entry.Price       = item.Price;
                        entry.Location    = item.BinLocation;
                        entry.Cost        = item.Cost;
                        entry.Qty         = item.QtyAvailable;
                        entry.Description = item.Description;
                        entry.UPC         = item.GTIN;

                        var pics = picRepository.GetPictures(entry.Brand, new List <string>()
                        {
                            entry.Sku
                        });

                        entry.PictureCount = pics.Count;

                        var titleMap = titleMaps.SingleOrDefault(p => p.Department.Equals(item.DepartmentName) && p.Category.Equals(item.CategoryName));

                        if (titleMap == null)
                        {
                            titleMap     = new TitleMapRule();
                            titleMap.Map = "";
                        }

                        string description = item.Description;
                        string dims        = string.Empty;

                        foreach (var attribute in item.Dimensions)
                        {
                            description = description.Replace(" " + attribute.Value.Value + " ", "");
                            dims       += attribute.Value.Value + " ";
                        }

                        entry.TitleFormula = textInfo.ToTitleCase((entry.Brand + " " + titleMap.Map + " Size " + dims + description).ToLower());

                        var ebayHistory = string.Join(" ", item.EbayListingItems.Where(p => p.Listing.Status.Equals(EbayMarketplace.STATUS_ACTIVE)).Select(p => p.ToString()));
                        var amznHistory = string.Join(" ", item.AmznListingItems.Where(p => p.IsActive).Select(p => p.ToString()));

                        entry.Status = ebayHistory + " " + amznHistory;

                        if (item.EbayListingItems.Where(w => w.Listing.IsVariation.HasValue && !w.Listing.IsVariation.Value).Count() > 0)
                        {
                            EbayListingItem listingItem = item.EbayListingItems.Single(p => p.ID == item.EbayListingItems.Where(w => w.Listing.IsVariation.HasValue && !w.Listing.IsVariation.Value).Max(s => s.ID));
                            entry.Title           = listingItem.Listing.Title;
                            entry.FullDescription = listingItem.Listing.FullDescription;
                        }
                        else if (dataContext.bsi_quantities.Any(p => p.itemLookupCode.Equals(entry.Sku)))
                        {
                            var postDetails = dataContext.bsi_quantities.Where(p => p.itemLookupCode.Equals(entry.Sku));

                            int lastPostDetailID = postDetails.Max(p => p.id);

                            bsi_quantities postDetail = postDetails.Single(p => p.id == lastPostDetailID);

                            entry.Title           = postDetail.title;
                            entry.FullDescription = postDetail.bsi_posts.bsi_posting.fullDescription;
                        }
                    }
                    catch (Exception e)
                    {
                        entry.Message = e.Message;
                    }
                }
            }
        }