public ParentItemViewModel(IUnitOfWork db, ParentItemDTO item)
        {
            Id = item.Id;

            ASIN          = item.ASIN;
            Market        = (MarketType)item.Market;
            MarketplaceId = item.MarketplaceId;

            SourceMarketId = item.SourceMarketId;

            SKU              = item.SKU;
            OnHold           = item.OnHold;
            LockMarketUpdate = item.LockMarketUpdate;

            AmazonName       = item.AmazonName;
            ImageSource      = item.ImageSource;
            ManualImage      = item.ManualImage;
            FirstStyleString = GetFirstStyle(item.ChildItems, item.ASIN);
            Comment          = item.LastComment?.Message;

            ChildItems = item.ChildItems != null?
                         item.ChildItems.Select(i => new ItemShortInfoViewModel(item.Market, item.MarketplaceId, i)).ToList() : new List <ItemShortInfoViewModel>();

            Comments = db.ProductComments.GetCommentsForProductId(item.Id).OrderBy(c => c.UpdateDate)
                       .Select(c => new CommentViewModel
            {
                Id      = c.Id,
                Comment = c.Message
            }).ToList();

            IsAmazonUpdated = item.IsAmazonUpdated;
        }
        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;
                }
            }
        }
Esempio n. 3
0
        private void AddActions(IUnitOfWork db, ParentItemDTO parentItemDto)
        {
            foreach (var item in parentItemDto.Variations)
            {
                var actions = new List <SystemActionType>()
                {
                    SystemActionType.UpdateOnMarketProductRelationship,
                    SystemActionType.UpdateOnMarketProductImage,
                };

                foreach (var actionType in actions)
                {
                    var newAction = new SystemActionDTO()
                    {
                        ParentId  = null,
                        Status    = (int)SystemActionStatus.None,
                        Type      = (int)actionType,
                        Tag       = item.Id.ToString(),
                        InputData = null,

                        CreateDate = _time.GetUtcTime(),
                        CreatedBy  = null,
                    };
                    db.SystemActions.AddAction(newAction);
                }
            }
            db.Commit();
        }
Esempio n. 4
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);
                    }
                }
            }
        }
Esempio n. 5
0
        private void UpdateParentItemInfo(ParentItem dbParentItem,
                                          ParentItemDTO dtoParentItem,
                                          DateTime?when)
        {
            //if success requested, override all
            if (dtoParentItem != null && dtoParentItem.IsAmazonUpdated == true)
            {
                dbParentItem.ASIN = dtoParentItem.ASIN;
                if (String.IsNullOrEmpty(dbParentItem.SKU))
                {
                    dbParentItem.SKU = dtoParentItem.SKU;
                }

                dbParentItem.AmazonName = dtoParentItem.AmazonName;
                if (!String.IsNullOrEmpty(dtoParentItem.ImageSource))
                {
                    dbParentItem.ImageSource = dtoParentItem.ImageSource;
                }
                dbParentItem.Description = dtoParentItem.Description;

                dbParentItem.SourceMarketId = dtoParentItem.ASIN;

                if (dtoParentItem.Rank != null)
                {
                    dbParentItem.Rank           = dtoParentItem.Rank;
                    dbParentItem.RankUpdateDate = when;
                }

                //Additional fields,
                dbParentItem.BrandName        = dtoParentItem.BrandName;
                dbParentItem.Type             = dtoParentItem.Type;
                dbParentItem.ListPrice        = dtoParentItem.ListPrice;
                dbParentItem.Color            = dtoParentItem.Color;
                dbParentItem.Department       = dtoParentItem.Department;
                dbParentItem.AdditionalImages = dtoParentItem.AdditionalImages;

                if (!String.IsNullOrEmpty(dtoParentItem.Features))
                {
                    dbParentItem.Features = dtoParentItem.Features;
                }
                if (!String.IsNullOrEmpty(dtoParentItem.SearchKeywords))
                {
                    dbParentItem.SearchKeywords = dtoParentItem.SearchKeywords;
                }

                //System
                dbParentItem.IsAmazonUpdated      = true;
                dbParentItem.LastUpdateFromAmazon = dtoParentItem.LastUpdateFromAmazon; //Only if success update
            }
            else
            {
                //System
                dbParentItem.IsAmazonUpdated = false;
            }
            dbParentItem.UpdateDate = when;
        }
Esempio n. 6
0
        public CallResult <string> Save(ParentItemDTO parentItemDto,
                                        string newComment,
                                        IUnitOfWork db,
                                        DateTime when,
                                        long?by)
        {
            //Prepare StyleId
            foreach (var item in parentItemDto.Variations)
            {
                var style = db.Styles.GetAllAsDto().FirstOrDefault(s => !s.Deleted && s.StyleID == item.StyleString);
                item.StyleId = style != null ? style.Id : (long?)null;
            }

            //Recheck ParentItem Id, prevent to check duplicates
            if (parentItemDto.Id == 0)
            {
                var existParentItem = db.ParentItems
                                      .GetAllAsDto()
                                      .FirstOrDefault(pi => pi.ASIN == parentItemDto.ASIN &&
                                                      pi.Market == parentItemDto.Market &&
                                                      (pi.MarketplaceId == parentItemDto.MarketplaceId ||
                                                       String.IsNullOrEmpty(parentItemDto.MarketplaceId)));

                if (existParentItem != null)
                {
                    _log.Info("Parent ASIN already exists, ASIN=" + parentItemDto.ASIN);
                    return(CallResult <string> .Fail("ASIN already exists", null));
                }
            }

            if (parentItemDto.Id != 0)
            {
                UpdateProduct(parentItemDto, db, when, by);
            }
            else
            {
                CreateProduct(parentItemDto, db, when, by);
            }

            if (parentItemDto.Id > 0 && !String.IsNullOrEmpty(newComment))
            {
                db.ProductComments.Add(new ProductComment()
                {
                    ProductId  = (int)parentItemDto.Id,
                    Message    = newComment,
                    CreatedBy  = by,
                    CreateDate = when,
                });
                db.Commit();
            }
            return(CallResult <string> .Success(""));
        }
Esempio n. 7
0
        public void UpdateParent(string currentASIN,
                                 ParentItemDTO dto,
                                 MarketType market,
                                 string marketplaceId,
                                 DateTime?when)
        {
            var parentItem = unitOfWork.GetSet <ParentItem>().FirstOrDefault(i =>
                                                                             i.ASIN == currentASIN &&
                                                                             i.Market == (int)market &&
                                                                             i.MarketplaceId == marketplaceId);

            if (parentItem != null)
            {
                UpdateParentItemInfo(parentItem, dto, when);
                unitOfWork.Commit();
            }
        }
