Exemple #1
0
        public IActionResult Index(string orderGuid)
        {
            var order = _orderService.GetByGuid(orderGuid);

            if (order == null || order.UserId != CurrentUser.Id)
            {
                return(NotFound());
            }
            var r     = R;
            var model = _orderModelFactory.Create(order);

            var canCancel = CanCancelOrder(order);
            var canReturn = CanReturnOrder(order, out _, out var lastReturnDate);

            r.With("canCancel", canCancel);
            r.With("canReturn", canReturn);
            if (canReturn)
            {
                r.With("lastReturnDate", lastReturnDate);
            }

            //are there any active return requests
            var returnRequests      = _returnRequestService.GetOrderReturnRequests(order.Id);
            var returnRequestModels = returnRequests.Select(_requestModelFactory.Create).ToList();

            //downloads
            if (order.OrderItems?.Any(x => x.IsDownloadable) ?? false)
            {
                var downloadableOrderItems = order.OrderItems.Where(x => x.IsDownloadable).ToList();
                var productIds             = downloadableOrderItems.Select(x => x.ProductId).ToList();
                var downloads = _downloadService.GetWithoutBytes(x => productIds.Contains(x.ProductId) && x.Published).ToList();
                if (downloads.Any())
                {
                    var downloadIds    = downloads.Select(x => x.Id).ToList();
                    var itemDownloads  = _orderItemDownloadService.Get(x => downloadIds.Contains(x.DownloadId));
                    var downloadModels = new List <DownloadModel>();
                    foreach (var itemDownload in itemDownloads)
                    {
                        var download = downloads.FirstOrDefault(x => x.Id == itemDownload.DownloadId);
                        if (download == null)
                        {
                            continue;//this should not be hit unless somebody has messed up with database and deleted the data manually
                        }
                        var downloadModel = _productModelFactory.Create(download);
                        downloadModel.Active = itemDownload.Active;
                        downloadModels.Add(downloadModel);
                    }

                    r.With("canDownload", downloadModels.Any());
                    r.With("downloads", downloadModels.OrderBy(x => x.DisplayOrder).ToList());
                }
            }
            //set breadcrumb nodes
            SetBreadcrumbToRoute("Account", RouteNames.AccountProfile);
            SetBreadcrumbToRoute("Orders", RouteNames.AccountOrders);
            SetBreadcrumbToRoute(order.OrderNumber, RouteNames.SingleOrder, new { orderGuid }, localize: false);

            return(r.Success.With("order", model).With("returnRequests", returnRequestModels).With("canDownloadInvoice", CanDownloadInvoice(order)).Result);
        }
        public ReviewModel Create(Review review)
        {
            var model = _modelMapper.Map <ReviewModel>(review);

            model.DisplayName = review.Private ? _catalogSettings.DisplayNameForPrivateReviews : review.User?.Name;
            if (model.DisplayName.IsNullEmptyOrWhiteSpace())
            {
                model.DisplayName =
                    LocalizationHelper.Localize("Store Customer", ApplicationEngine.CurrentLanguageCultureCode);
            }

            if (review.Product != null)
            {
                model.Product = _productModelFactory.Create(review.Product);
            }
            return(model);
        }
