コード例 #1
0
ファイル: MattressController.cs プロジェクト: duyukun/WebApp
        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
                }
            }));
        }
コード例 #2
0
        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
            }));
        }