示例#1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            string Userid = Context.User.Identity.GetUserId();

            Session["Userid"] = Userid;
            PurchaseModel model        = new PurchaseModel();
            ProductModel  productmodel = new ProductModel();

            if (Userid != null)
            {
                if (!string.IsNullOrWhiteSpace(Request.QueryString["id"]))
                {
                    int id    = Convert.ToInt32(Request.QueryString["id"]);
                    int value = 1;
                    int quan  = 0;
                    if (!string.IsNullOrWhiteSpace(Request.QueryString["quan"]))
                    {
                        value = Convert.ToInt32(Request.QueryString["quan"]);
                    }

                    if (!string.IsNullOrWhiteSpace(Request.QueryString["quantity"]))
                    {
                        quan = Convert.ToInt32(Request.QueryString["quantity"]);
                    }

                    Purchase cart = new Purchase
                    {
                        Quantity  = value,
                        UsersID   = Userid,
                        DateTime  = Convert.ToString(DateTime.Now),
                        ProductID = id
                    };
                    int purchaseid        = model.check_order_product(Userid, id);
                    int purchasequantity  = model.check_order_quantity(Userid, id);
                    int purchaseproductid = model.check_order_productid(Userid, id);
                    available_quantity = productmodel.available_quantity_product(id);


                    //Session["avail_quantity"] = available_quantity;
                    if (value > available_quantity && available_quantity == 0 || quan > available_quantity && available_quantity == 0)
                    {
                        //MessageBox.Show("Sorry Product is Out of Stock");
                        string script = "alert('Sorry Product is Out of Stock');";
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true);

                        string site = String.Format("product_detail.aspx?id={0}", id);
                        Response.Redirect(site);
                    }

                    else if (purchaseid == 0)
                    {
                        model.insert_purchase(cart);
                    }

                    else if (quan != 0)
                    {
                        if (quan > available_quantity)
                        {
                            string script = "alert('Quantity selected is more than Available Quantity');";
                            ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true);
                        }
                        else
                        {
                            update_quantity(purchaseid, quan);
                        }
                    }

                    else if (purchasequantity == available_quantity)
                    {
                        string script = "alert('Product Quantity is already equal to Available Quantity');";
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true);
                    }
                    else
                    {
                        update_quantity(purchaseid, purchasequantity + 1);
                    }
                }
                else if (!string.IsNullOrWhiteSpace(Request.QueryString["del"]))
                {
                    int id = Convert.ToInt32(Request.QueryString["del"]);
                    model.delete_purchase(id);
                    Response.Redirect("cart.aspx");
                }


                //else if (!string.IsNullOrWhiteSpace(Request.QueryString["pur_id"]))
                //{
                //    int id = Convert.ToInt32(Request.QueryString["pur_id"]);
                //    int value = Convert.ToInt32(Request.QueryString["quan"]);
                //    string userId = Convert.ToString(HttpContext.Current.Session["Userid"]);
                //    if (value > available_quantity)
                //        {
                //            System.Windows.Forms.MessageBox.Show("Quantity selected is more than Available Quantity");
                //        }
                //        else
                //        {

                //        }

                //}
            }
            else
            {
                Response.Redirect("loginPage.aspx");
            }
        }