Exemple #1
0
        public async Task <IActionResult> Create([Bind("ID, Ten, ProducerID, CategoryID, Gia")] ProductWithCurrentPrice model)
        {
            if (ModelState.IsValid)
            {
                var product = new Product
                {
                    ID         = model.ID,
                    Ten        = model.Ten,
                    SoLuong    = 0,
                    ProducerID = model.ProducerID,
                    CategoryID = model.CategoryID
                };
                _context.Products.Add(product);
                await _context.SaveChangesAsync();

                var productPrice = new ProductPrice
                {
                    Gia       = model.Gia,
                    TGBD      = DateTime.Now,
                    TGKT      = DateTime.Parse("9999-1-1"),
                    ProductID = product.ID
                };
                _context.ProductPrices.Add(productPrice);
                await _context.SaveChangesAsync();

                TempData["messageSuccess"] = $"Sản phẩm \"{model.Ten}\" đã được thêm";
                return(RedirectToAction("Details", "Products", new { id = product.ID }));
            }
            ViewBag.CategoryID = new SelectList(_context.Categories, "ID", "Ten", model.CategoryID);
            ViewBag.ProducerID = new SelectList(_context.Producers, "ID", "Ten", model.ProducerID);
            return(View(model));
        }
        public async Task <IActionResult> Create([Bind("ID,Ten")] Category category)
        {
            if (ModelState.IsValid)
            {
                _context.Categories.Add(category);
                await _context.SaveChangesAsync();

                TempData["messageSuccess"] = $"Loại sản phẩm \"{category.Ten}\" đã được thêm.";
                return(RedirectToAction("Details", "Categories", new { id = category.ID }));
            }
            return(View(category));
        }
        public async Task <IActionResult> Create([Bind("ID,Ten,SDT")] Customer customer)
        {
            if (ModelState.IsValid)
            {
                _context.Customers.Add(customer);
                await _context.SaveChangesAsync();

                TempData["messageSuccess"] = $"Khách hàng \"{customer.Ten}\" đã được thêm.";
                return(RedirectToAction("Details", "Customers", new { id = customer.ID }));
            }
            return(View(customer));
        }
Exemple #4
0
        public async Task <IActionResult> Create([Bind("ID,Ten")] Producer producer)
        {
            if (ModelState.IsValid)
            {
                _context.Producers.Add(producer);
                await _context.SaveChangesAsync();

                TempData["messageSuccess"] = $"Nhà sản xuất \"{producer.Ten}\" đã được thêm.";
                return(RedirectToAction("Details", "Producers", new { id = producer.ID }));
            }
            return(View(producer));
        }
Exemple #5
0
        public async Task <IActionResult> Create([Bind("Ten,NgaySinh,GioiTinh,SDT,Email,DiaChi,TaiKhoan,MatKhau,XacNhanMatKhau")] UserCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new User
                {
                    Ten      = model.Ten,
                    NgaySinh = model.NgaySinh,
                    GioiTinh = model.GioiTinh,
                    SDT      = model.SDT,
                    Email    = model.Email,
                    DiaChi   = model.DiaChi,
                    UserName = model.TaiKhoan
                };

                var result = await _userManager.CreateAsync(user, model.MatKhau);

                if (result.Succeeded)
                {
                    var nguoiCapNhat = await _userManager.GetUserAsync(User);

                    var userChange = new UserChange
                    {
                        ThoiGian       = DateTime.Now,
                        NguoiCapNhat   = nguoiCapNhat.UserName,
                        HanhDong       = "Thêm mới",
                        NguoiBiCapNhat = user.UserName
                    };
                    _context.UserChanges.Add(userChange);
                    await _context.SaveChangesAsync();

                    TempData["messageSuccess"] = $"Người dùng \"{user.Ten}\" đã được thêm!";
                    return(RedirectToAction("Details", "Users", new { taiKhoan = user.UserName }));
                }
                else
                {
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError("", error.Description);
                    }
                }
            }
            return(View(model));
        }
        public async Task <IActionResult> Create([Bind("CustomerID")] OrderCreateViewModel model)
        {
            var order = SessionHelper
                        .GetObjectFormJson <OrderCreateViewModel>(HttpContext.Session, "order");

            if (order.DetailOrders.Count == 0)
            {
                ModelState.AddModelError("", "Vui lòng thêm sản phẩm vào đơn hàng!");
            }
            if (ModelState.IsValid)
            {
                var customer = await _context.Customers
                               .AsNoTracking()
                               .FirstOrDefaultAsync(c => c.ID == model.CustomerID);

                if (customer == null)
                {
                    return(NotFound());
                }
                var user = await _userManager.GetUserAsync(User);

                if (user == null)
                {
                    RedirectToAction("Login", "Users");
                }
                var orderAdd = new Order
                {
                    ThoiGianTao = order.ThoiGianTao,
                    UserID      = user.Id,
                    CustomerID  = customer.ID
                };
                _context.Orders.Add(orderAdd);
                await _context.SaveChangesAsync();

                foreach (var i in order.DetailOrders)
                {
                    var detailOrder = new DetailOrder
                    {
                        Gia       = i.Gia,
                        SoLuong   = i.SoLuongBan,
                        OrderID   = orderAdd.ID,
                        ProductID = i.ProductID
                    };
                    _context.DetailOrders.Add(detailOrder);
                    var productEdit = await _context.Products
                                      .FirstOrDefaultAsync(p => p.ID == i.ProductID);

                    productEdit.SoLuong -= i.SoLuongBan;
                    _context.Products.Update(productEdit);
                }
                var orderChange = new OrderChange
                {
                    ThoiGian = orderAdd.ThoiGianTao,
                    UserName = user.UserName,
                    HanhDong = "Thêm",
                    OrderID  = orderAdd.ID
                };
                _context.OrderChanges.Add(orderChange);
                await _context.SaveChangesAsync();

                TempData["messageSuccess"] = $"Đơn hàng \"{orderAdd.ID}\" đã thêm";
                HttpContext.Session.Clear();
                return(RedirectToAction("Index"));
            }
            ViewBag.CustomerID = new SelectList(_context.Customers, "ID", "Ten", model.CustomerID);
            ViewBag.Order      = order;
            return(View(model));
        }