protected void BtnCheckout_Click(object sender, EventArgs e)
        {
            // when checkout is clicked, each item contained in the cart is saved into Invoice and InvoiceGame
            Invoice invoice = new Invoice
            {
                InvoiceDate = DateTime.Now,
                SubTotal    = subTotal,
                Tax         = tax,
                TotalAmount = total
            };

            // add invoice to db
            DataLayerAccess.AddInvoice(invoice);

            // get id of invoice just added
            Invoice inv = DataLayerAccess.GetLastInvoice();

            foreach (var item in cart)
            {
                InvoiceGame ig = new InvoiceGame
                {
                    InvoiceId    = inv.Id,
                    Price        = item.Item.Price,
                    QuantitySold = item.Quantity,
                    Item         = item.Item,
                    Invoice      = inv
                };

                DataLayerAccess.AddInvoiceGame(ig);

                // update stock
                int newStock = item.Item.InStock - item.Quantity;
                DataLayerAccess.UpdateStock(item.Item.Id, newStock);
            }

            // reset chart, hide buttons, erase cart display and show message
            cart = null;
            HttpContext.Current.Session["Cart"] = null;
            BtnCheckout.Visible  = false;
            BtnResetCart.Visible = false;
            cartContainer.Controls.Clear();
            LblPurchaseResult.Text = "Purchase Successful!";
        }
Example #2
0
 /// <summary>
 /// Adds an invoiceGame to DB
 /// </summary>
 /// <param name="invoiceGame"></param>
 public static void AddInvoiceGame(InvoiceGame invoiceGame)
 {
     _context.InvoiceGames.Add(invoiceGame);
     _context.SaveChanges();
 }