Esempio n. 8
0
        public void PrepareData(ParentItemDTO parentItemDto)
        {
            parentItemDto.ASIN       = StringHelper.TrimWhitespace(StringHelper.ToUpper(parentItemDto.ASIN));
            parentItemDto.AmazonName = StringHelper.TrimWhitespace(parentItemDto.AmazonName);

            foreach (var item in parentItemDto.Variations)
            {
                item.Barcode = StringHelper.TrimWhitespace(item.Barcode);
                item.SKU     = StringHelper.ToUpper(StringHelper.TrimWhitespace(item.SKU));

                item.Size  = StringHelper.TrimWhitespace(item.Size);
                item.Color = StringHelper.TrimWhitespace(item.Color);

                item.Market        = parentItemDto.Market;
                item.MarketplaceId = parentItemDto.MarketplaceId;

                if (String.IsNullOrEmpty(item.Size))
                {
                    item.Size = item.StyleSize;
                }
            }
        }
        public static IList <ExcelWFSProductUSViewModel> GetItemsFor(IUnitOfWork db,
                                                                     IMarketCategoryService categoryService,
                                                                     ExportToExcelMode exportMode,
                                                                     IList <FBAItemInfo> fbaItems,
                                                                     ParentItemDTO parent,
                                                                     IList <ItemExDTO> children,
                                                                     UseStyleImageModes useStyleImageMode,
                                                                     out string filename)
        {
            var models = new List <ExcelWFSProductUSViewModel>();

            filename = null;

            var defaultChild = children.OrderByDescending(ch => ch.IsExistOnAmazon).ThenBy(ch => ch.Id).FirstOrDefault();

            if (!children.Any() && exportMode != ExportToExcelMode.Normal)
            {
                return(models);
            }

            FeatureValueDTO defaultChildSubLicense = null;
            var             defaultChildItemStyle  = String.Empty;
            var             defaultChildGender     = String.Empty;
            var             defaultChildBrand      = String.Empty;
            StyleEntireDto  firstChildStyle        = null;
            string          mainBrandName          = null;
            decimal?        childListPrice         = null;
            string          childDepartment        = null;
            string          childSize            = null;
            string          childStyleString     = null;
            IList <string>  defaultChildFeatures = new List <string>();

            if (defaultChild != null && defaultChild.StyleId.HasValue)
            {
                defaultChildSubLicense = db.FeatureValues.GetValueByStyleAndFeatureId(defaultChild.StyleId.Value, StyleFeatureHelper.SUB_LICENSE1);
                defaultChildItemStyle  = ItemExportHelper.GetFeatureValue(db.FeatureValues.GetValueByStyleAndFeatureId(defaultChild.StyleId.Value, StyleFeatureHelper.ITEMSTYLE));
                defaultChildGender     = ItemExportHelper.GetFeatureValue(db.FeatureValues.GetValueByStyleAndFeatureId(defaultChild.StyleId.Value, StyleFeatureHelper.GENDER));
                defaultChildBrand      = ItemExportHelper.GetFeatureValue(db.FeatureValues.GetValueByStyleAndFeatureId(defaultChild.StyleId.Value, StyleFeatureHelper.BRAND));

                mainBrandName = StringHelper.GetFirstNotEmpty(parent.BrandName, defaultChild.BrandName, defaultChildBrand);

                defaultChildFeatures = !String.IsNullOrEmpty(defaultChild.Features)
                    ? defaultChild.Features.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList()
                    : new List <string>();
                childListPrice   = defaultChild.ListPrice;
                childDepartment  = defaultChild.Department;
                childSize        = defaultChild.Size;
                childStyleString = defaultChild.StyleString;

                if (defaultChild.StyleId.HasValue)
                {
                    firstChildStyle = db.Styles.GetByStyleIdAsDto(defaultChild.StyleId.Value);
                }
            }

            if (parent != null)
            {
                var variationType = ItemExportHelper.GetVariationType(children != null ? children.Select(ch => ch.Color).ToList() : null);

                //For simplicity, w can just use for parent SKU (unless it exist) style-%parentASIN%.
                string parentSku = parent.SKU;
                if (String.IsNullOrEmpty(parentSku))
                {
                    //NOTE: removed ASIN part (I guess it could be used for preventing listings mix)
                    parentSku = childStyleString;// + "-" + parent.ASIN;
                }
                var mainGender   = StringHelper.GetFirstNotEmpty(childDepartment, parent.Department, defaultChildGender);
                var sizeType     = ItemExportHelper.GetSizeType(childSize);
                var categoryInfo = categoryService.GetCategory((MarketType)parent.Market, parent.MarketplaceId, defaultChildItemStyle, mainGender, sizeType);

                //var type = ItemExportHelper.GetItemType(itemStyle);

                var mainItemType   = categoryInfo.Key1;// ItemExportHelper.ItemTypeConverter(childSize ?? "", type, itemStyle, gender);
                var mainDepartment = StringHelper.GetFirstNotEmpty(categoryInfo.Key2, ItemExportHelper.DepartmentConverter(mainGender, mainItemType, sizeType));
                var parentImage    = ItemExportHelper.ImageConverter(StringHelper.GetFirstNotEmpty(
                                                                         parent.LargeImage != null ? parent.LargeImage.Image : null,
                                                                         parent.ImageSource));
                string clothingType = categoryInfo.Key3;

                var searchTerms = (parent.SearchKeywords ?? "").Replace(";", ", ");
                searchTerms = ItemExportHelper.PrepareSearchTerms(searchTerms);

                var mainMsrp = firstChildStyle != null && firstChildStyle.MSRP.HasValue
                    ? firstChildStyle.MSRP.Value.ToString("G")
                    : (childListPrice.HasValue ? Math.Round(childListPrice.Value / 100).ToString("G") : "");

                var mainDescription = firstChildStyle?.Description ?? parent.Description;

                models.Add(new ExcelWFSProductUSViewModel
                {
                    SKU           = parentSku,
                    Title         = parent.AmazonName,
                    ASIN          = parent.ASIN,
                    ProductId     = "ASIN",
                    BrandName     = mainBrandName,
                    Description   = mainDescription,
                    Type          = mainItemType,
                    Update        = "Update",
                    StandardPrice = "",

                    SuggestedPrice = mainMsrp,
                    Currency       = childListPrice.HasValue ? "USD" : "",

                    Quantity = "",

                    KeyProductFeatures1 = defaultChildFeatures.Count > 0 ? defaultChildFeatures[0] : String.Empty,
                    KeyProductFeatures2 = defaultChildFeatures.Count > 1 ? defaultChildFeatures[1] : String.Empty,
                    KeyProductFeatures3 = defaultChildFeatures.Count > 2 ? defaultChildFeatures[2] : String.Empty,
                    KeyProductFeatures4 = defaultChildFeatures.Count > 3 ? defaultChildFeatures[3] : String.Empty,
                    KeyProductFeatures5 = defaultChildFeatures.Count > 4 ? defaultChildFeatures[4] : String.Empty,

                    SearchTerms1 = searchTerms,
                    //SearchTerms1 = searchTermList.Count > 0 ? searchTermList[0] : String.Empty,
                    //SearchTerms2 = searchTermList.Count > 1 ? searchTermList[1] : String.Empty,
                    //SearchTerms3 = searchTermList.Count > 2 ? searchTermList[2] : String.Empty,
                    //SearchTerms4 = searchTermList.Count > 3 ? searchTermList[3] : String.Empty,
                    //SearchTerms5 = searchTermList.Count > 4 ? searchTermList[4] : String.Empty,

                    MainImageURL   = parentImage,
                    Parentage      = ExcelHelper.ParentageParent,
                    VariationTheme = variationType,

                    Department      = mainDepartment,
                    Color           = "",
                    FeedProductType = clothingType,
                });

                foreach (var child in children)
                {
                    var childStyleImages = child.StyleId.HasValue ?
                                           db.StyleImages
                                           .GetAllAsDto()
                                           .Where(im => im.StyleId == child.StyleId.Value &&
                                                  im.Type != (int)StyleImageType.Swatch)
                                           .ToList()
                                           .OrderByDescending(im => ImageHelper.GetSortIndex(im.Category))
                                           .ThenByDescending(im => im.IsDefault)
                                           .ThenBy(im => im.Id)
                                           .ToList()
                            : null;
                    var childStyleGenderValue = child.StyleId.HasValue ? db.StyleFeatureValues.GetAllWithFeature()
                                                .FirstOrDefault(st => st.StyleId == child.StyleId.Value && st.FeatureId == StyleFeatureHelper.GENDER)?.Value : null;
                    var childStyle = child.StyleId.HasValue ? db.Styles.GetByStyleIdAsDto(child.StyleId.Value) : null;

                    var msrp = childStyle != null && childStyle.MSRP.HasValue
                        ? childStyle.MSRP.Value.ToString("G")
                        : (child.ListPrice.HasValue ? Math.Round(child.ListPrice.Value / 100).ToString("G") : "");

                    var description = childStyle?.Description ?? parent.Description;

                    var fbaInfo = fbaItems != null?fbaItems.FirstOrDefault(f => f.SKU == child.SKU) : null;

                    if (exportMode == ExportToExcelMode.FBA)
                    {
                        if (fbaInfo == null)
                        {
                            throw new ArgumentNullException("fbaInfo", "for SKU=" + child.SKU);
                        }
                    }

                    IList <string> features = !String.IsNullOrEmpty(child.Features)
                        ? child.Features.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList()
                        : new List <string>();
                    if (!String.IsNullOrEmpty(childStyle.BulletPoint1))
                    {
                        features = StringHelper.ToArray(childStyle.BulletPoint1,
                                                        childStyle.BulletPoint2,
                                                        childStyle.BulletPoint3,
                                                        childStyle.BulletPoint4,
                                                        childStyle.BulletPoint5);
                    }

                    var childImage = ItemExportHelper.ImageConverter(StringHelper.GetFirstNotEmpty(
                                                                         child.LargeImageUrl,
                                                                         child.ImageUrl));
                    var childImage1 = "";
                    var childImage2 = "";
                    var childImage3 = "";

                    if (useStyleImageMode == UseStyleImageModes.Auto)
                    {
                        if (String.IsNullOrEmpty(childImage))
                        {
                            if (childStyleImages != null && childStyleImages.Any())
                            {
                                childImage = childStyleImages.First().Image;
                            }
                        }
                        else
                        {
                            if (childStyleImages != null && childStyleImages.Any(im => im.Type == (int)StyleImageType.HiRes))
                            {
                                childImage = childStyleImages.First(im => im.Type == (int)StyleImageType.HiRes).Image;
                            }
                        }
                    }
                    if (useStyleImageMode == UseStyleImageModes.StyleImage ||
                        (useStyleImageMode == UseStyleImageModes.ListingImage && String.IsNullOrEmpty(childImage)))
                    {
                        if (childStyleImages != null && childStyleImages.Any())
                        {
                            childImage  = childStyleImages.First().Image;
                            childImage1 = childStyleImages.Count > 1 ? childStyleImages[1].Image : "";
                            childImage2 = childStyleImages.Count > 2 ? childStyleImages[2].Image : "";
                            childImage3 = childStyleImages.Count > 3 ? childStyleImages[3].Image : "";
                        }
                    }
                    if (useStyleImageMode == UseStyleImageModes.ListingImage)
                    {
                        //Nothing
                    }

                    var hasAmazonUpdates = child.IsExistOnAmazon == true;
                    var displaySize      = String.IsNullOrEmpty(child.Size) ? "" : ("Size " + child.Size);
                    models.Add(new ExcelWFSProductUSViewModel
                    {
                        Id              = child.Id,
                        StyleId         = child.StyleId,
                        StyleItemId     = child.StyleItemId,
                        IsExistOnAmazon = hasAmazonUpdates,

                        SKU         = ItemExportHelper.PrepareSKU(child.SKU, exportMode),
                        Title       = hasAmazonUpdates ? child.Name : StringHelper.Join(", ", child.Name, child.Color, displaySize),
                        ASIN        = hasAmazonUpdates ? child.ASIN : child.Barcode,
                        ProductId   = hasAmazonUpdates ? "ASIN" : "UPC",
                        BrandName   = mainBrandName,
                        Description = description,
                        Type        = mainItemType,
                        Update      = hasAmazonUpdates ? "PartialUpdate" : "Update",

                        StandardPrice  = (child.CurrentPrice + (exportMode == ExportToExcelMode.FBA || exportMode == ExportToExcelMode.FBP ? 5 : 0)).ToString("G"),
                        SuggestedPrice = msrp,
                        Currency       = child.ListPrice.HasValue ? "USD" : "",
                        Quantity       = exportMode == ExportToExcelMode.FBA ? fbaInfo.Quantity.ToString("G") : child.RealQuantity.ToString("G"),

                        KeyProductFeatures1 = features.Count > 0 ? features[0] : String.Empty,
                        KeyProductFeatures2 = features.Count > 1 ? features[1] : String.Empty,
                        KeyProductFeatures3 = features.Count > 2 ? features[2] : String.Empty,
                        KeyProductFeatures4 = features.Count > 3 ? features[3] : String.Empty,
                        KeyProductFeatures5 = features.Count > 4 ? features[4] : String.Empty,

                        SearchTerms1 = searchTerms,
                        //SearchTerms1 = searchTermList.Count > 0 ? searchTermList[0] : String.Empty,
                        //SearchTerms2 = searchTermList.Count > 1 ? searchTermList[1] : String.Empty,
                        //SearchTerms3 = searchTermList.Count > 2 ? searchTermList[2] : String.Empty,
                        //SearchTerms4 = searchTermList.Count > 3 ? searchTermList[3] : String.Empty,
                        //SearchTerms5 = searchTermList.Count > 4 ? searchTermList[4] : String.Empty,

                        MainImageURL     = childImage,
                        OtherImageUrl1   = childImage1,
                        OtherImageUrl2   = childImage2,
                        OtherImageUrl3   = childImage3,
                        Parentage        = ExcelHelper.ParentageChild,
                        ParentSKU        = parentSku,
                        RelationshipType = "Variation",
                        VariationTheme   = variationType,
                        Department       = mainDepartment,
                        Color            = child.Color,
                        Size             = child.Size,
                        //SpecialSize = child.SpecialSize,

                        FulfillmentCenterID        = exportMode == ExportToExcelMode.FBA ? "AMAZON_NA" : null,
                        PackageHeight              = exportMode == ExportToExcelMode.FBA && fbaInfo.PackageHeight.HasValue ? fbaInfo.PackageHeight.ToString() : null,
                        PackageWidth               = exportMode == ExportToExcelMode.FBA && fbaInfo.PackageWidth.HasValue ? fbaInfo.PackageWidth.ToString() : null,
                        PackageLength              = exportMode == ExportToExcelMode.FBA && fbaInfo.PackageLength.HasValue ? fbaInfo.PackageLength.ToString() : null,
                        PackageLengthUnitOfMeasure = exportMode == ExportToExcelMode.FBA ? "IN" : null,
                        PackageWeight              = exportMode == ExportToExcelMode.FBA ? child.Weight.ToString() : null,
                        PackageWeightUnitOfMeasure = exportMode == ExportToExcelMode.FBA ? "OZ" : null,

                        SupplierDeclaredDgHzRegulation1 = "Not Applicable",
                        BatteriesRequired = "FALSE",
                        FeedProductType   = clothingType,

                        MerchantShippingGroupName = child.IsPrime ? AmazonTemplateHelper.PrimeTemplate : (child.Weight > 16 ? AmazonTemplateHelper.OversizeTemplate : null),
                    });
                }

                //Copy to parent child Type
                if (children.Count > 0)
                {
                    models[0].Type = models[1].Type;
                }

                if (children.Count > 0 &&
                    useStyleImageMode == UseStyleImageModes.StyleImage)
                {
                    models[0].MainImageURL = models[1].MainImageURL;
                }
            }

            filename = models[0].SKU + "_" + (defaultChildSubLicense != null ? defaultChildSubLicense.Value : "none") + "_US" + Path.GetExtension(USTemplatePath);

            return(models);
        }
        private void CreateOrUpdateListing(IDbFactory dbFactory,
                                           ITime time,
                                           ILogService log,
                                           MarketType market,
                                           string marketplaceId,
                                           ParentItemDTO product,
                                           bool canCreate,
                                           bool updateQty,
                                           bool updateStyleInfo,
                                           bool mapByBarcode,
                                           bool processOnlyStyle)
        {
            using (var db = dbFactory.GetRWDb())
            {
                //var productImageUrl = product.ImageSource;

                if (!product.Variations.Any())
                {
                    log.Debug("No variations, productId=" + product.SourceMarketId + ", handle=" + product.SourceMarketUrl);
                }

                var skuList  = product.Variations.Select(v => v.SKU).ToList();
                var baseSKU  = product.Variations.FirstOrDefault()?.SKU ?? "";
                var skuParts = baseSKU.Split("-".ToCharArray());

                var mainSKU = (skuList.Distinct().Count() > 1 && skuParts.Count() > 1) ? String.Join("-", skuParts.Take(skuParts.Count() - 1)) : baseSKU;
                _log.Info("Main SKU: " + mainSKU + ", variations: " + String.Join(", ", skuList));
                //var firstMRSP = product.Variations.FirstOrDefault()?.ListPrice;
                //var firstBarcode = product.Variations.FirstOrDefault()?.Barcode;

                //Style

                Style existStyle = null;

                //Get by SKU/Name
                if (existStyle == null)
                {
                    existStyle = db.Styles.GetAll().FirstOrDefault(s => s.StyleID == mainSKU);
                    if (existStyle != null)
                    {
                        _log.Info("Style found by StyleID");
                    }
                }

                var isNewStyle = false;
                //Create new
                if (existStyle == null)
                {
                    if (!canCreate)
                    {
                        _log.Info("Unable to find style for, SKU: " + mainSKU);
                        return;
                    }

                    existStyle = new Style()
                    {
                        StyleID    = mainSKU,
                        CreateDate = time.GetAppNowTime(),
                    };
                    db.Styles.Add(existStyle);

                    isNewStyle = true;
                }
                else
                {
                }

                existStyle.DropShipperId = DSHelper.DefaultDSId;

                if (updateStyleInfo || isNewStyle)
                {
                    existStyle.StyleID     = mainSKU;
                    existStyle.Description = product.Description;
                    existStyle.Name        = product.AmazonName;

                    existStyle.Image        = product.ImageSource;
                    existStyle.Manufacturer = product.Department;
                    existStyle.BulletPoint1 = product.SearchKeywords;
                    existStyle.BulletPoint2 = product.Type;
                    //existStyle.MSRP = firstMRSP;
                    //existStyle.Price = product.Variations.Select(v => v.AmazonCurrentPrice).FirstOrDefault();

                    existStyle.UpdateDate = time.GetAppNowTime();
                }

                db.Commit();

                //if (!processOnlyStyle)
                {
                    //Style Image
                    var existStyleImages = db.StyleImages.GetAll().Where(si => si.StyleId == existStyle.Id).ToList();
                    if (!existStyleImages.Any() && !String.IsNullOrEmpty(product.ImageSource))
                    {
                        var newImage = new StyleImage()
                        {
                            Image      = product.ImageSource,
                            IsDefault  = true,
                            Type       = (int)StyleImageType.HiRes,
                            StyleId    = existStyle.Id,
                            CreateDate = _time.GetAppNowTime()
                        };
                        db.StyleImages.Add(newImage);
                        db.Commit();
                    }
                }

                //StyleFeatures
                if (isNewStyle && !processOnlyStyle)
                {
                    var existFeatures =
                        db.StyleFeatureTextValues.GetAll().Where(tv => tv.StyleId == existStyle.Id).ToList();
                    var existDdlFeatures =
                        db.StyleFeatureValues.GetAll().Where(tv => tv.StyleId == existStyle.Id).ToList();
                    var allFeatureValues = db.FeatureValues.GetAllAsDto().ToList();
                    var newFeatures      = ComposeFeatureList(db,
                                                              allFeatureValues,
                                                              product.Type,
                                                              product.SearchKeywords);
                    newFeatures.Add(ComposeFeatureValue(db, "Product Type", product.Type));
                    //newFeatures.Add(ComposeFeatureValue(db, "Brand Name", product.Department));
                    foreach (var feature in newFeatures)
                    {
                        if (feature == null)
                        {
                            continue;
                        }

                        var existFeature    = existFeatures.FirstOrDefault(f => f.FeatureId == feature.Id);
                        var existDdlFeature = existDdlFeatures.FirstOrDefault(f => f.FeatureId == feature.Id);
                        if (existFeature == null && existDdlFeature == null)
                        {
                            if (feature.Type == (int)FeatureValuesType.TextBox)
                            {
                                db.StyleFeatureTextValues.Add(new StyleFeatureTextValue()
                                {
                                    FeatureId = feature.FeatureId,
                                    Value     = StringHelper.Substring(feature.Value, 512),
                                    StyleId   = existStyle.Id,

                                    CreateDate = _time.GetAppNowTime()
                                });
                            }
                            if (feature.Type == (int)FeatureValuesType.DropDown)
                            {
                                db.StyleFeatureValues.Add(new StyleFeatureValue()
                                {
                                    FeatureId      = feature.FeatureId,
                                    FeatureValueId = feature.FeatureValueId.Value,
                                    StyleId        = existStyle.Id,

                                    CreateDate = _time.GetAppNowTime()
                                });
                            }
                        }
                    }
                    db.Commit();
                }

                ParentItem existParentItem = null;
                //if (!processOnlyStyle)
                {
                    //ParentItem
                    existParentItem = db.ParentItems.GetAll().FirstOrDefault(pi => pi.Market == (int)market
                                                                             &&
                                                                             (pi.MarketplaceId ==
                                                                              marketplaceId ||
                                                                              String.IsNullOrEmpty(
                                                                                  marketplaceId))
                                                                             &&
                                                                             pi.SourceMarketId ==
                                                                             product.SourceMarketId);
                    if (existParentItem == null)
                    {
                        existParentItem = new ParentItem()
                        {
                            Market         = (int)market,
                            MarketplaceId  = marketplaceId,
                            ASIN           = product.ASIN,
                            SourceMarketId = product.SourceMarketId,
                            CreateDate     = time.GetAppNowTime(),
                        };
                        db.ParentItems.Add(existParentItem);
                    }
                    existParentItem.AmazonName           = product.AmazonName;
                    existParentItem.Type                 = product.Type;
                    existParentItem.Description          = product.Description;
                    existParentItem.ImageSource          = product.ImageSource;
                    existParentItem.SourceMarketUrl      = product.SourceMarketUrl;
                    existParentItem.SearchKeywords       = product.SearchKeywords;
                    existParentItem.UpdateDate           = time.GetAppNowTime();
                    existParentItem.IsAmazonUpdated      = true;
                    existParentItem.LastUpdateFromAmazon = time.GetAppNowTime();

                    db.Commit();

                    product.Id = existParentItem.Id;
                }

                //if (!processOnlyStyle)
                {
                    foreach (var variation in product.Variations)
                    {
                        //StyleItem
                        var existStyleItem = db.StyleItems.GetAll()
                                             .FirstOrDefault(si => si.StyleId == existStyle.Id);
                        // && (si.Color == variation.Color || String.IsNullOrEmpty(variation.Color)));

                        var isNewStyleItem = false;
                        if (existStyleItem == null)
                        {
                            if (!canCreate && !isNewStyle)
                            {
                                _log.Info("Unable to find StyleItem for styleId: " + existStyle.StyleID);
                                return;
                            }

                            existStyleItem = new StyleItem()
                            {
                                StyleId = existStyle.Id,
                                Size    = variation.Size,
                                Color   = variation.Color,
                            };
                            db.StyleItems.Add(existStyleItem);

                            isNewStyleItem = true;
                        }

                        //TEMP:
                        //if (updateQty || isNewStyle)
                        //{
                        //    existStyleItem.Quantity = variation.AmazonRealQuantity;
                        //    existStyleItem.QuantitySetDate = time.GetAppNowTime();
                        //}
                        db.Commit();

                        variation.StyleItemId = existStyleItem.Id;

                        //StyleItem Barcode
                        if (!String.IsNullOrEmpty(variation.Barcode))
                        {
                            var existBarcode =
                                db.StyleItemBarcodes.GetAll().FirstOrDefault(b => b.Barcode == variation.Barcode);
                            if (existBarcode == null)
                            {
                                _log.Info("Added new barcode: " + variation.Barcode);
                                existBarcode = new StyleItemBarcode()
                                {
                                    Barcode    = variation.Barcode,
                                    CreateDate = time.GetAppNowTime(),
                                };
                                db.StyleItemBarcodes.Add(existBarcode);
                            }
                            existBarcode.StyleItemId = existStyleItem.Id;
                            existBarcode.UpdateDate  = time.GetAppNowTime();
                            db.Commit();
                        }

                        //if (!processOnlyStyle)
                        {
                            //Item
                            var existItem =
                                db.Items.GetAll().FirstOrDefault(v => v.SourceMarketId == variation.SourceMarketId &&
                                                                 v.Market == (int)market
                                                                 &&
                                                                 (v.MarketplaceId == marketplaceId ||
                                                                  String.IsNullOrEmpty(marketplaceId)));
                            if (existItem == null)
                            {
                                existItem = new Item()
                                {
                                    ParentASIN     = existParentItem.ASIN,
                                    ASIN           = variation.ASIN,
                                    SourceMarketId = variation.SourceMarketId,
                                    Market         = (int)market,
                                    MarketplaceId  = marketplaceId,
                                    CreateDate     = time.GetAppNowTime(),
                                };
                                db.Items.Add(existItem);
                            }

                            existItem.StyleId     = existStyle.Id;
                            existItem.StyleItemId = existStyleItem.Id;

                            existItem.Title   = product.AmazonName;
                            existItem.Barcode = variation.Barcode;
                            existItem.Color   = variation.Color;
                            existItem.Size    = variation.Size;

                            existItem.SourceMarketUrl = variation.SourceMarketUrl;

                            existItem.PrimaryImage = variation.ImageUrl;
                            existItem.ListPrice    = (decimal?)variation.ListPrice;

                            existItem.UpdateDate              = time.GetAppNowTime();
                            existItem.IsExistOnAmazon         = true;
                            existItem.LastUpdateFromAmazon    = time.GetAppNowTime();
                            existItem.ItemPublishedStatus     = variation.PublishedStatus;
                            existItem.ItemPublishedStatusDate = variation.PuclishedStatusDate ?? time.GetAppNowTime();

                            db.Commit();

                            variation.Id = existItem.Id;

                            //Listing
                            var existListing =
                                db.Listings.GetAll().FirstOrDefault(v => v.ListingId == variation.ListingId.ToString() &&
                                                                    v.Market == (int)market
                                                                    &&
                                                                    (v.MarketplaceId == marketplaceId ||
                                                                     String.IsNullOrEmpty(marketplaceId)));
                            if (existListing == null)
                            {
                                existListing = new Listing()
                                {
                                    ItemId = existItem.Id,

                                    ASIN      = variation.ASIN,
                                    ListingId = StringHelper.GetFirstNotEmpty(variation.ASIN, variation.ListingId),

                                    Market        = (int)market,
                                    MarketplaceId = marketplaceId,

                                    CreateDate = time.GetAppNowTime(),
                                };
                                db.Listings.Add(existListing);
                            }

                            existListing.SKU = variation.SKU;

                            existListing.CurrentPrice                 = (decimal)(variation.AmazonCurrentPrice ?? 0);
                            existListing.AmazonCurrentPrice           = (decimal?)variation.AmazonCurrentPrice;
                            existListing.AmazonCurrentPriceUpdateDate = time.GetAmazonNowTime();
                            existListing.PriceFromMarketUpdatedDate   = time.GetAppNowTime();

                            existListing.RealQuantity                 = variation.AmazonRealQuantity ?? 0;
                            existListing.AmazonRealQuantity           = variation.AmazonRealQuantity ?? 0;
                            existListing.AmazonRealQuantityUpdateDate = time.GetAppNowTime();

                            existListing.UpdateDate = time.GetAppNowTime();
                            existListing.IsRemoved  = false;

                            db.Commit();

                            variation.ListingEntityId = existListing.Id;
                        }
                    }
                }
            }
        }
