Exemple #1
0
        public async virtual Task <bool> MapRecurringOrderFrequencies(LineItemDetailViewModel vm, LineItem lineItem, CultureInfo cultureInfo)
        {
            if (lineItem == null)
            {
                return(false);
            }

            var scope = ComposerContext.Scope;
            var recurringProgramName = lineItem.RecurringOrderProgramName;

            if (string.IsNullOrEmpty(recurringProgramName) || !RecurringOrdersSettings.Enabled)
            {
                return(false);
            }

            var program = await RecurringOrderRepository.GetRecurringOrderProgram(scope, recurringProgramName).ConfigureAwait(false);

            vm.RecurringOrderFrequencyDisplayName = GetRecurringOrderFrequencyDisplayName(program, lineItem, cultureInfo);

            var programViewModel = RecurringOrderProgramViewModelFactory.CreateRecurringOrderProgramViewModel(program, cultureInfo);

            vm.RecurringOrderProgramFrequencies = programViewModel?.Frequencies;

            return(true);
        }
        public virtual async Task <RecurringOrderTemplateLineItemViewModel> MapToTemplateLineItemViewModel(MapToTemplateLineItemViewModelParam param)
        {
            if (param == null)
            {
                throw new ArgumentNullException(nameof(param));
            }
            if (param.RecurringOrderlineItem == null)
            {
                throw new ArgumentException(GetMessageOfNull(nameof(param.RecurringOrderlineItem)), nameof(param));
            }
            if (param.CultureInfo == null)
            {
                throw new ArgumentException(GetMessageOfNull(nameof(param.CultureInfo)), nameof(param));
            }
            if (param.BaseUrl == null)
            {
                throw new ArgumentException(GetMessageOfNull(nameof(param.BaseUrl)), nameof(param));
            }
            if (param.RecurringScheduleUrl == null)
            {
                throw new ArgumentException(GetMessageOfNull(nameof(param.RecurringScheduleUrl)), nameof(param));
            }

            var recrurringLineItem = param.RecurringOrderlineItem;

            var vm = ViewModelMapper.MapTo <RecurringOrderTemplateLineItemViewModel>(recrurringLineItem, param.CultureInfo);

            if (vm.IsValid == null)
            {
                vm.IsValid = true;
            }

            if (param.ImageDictionnary.TryGetValue((recrurringLineItem.ProductId, recrurringLineItem.VariantId), out ProductMainImage mainImage))
            {
                vm.ImageUrl         = mainImage.ImageUrl;
                vm.FallbackImageUrl = mainImage.FallbackImageUrl;
            }

            var getProductRequest = new Overture.ServiceModel.Requests.Products.GetProductRequest
            {
                ProductId            = recrurringLineItem.ProductId,
                ScopeId              = recrurringLineItem.ScopeId,
                CultureName          = param.CultureInfo.Name,
                IncludePriceLists    = true,
                IncludeRelationships = false,
                IncludeVariants      = true
            };

            var getProductResponse = await OvertureClient.SendAsync(getProductRequest).ConfigureAwait(false);

            if (getProductResponse == null || (getProductResponse != null && recrurringLineItem.VariantId != string.Empty &&
                                               recrurringLineItem.VariantId != null &&
                                               getProductResponse.Variants.SingleOrDefault(v => v.Id == recrurringLineItem.VariantId) == null))
            {
                var deleteRecurringLineItem = new DeleteRecurringOrderLineItemsRequest
                {
                    CustomerId = recrurringLineItem.CustomerId,
                    RecurringOrderLineItemIds = new List <Guid> {
                        recrurringLineItem.RecurringOrderLineItemId
                    },
                    ScopeId = recrurringLineItem.ScopeId
                };
                await OvertureClient.SendAsync(deleteRecurringLineItem);

                return(await Task.FromResult <RecurringOrderTemplateLineItemViewModel>(null));
            }

            var variant = getProductResponse.Variants.SingleOrDefault(v => v.Id == recrurringLineItem.VariantId);

            vm.FormattedNextOccurence = vm.NextOccurence == DateTime.MinValue
                    ? string.Empty
                    : string.Format(param.CultureInfo, "{0:D}", vm.NextOccurence);

            vm.NextOccurenceValue = vm.NextOccurence == DateTime.MinValue
                    ? string.Empty
                    : vm.NextOccurence.ToString("yyyy/MM/dd", CultureInfo.InvariantCulture);

            vm.Id             = recrurringLineItem.RecurringOrderLineItemId;
            vm.ProductSummary = new RecurringProductSummaryViewModel
            {
                DisplayName = ProductHelper.GetProductOrVariantDisplayName(getProductResponse, variant, param.CultureInfo)
            };

            var productsPricesVm = await ProductPriceViewService.CalculatePricesAsync(new GetProductsPriceParam
            {
                CultureInfo = param.CultureInfo,
                Scope       = recrurringLineItem.ScopeId,
                ProductIds  = new List <string>()
                {
                    recrurringLineItem.ProductId
                }
            });

            var productPriceVm = productsPricesVm.ProductPrices.SingleOrDefault(p => p.ProductId == recrurringLineItem.ProductId);

            if (productPriceVm != null)
            {
                var variantPriceVm = productPriceVm.VariantPrices.SingleOrDefault(v => v.VariantId == recrurringLineItem.VariantId);
                if (variantPriceVm != null)
                {
                    vm.DefaultListPrice = variantPriceVm.DefaultListPrice;
                    vm.ListPrice        = variantPriceVm.ListPrice;
                }
                else
                {
                    vm.DefaultListPrice = productPriceVm.DefaultListPrice;
                    vm.ListPrice        = productPriceVm.ListPrice;
                }
            }
            vm.IsOnSale = string.CompareOrdinal(vm.DefaultListPrice, vm.ListPrice) != 0;

            var conv = decimal.TryParse(vm.ListPrice, NumberStyles.Currency, param.CultureInfo.NumberFormat, out decimal price);

            if (conv)
            {
                vm.TotalWithoutDiscount = LocalizationProvider.FormatPrice((decimal)vm.Quantity * price, CurrencyProvider.GetCurrency());

                vm.Total = LocalizationProvider.FormatPrice((decimal)vm.Quantity * price, CurrencyProvider.GetCurrency());
            }

            //Adding brand display name
            var brandLookup = await OvertureClient.SendAsync(new GetProductLookupRequest { LookupName = "Brand" });

            var brandId = getProductResponse.Brand;

            if (brandId != null)
            {
                string brandValue = Convert.ToString(brandId);
                vm.ProductSummary.Brand = brandLookup?.GetDisplayName(brandValue, param.CultureInfo.Name) ?? brandId;
            }

            var list = await ProductHelper.GetKeyVariantAttributes(getProductResponse, variant, param.CultureInfo, OvertureClient);

            if (list != null && list.Count > 0)
            {
                vm.KeyVariantAttributesList = list.ToList();
            }

            vm.ShippingMethodName = recrurringLineItem.FulfillmentMethodName;

            vm.ProductUrl = ProductUrlProvider.GetProductUrl(new GetProductUrlParam
            {
                CultureInfo = param.CultureInfo,
                VariantId   = recrurringLineItem.VariantId,
                ProductId   = recrurringLineItem.ProductId,
                ProductName = vm.ProductSummary.DisplayName,
                SKU         = recrurringLineItem.Sku
            });

            var recurringScheduleEditUrl = RecurringScheduleUrlProvider.GetRecurringScheduleDetailsUrl(new GetRecurringScheduleDetailsUrlParam
            {
                CultureInfo         = param.CultureInfo,
                RecurringScheduleId = vm.Id.ToString()
            });

            vm.EditUrl     = recurringScheduleEditUrl;
            vm.ScheduleUrl = param.RecurringScheduleUrl;

            var program = await RecurringOrderRepository.GetRecurringOrderProgram(recrurringLineItem.ScopeId, recrurringLineItem.RecurringOrderProgramName);

            var programViewModel = RecurringOrderProgramViewModelFactory.CreateRecurringOrderProgramViewModel(program, param.CultureInfo);

            vm.RecurringOrderProgramFrequencies = programViewModel?.Frequencies;

            return(vm);
        }