コード例 #1
0
ファイル: CthdService.cs プロジェクト: haluha1/TMDT
 public void Update(CthdViewModel hoadonVm)
 {
     throw new NotImplementedException();
 }
コード例 #2
0
ファイル: HoadonController.cs プロジェクト: haluha1/TMDT
        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));
            }
        }
コード例 #3
0
ファイル: CthdService.cs プロジェクト: haluha1/TMDT
 public CthdViewModel Add(CthdViewModel hoadonVm)
 {
     throw new NotImplementedException();
 }