Exemple #1
0
        public void ExportSetupByMatch()
        {
            using (var db = _dbFactory.GetRWDb())
            {
                var allAmazonItems = db.Items.GetAllViewActual()
                                     .Where(i => i.Market == (int)MarketType.Amazon &&
                                            i.MarketplaceId == MarketplaceKeeper.AmazonComMarketplaceId)
                                     .Select(i => new ItemDTO()
                {
                    SKU          = i.SKU,
                    Barcode      = i.Barcode,
                    CurrentPrice = i.CurrentPrice,
                    StyleId      = i.StyleId,
                })
                                     .ToList();

                var allWalmartItems = db.Items.GetAllViewActual()
                                      .Where(i => i.Market == (int)MarketType.Walmart)
                                      .Select(i => new ItemDTO()
                {
                    SKU     = i.SKU,
                    Barcode = i.Barcode
                })
                                      .ToList();

                var results = new List <SetupByMatchProduct>(allAmazonItems.Count);
                foreach (var amzItem in allAmazonItems)
                {
                    var genderValue = amzItem.StyleId.HasValue
                           ? db.StyleFeatureValues.GetFeatureValueByStyleIdByFeatureId(amzItem.StyleId.Value, StyleFeatureHelper.GENDER)?.Value
                           : null;

                    if (allWalmartItems.All(i => i.Barcode != amzItem.Barcode))
                    {
                        var sku   = amzItem.SKU;
                        var index = 0;
                        while (allWalmartItems.Any(i => i.SKU == sku))
                        {
                            sku = SkuHelper.SetSKUMiddleIndex(sku, index);
                            index++;
                        }
                        results.Add(new SetupByMatchProduct()
                        {
                            ProductIdType1 = "UPC",
                            ProductId1     = amzItem.Barcode,

                            ProductTaxCode = WalmartUtils.GetProductTaxCode(genderValue, null)?.ToString(),

                            SKU      = sku,
                            Currency = "USD",
                            Price    = amzItem.CurrentPrice,

                            Weight     = PriceHelper.RoundToTwoPrecision((decimal)((amzItem.Weight == null || amzItem.Weight == 0) ? 5 : amzItem.Weight.Value) / (decimal)16),
                            WeightUnit = "lb",

                            Category    = "Clothing",
                            SubCategory = "Clothing"
                        });
                    }
                }

                var b       = new ExportColumnBuilder <SetupByMatchProduct>();
                var columns = new List <ExcelColumnInfo>()
                {
                    b.Build(p => p.ProductIdType1, "Product Identifier-Product Id Type (#1) *", 15),
                    b.Build(p => p.ProductId1, "Product Identifier-Product Id (#1) *", 15),
                    b.Build(p => p.ProductTaxCode, "Product Tax Code *", 15),
                    b.Build(p => p.ProductIdType2, "Additional Product Attribute-Product Attribute Name (#1) (Optional)", 15),
                    b.Build(p => p.ProductId2, "Additional Product Attribute-Product Attribute Value (#1) (Optional)", 15),
                    b.Build(p => p.SKU, "Sku *", 15),
                    b.Build(p => p.ASIN, "ASIN (Optional)", 15),
                    b.Build(p => p.Currency, "Price-Currency *", 15),
                    b.Build(p => p.Price, "Price-Amount *", 15),
                    b.Build(p => p.Weight, "Shipping Weight-Value *", 15),
                    b.Build(p => p.WeightUnit, "Shipping Weight-Unit *", 15),
                    b.Build(p => p.Category, "Category *", 15),
                    b.Build(p => p.SubCategory, "Sub-category *", 15),
                };

                var outputFilepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "WMSetupByMatch-" + DateTime.Now.ToString("yyyyddMMHHmmss") + ".xls");
                using (var stream = ExcelHelper.Export(results,
                                                       columns,
                                                       null))
                {
                    stream.Seek(0, SeekOrigin.Begin);
                    using (var fileStream = File.Create(outputFilepath))
                    {
                        stream.CopyTo(fileStream);
                    }
                }
            }
        }
        public static IList <ItemVariationExportViewModel> CreateStyleVariations(IUnitOfWork db,
                                                                                 string styleString,
                                                                                 IList <ItemSizeMapping> existSizeMapping,
                                                                                 MarketType market,
                                                                                 string marketplaceId)
        {
            var results = new List <ItemVariationExportViewModel>();

            var style = db.Styles.GetActiveByStyleIdAsDto(styleString);

            if (style == null)
            {
                return(results);
            }

            var styleMainLicenseDto = db.StyleFeatureValues.GetFeatureValueByStyleIdByFeatureId(style.Id,
                                                                                                StyleFeatureHelper.MAIN_LICENSE);
            var styleSubLicenseDto = db.StyleFeatureValues.GetFeatureValueByStyleIdByFeatureId(style.Id,
                                                                                               StyleFeatureHelper.SUB_LICENSE1);
            var styleMainLicense = styleMainLicenseDto != null ? styleMainLicenseDto.Value : null;
            var styleSubLicense  = styleSubLicenseDto != null ? styleSubLicenseDto.Value : null;

            var styleItems = db.StyleItems
                             .GetByStyleIdWithBarcodesAsDto(style.Id)
                             .OrderBy(o => SizeHelper.GetSizeIndex(o.Size))
                             .ToList();

            var forceReplace = styleItems.Any(s => (s.Size ?? "").Contains("/"));

            foreach (var styleItem in styleItems)
            {
                var newItem = new ItemVariationExportViewModel();

                var index   = 0;
                var baseSKU = style.StyleID + "-" + SizeHelper.PrepareSizeForSKU(styleItem.Size, forceReplace);

                while (db.Listings.CheckForExistenceSKU(SkuHelper.SetSKUMiddleIndex(baseSKU, index),
                                                        (MarketType)market,
                                                        marketplaceId))
                {
                    index++;
                }

                newItem.IsSelected  = true;
                newItem.SKU         = SkuHelper.SetSKUMiddleIndex(baseSKU, index);
                newItem.StyleId     = styleItem.StyleId;
                newItem.StyleString = style.StyleID;
                newItem.StyleItemId = styleItem.StyleItemId;
                newItem.StyleColor  = styleItem.Color;
                newItem.StyleSize   = styleItem.Size;

                newItem.BrandName = ItemExportHelper.GetBrandName(styleMainLicense, styleSubLicense);
                newItem.Size      = SizeHelper.PrepareSizeForExport(db, newItem.StyleSize, existSizeMapping);

                if (styleItem.Barcodes != null)
                {
                    foreach (var barcode in styleItem.Barcodes)
                    {
                        if (!String.IsNullOrEmpty(barcode.Barcode) &&
                            !db.Items.CheckForExistenceBarcode(barcode.Barcode, market, marketplaceId))
                        {
                            newItem.Barcode = barcode.Barcode;
                            break;
                        }
                    }
                }
                if (String.IsNullOrEmpty(newItem.Barcode))
                {
                    newItem.AutoGeneratedBarcode = true;
                }

                results.Add(newItem);
            }

            return(results);
        }
