Esempio n. 1
0
        public JsonResult CheckOut(string p, string tmp_total, string address, string discountcode)
        {
            var id_cus = Session[CommonSession.ACCOUNT_SESSION].ToString();

            if (id_cus != null)
            {
                if (address != "")
                {
                    int percent;
                    if (discountcode != "")
                    {
                        Discount_Code code = conn.Query <Discount_Code>("Select * from discount_code where code = '" + discountcode + "'").FirstOrDefault();
                        percent = code.Disc_Percent;
                    }
                    else
                    {
                        percent = 0;
                    }
                    // lấy thông tin khách hàng
                    Customer customer = conn.Query <Customer>("Select * from account where id_customer =" + Convert.ToInt64(id_cus)).FirstOrDefault();

                    dynamic json = JsonConvert.DeserializeObject(p);// lấy giỏ hàng từ local

                    string queryString = "INSERT INTO dbo.[order] (id,id_customer,temp_total,discount,total,deli_address,status) values (@id,@id_customer,@temp_total,@discount,@total,@deli_address,@status);";
                    // insert vào table order trước mới insert vào order_detail theo id_order
                    using (IDbConnection db = conn)
                    {
                        var    list     = conn.Query <Order>("Select * FROM dbo.[order]").ToList();
                        string id_order = "Oder_" + (list.Count() + 1).ToString();
                        // total thì tính dựa theo discount với temp_total
                        int totalReal = Convert.ToInt32(tmp_total) - (Convert.ToInt32(tmp_total) / 100 * percent);

                        conn.Execute(queryString, new { id = id_order, id_customer = Convert.ToInt64(id_cus), temp_total = Convert.ToInt32(tmp_total), discount = percent, total = totalReal, deli_address = address, status = 0 });
                        string query = "Insert into dbo.[order_detail] (id_order,id_product,quantity ,unit_price,total) values (@id_order,@id_product,@quantity,@unit_price,@total);";

                        foreach (var item in json)
                        {
                            string id_ordera = id_order;
                            string ID        = item["Id"];
                            int    quan      = item["Quantity"];
                            int    price     = item["Price"];
                            int    tong      = quan * price;
                            conn.Execute(query, new { id_order = id_ordera, id_product = ID, quantity = quan, unit_price = price, total = tong });
                        }
                        return(Json(true, JsonRequestBehavior.AllowGet));
                    }
                }
                else
                {
                    return(Json(false, JsonRequestBehavior.AllowGet));
                }
            }
            else
            {
                // viết thong bao can phai dang nhap mới checkout được
                return(Json(false, JsonRequestBehavior.AllowGet));
            }
        }
        public JsonResult Save_Discount(Discount_Code[] arr)
        {
            var value = false;
            var Id    = 0;

            try
            {
                if (arr[0].Id > 0)
                {
                    Id = arr[0].Id;
                    var update = db.Discount_Code.Find(arr[0].Id);
                    update.Name_discount      = arr[0].Name_discount;
                    update.Discount_code_view = arr[0].Discount_code_view;
                    update.Sale_code          = arr[0].Sale_code;
                    update.Note = arr[0].Note;
                    db.SaveChanges();
                    value = true;
                }
                else
                {
                    var discountnew = new Discount_Code();
                    var layid       = (from D in db.Discount_Code
                                       where D.Id > 0
                                       orderby D.Id descending
                                       select new
                    {
                        Id = D.Id
                    }).Take(1).ToArray();

                    if (layid.Any())
                    {
                        discountnew.Id = layid[0].Id + 1;
                    }
                    else
                    {
                        discountnew.Id = 1;
                    }
                    Id = discountnew.Id;
                    discountnew.Name_discount      = arr[0].Name_discount;
                    discountnew.Sale_code          = arr[0].Sale_code;
                    discountnew.Note               = arr[0].Note;
                    discountnew.Discount_code_view = arr[0].Discount_code_view;
                    discountnew.status             = 1;
                    db.Discount_Code.Add(discountnew);
                    db.SaveChanges();
                    value = true;
                }
            }
            catch (Exception)
            {
                value = false;
            }
            var data = new { sussecc = value, Id = Id };

            return(Json(data, JsonRequestBehavior.AllowGet));
        }