示例#1
0
        private void PreparePrices(ParentItemDTO parentItemDto)
        {
            IList <RateByCountryDTO> allRates = new List <RateByCountryDTO>();

            using (var db = _dbFactory.GetRWDb())
            {
                allRates = db.RateByCountries.GetAllAsDto().ToList();
                foreach (var item in parentItemDto.Variations)
                {
                    double?weight       = item.Weight;
                    var    shippingSize = item.StyleId.HasValue ? db.StyleCaches.GetAll().FirstOrDefault(sc => sc.Id == item.StyleId.Value)?.ShippingSizeValue : null;
                    item.CurrentPriceCurrency = PriceHelper.GetCurrencyAbbr((MarketType)item.Market, item.MarketplaceId);
                    var newPrice = CalculatePrice(weight,
                                                  shippingSize,
                                                  item.CurrentPrice,
                                                  allRates,
                                                  (MarketType)item.Market,
                                                  item.MarketplaceId);
                    _log.Info("Price changed, SKU=" + item.SKU + ": " + item.CurrentPrice + "=>" + newPrice);
                    if (newPrice.HasValue)
                    {
                        item.CurrentPrice = newPrice.Value;
                    }
                    else
                    {
                        _log.Error("Convert price issue: " + item.SKU);
                    }
                }
            }
        }
示例#2
0
        private decimal?CalculatePrice(double?weight,
                                       string shippingSize,
                                       decimal usListingPrice,
                                       IList <RateByCountryDTO> allRates,
                                       MarketType forMarket,
                                       string forMarketplaceId)
        {
            var usShipping = RateService.GetMarketShippingAmount(MarketType.Amazon,
                                                                 MarketplaceKeeper.AmazonComMarketplaceId); // 4.49M;
            var currency   = PriceHelper.GetCurrencyAbbr(forMarket, forMarketplaceId);
            var caShipping = PriceHelper.ConvertToUSD(RateService.GetMarketShippingAmount(forMarket, forMarketplaceId), currency);

            var item = new OrderItemRateInfo()
            {
                Quantity     = 1,
                ShippingSize = shippingSize
            };

            IList <RateByCountryDTO> rates = null;

            if (weight.HasValue && weight > 0)
            {
                rates = allRates.Where(r => r.Weight == Math.Floor(weight.Value)).ToList();
            }

            var localPackageType = PackageTypeCode.Flat;

            if (!String.IsNullOrEmpty(shippingSize))
            {
                localPackageType = ShippingServiceUtils.IsSupportFlatEnvelope(new List <OrderItemRateInfo>()
                {
                    item
                })
                        ? PackageTypeCode.Flat
                        : PackageTypeCode.Regular;
            }

            decimal?caRateActualCostRegular = null;
            decimal?usRateActualCost        = null;

            if (rates != null && rates.Any())
            {
                var usPackageType = localPackageType.ToString();

                caRateActualCostRegular = rates.FirstOrDefault(r => r.Country == "CA" && r.PackageType == "Regular")?.Cost;
                usRateActualCost        = rates.FirstOrDefault(r => r.Country == "US" && r.PackageType == usPackageType)?.Cost;
            }
            if (!caRateActualCostRegular.HasValue ||
                !usRateActualCost.HasValue)
            {
                return(null);
            }

            var usIncome = usListingPrice + usShipping - usRateActualCost.Value;
            var newPrice = usIncome - (caShipping - caRateActualCostRegular.Value);

            newPrice = PriceHelper.ConvertFromUSD(newPrice, currency);

            return(newPrice);
        }
示例#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);
            }
        }