public ActionResult CheckOut(CheckOutModel checkout) { try { checkout.userdata.UserId = @User.Identity.Name; ViewBag.checkOutMessage = LetsShopImplementation.PlaceOrder(checkout); ShoppingCartImplementation.EmptyCart(@User.Identity.Name); return View(); } catch (Exception ex) { bool rethrow = ExceptionPolicy.HandleException(ex, "UI Policy"); if (rethrow) throw; return View(); } }
//______________________________________________________________________________________ /// <summary> /// This method is used to fetch all the information from the cart and the user, during the placing of an order, and saves the order in the database. /// </summary> /// <param name="userupdate"></param> /// <returns></returns> public static List<string> PlaceOrder(CheckOutModel userupdate) { return DataProvider.PlaceOrder(userupdate); }
//_______________________________________________________________________ /// <summary> /// This method is used to fetch all the information from the cart and the user, during the placing of an order, and saves the order in the database. /// </summary> /// <param name="userupdate"></param> /// <returns></returns> public static List<string> PlaceOrder(CheckOutModel userupdate) { List<string> ls = new List<string>(); try { string orderstatus; int newOrderId; using (TransactionScope ts = new TransactionScope()) { Database _db = EnterpriseLibraryContainer.Current.GetInstance<Database>("LetsShopConnString"); DbCommand cmdObj = _db.GetStoredProcCommand("PlaceOrder"); _db.AddInParameter(cmdObj, "@UserId", DbType.String, userupdate.userdata.UserId); _db.AddInParameter(cmdObj, "@EmailId", DbType.String, userupdate.userdata.EmailId); _db.AddInParameter(cmdObj, "@ShippingAddress", DbType.String, userupdate.userdata.ShippingAddress); _db.AddInParameter(cmdObj, "@BillingAddress", DbType.String, userupdate.userdata.BillingAddress); _db.AddInParameter(cmdObj, "@PhoneNumber", DbType.Int64, Convert.ToInt64(userupdate.userdata.PhoneNumber)); _db.AddInParameter(cmdObj, "@OrderDate", DbType.DateTime, DateTime.Now); _db.AddInParameter(cmdObj, "@PaymentDate", DbType.DateTime, DateTime.Now); _db.AddInParameter(cmdObj, "@ShippingDate", DbType.DateTime, DateTime.Now.AddDays(2)); // 2 days after placing the order _db.AddInParameter(cmdObj, "@TotalAmount", DbType.Double, userupdate.Total); _db.AddOutParameter(cmdObj, "@strMessage", DbType.String, 255); _db.AddOutParameter(cmdObj, "@getOrderId", DbType.Int32, 255); _db.ExecuteNonQuery(cmdObj); orderstatus = _db.GetParameterValue(cmdObj, "@strMessage").ToString(); newOrderId = Int32.Parse(_db.GetParameterValue(cmdObj, "@getOrderId").ToString()); List<AddToCartModel> cartdata = ShoppingCartImplementation.GetFromCart(userupdate.userdata.UserId); try { foreach (var item in cartdata) { DbCommand cmdObj2 = _db.GetStoredProcCommand("ProductsInOrder"); _db.AddInParameter(cmdObj2, "@getOrderId", DbType.Int32, newOrderId); _db.AddInParameter(cmdObj2, "@ProductId", DbType.Int32, item.Product.ProductId); _db.AddInParameter(cmdObj2, "@Price", DbType.Double, item.Product.Price); _db.AddInParameter(cmdObj2, "@Quantity", DbType.Int32, item.Cart.Quantity); _db.AddOutParameter(cmdObj2, "@strMessage", DbType.String, 255); _db.ExecuteNonQuery(cmdObj2); string status = _db.GetParameterValue(cmdObj2, "@strMessage").ToString(); } ts.Complete(); ls.Add(orderstatus); return ls; } catch { //Rollback } if (ls.Count == 0) { //LetsShopImplementation.CancelMyOrder(newOrderId, userupdate.userdata.UserId); for (int i = 0; i < cartdata.Count; i++) { Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("LetsShopConnString"); DbCommand _cmdObj = db.GetStoredProcCommand("RemainingProducts"); db.AddInParameter(_cmdObj, "@ProductId", DbType.Int32, cartdata[i].Product.ProductId); db.AddInParameter(_cmdObj, "@Quantity", DbType.Int32, cartdata[i].Cart.Quantity); db.AddOutParameter(_cmdObj, "@strMessages", DbType.String, 255); db.ExecuteNonQuery(_cmdObj); string remain = db.GetParameterValue(_cmdObj, "@strMessages").ToString(); if (remain != "") { ls.Add(remain); } } return ls; } else return null; } } catch (Exception ex) { bool rethrow = ExceptionPolicy.HandleException(ex, "Database Policy"); if (rethrow) throw; string result = " Error in placing an order."; ls.Add(result); return ls; } }
//______________________________________________________________________________________ /// <summary> /// This method is used to populate the user and the cart details during the checkout of an order. /// </summary> /// <param name="UserId"></param> /// <returns></returns> public static CheckOutModel GetCheckOutDetails(string UserId) { CheckOutModel CheckOut = new CheckOutModel(); UserData userdata = DataProvider.GetUserDetailsByUserName(UserId); CheckOut.userdata = userdata; CheckOut.CartList = ShoppingCartImplementation.GetFromCart(UserId); double total = 0; foreach (var item in CheckOut.CartList) { total = total + item.Cart.Quantity * item.Product.Price; CheckOut.Total = total; } return CheckOut; }