Exemple #3
0
        public override IViewComponentResult Invoke(object data = null)
        {
            var dataAsDict = data as Dictionary <string, object>;

            if (dataAsDict == null)
            {
                return(R.Fail.ComponentResult);
            }

            dataAsDict.TryGetValue("productId", out var productIdObj);
            if (productIdObj == null || !int.TryParse(productIdObj.ToString(), out var productId))
            {
                return(R.Fail.ComponentResult);
            }

            //get the product
            var product = _productService.Get(productId);

            if (product == null)
            {
                return(R.Fail.ComponentResult);
            }
            if (!product.IsPublic(CurrentStore.Id) && !CurrentUser.Can(CapabilitySystemNames.EditProduct))
            {
                return(R.Fail.ComponentResult);
            }
            var productModel = _productModelFactory.Create(product);

            var response = R.Success;

            response.With("product", productModel);

            if (product.HasVariants)
            {
                //any variants
                var variants      = _productVariantService.GetByProductId(product.Id);
                var variantModels = new List <object>();
                foreach (var variant in variants)
                {
                    _priceAccountant.GetProductPriceDetails(product, null, variant.Price, out decimal priceWithoutTax, out decimal tax, out decimal taxRate, out _);
                    var variantObject = new
                    {
                        attributes = new Dictionary <string, string>(),
                        price      = _priceAccountant
                                     .ConvertCurrency(
                            (_taxSettings.DisplayProductPricesWithoutTax ? priceWithoutTax : priceWithoutTax + tax),
                            ApplicationEngine.CurrentCurrency).ToCurrency(),
                        isAvailable = !variant.TrackInventory ||
                                      (variant.TrackInventory && variant.IsAvailableInStock(product)),
                        sku  = !variant.Sku.IsNullEmptyOrWhiteSpace() ? variant.Sku : product.Sku,
                        gtin = !variant.Gtin.IsNullEmptyOrWhiteSpace() ? variant.Gtin : product.Gtin,
                        mpn  = !variant.Mpn.IsNullEmptyOrWhiteSpace() ? variant.Mpn : product.Mpn
                    };
                    foreach (var pva in variant.ProductVariantAttributes)
                    {
                        variantObject.attributes.Add(pva.ProductAttribute.Label, pva.ProductAttributeValue.AvailableAttributeValue.Value);
                    }
                    variantModels.Add(variantObject);
                }

                if (variantModels.Any())
                {
                    response.With("variants", variantModels);
                }
                productModel.IsAvailable = variantModels.Any(x => (bool)((dynamic)x).isAvailable);
            }

            return(response.ComponentResult);
        }
