예제 #1
0
        public async Task <(int?, int?, int?)> GetFirstOptionFromAttributeAsync(int catalogItemId)
        {
            var options = (default(int?), default(int?), default(int?));

            var spec    = new CatalogTypeFilterSpecification(catalogItemId);
            var product = await _itemRepository.GetBySpecAsync(spec);

            var group = product.Attributes.GroupBy(x => x.Type);

            foreach (var attribute in group)
            {
                if (!options.Item1.HasValue)
                {
                    options.Item1 = attribute.First().Id;
                }
                else if (!options.Item2.HasValue)
                {
                    options.Item2 = attribute.First().Id;
                }
                else if (!options.Item3.HasValue)
                {
                    options.Item3 = attribute.First().Id;
                }
            }

            return(options);
        }
예제 #2
0
        public async Task <DeliveryTimeDTO> CalculateDeliveryTime(int basketId) //TODO: Create unit test
        {
            var basketSpec = new BasketWithItemsSpecification(basketId);
            var basket     = (await _basketRepository.ListAsync(basketSpec)).SingleOrDefault();

            if (basket.Items.Any(x => x.CatalogTypeId.HasValue))
            {
                return(null);
            }
            DeliveryTimeDTO deliveryTime = new DeliveryTimeDTO
            {
                Min  = 2,
                Max  = 3,
                Unit = DeliveryTimeUnitType.Days
            };

            foreach (var item in basket.Items)
            {
                var spec    = new CatalogTypeFilterSpecification(item.CatalogItemId);
                var product = await _itemRepository.GetBySpecAsync(spec);

                if (product.CatalogType.DeliveryTimeUnit > deliveryTime.Unit)
                {
                    deliveryTime.Min  = product.CatalogType.DeliveryTimeMin;
                    deliveryTime.Max  = product.CatalogType.DeliveryTimeMax;
                    deliveryTime.Unit = product.CatalogType.DeliveryTimeUnit;
                }
                else if (product.CatalogType.DeliveryTimeUnit == deliveryTime.Unit)
                {
                    deliveryTime.Min = product.CatalogType.DeliveryTimeMin > deliveryTime.Min ? product.CatalogType.DeliveryTimeMin : deliveryTime.Min;
                    deliveryTime.Max = product.CatalogType.DeliveryTimeMax > deliveryTime.Max ? product.CatalogType.DeliveryTimeMax : deliveryTime.Max;
                }
            }
            return(deliveryTime);
        }
예제 #3
0
        private async Task <(List <BasketItemViewModel> Items, decimal ShippingCost)> GetBasketItemsAndShipping(IReadOnlyCollection <BasketItem> basketItems)
        {
            var     items              = new List <BasketItemViewModel>();
            decimal shippingCost       = _settings.DefaultShippingCost;
            int     totalWeight        = 0;
            bool    allItemsWithWeight = true;

            foreach (var item in basketItems)
            {
                var itemModel = new BasketItemViewModel()
                {
                    Id            = item.Id,
                    UnitPrice     = item.UnitPrice,
                    Quantity      = item.Quantity,
                    CatalogItemId = item.CatalogItemId,
                    CustomizeName = item.CustomizeName
                };

                if (item.CatalogItemId != 0)
                {
                    var spec        = new CatalogTypeFilterSpecification(item.CatalogItemId);
                    var catalogItem = await _itemRepository.GetBySpecAsync(spec);

                    if (catalogItem != null)
                    {
                        itemModel.PictureUrl   = _uriComposer.ComposePicUri(catalogItem.PictureUri);
                        itemModel.ProductName  = GetProductPrincipalName(catalogItem.Name);
                        itemModel.ProductName2 = GetProductSecondaryName(catalogItem.Name);
                        itemModel.Sku          = catalogItem.Sku;
                        itemModel.Slug         = catalogItem.Slug;

                        foreach (var attr in catalogItem.Attributes)
                        {
                            if ((item.CatalogAttribute1.HasValue && item.CatalogAttribute1 == attr.Id) ||
                                (item.CatalogAttribute2.HasValue && item.CatalogAttribute2 == attr.Id) ||
                                (item.CatalogAttribute3.HasValue && item.CatalogAttribute3 == attr.Id))
                            {
                                itemModel.Attributes.Add(new AttributeViewModel
                                {
                                    Name  = attr.Name,
                                    Label = EnumHelper <AttributeType> .GetDisplayValue(attr.Type)
                                });
                            }
                        }
                        if (!catalogItem.CatalogType.Weight.HasValue || catalogItem.CatalogType.Weight == 0)
                        {
                            allItemsWithWeight = false;
                        }
                        else
                        {
                            totalWeight += catalogItem.CatalogType.Weight.Value * item.Quantity;
                        }
                        //if (catalogItem != null && catalogItem.CatalogType.ShippingCost > shippingCost)
                        //    shippingCost = catalogItem.CatalogType.ShippingCost;
                    }
                }
                else if (item.CatalogTypeId.HasValue)
                {
                    itemModel.IsFromCustomize = true;
                    var typeEntity = await _typeRepository.GetByIdAsync(item.CatalogTypeId.Value);

                    if (typeEntity != null)
                    {
                        itemModel.PictureUrl  = typeEntity.PictureUri;
                        itemModel.ProductName = $"Personalização {typeEntity.Name}";
                    }
                }

                items.Add(itemModel);
            }
            if (allItemsWithWeight)
            {
                shippingCost = await CalculateShippingCostAsync(totalWeight);
            }
            return(items, shippingCost);
        }