예제 #1
0
        /// <summary>
        /// Add the book items to cart
        /// </summary>
        /// <returns></returns>
        public ActionResult AddToShoppingCart()
        {
            //Get the query strings
            var isbn = Request["Isbn"];

            var isAdded = false;

            var priceType = (Request["PriceType"] ?? "").ToLower();

            try
            {
                if (Session["BOOKS"] != null)
                {
                    var courseBooks = (IEnumerable<CourseSectionModel>)Session["BOOKS"];

                    var courseSectionModel = courseBooks.FirstOrDefault();
                    if (courseSectionModel != null)
                    {
                        var item = courseSectionModel.ItemModels.SingleOrDefault(b => b.Isbn.Equals(isbn));

                        //Check if the books is not out of stock then if will execute
                        // oos=Out of Stock
                        if (!priceType.Equals("oos") && (priceType.Equals("new") || priceType.Equals("used") || priceType.Equals("rental") || priceType.Equals("ebook")))
                        {
                            if (item != null)
                            {
                                var newPrice = 0.00;
                                var usedPrice = 0.00;
                                var rentalPrice = 0.00;
                                var ebookPrice = 0.00;
                                var cartPrice = 0.00;
                                var type = "new";

                                try
                                {
                                    newPrice = Convert.ToDouble(item.NewPrice.Replace("$", ""));
                                    usedPrice = Convert.ToDouble(item.UsedPrice.Replace("$", ""));
                                    rentalPrice = Convert.ToDouble(item.RentalPrice.Replace("$", ""));
                                    ebookPrice = Convert.ToDouble(item.EbookPrice.Replace("$", ""));
                                }
                                catch (Exception e)
                                {
                                    newPrice = 0.00;
                                    usedPrice = 0.00;
                                    rentalPrice = 0.00;
                                    ebookPrice = 0.00;
                                }

                                if (priceType.Equals("new"))
                                {
                                    cartPrice = newPrice;
                                }
                                else if (priceType.Equals("used"))
                                {
                                    cartPrice = usedPrice;
                                    type = "used";
                                }
                                else if (priceType.Equals("rental"))
                                {
                                    cartPrice = rentalPrice;
                                    type = "tx-rent";
                                }
                                else if (priceType.Equals("ebook"))
                                {
                                    cartPrice = ebookPrice;
                                    type = "ebook";
                                }

                                var shoppingCartModel = new ShoppingCartModel
                                {
                                    Id = isbn,
                                    Isbn = isbn,
                                    ActualPrice = cartPrice,
                                    NewUsed = priceType,
                                    Type = type,
                                    Title = item.Title,
                                    NewPrice = newPrice,
                                    OldPrice = usedPrice,
                                    Detail = courseBooks.FirstOrDefault() != null ? courseBooks.FirstOrDefault().Class : "",
                                    Qty = 1,
                                    Course = courseBooks.FirstOrDefault().Class
                                };

                                shoppingCartModel.Total = 1 * shoppingCartModel.ActualPrice;

                                //Checking for null
                                if (Session["CARTITEMS"] == null)
                                {
                                    var lstShoppingCartModels = new List<ShoppingCartModel> { shoppingCartModel };

                                    Session["CARTITEMS"] = lstShoppingCartModels;

                                }
                                else
                                {
                                    var lstShoppingCartModels = ((List<ShoppingCartModel>)Session["CARTITEMS"]).ToList();

                                    var shoppingModel =
                                        (from model in lstShoppingCartModels where model.Id.Equals(isbn) select model).
                                            FirstOrDefault();

                                    if (shoppingModel != null)
                                    {
                                        if (priceType.Equals(shoppingModel.Type.ToLower()))
                                        {
                                            return Json(new { success = false, errMsg = "Already exist" });
                                        }
                                        shoppingModel.ActualPrice = cartPrice;
                                        shoppingModel.Type = (type);
                                        Session["CARTITEMS"] = lstShoppingCartModels;
                                        return Json(new { success = true, errMsg = "", });
                                    }

                                    lstShoppingCartModels.Add(shoppingCartModel);

                                    //Item that added to the cart latest item added t0 cart
                                    Session["LASTITEMADDED"] = shoppingCartModel.Title;

                                    Session["CARTITEMS"] = lstShoppingCartModels;

                                }

                                ViewData["CarTItems"] = (List<ShoppingCartModel>)Session["CARTITEMS"];

                                isAdded = true;
                            }

                        }
                    }
                }

                return Json(new { success = isAdded, errMsg = "", });

            }
            catch (Exception x)
            {
                return Json(new { success = isAdded, errMsg = x.Message });
            }
        }
        /// <summary>
        /// Add the catalog items to the cart
        /// </summary>
        /// <returns></returns>
        public ActionResult AddCatalogItemsToCart()
        {
            var source = Request["Source"] ?? "";

            var qty = Convert.ToInt32(Request["Quantity"] ?? "1");

            var id = Request["MatrixId"] ?? "";

            id = id.Replace("-", "*");

            var rdo = (Request["NewUsed"] ?? "");
            var seosku = Request["Sku"] ?? "";

            seosku = seosku.Replace("-", "*");

            var lstCatalogProductLookUpItems = new ItemLookUpViewModel();

            //Get the items from the sessions
            if (Session["GetItemDetail"]!=null)
            {

                var itemModels = (List<ItemLookUpViewModel>)Session["GetItemDetail"];
                lstCatalogProductLookUpItems = itemModels.FirstOrDefault(m => m.Sku.Equals(seosku));
            }

            try
            {
                if (lstCatalogProductLookUpItems != null)
                {
                    //Check for the items
                    string color = "";
                    string size = "";
                    string itemoos = "";
                    string price, sku = "", title = "", newused = "", type = "";
                    string desc = "-";
                    double fprice = 0.00;
                    //Check if source type is gm then add to cart
                    if (source.ToLower().Equals("gm"))
                    {

                        try
                        {
                            if (string.IsNullOrEmpty(id))
                            {
                                id = lstCatalogProductLookUpItems.Sku;
                            }
                            sku = lstCatalogProductLookUpItems.Sku;

                            size =
                                lstCatalogProductLookUpItems.MatrixItem.FirstOrDefault(m => m.MatrixId.Equals(id)).Size;

                            price =
                                lstCatalogProductLookUpItems.MatrixItem.FirstOrDefault(m => m.MatrixId.Equals(id)).Price;

                            itemoos =
                                lstCatalogProductLookUpItems.MatrixItem.FirstOrDefault(m => m.MatrixId.Equals(id)).Qqh;

                            color =
                                lstCatalogProductLookUpItems.MatrixItem.FirstOrDefault(m => m.MatrixId.Equals(id)).Color;

                            title =
                                lstCatalogProductLookUpItems.MatrixItem.FirstOrDefault(m => m.MatrixId.Equals(id)).Desc;
                        }
                            //If Exception is came then there is no matrix items so catch block code will be executed
                        catch (Exception)
                        {
                            string saleprice = lstCatalogProductLookUpItems.SalePrice;

                            if (saleprice != "$0.00")
                            {
                                price = saleprice;

                            }
                            else
                            {
                                price = lstCatalogProductLookUpItems.Price;
                            }

                            sku = lstCatalogProductLookUpItems.Sku;

                            title = lstCatalogProductLookUpItems.Description;

                        }

                        fprice = Convert.ToDouble(price.Replace("$", ""));

                        newused = "N/A";

                        type = "GM";

                        desc = color + " - " + size;

                        if (!itemoos.Contains("Out"))
                        {
                        }

                    }
                    if (source.ToLower().Equals("tr"))
                    {

                        // made changes 18-06-2012
                        //  id = sku;

                        sku = lstCatalogProductLookUpItems.Sku;

                        // made changes 18-06-2012
                        id = sku;

                        title = lstCatalogProductLookUpItems.Description;

                        price = lstCatalogProductLookUpItems.Price;

                        newused = "N/A";

                        type = "TR";

                        desc = "Trade book";

                        fprice = Convert.ToDouble(price.Replace("$", ""));

                    }
                    if (source.ToLower().Equals("tx"))
                    {

                        string usedprice, newprice;

                        id = sku;

                        sku = lstCatalogProductLookUpItems.Sku;

                        title = lstCatalogProductLookUpItems.Description;

                        usedprice = lstCatalogProductLookUpItems.UsedPrice;

                        newprice = lstCatalogProductLookUpItems.NewPrice;
                        type = "TX";
                        desc = "Textbook";

                        if (rdo.Equals("1"))
                        {
                            fprice = Convert.ToDouble(newprice.Replace("$", ""));

                            newused = "New";
                        }
                        if (rdo.Equals("0"))
                        {
                            fprice = Convert.ToDouble(usedprice.Replace("$", ""));
                            newused = "Used";
                        }

                    }

                    var lstCartItems = new List<ShoppingCartModel>();

                    var model = lstCatalogProductLookUpItems;

                    //Create the object
                    var shoppingCartModel = new ShoppingCartModel
                                                {
                                                    Id = id,
                                                    ActualPrice = fprice,
                                                    Qty = qty,
                                                    Total = qty*fprice,
                                                    Type = source,
                                                    Title = title,
                                                    Detail = desc,
                                                    Isbn = sku,
                                                    Course = desc
                                                };

                    //Check for the null if nulll then create the object and assign
                    if (Session["CARTITEMS"] != null)
                    {
                        lstCartItems = (List<ShoppingCartModel>) Session["CARTITEMS"];

                        var alreadyExist = lstCartItems.FirstOrDefault(lst => lst.Id.Equals(id));

                        //Checking for already exist
                        if (alreadyExist == null)
                        {
                            lstCartItems.Add(shoppingCartModel);

                            //Latest item added to the cart
                            Session["LASTITEMADDED"] = shoppingCartModel.Title;
                        }
                        //else
                        //{
                        //    lstCartItems.Add(shoppingCartModel);

                        //    //Latest item added to the cart
                        //    Session["LASTITEMADDED"] = shoppingCartModel.Title;

                        //}

                    }
                    else
                    {
                        //Add to the list
                        lstCartItems.Add(shoppingCartModel);

                        //Latest item added to the cart
                        Session["LASTITEMADDED"] = shoppingCartModel.Title;
                    }

                    //Put the list to the session
                    Session["CARTITEMS"] = lstCartItems;

                    return Json(new {success = true});
                }
                return Json(new { success = false });
            }
            catch (Exception x)
            {
                return Json(new { errMsg = x.Message });
            }
        }