Пример #1
0
        public virtual async Task <IHttpActionResult> CalculatePrices(CalculatePricesRequest request)
        {
            if (request == null)
            {
                return(BadRequest("No request found."));
            }

            var vm = await ProductPriceViewService.CalculatePricesAsync(
                new GetProductsPriceParam
            {
                ProductIds  = request.Products.ToList(),
                CultureInfo = ComposerContext.CultureInfo,
                Scope       = ComposerContext.Scope,
            }
                );

            return(Ok(vm));
        }
        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(Tuple.Create(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, param.CultureInfo);

                vm.Total = LocalizationProvider.FormatPrice((decimal)vm.Quantity * price, param.CultureInfo);
            }

            //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);
        }