Esempio n. 11
0
        public void UpdateProduct(ParentItemDTO parentItemDto,
                                  IUnitOfWork db,
                                  DateTime when,
                                  long?by)
        {
            var parentItem   = db.ParentItems.Get(parentItemDto.Id);
            var previousASIN = parentItem.ASIN;

            if (parentItem.ASIN != parentItemDto.ASIN)
            {
                parentItem.ASIN = parentItemDto.ASIN;
            }
            parentItem.SKU = parentItemDto.ASIN;

            parentItem.ForceEnableColorVariations = parentItemDto.ForceEnableColorVariations;
            parentItem.OnHold     = parentItemDto.OnHold;
            parentItem.AmazonName = parentItemDto.AmazonName;

            parentItem.UpdateDate = when;
            parentItem.UpdatedBy  = by;

            db.Commit();

            var variationDtoList = parentItemDto.Variations
                                   .Select(v => new ItemDTO()
            {
                ParentASIN = parentItem.ASIN,
                Name       = parentItemDto.AmazonName,

                Id          = v.Id,
                StyleString = v.StyleString,
                StyleId     = v.StyleId,
                StyleItemId = v.StyleItemId,
                Size        = v.Size,
                Color       = v.Color,

                ASIN      = v.SKU,
                IsDefault = v.IsDefault,
                ListingId = v.SKU,

                SKU     = v.SKU,
                Barcode = v.AutoGeneratedBarcode ? BarcodeHelper.GenerateBarcode(_barcodeService, v.SKU, when) : v.Barcode,

                RealQuantity = v.RealQuantity,

                CurrentPrice = v.CurrentPrice,

                PublishedStatus = v.OverridePublishedStatus.HasValue && v.OverridePublishedStatus.Value != v.PublishedStatus ? v.OverridePublishedStatus.Value : (int)PublishedStatuses.None
            }).ToList();

            db.Items.UpdateItemsForParentItem(_itemHistoryService,
                                              "AutoCreateBaseListingService.UpdateProduct",
                                              previousASIN,
                                              parentItem.Market,
                                              parentItem.MarketplaceId,
                                              variationDtoList,
                                              when,
                                              by);

            _cacheService.RequestParentItemIdUpdates(db,
                                                     new List <long>()
            {
                parentItem.Id
            },
                                                     UpdateCacheMode.IncludeChild,
                                                     by);
        }
