protected void btnCheckout_Click(object sender, EventArgs e) { using (DataAccessContainer ctx = new DataAccessContainer()) { DateTime now = DateTime.Now; Int.User usr = ctx.Users.FirstOrDefault(u => u.Id == loggedInUser.Id); Invoice nInv = new Invoice() { Date = now, IsPaid = false }; Order nOrder = new Order() { Date = now, Invoice = nInv, Discount = 0 }; decimal totalAmount = 0; GetCartItems().ForEach(ci => { OrderProduct nOrderProd = new OrderProduct() { Quantity = ci.Quantity, Product = ctx.Products.FirstOrDefault(p=>p.Id == ci.ProductId) }; nOrder.OrderProducts.Add(nOrderProd); totalAmount += ci.Price * ci.Quantity; }); //calulate discount if (totalAmount >= 50) { nOrder.Discount += 10; } if(totalAmount >= 75) { nOrder.Discount += 10; } if (totalAmount >= 100) { nOrder.Discount += 10; } //save order and redirect usr.Orders.Add(nOrder); ctx.SaveChanges(); ClearCart(); Response.Redirect("ViewInvoiceAfterOrder.aspx?id=" + nOrder.Id); } }
/// <summary> /// Create a new OrderProduct object. /// </summary> /// <param name="id">Initial value of the Id property.</param> /// <param name="quantity">Initial value of the Quantity property.</param> public static OrderProduct CreateOrderProduct(global::System.Int32 id, global::System.Int32 quantity) { OrderProduct orderProduct = new OrderProduct(); orderProduct.Id = id; orderProduct.Quantity = quantity; return orderProduct; }
/// <summary> /// Deprecated Method for adding a new object to the OrderProducts EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToOrderProducts(OrderProduct orderProduct) { base.AddObject("OrderProducts", orderProduct); }