Ejemplo n.º 1
0
        private ProductVariantQuoteModel PrepareProductVariantQuoteModelForList(CustomerProductVariantQuote customerProductVariantQuote)
        {
            #region Product variant price
            var productVariant = _productService.GetProductVariantById(customerProductVariantQuote.ProductVariantId);
            if (productVariant == null || productVariant.Id == 0)
            {
                return(null);
            }
            string   productName = productVariant.FullProductName;
            Currency currency    = _currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId);
            if (productVariant != null && productVariant.CurrencyId.HasValue && (productVariant.CurrencyId.Value != _currencySettings.PrimaryStoreCurrencyId))
            {
                currency = _currencyService.GetCurrencyById(productVariant.CurrencyId.Value);
            }

            StatefulStorage.PerSession.Add <bool?>("SkipQuoteDiscountActivationCheck", () => (bool?)true);
            decimal taxRate      = decimal.Zero;
            var     customer     = _customerService.GetCustomerById(customerProductVariantQuote.CustomerId);
            decimal oldPriceBase = _taxService.GetProductPrice(productVariant, productVariant.OldPrice, out taxRate);
            decimal finalPriceWithoutDiscountBase = _taxService.GetProductPrice(productVariant, _priceCalculationService.GetFinalPrice(productVariant, customer, false, false), out taxRate);
            decimal finalPriceWithDiscountBase    = _taxService.GetProductPrice(productVariant, _priceCalculationService.GetFinalPrice(productVariant, customer, true, false), out taxRate);

            decimal oldPrice = _currencyService.ConvertFromPrimaryStoreCurrency(oldPriceBase, currency);
            decimal finalPriceWithoutDiscount = _currencyService.ConvertFromPrimaryStoreCurrency(finalPriceWithoutDiscountBase, currency);
            decimal finalPriceWithDiscount    = _currencyService.ConvertFromPrimaryStoreCurrency(finalPriceWithDiscountBase, currency);

            string productVariantPriceOldPrice = String.Empty;
            if (finalPriceWithoutDiscountBase != oldPriceBase && oldPriceBase > decimal.Zero)
            {
                productVariantPriceOldPrice = _priceFormatter.FormatPrice(oldPrice, false, false);
            }
            string  productVariantPrice = _priceFormatter.FormatPrice(finalPriceWithoutDiscount, false, currency);
            string  productVariantPricePriceWithDiscount = "", productVariantPriceDiscountPrice = "", productVariantDiscountPercentage = "", productVariantPriceCurrency = "";
            decimal productVariantPricediscountValueBase = 0, productVariantPriceDiscountValue = 0, productVariantPriceValue = 0, productVariantPricePriceWithDiscountValue = 0;

            if (finalPriceWithoutDiscountBase != finalPriceWithDiscountBase)
            {
                IList <Discount> discounts = null;
                productVariantPricePriceWithDiscount = _priceFormatter.FormatPrice(finalPriceWithDiscount, false, currency);
                productVariantPricediscountValueBase = _priceCalculationService.GetDiscountAmount(productVariant, customer, 0, out discounts, false);
                productVariantPriceDiscountValue     = _currencyService.ConvertFromPrimaryStoreCurrency(productVariantPricediscountValueBase, _workContext.WorkingCurrency);
                productVariantPriceDiscountPrice     = _priceFormatter.FormatPrice(productVariantPricediscountValueBase, true, false);

                if (_workContext.WorkingLanguage.DisplayOrder == 2)
                {
                    productVariantDiscountPercentage = String.Format("({0}%)", ((int)discounts.First().DiscountPercentage).ToString());
                }
                else
                {
                    productVariantDiscountPercentage = String.Format("(%{0})", ((int)discounts.First().DiscountPercentage).ToString());
                }
            }
            productVariantPriceValue = finalPriceWithoutDiscount;
            productVariantPricePriceWithDiscountValue = finalPriceWithDiscount;
            productVariantPriceCurrency = CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol;
            //////

            StatefulStorage.PerSession.Remove <bool?>("SkipQuoteDiscountActivationCheck");

            var pvpModel = new ProductVariantQuoteModel.ProductVariantPriceModel()
            {
                Price                  = productVariantPrice,
                PriceWithDiscount      = productVariantPricePriceWithDiscount,
                DiscountValue          = productVariantPriceDiscountValue,
                DiscountPrice          = productVariantPriceDiscountPrice,
                DiscountPercentage     = productVariantDiscountPercentage,
                PriceValue             = productVariantPriceValue,
                PriceWithDiscountValue = productVariantPricePriceWithDiscountValue,
                Currency               = productVariantPriceCurrency,
                OldPrice               = productVariantPriceOldPrice,
            };


            #endregion

            string name  = customerProductVariantQuote.Name;
            string phoneNumber = customerProductVariantQuote.PhoneNumber;
            try
            {
                Customer cust = _customerService.GetCustomerById(customerProductVariantQuote.CustomerId);
                if (cust != null)
                {
                    if (!cust.IsGuest())
                    {
                        name = cust.GetFullName();
                        if (cust.Addresses.Count() > 0)
                        {
                            phoneNumber = cust.Addresses.FirstOrDefault().PhoneNumber;
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
            return(new ProductVariantQuoteModel()
            {
                Id = customerProductVariantQuote.Id,
                ProductVariantId = customerProductVariantQuote.ProductVariantId,
                Email = customerProductVariantQuote.Email,
                PhoneNumber = phoneNumber,
                Enquiry = customerProductVariantQuote.Enquiry,
                Name = name,
                ManufacturerName = _productService.GetProductById(customerProductVariantQuote.ProductVariant.ProductId).GetDefaultManufacturer().Name,
                ProductName = productName,
                Sku = productVariant.Sku,
                ProductVariantPrice = pvpModel,
                Description = customerProductVariantQuote.Description,
                RequestDate = _dateTimeHelper.ConvertToUserTime(customerProductVariantQuote.RequestDate, DateTimeKind.Utc),
                PriceWithDiscount = customerProductVariantQuote.PriceWithDiscount,
                PriceWithoutDiscount = customerProductVariantQuote.PriceWithoutDiscount,
                DiscountPercentage = customerProductVariantQuote.DiscountPercentage,
                ActivateDate = customerProductVariantQuote.ActivateDate,
            });
        }
        public void Can_get_product_discount()
        {
            var product = new Product
            {
                Id    = 1,
                Name  = "Product name 1",
                Price = 12.34M,
                CustomerEntersPrice = false,
                Published           = true,
            };

            //customer
            Customer customer = null;

            //discounts
            var discount1 = new Discount()
            {
                Id                 = 1,
                Name               = "Discount 1",
                DiscountType       = DiscountType.AssignedToSkus,
                DiscountAmount     = 3,
                DiscountLimitation = DiscountLimitationType.Unlimited
            };

            discount1.AppliedToProducts.Add(product);
            product.AppliedDiscounts.Add(discount1);
            //set HasDiscountsApplied property
            product.HasDiscountsApplied = true;
            _discountService.Expect(ds => ds.IsDiscountValid(discount1, customer)).Return(true);
            _discountService.Expect(ds => ds.GetAllDiscounts(DiscountType.AssignedToCategories)).Return(new List <Discount>());

            var discount2 = new Discount()
            {
                Id                 = 2,
                Name               = "Discount 2",
                DiscountType       = DiscountType.AssignedToSkus,
                DiscountAmount     = 4,
                DiscountLimitation = DiscountLimitationType.Unlimited
            };

            discount2.AppliedToProducts.Add(product);
            product.AppliedDiscounts.Add(discount2);
            _discountService.Expect(ds => ds.IsDiscountValid(discount2, customer)).Return(true);

            var discount3 = new Discount()
            {
                Id                 = 3,
                Name               = "Discount 3",
                DiscountType       = DiscountType.AssignedToOrderSubTotal,
                DiscountAmount     = 5,
                DiscountLimitation = DiscountLimitationType.Unlimited,
                RequiresCouponCode = true,
                CouponCode         = "SECRET CODE"
            };

            discount3.AppliedToProducts.Add(product);
            product.AppliedDiscounts.Add(discount3);
            //discount is not valid
            _discountService.Expect(ds => ds.IsDiscountValid(discount3, customer)).Return(false);


            Discount appliedDiscount;

            _priceCalcService.GetDiscountAmount(product, customer, 0, 1, out appliedDiscount).ShouldEqual(4);
            appliedDiscount.ShouldNotBeNull();
            appliedDiscount.ShouldEqual(discount2);
        }