Esempio n. 12
0
        public long CreateProduct(ParentItemDTO parentItemDto,
                                  IUnitOfWork db,
                                  DateTime when,
                                  long?by)
        {
            var id = 0;

            var parentItem = new ParentItem()
            {
                ASIN          = parentItemDto.ASIN,
                Market        = parentItemDto.Market,
                MarketplaceId = parentItemDto.MarketplaceId,

                ForceEnableColorVariations = parentItemDto.ForceEnableColorVariations,
                OnHold = parentItemDto.OnHold,

                SKU        = parentItemDto.ASIN,
                AmazonName = parentItemDto.AmazonName,

                CreateDate = when,
                CreatedBy  = by
            };

            db.ParentItems.Add(parentItem);
            db.Commit();

            parentItemDto.Id = parentItem.Id;
            id = parentItem.Id;

            var variationDtoList = parentItemDto.Variations
                                   .Select(v => new ItemDTO()
            {
                ParentASIN = parentItem.ASIN,
                Name       = parentItem.AmazonName, //Equal parent name

                StyleString = v.StyleString,
                StyleId     = v.StyleId,
                StyleItemId = v.StyleItemId,
                Size        = v.Size,
                Color       = v.Color,

                ASIN      = v.SKU,
                SKU       = v.SKU,
                IsDefault = v.IsDefault,
                ListingId = v.SKU,

                Barcode = v.AutoGeneratedBarcode ? BarcodeHelper.GenerateBarcode(_barcodeService, v.SKU, when) : v.Barcode,

                CurrentPrice = v.CurrentPrice,
                RealQuantity = 0,

                PublishedStatus = (int)PublishedStatuses.New,
            }).ToList();

            db.Items.UpdateItemsForParentItem(_itemHistoryService,
                                              "AutoCreateBaseListingService.CreateProduct",
                                              parentItem.ASIN,
                                              parentItem.Market,
                                              parentItem.MarketplaceId,
                                              variationDtoList,
                                              when,
                                              by);

            _cacheService.RequestParentItemIdUpdates(db,
                                                     new List <long>()
            {
                parentItem.Id
            },
                                                     UpdateCacheMode.IncludeChild,
                                                     by);

            return(id);
        }
