예제 #1
0
 public JsonResult GetCTSP(int id)
 {
     try
     {
         var data = _sanphamService.GetById(id);
         return(Json(new { Result = data }, JsonRequestBehavior.AllowGet));
     }
     catch (Exception ex)
     {
         Response.StatusCode = (int)HttpStatusCode.BadRequest;
         return(Json(ex.Message, JsonRequestBehavior.AllowGet));
     }
 }
예제 #2
0
        public JsonResult DuyetPostTin(int id, ProductStatus status)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    IEnumerable <ModelError> allErrors = ModelState.Values.SelectMany(v => v.Errors);
                    return(Json(allErrors, JsonRequestBehavior.AllowGet));
                }
                else
                {
                    var sp = _sanphamService.GetById(id);
                    sp.Status = status;
                    _sanphamService.Update(sp);
                    if (_sanphamService.Save())
                    {
                        return(Json(new { Result = sp, Status = "OK" }, JsonRequestBehavior.AllowGet));
                    }
                }


                return(Json(new { Result = "", Status = "FAIL" }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(new { Result = ex.Message, Status = "FAIL" }, JsonRequestBehavior.AllowGet));
            }
        }
예제 #3
0
 public JsonResult AddToCart(int id)
 {
     try
     {
         var spVm = _sanphamService.GetById(id);
         var user = _userService.GetUser(UserLoginViewModel.Current.KeyId);
         if (user != null)
         {
             Sanpham sp = Mapper.Map <SanphamViewModel, Sanpham>(spVm);
             //user.KhachhangNavigation.GiohangNavigation.CtGiohangs.Add(sp);
             _userService.Save();
             return(Json("Đã thêm vào giỏ hàng!", JsonRequestBehavior.AllowGet));
         }
         return(Json(const_Error.NOT_LOGIN, JsonRequestBehavior.AllowGet));
     }
     catch (Exception ex)
     {
         Response.StatusCode = (int)HttpStatusCode.BadRequest;
         return(Json(new { Result = ex.Message, Status = "FAIL" }, JsonRequestBehavior.AllowGet));
     }
 }
예제 #4
0
        public JsonResult SaveAllEntity(HoadonViewModel Information, List <CtGiohangViewModel> Products)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    IEnumerable <ModelError> allErrors = ModelState.Values.SelectMany(v => v.Errors);
                    return(Json(allErrors, JsonRequestBehavior.AllowGet));
                }
                else
                {
                    #region Add new invoice
                    //Current Customer
                    var kh = UserLoginViewModel.Current;
                    //Get all ncc distinct
                    List <int> ncc = new List <int>();
                    List <SanphamViewModel> spVm = new List <SanphamViewModel>();
                    foreach (var item in Products)
                    {
                        var sp = _sanphamService.GetById(item.masp);
                        spVm.Add(sp);
                        if (ncc.FirstOrDefault(x => x == sp.NccNavigation.User_FK) == 0)
                        {
                            ncc.Add(sp.NccNavigation.User_FK);
                        }
                    }
                    List <HoadonViewModel> hdVm = new List <HoadonViewModel>();
                    //Create Invoice foreach ncc
                    foreach (var i in ncc)
                    {
                        //var accountNcc = _userService.GetUser(i);
                        HoadonViewModel hd = new HoadonViewModel();
                        hd.KeyId     = 0;
                        hd.mahd      = 0;
                        hd.makh      = kh.KeyId;
                        hd.ncc_FK    = i;
                        hd.tongtien  = 0;
                        hd.thoigian  = DateTime.Now.ToShortDateString();
                        hd.tinhtrang = "Chờ xác nhận";
                        hd.Name      = Information.Name;
                        hd.Phone     = Information.Phone;
                        hd.Address   = Information.Address;
                        hd.Note      = Information.Note;
                        hd.Cthdons   = new HashSet <CthdViewModel>();
                        foreach (var item in Products)
                        {
                            var pro = spVm.FirstOrDefault(x => x.KeyId == item.masp);
                            if (pro.NccNavigation.User_FK == i)
                            {
                                CthdViewModel cthd = new CthdViewModel();
                                cthd.mahd      = 0;
                                cthd.masp      = pro.KeyId;
                                cthd.soluong   = item.soluong;
                                cthd.thanhtien = pro.dongia * (double)item.soluong;
                                hd.Cthdons.Add(cthd);
                                hd.tongtien += cthd.thanhtien;
                                //Products.Remove(item);

                                //Update Number of Product
                                pro.conlai -= cthd.soluong;
                                if (pro.conlai < 1)
                                {
                                    pro.Status = Data.Enum.ProductStatus.Sold;
                                }
                                _sanphamService.Update(pro);
                            }
                        }
                        hdVm.Add(_hoadonService.Add(hd));
                    }
                    #endregion

                    #region Clear cart
                    var        Customer = _userService.GetUser(kh.KeyId);
                    List <int> cart     = new List <int>();

                    foreach (var i in Customer.KhachhangNavigation.CtGiohangs)
                    {
                        cart.Add(i.KeyId);
                    }
                    foreach (var cartItem in cart)
                    {
                        _ctGiohangService.Delete(cartItem);
                    }
                    #endregion


                    if (_hoadonService.Save())
                    {
                        return(Json(new { Result = hdVm, Status = "OK" }, JsonRequestBehavior.AllowGet));
                    }
                }
                return(Json(new { Result = "Đã có lỗi xảy ra khi đặt hàng!", Status = "FAIL" }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(new { Result = ex.Message, Status = "FAIL" }, JsonRequestBehavior.AllowGet));
            }
        }