public ActionResult Checkout() { checkOut cout = new checkOut(); double totalPrice = 0; var UserID = User.Identity.GetUserId(); string tokenID = Session.SessionID.ToString(); //update userCart table user id coluam var updatesCart = db.userCart_tbl.Where(a => a.userID == UserID || a.sessionID == tokenID).ToList(); foreach (var c in updatesCart) { c.userID = User.Identity.GetUserId(); db.Entry(c).State = EntityState.Modified; db.SaveChanges(); totalPrice = totalPrice + Convert.ToDouble(c.totalPrice); } cout.totaltaka = totalPrice; ViewBag.totalprice = totalPrice; ViewBag.showMenu = home.menu(); ViewBag.footer = home.footerSocial(); ViewBag.cartPanal = home.cartPanl(tokenID); ViewBag.simpalCart = home.simpalCart(tokenID); ViewBag.paymentMethod = paymentMethod();; ViewBag.shippingMethod = shippingMethod(); //show checkout view user detials var user = adb.Users.Find(UserID); cout.email = user.Email; cout.aspnetuserid = user.Id; //get customer detital var customer = db.customer_tbl.SingleOrDefault(a => a.aspnetuserid == user.Id); if (customer != null) { cout.firstname = customer.firstname; cout.lastname = customer.lastname; cout.telephone = customer.telephone; cout.customer_id = customer.customer_id; //get address detitlas var address = db.address_tbl.SingleOrDefault(a => a.customer_id == customer.customer_id); cout.address_id = address.address_id; cout.shiping_Address = address.shepingAddress; cout.shippingPhoneNumber = address.SheppingPhoneNumber; cout.customerAddress = address.address_2; } ViewBag.Message = TempData["message"]; return(View(cout)); }
public ActionResult Checkout(checkOut couts) { int cID; //customer object customer_tbl ctbl = new customer_tbl(); //address object address_tbl atbl = new address_tbl(); if (ModelState.IsValid) { //get user email form var user = adb.Users.Find(couts.aspnetuserid); //save customer ctbl.lastname = couts.lastname; ctbl.firstname = couts.firstname; ctbl.email = couts.email; ctbl.newsletter = true; ctbl.telephone = couts.telephone; ctbl.aspnetuserid = couts.aspnetuserid; //save addrerss atbl.address_2 = couts.customerAddress; atbl.firstname = couts.firstname; atbl.lastname = couts.lastname; atbl.shepingAddress = couts.shiping_Address; atbl.SheppingPhoneNumber = couts.shippingPhoneNumber; if ((couts.customer_id == null || couts.customer_id == 0) && (couts.address_id == null || couts.address_id == 0)) { //for new customer ctbl.date_added = DateTime.Now; //save in custeomer database db.customer_tbl.Add(ctbl); db.SaveChanges(); //get user id var c = db.customer_tbl.Find(ctbl.customer_id); //save address in database cID = c.customer_id; atbl.customer_id = cID; db.address_tbl.Add(atbl); db.SaveChanges(); } else { ctbl.customer_id = couts.customer_id; db.Entry(ctbl).State = EntityState.Modified; db.SaveChanges(); cID = ctbl.customer_id; atbl.customer_id = cID; atbl.address_id = Convert.ToInt32(couts.address_id); db.Entry(atbl).State = EntityState.Modified; db.SaveChanges(); } //user identiiy field user.PhoneNumber = couts.telephone; user.UserName = couts.firstname; adb.Entry(user).State = EntityState.Modified; adb.SaveChanges(); //get the payment method if (couts.shipping_id != null) { var s = db.payment_method_tbl.Find(couts.shipping_id); couts.belingMethod = s.title; couts.sAmount = Convert.ToDecimal(s.amount); couts.totaltaka += Convert.ToDouble(s.amount); } //get the shipping method if (couts.sid != null) { var s = db.shipping_method.Find(couts.sid); couts.shipping_method = s.Name; } //now start play with order //create order_tbl object order_tbl otbl = new order_tbl(); //save data in order_tbl otbl.customer_id = cID; otbl.date_added = DateTime.Now; otbl.email = couts.email; otbl.firstname = couts.firstname; otbl.lastname = couts.lastname; otbl.payment_address_1 = couts.customerAddress; otbl.payment_method = couts.belingMethod; otbl.payment_telephone = couts.telephone; otbl.shipping_address_1 = couts.shiping_Address; otbl.shipping_telephone = couts.shippingPhoneNumber; otbl.total = Convert.ToDecimal(couts.totaltaka); otbl.amont = couts.sAmount; otbl.shipping_method = couts.shipping_method; db.order_tbl.Add(otbl); db.SaveChanges(); //get otbl id //get order table id var order = db.order_tbl.Find(otbl.order_id); //next save prodect data in product order table order_product_tbl optbl = new order_product_tbl(); var ucart = db.userCart_tbl.Where(a => a.userID == user.Id).ToList(); foreach (var u in ucart) { optbl.order_id = order.order_id; optbl.product_id = u.product_ID; optbl.quantity = u.Quantity; optbl.dicount = u.discountPrice; optbl.price = u.varintPrice + u.mainPrice; optbl.total = u.totalPrice; optbl.name = u.productName; optbl.varints = u.pvvalueID; //save single product db.order_product_tbl.Add(optbl); db.SaveChanges(); ////get order_product tbl //var o = db.order_product_tbl.Find(optbl.order_product_id); //if (o.varints != null || o.varints != "null") //{ // List<int> s = JsonConvert.DeserializeObject<List<int>>(o.varints); // if (s != null) // { // foreach (var i in s) // { // //update varinat unit // var pvv = db.product_variant_value.Find(i); // pvv.quentity -= u.Quantity; // db.Entry(pvv).State = EntityState.Modified; // db.SaveChanges(); // } // } //} ////update product quantity //var p = db.product_tbl.Find(u.product_ID); //p.quantity -= u.Quantity; //db.Entry(p).State = EntityState // .Modified; //db.SaveChanges(); } //delete cart table data foreach (var d in ucart) { db.userCart_tbl.Remove(d); db.SaveChanges(); } //add successfully message message = "Your Order Is Successfully Complete. We Contect YOu Very Soon Thank you"; //update quantity } else { message = "Insert All Data"; } TempData["message"] = message; Session["cart"] = null; return(RedirectToAction("Checkout")); }