public int BuyCart(List <Book> list, string usernm, Guid id, double price) // need to change { BookshopEntities context = new BookshopEntities(); IssueTran tran = new IssueTran(); tran.UserId = id; tran.DateTimePurchased = DateTime.Now; tran.TotalPrice = Decimal.Parse(price.ToString()); context.IssueTrans.Add(tran); foreach (Book b in list) { IssueTransBookList transbook = new IssueTransBookList(); transbook.TransactionID = tran.TransactionID; transbook.BookID = b.BookID; transbook.Quantity = b.Stock; context.IssueTransBookLists.Add(transbook); Book xe = context.Books.Where(x => x.BookID == transbook.BookID).First(); if (xe.Stock < b.Stock) { return(-1); } xe.Stock -= b.Stock; } context.SaveChanges(); return(tran.TransactionID); }
private void Checkout_Click(object sender, EventArgs e) { StatusLabel.Text = ""; // Verify Customer try { Customer c = GetCustomerById(LoanCustomerID.Text); List <DataGridViewRow> checkedOutMovies = new List <DataGridViewRow>(); foreach (DataGridViewRow row in LoanOutput.Rows) { // Check stock short movieId = (short)row.Cells["LoanVideoCode"].Value; Movie m = context.Movies.First(x => movieId == x.VideoCode); if (m.TotalStock - m.NumberRented > 0) { // Create new IssueTran object IssueTran tran = new IssueTran(); tran.CustomerID = LoanCustomerID.Text; tran.Movie = m; tran.DateIssue = today; tran.DateDue = dueDate; tran.DateActualReturn = null; tran.RentalStatus = "out"; if (!(row.Cells["LoanRemarks"].Value is null)) { tran.Remarks = row.Cells["LoanRemarks"].Value.ToString(); } else { tran.Remarks = ""; } context.IssueTrans.Add(tran); m.NumberRented++; context.SaveChanges(); checkedOutMovies.Add(row); } } foreach (DataGridViewRow row in checkedOutMovies) { LoanOutput.Rows.Remove(row); } if (LoanOutput.Rows.Count > 0) { StatusLabel.Text = "There should not be any stock remaining for the above movies. Please double check."; } else { ClearForms(); StatusLabel.Text = String.Format("Successfully checkedout movies for {0}.", c.CustomerName); } }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //TO-DO //Should store username inside Session["User"] in order to display it on our page.; if (Session["tranid"] == null || (int)Session["tranid"] == 0) { Response.Redirect("/Team13Bookshop/Default.aspx", false); return; } int tranId = (int)Session["tranid"]; BookshopEntities maketransaction = new BookshopEntities(); List <IssueTransBookList> tranList = maketransaction.IssueTransBookLists.Where(x => x.TransactionID == tranId).ToList(); IssueTran issuetran = maketransaction.IssueTrans.Where(x => x.TransactionID == tranId).ToList().First(); TextBoxTranID.Text = issuetran.TransactionID.ToString(); TextBoxTranDate.Text = issuetran.DateTimePurchased.ToString(); List <TransactionModel> books = new List <TransactionModel>(); foreach (IssueTransBookList a in tranList) { TransactionModel b = new TransactionModel(); Book foundBook = maketransaction.Books.Where(x => x.BookID == a.BookID).ToList().First(); b.ISBN = foundBook.ISBN; b.Price = Math.Round((double)(foundBook.Price * (100 - foundBook.Discount)) * 0.01, 2); b.Quantity = a.Quantity; b.Title = foundBook.Title; books.Add(b); } double totalPrice = 0; StringBuilder htmlWriter = new StringBuilder(); foreach (TransactionModel tran in books) { htmlWriter.Append("<div class='card'>"); htmlWriter.Append("<div class='image'>"); htmlWriter.Append("<img alt='Image Unavailable' src='/Team13BookShop/images/" + tran.ISBN + ".jpg' />"); htmlWriter.Append("</div>"); htmlWriter.Append("<div class='content'>"); htmlWriter.Append("<div class='header'>" + tran.Title + "</div>"); htmlWriter.Append("<div class='meta'>"); htmlWriter.Append("<a>Prices: " + tran.Price + "</a>"); htmlWriter.Append("</div>"); htmlWriter.Append("</div>"); htmlWriter.Append("<div class='extra content'>"); htmlWriter.Append("<span class='right floated'>Total Price: " + tran.Quantity * tran.Price + "</span>"); htmlWriter.Append("<span>Quantity: " + tran.Quantity + "</span>"); htmlWriter.Append("</div>"); htmlWriter.Append("</div>"); totalPrice += tran.Quantity * tran.Price; } PlaceHolder1.Controls.Add(new Literal { Text = htmlWriter.ToString() }); InputTotalPrice.Value = "$ " + totalPrice.ToString(); } ////TO-DO ////Should store username inside Session["User"] in order to display it on our page.; //int tranId = Int32.Parse(Request.QueryString["tranId"]); //BookshopEntities maketransaction = new BookshopEntities(); //List<IssueTransBookLists> tranList = maketransaction.IssueTransBookLists.Where(x => x.TransactionID == tranId).ToList(); //IssueTrans issuetran = maketransaction.IssueTrans.Where(x => x.TransactionID == tranId).ToList().First(); //TextBoxTranID.Text = issuetran.TransactionID.ToString(); //TextBoxTranDate.Text = issuetran.DateTimePurchased.ToString(); //List<TransactionModel> books = new List<TransactionModel>(); //foreach (IssueTransBookLists a in tranList) //{ // TransactionModel b = new TransactionModel(); // Book foundBook = maketransaction.Book.Where(x => x.BookID == a.BookID).ToList().First(); // b.ISBN = foundBook.ISBN; // b.Price = Double.Parse(foundBook.Price.ToString()); // b.Quantity = a.Qty; // b.Title = foundBook.Title; // books.Add(b); //} //double totalPrice = 0; //StringBuilder htmlWriter = new StringBuilder(); //foreach (TransactionModel tran in books) //{ // htmlWriter.Append("<div class='card'>"); // htmlWriter.Append("<div class='image'>"); // htmlWriter.Append("<img src='images/" + tran.ISBN + ".jpg' />"); // htmlWriter.Append("</div>"); // htmlWriter.Append("<div class='content'>"); // htmlWriter.Append("<div class='header'>" + tran.Title + "</div>"); // htmlWriter.Append("<div class='meta'>"); // htmlWriter.Append("<a>Prices: " + tran.Price + "</a>"); // htmlWriter.Append("</div>"); // htmlWriter.Append("</div>"); // htmlWriter.Append("<div class='extra content'>"); // htmlWriter.Append("<span class='right floated'>Total Price: " + tran.Quantity * tran.Price + "</span>"); // htmlWriter.Append("<span>Quantity: " + tran.Quantity + "</span>"); // htmlWriter.Append("</div>"); // htmlWriter.Append("</div>"); // totalPrice += tran.Quantity * tran.Price; //} //PlaceHolder1.Controls.Add(new Literal { Text = htmlWriter.ToString() }); //InputTotalPrice.Value = "$ " + totalPrice.ToString(); }