public ActionResult Create(AddOrderProducts model) { //AddOrderProducts model = new AddOrderProducts(); try { if (ModelState.IsValid) { using (var dbContext = new DBContext()) { decimal sallary1 = 0; decimal sallary2 = 0; List <Basket> basketProduct = Basket; if (basketProduct.Count == 0) { model.Products = GetAllProducts(); model.Orders = GetOrders(model.ClientId); model.ClientId = model.ClientId; throw new Exception("Nie wybrano żadengo produktu"); } Client client = dbContext.Clients.Find(model.ClientId); Order newOrder = new Order(); newOrder.StartDate = DateTime.Now; if (model.Order.EndDate != null) { newOrder.EndDate = model.Order.EndDate; } newOrder.Seller = this.Session["FirstName"] != null && this.Session["LastName"] != null ? this.Session["FirstName"].ToString() + " " + this.Session["LastName"].ToString() : "Brak"; newOrder.Client = client; foreach (Basket product in basketProduct) { Product item = dbContext.Products.Find(product.ProductId); newOrder.ProductOrders.Add(new ProductOrders() { ProductId = product.ProductId, OrderId = model.Order.OrderId, Amount = product.Amount }); sallary1 += product.Product.ListPrice; sallary2 += product.Product.PurchasePrice; } newOrder.ListPrice = sallary1; newOrder.PurchasePrice = sallary2; dbContext.Orders.Add(newOrder); dbContext.SaveChanges(); Basket = null; } } else { model.Clients = db.Clients.ToList(); return(View(model)); } } catch (Exception ex) { ViewBag.Error = ex.Message; return(View(model)); } return(RedirectToAction("Orders", "Orders", new { id = model.ClientId })); }
public ActionResult Edit(AddOrderProducts model) { if (ModelState.IsValid) { try { decimal sallary1 = 0; decimal sallary2 = 0; List <Basket> basketProduct = Basket; if (basketProduct.Count == 0) { model.Products = GetAllProducts(); model.Orders = GetOrders(model.ClientId); model.ClientId = model.ClientId; throw new Exception("Nie wybrano żadengo produktu"); } Order newOrder = db.Orders.Find(model.Order.OrderId); newOrder.ProductOrders = new List <ProductOrders>(); foreach (Basket item in basketProduct) { newOrder.ProductOrders.Add(new ProductOrders() { ProductId = item.ProductId, Amount = item.Amount, OrderId = newOrder.OrderId }); Product product = db.Products.Find(item.ProductId); sallary1 += product.ListPrice; sallary2 += product.PurchasePrice; } newOrder.Seller = model.Order.Seller; newOrder.StartDate = model.Order.StartDate; newOrder.EndDate = model.Order.EndDate; db.Entry(newOrder).State = EntityState.Modified; db.SaveChanges(); } catch (Exception ex) { Basket = null; } Basket = null; return(RedirectToAction("Orders")); } return(View(model)); }
// GET: Orders/Create public ActionResult Create(int ClientId) { AddOrderProducts model = new AddOrderProducts(); if (ClientId == 0) { model.ClientId = 0; model.Clients = db.Clients.ToList(); model.Order.Seller = this.Session["FirstName"] != null && this.Session["LastName"] != null ? this.Session["FirstName"].ToString() + " " + this.Session["LastName"].ToString() : ""; } else { model.Orders = GetOrders(ClientId); model.ClientId = ClientId; } model.Products = GetAllProducts(); if (Basket != null) { model.Basket = Basket; } return(View(model)); }
// GET: Orders/Edit/5 public ActionResult Edit(int?id) { Basket = null; if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Order order = db.Orders.Find(id); if (order == null) { return(HttpNotFound()); } AddOrderProducts model = new AddOrderProducts(); model.Clients = db.Clients.ToList(); model.Order = order; model.ClientId = order.Client.ClientId; model.Products = GetAllProducts(); Basket = GetBasket(order.ProductOrders); model.Basket = Basket; return(View(model)); }