Exemplo n.º 1
0
        private async Task SyncSynchronyPaymentsDataAsync(Product product, AbcMattressModel model)
        {
            var entries  = _abcMattressEntryService.GetAbcMattressEntriesByModelId(model.Id);
            var packages = _abcMattressPackageService.GetAbcMattressPackagesByEntryIds(entries.Select(e => e.Id));

            var itemNos = entries.Select(e => e.ItemNo).Union(packages.Select(p => p.ItemNo));

            int?     months           = null;
            bool?    isMinimumPayment = null;
            DateTime?startDate        = null;
            DateTime?endDate          = null;

            foreach (var itemNo in itemNos)
            {
                var productAbcFinance = await _productAbcFinanceService.GetProductAbcFinanceByAbcItemNumberAsync(itemNo);

                if (productAbcFinance == null)
                {
                    continue;
                }

                months           = productAbcFinance.Months;
                isMinimumPayment = productAbcFinance.IsDeferredPricing;
                startDate        = productAbcFinance.StartDate.Value;
                endDate          = productAbcFinance.EndDate.Value;
            }

            await _genericAttributeService.SaveAttributeAsync <int?>(
                product,
                "SynchronyPaymentMonths",
                months
                );

            await _genericAttributeService.SaveAttributeAsync <bool?>(
                product,
                "SynchronyPaymentIsMinimum",
                isMinimumPayment
                );

            await _genericAttributeService.SaveAttributeAsync <DateTime?>(
                product,
                "SynchronyPaymentOfferValidFrom",
                startDate
                );

            await _genericAttributeService.SaveAttributeAsync <DateTime?>(
                product,
                "SynchronyPaymentOfferValidTo",
                endDate
                );
        }
        /*
         * 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));
        }