private async Task <IActionResult> DoSell(SellToCustomerViewModel model) { var loginUser = await GetCurrentUserAsync(); ProductDesc productDesc = await _applicationDbContext.ProductDesc .Where(m => m.ID.Equals(model.MattressTypeID, StringComparison.InvariantCultureIgnoreCase)) .FirstOrDefaultAsync(); Mattress mattress = new Mattress { ID = model.MattressID, TypeDesc = productDesc, RegisterDate = model.SaleDate, SaleDate = model.SaleDate, }; SaleToCustomerDetail saleToCustomerDetail = new SaleToCustomerDetail { Gifts = model.Gifts, DeliveryAddress = model.DeliveryAddress, Prodect = mattress, Price = productDesc.Price }; SaleToCustomer saleToCustomer = new SaleToCustomer { ID = IDGenerator.GetSaleToCustomerIDGenerator(_applicationDbContext).GetNext(), Customer = new Member { MemberID = model.CustomerID }, SellingAgents = new Member { MemberID = loginUser.UserName }, DealDate = model.SaleDate, }; saleToCustomerDetail.Sale = saleToCustomer; saleToCustomer.DetailItems.Add(saleToCustomerDetail); _applicationDbContext.Mattress.Add(mattress); _applicationDbContext.SaleToCustomer.Add(saleToCustomer); _applicationDbContext.SaleToCustomeDetails.Add(saleToCustomerDetail); var pointItems = await AddMemberPoint(saleToCustomerDetail); _applicationDbContext.SaveChanges(); return(Json(new { saleToCustomerID = saleToCustomer.ID, memberPointItems = pointItems, sellMattressData = new { MattressID = mattress.ID, MattressTypeName = mattress.TypeDesc.Name, DeliveryAddress = saleToCustomerDetail.DeliveryAddress, CustomerID = saleToCustomer.Customer.MemberID, SaleDate = saleToCustomer.DealDate.Date.ToString("yyyy'-'MM'-'dd"), Gifts = saleToCustomerDetail.Gifts } })); }
public async Task <IActionResult> Sell(SellToCustomerViewModel model) { if (ModelState.IsValid) { var existMattresID = await _applicationDbContext.Mattress .Where(m => m.ID.Equals(model.MattressID, StringComparison.InvariantCultureIgnoreCase)) .Take(1) .Select(m => m.ID) .FirstOrDefaultAsync(); if (existMattresID != null) { return(ErrorMessage.BadRequestJsonResult("MattressID is Exist.")); } var existMattressTypeID = await _applicationDbContext.ProductDesc .Where(m => m.ID.Equals(model.MattressTypeID, StringComparison.InvariantCultureIgnoreCase)) .Take(1) .Select(m => m.ID) .FirstOrDefaultAsync(); if (existMattressTypeID == null) { return(ErrorMessage.BadRequestJsonResult("MattressTypeID is not Exist.")); } var existCustomerID = await _applicationDbContext.Members .Where(m => m.MemberID.Equals(model.CustomerID, StringComparison.InvariantCultureIgnoreCase)) .Take(1) .Select(m => m.MemberID) .FirstOrDefaultAsync(); if (existCustomerID == null) { return(ErrorMessage.BadRequestJsonResult("CustomerID is not Exist.")); } return(await DoSell(model)); } return(ErrorMessage.BadRequestJsonResult(ModelState.Values.SelectMany(x => x.Errors))); }
private async Task <ActionResult> DoSell(SellToCustomerViewModel model) { var loginUser = await GetCurrentUserAsync(); ProductDesc productDesc = await AppDbContext.ProductDesc .Where(m => m.ID.Equals(model.MattressTypeID, StringComparison.InvariantCultureIgnoreCase)) .FirstOrDefaultAsync(); Mattress mattress = new Mattress { ID = model.MattressID, TypeDesc = productDesc, RegisterDate = model.SaleDate, SaleDate = model.SaleDate, }; SaleToCustomerDetail saleToCustomerDetail = new SaleToCustomerDetail { Gifts = model.Gifts, DeliveryAddress = model.DeliveryAddress, Prodect = mattress, Price = productDesc.Price, CashCoupon = model.IsUseCashCoupon ? 1000 : 0 }; SaleToCustomer saleToCustomer = new SaleToCustomer { ID = IDGenerator.GetSaleToCustomerIDGenerator(AppDbContext).GetNext(), Customer = this.AppDbContext.FindOrAttachToLocal(model.CustomerID), SellingAgents = this.AppDbContext.FindOrAttachToLocal(loginUser.UserName), DealDate = model.SaleDate, }; saleToCustomerDetail.Sale = saleToCustomer; saleToCustomer.DetailItems.Add(saleToCustomerDetail); // this.AppDbContext.TryToAttach(saleToCustomer.Customer, (l, r) => r.MemberID.Equals(l.MemberID)); // this.AppDbContext.TryToAttach(saleToCustomer.SellingAgents, (l, r) => r.MemberID.Equals(l.MemberID)); //AppDbContext.Members.Attach(saleToCustomer.Customer); // AppDbContext.Members.Attach(saleToCustomer.SellingAgents); AppDbContext.Mattress.Add(mattress); AppDbContext.SaleToCustomer.Add(saleToCustomer); AppDbContext.SaleToCustomeDetails.Add(saleToCustomerDetail); var pointItems = await AddMemberPoint(saleToCustomerDetail); AppDbContext.SaveChanges(); return(Json(new { saleToCustomerID = saleToCustomer.ID, memberPointItems = pointItems, sellMattressData = new { MattressID = mattress.ID, MattressTypeName = mattress.TypeDesc.DisplayName, DeliveryAddress = saleToCustomerDetail.DeliveryAddress, CustomerID = saleToCustomer.Customer.MemberID, SaleDate = saleToCustomer.DealDate.Date.ToString("yyyy'-'MM'-'dd"), Gifts = saleToCustomerDetail.Gifts, IsUseCashCoupon = model.IsUseCashCoupon }, JsonRequestBehavior.AllowGet })); }