Esempio n. 13
0
        public ParentItemDTO CreateFromParentASIN(IUnitOfWork db,
                                                  string asin,
                                                  int market,
                                                  string marketplaceId,
                                                  bool includeAllChild,
                                                  bool includeZeroQtyChild,
                                                  int?minQty,
                                                  out IList <MessageString> messages)
        {
            messages = new List <MessageString>();

            var parentItem = db.ParentItems.GetAllAsDto().FirstOrDefault(s => s.ASIN == asin &&
                                                                         s.Market == market &&
                                                                         s.MarketplaceId == marketplaceId);

            if (parentItem == null)
            {
                messages.Add(MessageString.Error("Parent ASIN was not found"));
                return(null);
            }

            minQty = minQty ?? 0;
            var itemsQuery = from i in db.Items.GetAllViewAsDto()
                             join sic in db.StyleItemCaches.GetAll() on i.StyleItemId equals sic.Id
                             where i.ParentASIN == parentItem.ASIN &&
                             sic.RemainingQuantity > minQty &&    //Exclude items w/o qty
                             i.Market == parentItem.Market &&
                             i.MarketplaceId == parentItem.MarketplaceId
                             select i;

            if (!includeAllChild)
            {
                itemsQuery = itemsQuery.Where(i => !i.IsFBA && //Exclude FBA
                                              !i.SKU.Contains("-FBP") &&       //Exclude FBP
                                              !i.StyleString.Contains("-tmp")          //Remove items linked to tmp styles
                                              );
            }

            var items = itemsQuery.ToList();

            var model = new ParentItemDTO();

            var firstStyleString = items.Any() ? items.First().StyleString : "";

            model.ASIN = firstStyleString;

            model.Market        = (int)market;
            model.MarketplaceId = marketplaceId;

            model.AmazonName = parentItem.AmazonName;

            foreach (var item in items)
            {
                item.Size         = SizeHelper.SizeCorrection(item.Size, item.StyleSize);
                item.RealQuantity = 0;
            }

            model.Variations = items
                               .OrderBy(i => i.StyleId)
                               .ThenBy(i => SizeHelper.GetSizeIndex(i.StyleSize))
                               .ToList();

            return(model);
        }
