Exemple #1
0
        PrepareProductOverviewPriceModelAsync(
            Product product,
            bool forceRedirectionAfterAddingToCart = false
            )
        {
            var model = await base.PrepareProductOverviewPriceModelAsync(product, forceRedirectionAfterAddingToCart);

            var newPrice = await _abcMattressListingPriceService.GetListingPriceForMattressProductAsync(
                product.Id
                );

            if (newPrice != null)
            {
                model.Price = (await _priceFormatter.FormatPriceAsync(newPrice.Value)).Replace(".00", "");
            }

            return(model);
        }
        /*
         * Because of the way AJAX loading of products works, we should always
         * pass in a View instead of an EmptyResult. This ensures the CSS for
         * the modal is always loaded.
         */
        public async Task <IViewComponentResult> InvokeAsync(
            string widgetZone,
            object additionalData = null
            )
        {
            const string productListingCshtml =
                "~/Plugins/Widgets.AbcSynchronyPayments/Views/ProductListing.cshtml";
            SynchronyPaymentModel model = null;

            var productId = GetProductId(additionalData);

            if (productId == -1)
            {
                await _logger.ErrorAsync(
                    "Incorrect data model passed to ABC Warehouse " +
                    "Synchrony Payments widget.");

                return(View(productListingCshtml, model));
            }

            var productAbcDescription = await _productAbcDescriptionService.GetProductAbcDescriptionByProductIdAsync(productId);

            var abcItemNumber = productAbcDescription?.AbcItemNumber;

            // also allow getting info from generic attribute
            var productGenericAttributes = await _genericAttributeService.GetAttributesForEntityAsync(productId, "Product");

            var monthsGenericAttribute = productGenericAttributes.Where(ga => ga.Key == "SynchronyPaymentMonths")
                                         .FirstOrDefault();

            if (abcItemNumber == null && monthsGenericAttribute == null)
            {
                // No ABC Item number (or no months indicator), skip processing
                return(View(productListingCshtml, model));
            }

            var productAbcFinance =
                await _productAbcFinanceService.GetProductAbcFinanceByAbcItemNumberAsync(
                    abcItemNumber
                    );

            if (productAbcFinance == null && monthsGenericAttribute == null)
            {
                // No financing information
                return(View(productListingCshtml, model));
            }

            // generic attribute data
            var isMinimumGenericAttribute = productGenericAttributes.Where(ga => ga.Key == "SynchronyPaymentIsMinimum")
                                            .FirstOrDefault();
            var offerValidFromGenericAttribute = productGenericAttributes.Where(ga => ga.Key == "SynchronyPaymentOfferValidFrom")
                                                 .FirstOrDefault();
            var offerValidToGenericAttribute = productGenericAttributes.Where(ga => ga.Key == "SynchronyPaymentOfferValidTo")
                                               .FirstOrDefault();

            var product = await _productService.GetProductByIdAsync(productId);

            var months = productAbcFinance != null ?
                         productAbcFinance.Months :
                         int.Parse(monthsGenericAttribute.Value);
            var isMinimumPayment = productAbcFinance != null ?
                                   productAbcFinance.IsDeferredPricing :
                                   bool.Parse(isMinimumGenericAttribute.Value);
            var offerValidFrom = productAbcFinance != null ?
                                 productAbcFinance.StartDate.Value :
                                 DateTime.Parse(offerValidFromGenericAttribute.Value);
            var offerValidTo = productAbcFinance != null ?
                               productAbcFinance.EndDate.Value :
                               DateTime.Parse(offerValidToGenericAttribute.Value);

            var price = await _abcMattressListingPriceService.GetListingPriceForMattressProductAsync(productId) ?? product.Price;

            var isMattressProduct = _abcMattressProductService.IsMattressProduct(productId);

            model = new SynchronyPaymentModel
            {
                MonthCount     = months,
                MonthlyPayment = CalculatePayment(
                    price, isMinimumPayment, months
                    ),
                ProductId             = productId,
                ApplyUrl              = await GetApplyUrlAsync(),
                IsMonthlyPaymentStyle = !isMinimumPayment,
                EqualPayment          = CalculateEqualPayments(
                    price, months
                    ),
                ModalHexColor  = await HtmlHelpers.GetPavilionPrimaryColorAsync(),
                StoreName      = (await _storeContext.GetCurrentStoreAsync()).Name,
                ImageUrl       = await GetImageUrlAsync(),
                OfferValidFrom = offerValidFrom.ToShortDateString(),
                OfferValidTo   = offerValidTo.ToShortDateString()
            };

            model.FullPrice    = price;
            model.FinalPayment = model.FullPrice -
                                 (model.MonthlyPayment * (model.MonthCount - 1));
            model.IsHidden = isMattressProduct && price < 697.00M;

            if (model.MonthlyPayment == 0)
            {
                await _logger.WarningAsync(
                    $"ABC Product #{productAbcFinance.AbcItemNumber} has a " +
                    "$0 monthly fee, likely not marked with a correct " +
                    "payment type.");

                return(View(productListingCshtml, model));
            }

            switch (widgetZone)
            {
            case "productbox_addinfo_middle":
                return(View(productListingCshtml, model));

            case CustomPublicWidgetZones.ProductDetailsAfterPrice:
                return(View("~/Plugins/Widgets.AbcSynchronyPayments/Views/ProductDetail.cshtml", model));
            }

            await _logger.WarningAsync(
                "ABC Synchrony Payments: Did not match with any passed " +
                "widgets, skipping display");

            return(View(productListingCshtml, model));
        }