// GET: Orders public ActionResult NewOrder() { var orderView = new OrderView(); orderView.Customer = new Customer(); orderView.Products = new List <ProductOrder>(); Session["orderView"] = orderView; ViewBag.CustomerID = new SelectList(CombosHelper.GetCustomersNames(), "CustomerID", "Name"); return(View(orderView)); }
public ActionResult AddProduct(ProductOrder productOrder) { var orderView = Session["orderView"] as OrderView; var ProductID = int.Parse(Request["ProductID"]); //Caso não haja produto escolhido if (ProductID == 0) { ViewBag.ProductID = new SelectList(CombosHelper.GetProducts(), "ProductID", "Description"); ViewBag.Error = "Deve selecionar um produto"; return(View(productOrder)); } //Verifica se produto existe var Product = db.Products.Find(ProductID); if (Product == null) { ViewBag.ProductID = new SelectList(CombosHelper.GetProducts(), "ProductID", "Description"); ViewBag.Error = "Produto não existe"; return(View(productOrder)); } productOrder = orderView.Products.Find(p => p.ProductID == ProductID); if (productOrder == null) { productOrder = new ProductOrder { Description = Product.Description, Price = Product.Price, ProductID = Product.ProductID, Quantity = float.Parse(Request["Quantity"]) }; orderView.Products.Add(productOrder); } else { productOrder.Quantity += float.Parse(Request["Quantity"]); } ViewBag.CustomerID = new SelectList(CombosHelper.GetCustomersNames(), "CustomerID", "Name"); return(View("NewOrder", orderView)); }
public ActionResult AddProduct(ProductOrder productOrder) { var orderView = Session["orderView"] as OrderView; //isto serve para passar a orderview que esta em cima num outro controlador para este controlador var ProductID = int.Parse(Request["ProductID"]); //isto tras o valor da combo que e escolhido quando se carrega no botao adiciona produto //Caso nao haja produto escolhido if (ProductID == 0) { ViewBag.ProductID = new SelectList(CombosHelper.GetProducts(), "ProductID", "Description"); ViewBag.Error = "Deve selecionar um produto"; return(View(productOrder)); } //Verifica se produto existe var Product = db.Products.Find(ProductID); if (Product == null) { ViewBag.ProductID = new SelectList(CombosHelper.GetProducts(), "ProductID", "Description"); ViewBag.Error = "O produto selecionado nao se encontra disponivel"; return(View(productOrder)); } productOrder = orderView.Products.Find(p => p.ProductID == ProductID); if (productOrder == null) { productOrder = new ProductOrder { Description = Product.Description,//Isto e possivel porque o product order herda do product Price = Product.Price, ProductID = Product.ProductID, Quantity = float.Parse(Request["Quantity"])//Valor que esta na caixa quantity do lado do form }; orderView.Products.Add(productOrder); } else { productOrder.Quantity += float.Parse(Request["Quantity"]);//vai buscar a quantidade da caixa de texto e adiciona } ViewBag.CustomerID = new SelectList(CombosHelper.GetCustomersNames(), "CustomerID", "CustomerFullName"); return(View("NewOrder", orderView)); }
// GET: Orders public ActionResult NewOrder() { var orderView = new OrderView(); orderView.Customer = new Models.Customer(); orderView.Products = new List <ProductOrder>(); //Construir a view baseada neste controlador Session["OrderView"] = orderView;//variavel de sessao //var list = db.Customers.ToList(); //passar os dados para o lado da view ViewBag.CustomerId = new SelectList(CombosHelper.GetCustomersNames(), "CustomerID", "CustomerFullName"); return(View(orderView)); }
public ActionResult NewOrder(OrderView orderView) { orderView = Session["orderView"] as OrderView; var CustomerID = int.Parse(Request["CustomerID"]); if (CustomerID == 0) { ViewBag.CustomerID = new SelectList(CombosHelper.GetCustomersNames(), "CustomerID", "Name"); ViewBag.Error = "Deve selecionar um cliente"; return(View(orderView)); } var customer = db.Customers.Find(CustomerID); if (customer == null) { ViewBag.CustomerID = new SelectList(CombosHelper.GetCustomersNames(), "CustomerID", "Name"); ViewBag.Error = "O cliente não existe"; return(View(orderView)); } if (orderView.Products.Count == 0) { ViewBag.CustomerID = new SelectList(CombosHelper.GetCustomersNames(), "CustomerID", "Name"); ViewBag.Error = "Deve escolher os produtos a encomendar"; return(View(orderView)); } int orderID = 0; using (var transaction = db.Database.BeginTransaction()) { try { var order = new Order { CustomerID = CustomerID, DateOrder = DateTime.Now, OrderStatus = OrderStatus.Created }; db.Orders.Add(order); db.SaveChanges(); orderID = order.OrderID; foreach (var item in orderView.Products) { var orderDetail = new OrderDetail { Description = item.Description, Price = item.Price, Quantity = item.Quantity, OrderID = orderID, ProductID = item.ProductID }; db.OrderDetails.Add(orderDetail); db.SaveChanges(); } transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); ViewBag.Error = $"Erro: {ex.Message}"; ViewBag.CustomerID = new SelectList(CombosHelper.GetCustomersNames(), "CustomerID", "Name"); return(View(orderView)); } } ViewBag.Message = $"A encomenda: {orderID} foi efetuada com sucesso!"; ViewBag.CustomerID = new SelectList(CombosHelper.GetCustomersNames(), "CustomerID", "Name"); orderView = new OrderView(); orderView.Customer = new Customer(); orderView.Products = new List <ProductOrder>(); Session["orderView"] = orderView; return(View(orderView)); }