public ActionResult CheckOut(string name, string lname, string address)
 {
     if (Session["MEmail"] != null && Session["MemID"] != null && Session["cart"] != null)
     {
         SELLPRODUCT model    = new SELLPRODUCT();
         decimal     subtotal = 0;
         var         memID    = Session["MemID"].ToString();
         foreach (var item in (List <Item>)Session["cart"])
         {
             var check = db.PRODUCT.Where(x => x.PrdID == item.Product.PrdID).FirstOrDefault().TempStock >= item.Quantity;
             if (check)
             {
                 model.SELLPRODUCT_DTA.Add(new SELLPRODUCT_DTA()
                 {
                     PrdID     = item.Product.PrdID,
                     SpdAmount = item.Quantity
                     ,
                     SpdPrice = item.Product.Price - (item.Product.Price * (decimal.Parse(item.Discount) / 100))
                     ,
                     Discount = Int32.Parse(item.Discount)
                 });
                 subtotal += ((item.Product.Price - (item.Product.Price * (decimal.Parse(item.Discount) / 100))) * item.Quantity);
             }
             else
             {
                 Session["cart"] = null;
                 return(Json(new { success = false }, JsonRequestBehavior.AllowGet));
             }
         }
         model.SlpSum = (subtotal + (subtotal * (decimal.Parse("0.07")))) + 100;
         if (name.Length > 0 && lname.Length > 0 && address.Length > 0)
         {
             model.FnameRec = name; model.LnameRec = lname; model.AdressRec = address;
             model.MemID    = memID; model.SlpDate = DateTime.Now;
         }
         else
         {
             model.FnameRec = Session["MFname"].ToString(); model.LnameRec = Session["MLname"].ToString(); model.AdressRec = Session["MAddress"].ToString();
             model.MemID    = memID; model.SlpDate = DateTime.Now;
         }
         if (_sellRepository.InsertSellProduct(model))
         {
             Session["cart"] = null;
             return(Json(new { success = true }, JsonRequestBehavior.AllowGet));
         }
         return(Json(new { success = false }, JsonRequestBehavior.AllowGet));
     }
     return(RedirectToAction("Error404", "Shipping"));
 }
        public bool InsertSellProduct(SELLPRODUCT sellpd)
        {
            string sql         = @"sp_sellproduct_INSERT";
            string sql_dta     = @"sp_sellproductDTA_INSERT";
            string sql_reserve = @"sp_checkout_reserve";
            string retID       = "";

            using (TransactionScope ts = new TransactionScope())
            {
                object[] obj = { "@MemID",     sellpd.MemID,        "@SlpDate",   sellpd.SlpDate,   "@FnameRec", sellpd.FnameRec,
                                 "@LnameRec",  sellpd.LnameRec,     "@AdressRec", sellpd.AdressRec,
                                 "@SlpStatus", "ยังไม่ได้ชำระเงิน", "@SlpSum",    sellpd.SlpSum };
                retID = Db.InsertReturnID(sql, obj);
                if (retID != null)
                {
                    int Num = 0;
                    foreach (var ls in sellpd.SELLPRODUCT_DTA)
                    {
                        object[] obj_dta = { "@SlpID",     retID,        "@SlpNo",    Num++,       "@PrdID",    ls.PrdID,
                                             "@SpdAmount", ls.SpdAmount, "@SpdPrice", ls.SpdPrice, "@Discount", ls.Discount };
                        object[] obj_reserve = { "@PrdID", ls.PrdID, "@Amount", ls.SpdAmount };
                        try
                        {
                            Db.Insert(sql_dta, obj_dta);
                            Db.Update(sql_reserve, obj_reserve);
                        }
                        catch
                        {
                            return(false);
                        }
                    }
                    ts.Complete();
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }