public void ImportProductVariantsService_ImportVariants_ShouldSetProductVariantPrimaryProperties()
        {
            var productVariantDTO = new ProductVariantImportDataTransferObject
                                        {
                                            PreviousPrice = 2,
                                            Price = 1,
                                            SKU = "123",
                                            Name = "Test Product Variant",
                                            TrackingPolicy = TrackingPolicy.Track,
                                            Weight = 0,
                                            Barcode = "456",
                                            Stock = 5
                                        };
            var productDTO = new ProductImportDataTransferObject
                                 {
                                     UrlSegment = "test-url",
                                     ProductVariants = new List<ProductVariantImportDataTransferObject>() { productVariantDTO }
                                 };

            var product = new Product() { Name = "Test Product" };

            var result = _importProductVariantsService.ImportVariants(productDTO, product);

            result.First().PreviousPrice.ShouldBeEquivalentTo(2);
            result.First().BasePrice.ShouldBeEquivalentTo(1);
            result.First().SKU.ShouldBeEquivalentTo("123");
            result.First().Name.ShouldBeEquivalentTo("Test Product Variant");
            result.First().TrackingPolicy.ShouldBeEquivalentTo(TrackingPolicy.Track);
            result.First().Weight.ShouldBeEquivalentTo(0);
            result.First().Barcode.ShouldBeEquivalentTo("456");
            result.First().StockRemaining.ShouldBeEquivalentTo(5);
        }
        public IEnumerable<PriceBreak> ImportVariantPriceBreaks(ProductVariantImportDataTransferObject dto, ProductVariant productVariant)
        {

            var priceBreaksToAdd = dto.PriceBreaks.Where(s => !productVariant.PriceBreaks.Select(@break => @break.Quantity).Contains(s.Key)).ToList();
            var priceBreaksToRemove = productVariant.PriceBreaks.Where(@break => !dto.PriceBreaks.Keys.Contains(@break.Quantity)).ToList();
            var priceBreaksToUpdate = productVariant.PriceBreaks.Where(@break => !priceBreaksToRemove.Contains(@break)).ToList();
            foreach (var item in priceBreaksToAdd)
            {
                var priceBreak = new PriceBreak
                                     {
                                         Quantity = item.Key,
                                         Price = item.Value,
                                         ProductVariant = productVariant
                                     };
                productVariant.PriceBreaks.Add(priceBreak);
            }

            foreach (var priceBreak in priceBreaksToRemove)
            {
                productVariant.PriceBreaks.Remove(priceBreak);
                _session.Delete(priceBreak);
            }
            foreach (var priceBreak in priceBreaksToUpdate)
                priceBreak.Price = dto.PriceBreaks[priceBreak.Quantity];
            return productVariant.PriceBreaks;
        }
 public IEnumerable<string> GetErrors(ProductVariantImportDataTransferObject productVariant)
 {
     if (productVariant.TrackingPolicy == TrackingPolicy.Track)
     {
         if (!productVariant.Stock.HasValue)
            yield return ("Variant Stock must have value if Tracking Policy is set to 'Track'.");
     }
 }
 public IEnumerable<string> GetErrors(ProductVariantImportDataTransferObject productVariant)
 {
     var value = Selector(productVariant);
     if (!String.IsNullOrWhiteSpace(value))
     {
         if (value.Length > Length)
             yield return
                 string.Format(
                     "{0} is too long - max length is {1} characters and your value is {2} characters in length.",
                     DisplayName, Length, value.Length);
     }
 }
 public IEnumerable<string> GetErrors(ProductVariantImportDataTransferObject productVariant)
 {
     var errors=new List<string>();
     foreach (var item in productVariant.Options)
     {
         if (item.Key.Length > 255)
             errors.Add(string.Format(
                 "{0} is too long - max length is {1} characters and your value is {2} characters in length",
                 item.Key + " Name", 255, item.Key.Length));
         if (item.Value.Length > 255)
             errors.Add(string.Format(
                 "{0} is too long - max length is {1} characters and your value is {2} characters in length",
                 item.Key + " Value", 255, item.Value.Length));
     }
     return errors;
 }
 public IEnumerable<string> GetErrors(ProductVariantImportDataTransferObject productVariant)
 {
     var value = Selector(productVariant);
     if (value.HasValue)
     {
         if (value < Min)
            yield return 
                string.Format(
                 "{0} value must be greather than or equal to {1}.",
                 DisplayName,Min);
     }
     else
     {
         yield return
               string.Format(
                "{0} value must be greather than or equal to {1}.",
                DisplayName, Min);
     }
 }
 public IEnumerable<string> GetErrors(ProductVariantImportDataTransferObject productVariant)
 {
     var errors = new List<string>();
     var productVariantPriceBreaks = new List<PriceBreak>();
     foreach (var item in productVariant.PriceBreaks)
     {
         var priceBreak = new PriceBreak() {Price = item.Value, Quantity = item.Key};
         productVariantPriceBreaks.Add(priceBreak);
         if (!IsPriceBreakQuantityValid(item.Key, productVariantPriceBreaks) ||
             !IsPriceBreakPriceValid(item.Key, item.Value, productVariant.Price, productVariantPriceBreaks))
         {
             productVariantPriceBreaks.Remove(priceBreak);
             errors.Add(
                 string.Format(
                     "Price Break with Quantity: {0} and Price:{1} is not valid for this product variant.",
                     item.Key, item.Value));
         }
     }
     return errors;
 }
        public void ImportProductVariantPriceBreakService_ImportVariantPriceBreaks_ShouldCallProductVariantServiceAddPriceBreak()
        {
            var mockingKernel = new MockingKernel();
            var session = A.Fake<ISession>();
            mockingKernel.Bind<ISession>().ToMethod(context => session).InSingletonScope();
            MrCMSKernel.OverrideKernel(mockingKernel);

            var productVariantDTO = new ProductVariantImportDataTransferObject
            {
                PriceBreaks = new Dictionary<int, decimal>() { { 10, 299 } }
            };
            var productVariant = new ProductVariant();

            var priceBreaks = _importProductUrlHistoryService.ImportVariantPriceBreaks(productVariantDTO, productVariant);

            priceBreaks.Should().HaveCount(1);
            var priceBreak = priceBreaks.ToList()[0];
            priceBreak.Price.Should().Be(299);
            priceBreak.Quantity.Should().Be(10);
            priceBreak.ProductVariant.Should().Be(productVariant);
        }
        public void ImportProductVariantsService_ImportVariants_ShouldSetProductVariantTaxRate()
        {
            var productVariantDTO = new ProductVariantImportDataTransferObject
                                        {
                                            SKU = "123",
                                            TaxRate = 1
                                        };
            var productDTO = new ProductImportDataTransferObject
                                 {
                                     UrlSegment = "test-url",
                                     ProductVariants = new List<ProductVariantImportDataTransferObject>() { productVariantDTO }
                                 };

            var product = new Product { Name = "Test Product" };

            var taxRate = new TaxRate() { Id = 1, Name = "GLOBAL" };
            A.CallTo(() => _taxRateManager.Get(productVariantDTO.TaxRate.Value)).Returns(taxRate);

            var result = _importProductVariantsService.ImportVariants(productDTO, product);

            result.First().TaxRate.Name.ShouldBeEquivalentTo("GLOBAL");
        }
 public IEnumerable<string> GetErrors(ProductVariantImportDataTransferObject productVariant)
 {
     if (productVariant.TaxRate.HasValue && productVariant.TaxRate!=0 &&  _taxRateManager.Get(productVariant.TaxRate.Value) == null)
         yield return "The tax rate Id specified is not present within the system.";
 }
        private static ProductVariantImportDataTransferObject GetProductVariant(Dictionary<string, List<string>> parseErrors, ExcelWorksheet worksheet,
            int rowId, string handle)
        {
            var productVariant = new ProductVariantImportDataTransferObject
            {
                Name = worksheet.GetValue<string>(rowId, 11)
            };

            if (!GeneralHelper.IsValidInput<decimal>(worksheet.GetValue<string>(rowId, 12)))
                parseErrors[handle].Add("Price value is not a valid decimal number.");
            else if (worksheet.GetValue<string>(rowId, 12).HasValue())
                productVariant.Price =
                    GeneralHelper.GetValue<decimal>(worksheet.GetValue<string>(rowId, 12));
            else
                parseErrors[handle].Add("Price is required.");

            if (!GeneralHelper.IsValidInput<decimal>(worksheet.GetValue<string>(rowId, 13)))
                parseErrors[handle].Add(
                    "Previous Price value is not a valid decimal number.");
            else
                productVariant.PreviousPrice =
                    GeneralHelper.GetValue<decimal>(worksheet.GetValue<string>(rowId, 13));

            if (!GeneralHelper.IsValidInput<int>(worksheet.GetValue<string>(rowId, 14)))
                parseErrors[handle].Add("Tax Rate Id value is not a valid number.");
            else
                productVariant.TaxRate =
                    GeneralHelper.GetValue<int>(worksheet.GetValue<string>(rowId, 14));

            if (!GeneralHelper.IsValidInput<decimal>(worksheet.GetValue<string>(rowId, 15)))
                parseErrors[handle].Add("Weight value is not a valid decimal number.");
            else
                productVariant.Weight =
                    GeneralHelper.GetValue<decimal>(worksheet.GetValue<string>(rowId, 15));

            if (!GeneralHelper.IsValidInput<int>(worksheet.GetValue<string>(rowId, 16)))
                parseErrors[handle].Add("Stock value is not a valid decimal number.");
            else
                productVariant.Stock = worksheet.HasValue(rowId, 16)
                    ? GeneralHelper.GetValue<int>(
                        worksheet.GetValue<string>(rowId, 16))
                    : (int?) null;

            if (!worksheet.GetValue<string>(rowId, 17).HasValue() ||
                (worksheet.GetValue<string>(rowId, 17) != "Track" &&
                 worksheet.GetValue<string>(rowId, 17) != "DontTrack"))
                parseErrors[handle].Add(
                    "Tracking Policy must have either 'Track' or 'DontTrack' value.");
            else
            {
                productVariant.TrackingPolicy = worksheet.GetValue<string>(rowId, 17) == "Track"
                    ? TrackingPolicy.Track
                    : TrackingPolicy.DontTrack;
            }
            if (worksheet.GetValue<string>(rowId, 18).HasValue())
                productVariant.SKU = worksheet.GetValue<string>(rowId, 18);
            else
                parseErrors[handle].Add("SKU is required.");
            productVariant.Barcode = worksheet.GetValue<string>(rowId, 19);

            productVariant.ManufacturerPartNumber = worksheet.GetValue<string>(rowId, 20);

            productVariant.ETag = worksheet.GetValue<string>(rowId, 33);
            
            return productVariant;
        }
 private static void GetProductVariantOptions(ExcelWorksheet worksheet, int rowId,
     ProductVariantImportDataTransferObject productVariant)
 {
     if (worksheet.GetValue<string>(rowId, 21).HasValue() &&
         worksheet.GetValue<string>(rowId, 22).HasValue())
         productVariant.Options.Add(worksheet.GetValue<string>(rowId, 21),
             worksheet.GetValue<string>(rowId, 22));
     if (worksheet.GetValue<string>(rowId, 23).HasValue() &&
         worksheet.GetValue<string>(rowId, 24).HasValue())
         productVariant.Options.Add(worksheet.GetValue<string>(rowId, 23),
             worksheet.GetValue<string>(rowId, 24));
     if (worksheet.GetValue<string>(rowId, 25).HasValue() &&
         worksheet.GetValue<string>(rowId, 26).HasValue())
         productVariant.Options.Add(worksheet.GetValue<string>(rowId, 25),
             worksheet.GetValue<string>(rowId, 26));
 }
 private static void GetPriceBreaks(Dictionary<string, List<string>> parseErrors, ExcelWorksheet worksheet, int rowId, string handle,
     ProductVariantImportDataTransferObject productVariant)
 {
     if (!String.IsNullOrWhiteSpace(worksheet.GetValue<string>(rowId, 30)))
     {
         try
         {
             var value = worksheet.GetValue<string>(rowId, 30);
             if (!value.Contains(":"))
             {
                 parseErrors[handle].Add(
                     "Product Variant Price Breaks field value contains illegal characters / not in correct format. Quantity and Price (Item) must be split with :, and items must be split by ;");
             }
             else
             {
                 var priceBreaks = value.Split(';');
                 foreach (var item in priceBreaks)
                 {
                     if (!String.IsNullOrWhiteSpace(item))
                     {
                         var priceBreak = item.Split(':');
                         if (!String.IsNullOrWhiteSpace(priceBreak[0]) &&
                             !String.IsNullOrWhiteSpace(priceBreak[1]) &&
                             !productVariant.PriceBreaks.ContainsKey(
                                 Int32.Parse(priceBreak[0])))
                         {
                             var quantity = Int32.Parse(priceBreak[0]);
                             var price = Decimal.Parse(priceBreak[1]);
                             productVariant.PriceBreaks.Add(quantity, price);
                         }
                     }
                 }
             }
         }
         catch (ArgumentException)
         {
             parseErrors[handle].Add(
                 "Product Variant Price Breaks field contains duplicate price breaks.");
         }
         catch (Exception)
         {
             parseErrors[handle].Add(
                 "Product Variant Price Breaks field value contains illegal characters / not in correct format. Quantity and Price (Item) must be split with :, and items must be split by ;");
         }
     }
 }
        public void ImportProductVariantsService_ImportVariants_ShouldAddVariantsToProduct()
        {
            var productVariantDTO = new ProductVariantImportDataTransferObject
                                        {
                                            SKU = "123"
                                        };
            var productDTO = new ProductImportDataTransferObject
                                 {
                                     UrlSegment = "test-url",
                                     ProductVariants = new List<ProductVariantImportDataTransferObject>() { productVariantDTO }
                                 };

            var product = new Product() { Name = "Test Product" };

            _importProductVariantsService.ImportVariants(productDTO, product);

            product.Variants.Should().HaveCount(1);
        }
        public void ImportProductVariantsService_ImportVariants_ShouldCallImportVariantPriceBreaksOfImportProductVariantPriceBreaksService()
        {
            var productVariantDTO = new ProductVariantImportDataTransferObject
            {
                SKU = "123",
                PriceBreaks = new Dictionary<int, decimal>() { { 10, 299 } }
            };
            var productDTO = new ProductImportDataTransferObject
            {
                UrlSegment = "test-url",
                ProductVariants = new List<ProductVariantImportDataTransferObject>() { productVariantDTO }
            };

            var product = new Product();

            var productVariant = new ProductVariant() { Name = "Test Product Variant" };

            _importProductVariantsService.ImportVariants(productDTO, product);

            A.CallTo(() => _importProductVariantPriceBreaksService.ImportVariantPriceBreaks(productVariantDTO, productVariant)).MustHaveHappened();
        }
        public void ImportProductVariantsService_ImportVariants_ShouldCallGetProductVariantBySKUOfProductVariantService()
        {
            var productVariant = new ProductVariantImportDataTransferObject
                                     {
                                         SKU = "123"
                                     };
            var productDTO = new ProductImportDataTransferObject
                                 {
                                     UrlSegment = "test-url",
                                     ProductVariants = new List<ProductVariantImportDataTransferObject>() { productVariant }
                                 };

            var product = new Product();
            _importProductVariantsService.ImportVariants(productDTO, product);
        }