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 Invoice object. /// </summary> /// <param name="id">Initial value of the Id property.</param> /// <param name="date">Initial value of the Date property.</param> /// <param name="isPaid">Initial value of the IsPaid property.</param> public static Invoice CreateInvoice(global::System.Int32 id, global::System.DateTime date, global::System.Boolean isPaid) { Invoice invoice = new Invoice(); invoice.Id = id; invoice.Date = date; invoice.IsPaid = isPaid; return invoice; }
/// <summary> /// Deprecated Method for adding a new object to the Invoices EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToInvoices(Invoice invoice) { base.AddObject("Invoices", invoice); }