Exemple #3
0
        public static void CopyToMarketplaces(IUnitOfWork db,
                                              ICacheService cache,
                                              IBarcodeService barcodeService,
                                              ISystemActionService actionService,
                                              IAutoCreateListingService autoCreateListingService,
                                              IItemHistoryService itemHistoryService,
                                              int id,
                                              DateTime when,
                                              long?by,
                                              IList <ItemMarketViewModel> toMarketplaces,
                                              out IList <MessageString> messages)
        {
            var parent = db.ParentItems.GetAsDTO(id);

            messages = new List <MessageString>();

            foreach (var toMarketplace in toMarketplaces)
            {
                var model = ItemEditViewModel.CreateFromParentASIN(db,
                                                                   autoCreateListingService,
                                                                   parent.ASIN,
                                                                   parent.Market,
                                                                   parent.MarketplaceId,
                                                                   false, //NOTE: false - ex.: exclude to copy FBP to Walmart
                                                                   out messages);

                model.Id            = null;
                model.Market        = toMarketplace.Market;
                model.MarketplaceId = toMarketplace.MarketplaceId;
                //model.OnHold = true;

                var parentBaseASIN = SkuHelper.RemoveSKULastIndex(model.ASIN);
                var parentIndex    = 0;
                while (db.ParentItems.GetAsDTO(parentBaseASIN + ((parentIndex == 0) ? "" : "-" + parentIndex), (MarketType)toMarketplace.Market, toMarketplace.MarketplaceId) != null)
                {
                    parentIndex++;
                }
                var parentSKU = parentBaseASIN + ((parentIndex == 0) ? "" : "-" + parentIndex);

                var forceReplace = model.VariationList.Any(s => (s.Size ?? "").Contains("/"));

                model.ASIN = parentSKU;

                foreach (var item in model.VariationList)
                {
                    item.Id = null;

                    if (model.Market == (int)MarketType.Walmart ||
                        model.Market == (int)MarketType.WalmartCA)
                    {
                        item.Barcode = null;
                        item.AutoGeneratedBarcode = true;
                    }

                    if (item.StyleItemId.HasValue)
                    {
                        var sourceUSDPrice = item.Price;
                        var fromCurrency   = PriceHelper.GetCurrencyAbbr((MarketType)parent.Market, parent.MarketplaceId);
                        if (fromCurrency != PriceHelper.USDSymbol)
                        {
                            sourceUSDPrice = PriceHelper.ConvertToUSD(item.Price, fromCurrency);
                        }

                        var rateForMarketplace = RateHelper.GetRatesByStyleItemId(db, item.StyleItemId.Value);
                        var newPrice           = RateHelper.CalculateForMarket((MarketType)toMarketplace.Market,
                                                                               toMarketplace.MarketplaceId,

                                                                               sourceUSDPrice,
                                                                               rateForMarketplace[MarketplaceKeeper.AmazonComMarketplaceId],
                                                                               rateForMarketplace[MarketplaceKeeper.AmazonCaMarketplaceId],
                                                                               rateForMarketplace[MarketplaceKeeper.AmazonUkMarketplaceId],
                                                                               rateForMarketplace[MarketplaceKeeper.AmazonAuMarketplaceId],
                                                                               RateService.GetMarketShippingAmount(MarketType.Amazon, MarketplaceKeeper.AmazonComMarketplaceId),
                                                                               RateService.GetMarketShippingAmount((MarketType)toMarketplace.Market, toMarketplace.MarketplaceId),
                                                                               RateService.GetMarketExtraAmount((MarketType)toMarketplace.Market, toMarketplace.MarketplaceId));

                        if (newPrice.HasValue)
                        {
                            item.Price = newPrice.Value;
                        }
                    }

                    if (db.Listings.CheckForExistenceSKU(item.SKU,
                                                         (MarketType)toMarketplace.Market,
                                                         toMarketplace.MarketplaceId))
                    {
                        var baseSKU = item.StyleString + "-" + SizeHelper.PrepareSizeForSKU(item.StyleSize, forceReplace);
                        var index   = parentIndex;

                        while (db.Listings.CheckForExistenceSKU(SkuHelper.SetSKUMiddleIndex(baseSKU, index),
                                                                (MarketType)toMarketplace.Market,
                                                                toMarketplace.MarketplaceId))
                        {
                            index++;
                        }

                        item.SKU = SkuHelper.SetSKUMiddleIndex(baseSKU, index);
                    }
                }

                model.Save(db,
                           cache,
                           barcodeService,
                           actionService,
                           itemHistoryService,
                           when,
                           by);
            }
        }