Esempio n. 14
0
        public ParentItemDTO CreateFromStyle(IUnitOfWork db,
                                             StyleEntireDto style,
                                             MarketType market,
                                             string marketplaceId,
                                             out IList <MessageString> messages)
        {
            messages = new List <MessageString>();

            var styleItems = db.StyleItems.GetByStyleIdWithBarcodesAsDto(style.Id);

            var model = new ParentItemDTO();

            model.ASIN = style.StyleID;

            model.Market        = (int)market;
            model.MarketplaceId = marketplaceId;

            model.AmazonName = style.Name;

            var items = new List <ItemDTO>();

            foreach (var styleItem in styleItems)
            {
                var newItem = new ItemDTO();
                newItem.Market        = (int)market;
                newItem.MarketplaceId = marketplaceId;

                newItem.StyleString = style.StyleID;
                newItem.StyleId     = styleItem.StyleId;
                newItem.StyleItemId = styleItem.StyleItemId;
                newItem.StyleSize   = styleItem.Size;
                newItem.StyleColor  = styleItem.Color;

                newItem.Size  = styleItem.Size;
                newItem.Color = styleItem.Color;

                newItem.Weight = styleItem.Weight;

                var barcode = styleItem.Barcodes?.FirstOrDefault();
                if (barcode != null)
                {
                    newItem.Barcode = barcode.Barcode;
                }

                newItem.CurrentPrice = 0;
                newItem.SKU          = style.StyleID + "-" + SizeHelper.PrepareSizeForSKU(styleItem.Size, false);
                if (!String.IsNullOrEmpty(styleItem.Color))
                {
                    newItem.SKU += "-" + styleItem.Color;
                }

                items.Add(newItem);
            }

            model.Variations = items
                               .OrderBy(si => SizeHelper.GetSizeIndex(si.StyleSize))
                               .ThenBy(si => si.Color)
                               .ToList();

            return(model);
        }
        private void CreateOrUpdateListing(IDbFactory dbFactory,
                                           ITime time,
                                           ILogService log,
                                           MarketType market,
                                           string marketplaceId,
                                           ParentItemDTO product)
        {
            using (var db = dbFactory.GetRWDb())
            {
                //var productImageUrl = product.ImageSource;

                if (!product.Variations.Any())
                {
                    log.Debug("No variations, productId=" + product.SourceMarketId + ", handle=" + product.SourceMarketUrl);
                }

                var firstSKU     = product.Variations.FirstOrDefault()?.SKU;
                var firstMRSP    = product.Variations.FirstOrDefault()?.ListPrice;
                var firstBarcode = product.Variations.FirstOrDefault()?.Barcode;

                //Style

                Style existStyle = null;

                //Get by SKU/Name
                if (existStyle == null)
                {
                    existStyle = db.Styles.GetAll().FirstOrDefault(s => s.StyleID == firstSKU);
                    if (existStyle != null)
                    {
                        _log.Info("Style found by StyleID");
                    }
                }

                //Get by barcode
                if (existStyle == null && !String.IsNullOrEmpty(firstBarcode))
                {
                    var barcode = db.StyleItemBarcodes.GetAll().FirstOrDefault(b => b.Barcode == firstBarcode);
                    if (barcode != null)
                    {
                        var styleItem = db.StyleItems.GetAll().FirstOrDefault(si => si.Id == barcode.StyleItemId);
                        existStyle = db.Styles.GetAll().FirstOrDefault(s => s.Id == styleItem.StyleId);
                    }
                    if (existStyle != null)
                    {
                        _log.Info("Style found by barcode");
                    }
                }


                //ParentItem
                var existParentItem = db.ParentItems.GetAll().FirstOrDefault(pi => pi.Market == (int)market &&
                                                                             (pi.MarketplaceId == marketplaceId || String.IsNullOrEmpty(marketplaceId)) &&
                                                                             pi.SourceMarketId == product.SourceMarketId);
                if (existParentItem == null)
                {
                    existParentItem = new ParentItem()
                    {
                        Market         = (int)market,
                        MarketplaceId  = marketplaceId,
                        ASIN           = product.ASIN,
                        SourceMarketId = product.SourceMarketId,
                        CreateDate     = time.GetAppNowTime(),
                    };
                    db.ParentItems.Add(existParentItem);
                }
                existParentItem.SourceMarketUrl      = product.SourceMarketUrl;
                existParentItem.UpdateDate           = time.GetAppNowTime();
                existParentItem.IsAmazonUpdated      = true;
                existParentItem.LastUpdateFromAmazon = time.GetAppNowTime();

                db.Commit();

                product.Id = existParentItem.Id;

                foreach (var variation in product.Variations)
                {
                    //StyleItem
                    var existStyleItem = existStyle != null?db.StyleItems.GetAll()
                                         .FirstOrDefault(si => si.StyleId == existStyle.Id) : null;

                    variation.StyleItemId = existStyleItem?.Id;

                    //StyleItem Barcode
                    if (!String.IsNullOrEmpty(variation.Barcode) &&
                        existStyleItem != null)
                    {
                        var existBarcode = db.StyleItemBarcodes.GetAll().FirstOrDefault(b => b.StyleItemId == existStyleItem.Id &&
                                                                                        b.Barcode == variation.Barcode);
                        if (existBarcode == null)
                        {
                            _log.Info("Added new barcode: " + variation.Barcode);
                            existBarcode = new StyleItemBarcode()
                            {
                                Barcode    = variation.Barcode,
                                CreateDate = time.GetAppNowTime(),
                            };
                            db.StyleItemBarcodes.Add(existBarcode);
                        }
                        existBarcode.StyleItemId = existStyleItem.Id;
                        existBarcode.UpdateDate  = time.GetAppNowTime();
                        db.Commit();
                    }

                    //Item
                    var existItem = db.Items.GetAll().FirstOrDefault(v => v.SourceMarketId == variation.SourceMarketId &&
                                                                     v.Market == (int)market &&
                                                                     (v.MarketplaceId == marketplaceId || String.IsNullOrEmpty(marketplaceId)));
                    if (existItem == null)
                    {
                        existItem = new Item()
                        {
                            ParentASIN     = existParentItem.ASIN,
                            ASIN           = variation.ASIN,
                            SourceMarketId = variation.SourceMarketId,
                            Market         = (int)market,
                            MarketplaceId  = marketplaceId,
                            CreateDate     = time.GetAppNowTime(),
                        };
                        db.Items.Add(existItem);
                    }

                    existItem.StyleId     = existStyle.Id;
                    existItem.StyleItemId = existStyleItem.Id;

                    //existItem.Title = product.AmazonName;
                    existItem.Barcode = variation.Barcode;
                    //existItem.Color = variation.Color;

                    existItem.SourceMarketUrl = variation.SourceMarketUrl;

                    //existItem.PrimaryImage = variation.ImageUrl;
                    //existItem.ListPrice = (decimal?)variation.ListPrice;

                    existItem.UpdateDate              = time.GetAppNowTime();
                    existItem.IsExistOnAmazon         = true;
                    existItem.LastUpdateFromAmazon    = time.GetAppNowTime();
                    existItem.ItemPublishedStatus     = variation.PublishedStatus;
                    existItem.ItemPublishedStatusDate = variation.PuclishedStatusDate ?? time.GetAppNowTime();

                    db.Commit();

                    variation.Id = existItem.Id;

                    //Listing
                    var existListing = db.Listings.GetAll().FirstOrDefault(v => v.ListingId == variation.ListingId.ToString() &&
                                                                           v.Market == (int)market &&
                                                                           (v.MarketplaceId == marketplaceId || String.IsNullOrEmpty(marketplaceId)));
                    if (existListing == null)
                    {
                        existListing = new Listing()
                        {
                            ItemId = existItem.Id,

                            ASIN      = variation.ASIN,
                            ListingId = variation.ListingId,

                            Market        = (int)market,
                            MarketplaceId = marketplaceId,

                            CreateDate = time.GetAppNowTime(),
                        };
                        db.Listings.Add(existListing);
                    }

                    existListing.SKU = variation.SKU;
                    //existListing.CurrentPrice = (decimal)(variation.AmazonCurrentPrice ?? 0);
                    existListing.AmazonCurrentPrice           = (decimal?)variation.AmazonCurrentPrice;
                    existListing.AmazonCurrentPriceUpdateDate = time.GetAmazonNowTime();
                    existListing.PriceFromMarketUpdatedDate   = time.GetAppNowTime();

                    //existListing.RealQuantity = variation.AmazonRealQuantity ?? 0;
                    existListing.AmazonRealQuantity           = variation.AmazonRealQuantity ?? 0;
                    existListing.AmazonRealQuantityUpdateDate = time.GetAppNowTime();

                    existListing.UpdateDate = time.GetAppNowTime();
                    existListing.IsRemoved  = false;

                    db.Commit();

                    variation.ListingEntityId = existListing.Id;
                }
            }
        }