Exemple #4
0
        public IActionResult Index(int id)
        {
            if (id < 1)
            {
                return(NotFound());
            }
            var product = _productService.Get(id);

            if (!product.IsPublic() && !CurrentUser.Can(CapabilitySystemNames.EditProduct))
            {
                return(NotFound());
            }
            //is the product restricted to roles
            if (product.RestrictedToRoles)
            {
                var userRoleIds = CurrentUser?.Roles?.Select(x => x.Id).ToList();
                if (userRoleIds == null || product.EntityRoles.All(x => !userRoleIds.Contains(x.RoleId)))
                {
                    return(NotFound());
                }
            }
            var productModel = _productModelFactory.Create(product);

            var response = R.Success;

            response.With("product", productModel);

            if (_catalogSettings.EnableRelatedProducts)
            {
                var productRelations = _productRelationService.GetByProductId(id, ProductRelationType.RelatedProduct, 1,
                                                                              _catalogSettings.NumberOfRelatedProducts);
                if (productRelations.Any())
                {
                    var relatedProductsModel = productRelations.Select(x => x.DestinationProduct).Select(_productModelFactory.Create).ToList();
                    response.With("relatedProducts", relatedProductsModel);
                }
            }

            if (product.HasVariants)
            {
                //any variants
                var variants      = _productVariantService.GetByProductId(product.Id);
                var variantModels = new List <object>();
                foreach (var variant in variants)
                {
                    _priceAccountant.GetProductPriceDetails(product, null, variant.Price, out decimal priceWithoutTax, out decimal tax, out decimal taxRate, out _);
                    var variantObject = new
                    {
                        attributes = new Dictionary <string, string>(),
                        price      = _priceAccountant
                                     .ConvertCurrency(
                            (_taxSettings.DisplayProductPricesWithoutTax ? priceWithoutTax : priceWithoutTax + tax),
                            ApplicationEngine.CurrentCurrency).ToCurrency(),
                        isAvailable = !variant.TrackInventory ||
                                      (variant.TrackInventory && variant.IsAvailableInStock(product)),
                        sku  = !variant.Sku.IsNullEmptyOrWhiteSpace() ? variant.Sku : product.Sku,
                        gtin = !variant.Gtin.IsNullEmptyOrWhiteSpace() ? variant.Gtin : product.Gtin,
                        mpn  = !variant.Mpn.IsNullEmptyOrWhiteSpace() ? variant.Mpn : product.Mpn
                    };
                    foreach (var pva in variant.ProductVariantAttributes)
                    {
                        variantObject.attributes.Add(pva.ProductAttribute.Label, pva.ProductAttributeValue.AvailableAttributeValue.Value);
                    }
                    variantModels.Add(variantObject);
                }

                if (variantModels.Any())
                {
                    response.With("variants", variantModels);
                }
                productModel.IsAvailable = variantModels.Any(x => (bool)((dynamic)x).isAvailable);
            }

            //downloads
            if (product.IsDownloadable)
            {
                var downloads = _downloadService.GetWithoutBytes(x => x.ProductId == product.Id && !x.RequirePurchase).ToList();
                if (CurrentUser.IsVisitor())
                {
                    downloads = downloads.Where(x => !x.RequireLogin).ToList();
                }

                var downloadModels = downloads.Select(_productModelFactory.Create).ToList();
                response.With("downloads", downloadModels);
            }
            //reviews
            if (_catalogSettings.EnableReviews)
            {
                var reviews = _reviewService.Get(x => x.ProductId == product.Id, 1,
                                                 _catalogSettings.NumberOfReviewsToDisplayOnProductPage).ToList();

                if (reviews.Any())
                {
                    var reviewModels = reviews.Select(x =>
                    {
                        var model         = _modelMapper.Map <ReviewModel>(x);
                        model.DisplayName = x.Private ? _catalogSettings.DisplayNameForPrivateReviews : x.User?.Name;
                        if (model.DisplayName.IsNullEmptyOrWhiteSpace())
                        {
                            model.DisplayName = T("Store Customer");
                        }
                        return(model);
                    }).ToList();
                    response.With("reviews", reviewModels);
                }
            }

            //breadcrumbs
            if (_generalSettings.EnableBreadcrumbs)
            {
                var categoryTree        = _categoryService.GetFullCategoryTree();
                var category            = product.Categories?.FirstOrDefault();
                var currentCategoryFull = categoryTree.FirstOrDefault(x => x.Id == category?.Id);
                BreadcrumbHelper.SetCategoryBreadcrumb(currentCategoryFull, categoryTree);
                SetBreadcrumbToRoute(product.Name, RouteNames.SingleProduct, new { seName = product.SeoMeta.Slug }, localize: false);
            }
            //seo data
            SeoMetaHelper.SetSeoData(product.Name, product.Description, product.Description);
            return(response.With("preview", !product.Published).Result);
        }
        public IActionResult ReviewEditor(int productId, int reviewId)
        {
            //check if the product is valid
            var product = _productService.Get(productId);

            if (!product.IsPublic(CurrentStore.Id))
            {
                return(NotFound());
            }
            var currentUser = ApplicationEngine.CurrentUser;
            var review      = reviewId > 0 ? _reviewService.Get(reviewId) : new Review()
            {
                ProductId = productId,
                UserId    = currentUser.Id
            };

            if (review == null || review.ProductId != productId)
            {
                return(NotFound());
            }
            if (review.UserId != ApplicationEngine.CurrentUser.Id || (review.Id > 0 && !_catalogSettings.AllowReviewModification))
            {
                return(NotFound());
            }

            var response = R.Success;

            //check if review should be allowed for this product
            if (_catalogSettings.AllowReviewsForStorePurchaseOnly)
            {
                //check if user has purchased anything here
                var orders = _orderService.GetOrders(out int _, userId: currentUser.Id, storeId: CurrentStore.Id,
                                                     paymentStatus: new List <PaymentStatus>()
                {
                    PaymentStatus.Complete
                },
                                                     orderStatus: new List <OrderStatus>()
                {
                    OrderStatus.Complete
                });
                if (orders.SelectMany(x => x.OrderItems).All(y => y.ProductId != productId))
                {
                    return(NotFound());
                }
            }

            if (_catalogSettings.AllowOneReviewPerUserPerItem && reviewId == 0)
            {
                //check if user has already reviewed this product
                var savedReview =
                    _reviewService.FirstOrDefault(x => x.ProductId == productId && x.UserId == CurrentUser.Id);
                if (savedReview != null)
                {
                    return(RedirectToRoute(RouteNames.AccountReviews));
                }
            }

            var reviewModel  = _reviewModelFactory.Create(review);
            var productModel = _productModelFactory.Create(product);

            //set breadcrumb nodes
            SetBreadcrumbToRoute("Account", RouteNames.AccountProfile);
            SetBreadcrumbToRoute("Review Center", RouteNames.AccountReviews);
            SetBreadcrumbToRoute("Edit Review", RouteNames.ReviewEditor);
            return(response.With("review", reviewModel).With("product", productModel).Result);
        }