protected void PreparePrice(ParentItemDTO parentItemDto) { var priceService = new PriceService(_dbFactory); IList <RateByCountryDTO> allRates = new List <RateByCountryDTO>(); using (var db = _dbFactory.GetRWDb()) { foreach (var item in parentItemDto.Variations) { var rateForMarketplace = RateHelper.GetRatesByStyleItemId(db, item.StyleItemId.Value); var newPrice = priceService.GetMarketPrice(item.CurrentPrice, null, item.IsPrime, item.IsFBA, item.Weight, (MarketType)item.Market, item.MarketplaceId, rateForMarketplace); _log.Info("Price changed, SKU=" + item.SKU + ": " + item.CurrentPrice + "=>" + newPrice); item.CurrentPrice = newPrice; } } }
public decimal GetMarketPrice(decimal sourcePrice, decimal?sourceSFPPrice, bool isPrime, bool isFBA, double?weight, MarketType market, string marketplaceId, long styleItemId) { IDictionary <string, decimal?> rateForMarketplace; using (var db = _dbFactory.GetRWDb()) { rateForMarketplace = RateHelper.GetRatesByStyleItemId(db, styleItemId); } return(GetMarketPrice(sourcePrice, sourceSFPPrice, isPrime, isFBA, weight, market, marketplaceId, rateForMarketplace)); }
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); } }
//public static void UpdateSalePrices(IUnitOfWork db, // long styleItemId, // long saleId, // decimal newSalePrice, // decimal? newSFPSalePrice) //{ // var rateForMarketplace = RateHelper.GetRatesByStyleItemId(db, styleItemId); // var marketPrices = db.StyleItemSaleToMarkets.GetAll().Where(s => s.SaleId == saleId).ToList(); // foreach (var marketPrice in marketPrices) // { // if (marketPrice.SalePrice.HasValue) // { // var marketListings = db.StyleItemSaleToListings.GetAll().Where(l => l.SaleToMarketId == ) // marketPrice.SalePrice = RateHelper.CalculateForMarket((MarketType) marketPrice.Market, // marketPrice.MarketplaceId, // newSalePrice, // rateForMarketplace[MarketplaceKeeper.AmazonComMarketplaceId], // rateForMarketplace[MarketplaceKeeper.AmazonCaMarketplaceId], // rateForMarketplace[MarketplaceKeeper.AmazonUkMarketplaceId], // rateForMarketplace[MarketplaceKeeper.AmazonAuMarketplaceId], // RateService.GetMarketShippingAmount(MarketType.Amazon, MarketplaceKeeper.AmazonComMarketplaceId), // RateService.GetMarketShippingAmount((MarketType)marketPrice.Market, marketPrice.MarketplaceId), // RateService.GetMarketExtraAmount((MarketType)marketPrice.Market, marketPrice.MarketplaceId)); // } // } // db.Commit(); //} public static IList <MarketPriceEditViewModel> GetForStyleItemId(IUnitOfWork db, IDbFactory dbFactory, long styleItemId, decimal?initSalePrice, decimal?initSFPSalePrice) { var priceService = new PriceService(dbFactory); var results = new List <MarketPriceEditViewModel>(); var allMarketplaces = UIMarketHelper.GetSalesMarketplaces(); var allListings = db.Listings.GetListingsAsListingDto() .Where(l => l.StyleItemId == styleItemId) .ToList() .Where(l => !l.IsFBA) .ToList(); var rateForMarketplace = RateHelper.GetRatesByStyleItemId(db, styleItemId); var sale = db.StyleItemSales.GetAllAsDto().FirstOrDefault(s => s.StyleItemId == styleItemId && !s.IsDeleted); var checkedListings = new List <StyleItemSaleToListingDTO>(); if (sale != null) { checkedListings = db.StyleItemSaleToListings.GetAllAsDto().Where(s => s.SaleId == sale.Id).ToList(); } var checkedMarkets = new List <StyleItemSaleToMarketDTO>(); if (sale != null) { checkedMarkets = db.StyleItemSaleToMarkets.GetAllAsDto().Where(s => s.SaleId == sale.Id).ToList(); } foreach (var market in allMarketplaces) { var checkedMarket = checkedMarkets.FirstOrDefault(l => l.Market == (int)market.Market && (l.MarketplaceId == market.MarketplaceId || String.IsNullOrEmpty(market.MarketplaceId))); var marketListings = allListings .Where(l => l.Market == (int)market.Market && (l.MarketplaceId == market.MarketplaceId || String.IsNullOrEmpty(market.MarketplaceId))) .Select(l => new ListingPriceEditViewModel(l)) .ToList(); foreach (var listing in marketListings) { listing.IsChecked = sale == null ? ((listing.IsPrime || SkuHelper.IsPrime(listing.SKU)) ? initSFPSalePrice.HasValue : initSalePrice.HasValue) : checkedListings.Any(l => l.ListingId == listing.ListingId); } var defaultPrice = //sale == null && initSalePrice.HasValue && marketListings.Any() ? priceService.GetMarketDefaultPrice(initSalePrice.Value, market.Market, market.MarketplaceId, rateForMarketplace) : (decimal?)null; foreach (var marketListing in marketListings) { var newDefaultPrice = priceService.ApplyMarketSpecified(defaultPrice, initSFPSalePrice, market.Market, market.MarketplaceId, marketListing.Weight, marketListing.IsPrime, marketListing.IsFBA); if (newDefaultPrice != defaultPrice) { marketListing.OverrideSalePrice = newDefaultPrice; } } var saleToMarket = new MarketPriceEditViewModel() { Id = checkedMarket != null ? checkedMarket.Id : 0, Market = (MarketType)market.Market, MarketplaceId = market.MarketplaceId, MarketName = MarketHelper.GetMarketName((int)market.Market, market.MarketplaceId), MarketCurrency = PriceHelper.GetCurrencySymbol(market.Market, market.MarketplaceId), SalePrice = checkedMarket != null ? checkedMarket.SalePrice : defaultPrice, SFPSalePrice = market.MarketplaceId == MarketplaceKeeper.AmazonComMarketplaceId ? initSFPSalePrice : null, SalePercent = checkedMarket != null ? checkedMarket.SalePercent : null, ApplyToNewListings = checkedMarket != null ? checkedMarket.ApplyToNewListings : false, Listings = marketListings }; results.Add(saleToMarket); } return(results); }