public async Task <IActionResult> Index()
        {
            var basket = await _basketservice.GetBasketItems(Request);

            foreach (var item in basket.CartItems)
            {
                item.Product.PriceModel = await _priceCalculator.CalculateBookPrice(item.Product.P_Id, _lang.GetLang());
            }
            ViewBag.CVRT    = 1000;
            ViewBag.BINPERC = 1;
            var cvrtbase = ((List <OptionsModel>)(await _optionrepository.GetAll()).Data).FirstOrDefault(x => x.O_Key == "CVRTTOMANTOPOINT");
            var bin      = ((List <OptionsModel>)(await _optionrepository.GetAll()).Data).FirstOrDefault(x => x.O_Key == "GIFTBINPERCENTAGE");

            if (bin != null)
            {
                ViewBag.BINPERC = int.Parse(bin.O_Value);
            }
            if (cvrtbase != null)
            {
                ViewBag.CVRT = int.Parse(cvrtbase.O_Value);
            }
            ViewBag.LoggedIn = TokenService.AuthorizeUser(Request) > 0;
            ViewBag.User     = await _userrepository.GetById(TokenService.AuthorizeUser(Request));

            return(View(basket));
        }
예제 #2
0
        public async Task <ResponseStructure> AddToCart(HttpRequest httpRequest, HttpResponse httpResponse)
        {
            try
            {
                AllProducts = ((List <ProductModel>)(await _productrepo.GetAll()).Data);
                StringValues data;
                StringValues PrintAble;
                if (!httpRequest.Headers.TryGetValue("AddToCart", out data))
                {
                    return(ResponseModel.Error("Invalid access detect."));
                }
                if (!httpRequest.Headers.TryGetValue("PrintAble", out PrintAble))
                {
                    return(ResponseModel.Error("Invalid access detect."));
                }
                var        dec         = Barayand.Common.Services.CryptoJsService.DecryptStringAES(data);
                BasketItem rm          = JsonConvert.DeserializeObject <BasketItem>(dec);
                var        findProduct = AllProducts.FirstOrDefault(x => x.P_Id == rm.ProductId);
                if (findProduct == null)
                {
                    return(ResponseModel.Error("Invalid access detect."));
                }
                var product = new ProductBasketModel()
                {
                    P_BinPrice           = findProduct.P_BinPrice,
                    P_Code               = findProduct.P_Code,
                    P_Discount           = findProduct.P_Discount,
                    P_DiscountPeriodTime = findProduct.P_DiscountPeriodTime,
                    P_DiscountType       = findProduct.P_DiscountType,
                    P_ExternalPrice      = findProduct.P_ExternalPrice,
                    P_GiftBin            = findProduct.P_GiftBin,
                    P_Id                       = findProduct.P_Id,
                    P_Image                    = findProduct.P_Image,
                    P_PageCount                = findProduct.P_PageCount,
                    P_PeriodDiscountPrice      = findProduct.P_PeriodDiscountPrice,
                    P_PeriodDiscountPriceType  = findProduct.P_PeriodDiscountPriceType,
                    P_PeriodPrintableFomrulaId = findProduct.P_PeriodPrintableFomrulaId,
                    P_PeriodPrintablePrice     = findProduct.P_PeriodPrintablePrice,
                    P_PeriodPrintablePriceType = findProduct.P_PeriodPrintablePriceType,
                    P_Price                    = findProduct.P_Price,
                    P_PriceFormulaId           = findProduct.P_PriceFormulaId,
                    P_PriceType                = findProduct.P_PriceType,
                    P_PrintAbleVerFormulaId    = findProduct.P_PrintAbleVerFormulaId,
                    P_PrintAbleVerPrice        = findProduct.P_PrintAbleVerPrice,
                    P_PrintAbleVerPriceType    = findProduct.P_PrintAbleVerPriceType,
                    P_PrintAbleVersion         = findProduct.P_PrintAbleVersion,
                    P_PriodDiscountFormulaId   = findProduct.P_PriodDiscountFormulaId,
                    P_Title                    = findProduct.P_Title,
                    P_Type                     = findProduct.P_Type,
                    P_Weight                   = findProduct.P_Weight,
                    PriceModel                 = await _priceCalculator.CalculateBookPrice(findProduct.P_Id, _lang.GetLang())
                };
                rm.PrintAble = bool.Parse(PrintAble);
                BasketModel basket = new BasketModel();
                string      cookie;
                if (httpRequest.Cookies.TryGetValue("Cart", out cookie))
                {
                    if (cookie != null)
                    {
                        var basketInfo = Barayand.Common.Services.CryptoJsService.DecryptStringAES(cookie);
                        basket = JsonConvert.DeserializeObject <BasketModel>(basketInfo);
                        if (basket.CartItems.Count > 0)
                        {
                            if (basket.CartItems.FirstOrDefault(x => x.Product.P_Type != product.P_Type) != null)
                            {
                                return(ResponseModel.Error("You can only add a type of product (Book or Art or Learning trains) in same time."));
                            }
                        }
                        if (bool.Parse(PrintAble) == false)
                        {
                            var checkDuplicatePdf = basket.CartItems.FirstOrDefault(x => x.Product.P_Id == rm.ProductId && x.PrintAble == false);
                            if (checkDuplicatePdf != null)
                            {
                                return(ResponseModel.Error("از هر کتاب شما تنها میتوانید یک نسخه PDF سفارش دهید"));
                            }
                        }
                        var existsProduct = basket.CartItems.FirstOrDefault(x => x.Product.P_Id == rm.ProductId && x.PrintAble == bool.Parse(PrintAble));
                        if (existsProduct != null)
                        {
                            existsProduct.Quantity += rm.Quantity;
                        }
                        else
                        {
                            rm.Product = product;
                            basket.CartItems.Add(rm);
                        }
                    }
                    else
                    {
                        rm.Product = product;
                        basket.CartItems.Add(rm);
                    }
                }
                else
                {
                    rm.Product = product;
                    basket.CartItems.Add(rm);
                    basket.OrderDate = DateTime.Now;
                }

                string token = Barayand.Common.Services.CryptoJsService.EncryptStringToAES(JsonConvert.SerializeObject(basket));
                httpResponse.Cookies.Delete("Cart");
                httpResponse.Cookies.Append("Cart", token);
                return(ResponseModel.Success("Product added."));
            }
            catch (Exception ex)
            {
                _logger.LogError("Error in adding entity to customer basket", ex);
                return(ResponseModel.ServerInternalError(data: ex));
            }
        }