コード例 #1
0
        public ActionResult Checkout(FormCollection form)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var lstIP = CommonHelper.GetIPAddresses();

                    OrderBO order = new OrderBO();
                    order.id         = DateTime.Now.Ticks.ToString();
                    order.createdate = DateTime.Now;
                    order.fullname   = form["fullname"].ToString();

                    int districtID = Convert.ToInt32(form["district"].ToString());
                    int provinceID = Convert.ToInt32(form["province"].ToString());
                    int wardID     = Convert.ToInt32(form["ward"].ToString());
                    var provinceBO = LocationDAO.GetProvinceByID(provinceID);
                    var districtBO = LocationDAO.GetDistrictByID(districtID);
                    var wardBO     = LocationDAO.GetWardByID(wardID);



                    order.district = districtBO.name;
                    order.province = provinceBO.name;
                    order.ward     = wardBO.name;

                    order.email       = form["email"].ToString();
                    order.phone       = form["phone"].ToString();
                    order.fulladdress = form["orderfulladdress"].ToString();

                    order.note = form["ordernote"].ToString();
                    var listItemKey = form["listitemkey"].Split('|');
                    if (listItemKey == null || listItemKey.Count() <= 0 ||
                        String.IsNullOrEmpty(order.fullname) ||
                        String.IsNullOrEmpty(order.phone) || String.IsNullOrEmpty(order.fulladdress) ||
                        String.IsNullOrEmpty(order.district) || String.IsNullOrEmpty(order.province) ||
                        String.IsNullOrEmpty(order.ward))
                    {//phai dien day du thong tin
                        return(RedirectToAction("Checkout"));
                    }
                    //get list item
                    List <CartSession> lstCartItem = new List <CartSession>();
                    lstCartItem = Session["fancycart"] as List <CartSession>;
                    if (lstCartItem == null)
                    {
                        return(RedirectToAction("Checkout"));
                    }
                    List <CartItemBO> lstCartItemBO = new List <CartItemBO>();
                    foreach (var item in lstCartItem)
                    {
                        CartItemBO cartItem = JsonConvert.DeserializeObject <CartItemBO>(JsonConvert.SerializeObject(item));
                        lstCartItemBO.Add(cartItem);
                    }
                    order.listitem = lstCartItemBO;

                    //check payment method
                    var payment = form["payment_method"];
                    if (payment.ToLower().Trim() == "bacs")
                    {
                        order.paymenttype = 2;
                    }
                    else
                    {
                        order.paymenttype = 1;
                    }

                    //tính discount
                    if (order.paymenttype == 2)
                    {
                        order.totaldiscount = order.listitem.Sum(x => x.FinalPrice) * 5 / 100;
                    }
                    order.iporder    = lstIP != null && lstIP.Count() > 0 ? lstIP.First() : "";
                    order.status     = 1;
                    order.statusname = "Chưa xử lý";

                    //insert to mongodb

                    string error  = "";
                    var    result = OrderDAO.CreateOrder(order, ref error);
                    if (result)
                    {
                        //remove item in cart
                        lstCartItem.Clear();
                        #region create account
                        //add user vào thông tin tài khoản luôn
                        //check đã tồn tại username
                        string username = !String.IsNullOrEmpty(order.email) ? order.email : order.phone;
                        var    userBO   = UserDAO.GetUserByUserNameNotAdmin(username);
                        if (userBO != null && userBO.id > 0)
                        {
                            return(RedirectToAction("ChekoutSuccess", new { orderid = order.id }));
                        }

                        UserBO user = new UserBO();
                        user.username    = username;
                        user.password    = "******";//123456
                        user.phone       = order.phone;
                        user.isactive    = 1;
                        user.role        = "customer";
                        user.fullname    = order.fullname;
                        user.address     = order.fulladdress + ", " + order.ward + ", " + order.district + ", " + order.province;
                        user.email       = order.email;
                        user.createddate = DateTime.Now;
                        user.note        = "User được tạo tự động khi submit đơn hàng thành công";
                        string errorMessage = String.Empty;
                        result = UserDAO.CreateUser(user, ref errorMessage);
                        #endregion
                    }

                    return(RedirectToAction("ChekoutSuccess", new { orderid = order.id }));
                }
                else
                {
                    List <CartSession> lstCartItem = Session["fancycart"] as List <CartSession>;
                    if (lstCartItem == null)
                    {
                        lstCartItem = new List <CartSession>();
                    }
                    if (lstCartItem.Count <= 0)
                    {
                        return(RedirectToAction("Index"));
                    }

                    //load danh sách Province
                    var listProvince = LocationDAO.GetListProvinces();
                    ViewBag.ListProvince = listProvince;
                    ViewBag.ListCartItem = lstCartItem;
                    var commonInfo = LocationDAO.GetCommonInfoByID("commoninfo");
                    ViewBag.CommonInfo = commonInfo;

                    return(View("Checkout"));
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "* Quý khách vui lòng nhập đầy đủ thông tin để gửi đơn hàng ạ!");
                List <CartSession> lstCartItem = Session["fancycart"] as List <CartSession>;
                if (lstCartItem == null)
                {
                    lstCartItem = new List <CartSession>();
                }
                if (lstCartItem.Count <= 0)
                {
                    return(RedirectToAction("Index"));
                }

                //load danh sách Province
                var listProvince = LocationDAO.GetListProvinces();
                ViewBag.ListProvince = listProvince;
                ViewBag.ListCartItem = lstCartItem;
                var commonInfo = LocationDAO.GetCommonInfoByID("commoninfo");
                ViewBag.CommonInfo = commonInfo;

                return(View("Checkout"));
            }
        }