public static string GenerateBarcode(IBarcodeService barcodeService,
                                             string sku,
                                             DateTime when)
        {
            var barcodeInfo = barcodeService.AssociateBarcodes(sku, when, null);

            if (barcodeInfo != null)
            {
                return(barcodeInfo.Barcode);
            }
            return(String.Empty);
        }
        public static IList <CustomBarcodeViewModel> AssociateBarcodes(IBarcodeService barcodeService,
                                                                       string skuText,
                                                                       DateTime when,
                                                                       long?by)
        {
            var skuList = skuText.Split(" \r\n\t".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            var results = new List <CustomBarcodeViewModel>();

            foreach (var sku in skuList)
            {
                var formattedSku = sku.Trim();
                if (String.IsNullOrEmpty(formattedSku))
                {
                    continue;
                }

                var barcodeDto = barcodeService.AssociateBarcodes(sku, when, by);
                results.Add(new CustomBarcodeViewModel(barcodeDto));
            }

            return(results);
        }
Example #3
0
        public override void CreateListings()
        {
            _log.Info("CreateWalmartListings");

            var destMarket        = MarketType.FtpMarket;
            var destMarketplaceId = "";

            var sourcePriceMarket        = MarketType.Amazon;
            var sourcePriceMarketplaceId = MarketplaceKeeper.AmazonComMarketplaceId;

            using (var db = _dbFactory.GetRWDb())
            {
                var groupByStyle = from siCache in db.StyleItemCaches.GetAll()
                                   group siCache by siCache.StyleId
                                   into byStyle
                                   select new
                {
                    StyleId         = byStyle.Key,
                    OneHasStrongQty = byStyle.Any(s => s.RemainingQuantity > 20),                    //NOTE: >20
                    Qty             = byStyle.Sum(s => s.RemainingQuantity)
                };

                var query = from s in db.Styles.GetAll()
                            join sCache in db.StyleCaches.GetAll() on s.Id equals sCache.Id
                            join qty in groupByStyle on s.Id equals qty.StyleId
                            where qty.Qty > 30 &&
                            qty.OneHasStrongQty
                            orderby qty.Qty descending
                            where !s.Deleted
                            select new
                {
                    StyleId     = s.Id,
                    StyleString = s.StyleID,
                    Qty         = qty.Qty,
                    Name        = s.Name,
                    MainLicense = sCache.MainLicense,
                };

                var styleInfoList = query
                                    .Where(p => !p.Name.Contains("Saras") &&
                                           !p.Name.Contains("Sara's") &&
                                           !p.Name.Contains("Widgeon") &&
                                           p.MainLicense != "Widgeon" &&
                                           p.MainLicense != "Saras Prints" &&
                                           !p.Name.Contains("Santa") &&
                                           !p.Name.Contains("Claus") &&
                                           !p.Name.Contains("Christmas") &&
                                           !p.Name.Contains("Rudolph") &&
                                           !p.Name.Contains("Dr. Seuss") &&
                                           !p.Name.Contains("Grinch"))
                                    .ToList();

                var sourcePriceInfoes = (from i in db.Items.GetAll()
                                         join l in db.Listings.GetAll() on i.Id equals l.ItemId
                                         where i.StyleItemId.HasValue &&
                                         i.StyleId.HasValue &&
                                         i.Market == (int)sourcePriceMarket &&
                                         i.MarketplaceId == sourcePriceMarketplaceId
                                         group new { i, l } by i.StyleItemId.Value into byStyleItem
                                         select new
                {
                    StyleItemId = byStyleItem.Key,
                    StyleId = byStyleItem.Max(s => s.i.StyleId.Value),
                    Price = byStyleItem.Min(l => l.l.CurrentPrice)
                }).ToList();

                var existMarketItems = db.Items.GetAll().Where(i => i.Market == (int)MarketType.FtpMarket).ToList();

                var newStyleInfoes = styleInfoList.Where(s => existMarketItems.All(i => i.StyleId != s.StyleId)).ToList();

                newStyleInfoes = newStyleInfoes.Take(1000).ToList();

                IList <MessageString> messages;
                foreach (var styleInfo in newStyleInfoes)
                {
                    _log.Info("Creating styleId=" + styleInfo.StyleString + " (" + styleInfo.StyleId + ")");
                    if (existMarketItems.Any(i => i.StyleId == styleInfo.StyleId))
                    {
                        _log.Info("Skipped, style already exists");
                        continue;
                    }

                    if (!sourcePriceInfoes.Any(i => i.StyleId == styleInfo.StyleId))
                    {
                        _log.Info("Skipped, no source price info");
                        continue;
                    }

                    var model = CreateFromStyle(db,
                                                styleInfo.StyleId,
                                                destMarket,
                                                destMarketplaceId,
                                                out messages);

                    if (model == null || model.Variations == null)
                    {
                        _log.Info("Skipped, model/variations is NULL");
                        continue;
                    }

                    if (model.Variations.Count == 0)
                    {
                        _log.Info("Skipped, no variations");
                        continue;
                    }

                    model.Market        = (int)destMarket;
                    model.MarketplaceId = destMarketplaceId;

                    PrepareData(model);

                    foreach (var variant in model.Variations)
                    {
                        var price = sourcePriceInfoes.FirstOrDefault(p => p.StyleItemId == variant.StyleItemId)?.Price;
                        if (price == null || price == 0)
                        {
                            price = sourcePriceInfoes.Where(p => p.StyleId == variant.StyleId).Max(p => p.Price);
                        }
                        variant.CurrentPrice = (price ?? 0) + (variant.Weight <= 16 ? 5M : 7.5M);

                        var barcodeInfo = _barcodeService.AssociateBarcodes(variant.SKU, _time.GetAppNowTime(), null);
                        if (barcodeInfo != null)
                        {
                            variant.Barcode = barcodeInfo.Barcode;
                        }
                    }

                    model.Variations = model.Variations.Where(v => v.CurrentPrice > 0).ToList();

                    Save(model, null, db, _time.GetAppNowTime(), null);

                    //Add to exist list the new items
                    existMarketItems.AddRange(model.Variations.Select(i => new Item()
                    {
                        StyleId = i.StyleId
                    }));
                }
            }
        }
        public override void CreateListings()
        {
            _log.Info("CreateWalmartListings");
            using (var db = _dbFactory.GetRWDb())
            {
                var groupByStyle = from siCache in db.StyleItemCaches.GetAll()
                                   group siCache by siCache.StyleId
                                   into byStyle
                                   select new
                {
                    StyleId         = byStyle.Key,
                    OneHasStrongQty = byStyle.Any(s => s.RemainingQuantity > 20),                    //NOTE: >20
                    Qty             = byStyle.Sum(s => s.RemainingQuantity)
                };

                var fromDate = new DateTime(2017, 12, 12);
                var query    = from s in db.Styles.GetAll()
                               join sCache in db.StyleCaches.GetAll() on s.Id equals sCache.Id
                               join qty in groupByStyle on s.Id equals qty.StyleId
                               where qty.Qty > 30 &&
                               qty.OneHasStrongQty
                               //where s.CreateDate >= fromDate
                               orderby qty.Qty descending
                               where !s.Deleted
                               select new
                {
                    StyleId       = s.Id,
                    StyleString   = s.StyleID,
                    ParentASIN    = sCache.AssociatedASIN,
                    Market        = sCache.AssociatedMarket,
                    MarketplaceId = sCache.AssociatedMarketplaceId,
                    Qty           = qty.Qty,
                    Name          = s.Name,
                    MainLicense   = sCache.MainLicense,
                };



                var styleInfoList = query
                                    .Where(p => p.MarketplaceId == MarketplaceKeeper.AmazonComMarketplaceId &&
                                           !p.Name.Contains("Saras") &&
                                           !p.Name.Contains("Sara's") &&
                                           !p.Name.Contains("Widgeon") &&
                                           p.MainLicense != "Widgeon" &&
                                           p.MainLicense != "Saras Prints")
                                    .ToList();


                var existMarketItems = db.Items.GetAll().Where(i => i.Market == (int)MarketType.Walmart).ToList();

                var newStyleInfoes = styleInfoList.Where(s => existMarketItems.All(i => i.StyleId != s.StyleId)).ToList();

                IList <MessageString> messages;
                foreach (var styleInfo in newStyleInfoes)
                {
                    _log.Info("Creating styleId=" + styleInfo.StyleString + " (" + styleInfo.StyleId + ")" + ", ASIN=" +
                              styleInfo.ParentASIN);
                    if (existMarketItems.Any(i => i.StyleId == styleInfo.StyleId))
                    {
                        _log.Info("Skipped, style already exists");
                        continue;
                    }

                    var model = CreateFromParentASIN(db,
                                                     styleInfo.ParentASIN,
                                                     styleInfo.Market.Value,
                                                     styleInfo.MarketplaceId,
                                                     false,
                                                     false,
                                                     5,
                                                     out messages);

                    if (model == null)
                    {
                        var item = db.Items.GetAllViewAsDto(MarketType.Walmart, "")
                                   .FirstOrDefault(i => i.StyleId == styleInfo.StyleId);
                        if (item != null)
                        {
                            model = CreateFromStyle(db,
                                                    styleInfo.StyleString,
                                                    null,
                                                    MarketType.Walmart,
                                                    null,
                                                    out messages);

                            model.Variations.ToList().ForEach(v => v.CurrentPrice = item.CurrentPrice);
                            model.Variations.ToList().ForEach(v => v.Color        = item.Color);
                        }
                    }

                    if (model == null || model.Variations == null)
                    {
                        _log.Info("Skipped, variations is NULL");
                        continue;
                    }

                    if (model.Variations.Count == 0)
                    {
                        _log.Info("Skipped, no variations");
                        continue;
                    }

                    if (model.Variations.Count > 10)
                    {
                        _log.Info("Skipped, a lot of variations");
                        continue;
                    }

                    model.Market        = (int)MarketType.Walmart;
                    model.MarketplaceId = null;

                    PrepareData(model);

                    var existAnyBarcode = false;
                    foreach (var variant in model.Variations)
                    {
                        var foundItems = ((WalmartOpenApi)_openApi).SearchProductsByBarcode(variant.Barcode, WalmartUtils.ApparelCategoryId);
                        if (foundItems.IsSuccess && foundItems.Total > 0)
                        {
                            existAnyBarcode = true;
                        }
                    }

                    if (!existAnyBarcode)
                    {
                        foreach (var variant in model.Variations)
                        {
                            var barcodeInfo = _barcodeService.AssociateBarcodes(variant.SKU, _time.GetAppNowTime(), null);
                            if (barcodeInfo != null)
                            {
                                _log.Info("Set custom barcode for: " + variant.SKU + " :" + variant.Barcode + "=>" + barcodeInfo.Barcode);
                                variant.Barcode = barcodeInfo.Barcode;
                            }
                        }
                    }

                    Save(model, null, db, _time.GetAppNowTime(), null);

                    _emailService.SendSystemEmailToAdmin("Auto-created Walmart listing, ParentASIN: " + model.ASIN, "");

                    //Add to exist list the new items
                    existMarketItems.AddRange(model.Variations.Select(i => new Item()
                    {
                        StyleId = i.StyleId
                    }));
                }
            }
        }
Example #5
0
        public void SyncItems(DropShipperApi api)
        {
            var destMarket          = _destMarket;          // MarketType.Walmart;
            var destMarketplaceId   = _destMarketplaceId;   // "";
            var sourceMarketplaceId = _sourceMarketplaceId; // MarketplaceKeeper.DsPAWMCom;
            var parentItems         = api.GetItems(_log, _sourceMarket /* MarketType.DropShipper */, sourceMarketplaceId);

            using (var db = _dbFactory.GetRWDb())
            {
                foreach (var parentItem in parentItems)
                {
                    var existParentItem = db.ParentItems.GetAll().FirstOrDefault(pi => pi.ASIN == parentItem.ASIN &&
                                                                                 pi.Market == (int)destMarket &&
                                                                                 (String.IsNullOrEmpty(destMarketplaceId) || pi.MarketplaceId == destMarketplaceId));

                    if (existParentItem == null)
                    {
                        _log.Info("Creating parentItem, ASIN=" + parentItem.ASIN);

                        var newParentItem = new ParentItem()
                        {
                            ASIN          = parentItem.ASIN,
                            Market        = (int)destMarket,
                            MarketplaceId = destMarketplaceId,

                            SKU          = parentItem.SKU,
                            AmazonName   = parentItem.AmazonName,
                            BrandName    = parentItem.BrandName,
                            BulletPoint1 = parentItem.BulletPoint1,
                            BulletPoint2 = parentItem.BulletPoint2,
                            BulletPoint3 = parentItem.BulletPoint3,
                            BulletPoint4 = parentItem.BulletPoint4,
                            BulletPoint5 = parentItem.BulletPoint5,

                            CreateDate = _time.GetAppNowTime(),
                        };
                        db.ParentItems.Add(newParentItem);
                        db.Commit();

                        foreach (var v in parentItem.Variations)
                        {
                            var findStyle = db.Styles.GetAll().FirstOrDefault(st => st.StyleID == v.StyleString &&
                                                                              !st.Deleted);
                            StyleItem findStyleItem = null;
                            if (findStyle != null)
                            {
                                findStyleItem = db.StyleItems.GetAll().FirstOrDefault(si => si.StyleId == findStyle.Id &&
                                                                                      si.Size == v.StyleSize &&
                                                                                      (String.IsNullOrEmpty(v.StyleColor) ||
                                                                                       si.Color == v.StyleColor));
                            }

                            var barcode = v.Barcode;
                            if (string.IsNullOrEmpty(barcode) && !String.IsNullOrEmpty(v.SKU))
                            {
                                var result = _barcodeService.AssociateBarcodes(v.SKU, _time.GetAppNowTime(), null);
                                if (!String.IsNullOrEmpty(result.Barcode))
                                {
                                    barcode = result.Barcode;
                                }
                            }

                            var newItem = new Item()
                            {
                                ParentASIN    = newParentItem.ASIN,
                                Market        = (int)destMarket,
                                MarketplaceId = destMarketplaceId,

                                ASIN        = v.ASIN,
                                Size        = v.Size,
                                Color       = v.Color,
                                StyleString = v.StyleString,
                                StyleId     = findStyle?.Id,
                                StyleItemId = findStyleItem?.Id,

                                ItemPublishedStatus = (int)PublishedStatuses.New,

                                BrandName = v.BrandName,
                                Title     = parentItem.AmazonName, //NOTE: v.Name is empty and always equal parent item Name
                                Barcode   = barcode,

                                CreateDate = _time.GetAppNowTime()
                            };
                            db.Items.Add(newItem);
                            db.Commit();

                            var isPrime = sourceMarketplaceId == MarketplaceKeeper.DsPAWMCom &&
                                          destMarket == MarketType.Walmart ? true : false;
                            var newListing = new Listing()
                            {
                                ASIN          = v.ASIN,
                                Market        = (int)destMarket,
                                MarketplaceId = destMarketplaceId,

                                IsPrime      = isPrime,
                                SKU          = v.SKU,
                                ListingId    = v.ListingId,
                                ItemId       = newItem.Id,
                                RealQuantity = 0,
                                CurrentPrice = v.CurrentPrice,
                            };
                            db.Listings.Add(newListing);
                            db.Commit();
                        }
                    }
                    else
                    {
                        _log.Info("ParentItem.AmazonName changed: " + existParentItem.AmazonName + " => " + parentItem.AmazonName);
                        existParentItem.AmazonName = parentItem.AmazonName;
                        db.Commit();

                        foreach (var v in parentItem.Variations)
                        {
                            var item = db.Items.GetAll().FirstOrDefault(i => i.ParentASIN == existParentItem.ASIN &&
                                                                        i.Market == (int)destMarket &&
                                                                        (String.IsNullOrEmpty(destMarketplaceId) || i.MarketplaceId == destMarketplaceId) &&
                                                                        i.Size == v.Size &&
                                                                        (i.Color == v.Color ||
                                                                         String.IsNullOrEmpty(v.Color)));

                            if (item == null)
                            {
                                var itemsCandidats = db.Items.GetAll()
                                                     .Where(i => i.ParentASIN == existParentItem.ASIN &&
                                                            i.Market == (int)destMarket &&
                                                            (String.IsNullOrEmpty(destMarketplaceId) || i.MarketplaceId == destMarketplaceId) &&
                                                            i.Size == v.Size)
                                                     .ToList();
                                if (itemsCandidats.Count == 1)
                                {
                                    item = itemsCandidats[0];
                                }

                                if (item == null)
                                {
                                    continue;
                                    throw new Exception("Warning: missing item");
                                }
                            }
                            if (!v.StyleId.HasValue ||
                                !v.StyleItemId.HasValue)
                            {
                                continue;
                                throw new Exception("Warning: missing StyleId");
                            }

                            if (String.IsNullOrEmpty(item.Barcode))
                            {
                                if (!String.IsNullOrEmpty(v.Barcode))
                                {
                                    item.Barcode = v.Barcode;
                                }
                                else
                                {
                                    var listing = db.Listings.GetAll().FirstOrDefault(l => l.ItemId == item.Id);
                                    var result  = _barcodeService.AssociateBarcodes(listing?.SKU, _time.GetAppNowTime(), null);
                                    if (!String.IsNullOrEmpty(result.Barcode))
                                    {
                                        item.Barcode = result.Barcode;
                                    }
                                }
                            }

                            _log.Info("Item Name changed: " + item.Title + " => " + parentItem.AmazonName);
                            item.Title = parentItem.AmazonName;

                            db.Commit();
                        }

                        _log.Info("Already exists: " + parentItem.ASIN);
                    }
                }
            }
        }