Esempio n. 16
0
        public ParentItem CreateOrUpdateParent(ParentItemDTO dto,
                                               MarketType market,
                                               string marketplaceId,
                                               DateTime?when)
        {
            if (!ArgumentHelper.CheckMarket(market))
            {
                throw new ArgumentNullException("market");
            }
            if (!ArgumentHelper.CheckMarketplaceId(market, marketplaceId))
            {
                throw new ArgumentNullException("marketplaceId");
            }

            var parentItem = unitOfWork.GetSet <ParentItem>().FirstOrDefault(i =>
                                                                             i.ASIN == dto.ASIN &&
                                                                             i.Market == (int)market &&
                                                                             i.MarketplaceId == marketplaceId);

            if (parentItem == null)
            {
                parentItem = new ParentItem
                {
                    ASIN           = dto.ASIN,
                    Market         = (int)market,
                    MarketplaceId  = marketplaceId,
                    SourceMarketId = dto.ASIN,

                    AmazonName  = dto.AmazonName,
                    SKU         = dto.SKU,
                    ImageSource = dto.ImageSource,

                    Rank           = dto.Rank,
                    RankUpdateDate = when,

                    //Additional fields
                    BrandName        = dto.BrandName,
                    Type             = dto.Type,
                    ListPrice        = dto.ListPrice,
                    Color            = dto.Color,
                    Department       = dto.Department,
                    Features         = dto.Features,
                    AdditionalImages = dto.AdditionalImages,
                    SearchKeywords   = dto.SearchKeywords,

                    //System
                    CreateDate           = when,
                    IsAmazonUpdated      = dto.IsAmazonUpdated,
                    LastUpdateFromAmazon = dto.LastUpdateFromAmazon
                };
                Add(parentItem);
            }
            else
            {
                UpdateParentItemInfo(parentItem, dto, when);
            }

            unitOfWork.Commit();

            return(parentItem);
        }