public ActionResult index(CheckoutViewModel checkoutViewModel) { try { if (checkoutViewModel.BillingDistrictID == 0) { checkoutViewModel.BillingDistrictID = null; } if (checkoutViewModel.BillingProvinceID == 0) { checkoutViewModel.BillingProvinceID = null; } if (checkoutViewModel.ShippingDistrictID == 0) { checkoutViewModel.ShippingDistrictID = null; } if (checkoutViewModel.ShippingProvinceID == 0) { checkoutViewModel.ShippingProvinceID = null; } TblCart cart = cartService.GetByCookieID(checkoutViewModel.CookieID); cart.CartItems = cartItemService.GetByCartID(cart.CartID); if (cart.CartItems != null && cart.CartItems.Count > 0) { foreach (var item in cart.CartItems) { item.Variant = variantService.GetByPrimaryKey(item.VariantID); if (item.Variant != null) { item.Variant.Product = productService.GetByPrimaryKey(item.Variant.ProductID); } } } checkoutViewModel.CartItems = cart.CartItems; checkoutViewModel.CartID = cart.CartID; // create or update customer Customer customer = customerService.GetByEmail(checkoutViewModel.CustomerEmail); if (customer == null) { customer = new Customer(); customer.CustomerFirstName = checkoutViewModel.BillingAddress.CustomerName; customer.CustomerEmail = checkoutViewModel.CustomerEmail; customer.TotalCount = 0; customer.TotalOrder = 1; customer.CreatedDateTime = SDateTime.GetYYYYMMddHmmSSNow(); customer.CustomerID = customerService.Insert(customer); } else { customer.TotalCount += checkoutViewModel.TotalPrice; customer.TotalOrder += 1; customer.ModifiedDateTime = SDateTime.GetYYYYMMddHmmSSNow(); customerService.Update(customer); } AddressBook addressBook = new AddressBook(); addressBook.AddressBookFirstName = checkoutViewModel.BillingAddress.CustomerName; addressBook.Phone = checkoutViewModel.BillingAddress.Phone; addressBook.HomeAddress = checkoutViewModel.BillingAddress.HomeAddress; addressBook.DistrictID = checkoutViewModel.BillingDistrictID; addressBook.ProvinceID = checkoutViewModel.BillingProvinceID; addressBook.ProvinceName = provinceService.GetProvinceName(checkoutViewModel.BillingProvinceID == null ? 0 : checkoutViewModel.BillingProvinceID.Value); addressBook.CountryID = Common.Country_VietNameID; addressBook.CustomerID = customer.CustomerID; addressBook.IsDefault = true; addressBookService.Insert(addressBook); if (checkoutViewModel.OtherShippingAddress) { addressBook.AddressBookFirstName = checkoutViewModel.ShippingAddress.CustomerName; addressBook.Phone = checkoutViewModel.ShippingAddress.Phone; addressBook.HomeAddress = checkoutViewModel.ShippingAddress.HomeAddress; addressBook.DistrictID = checkoutViewModel.ShippingDistrictID; addressBook.ProvinceID = checkoutViewModel.ShippingProvinceID; addressBook.ProvinceName = provinceService.GetProvinceName(checkoutViewModel.ShippingProvinceID == null ? 0 : checkoutViewModel.ShippingProvinceID.Value); addressBook.CountryID = Common.Country_VietNameID; addressBook.CustomerID = customer.CustomerID; addressBook.IsDefault = false; addressBookService.Insert(addressBook); } int billingAddressID = 0, shippingAddressID = 0; // contructor billing address checkoutViewModel.BillingAddress.ProvinceID = checkoutViewModel.BillingProvinceID; checkoutViewModel.BillingAddress.ProvinceName = provinceService.GetProvinceName(checkoutViewModel.BillingProvinceID == null ? 0 : checkoutViewModel.BillingProvinceID.Value); checkoutViewModel.BillingAddress.CountryID = 1; if (SNumber.ToNumber(checkoutViewModel.BillingDistrictID) > 0) { checkoutViewModel.BillingAddress.DistrictID = checkoutViewModel.BillingDistrictID; //checkoutViewModel.BillingAddress.DistrictName = districtService.get } else { checkoutViewModel.BillingAddress.DistrictID = null; } // contructor shipping address // shipping address different billing address if (checkoutViewModel.OtherShippingAddress) { checkoutViewModel.ShippingAddress.ProvinceID = checkoutViewModel.ShippingProvinceID; checkoutViewModel.ShippingAddress.ProvinceName = provinceService.GetProvinceName(checkoutViewModel.ShippingProvinceID == null ? 0 : checkoutViewModel.ShippingProvinceID.Value); checkoutViewModel.ShippingAddress.CountryID = 1; if (SNumber.ToNumber(checkoutViewModel.ShippingDistrictID) > 0) { checkoutViewModel.ShippingAddress.DistrictID = checkoutViewModel.ShippingDistrictID; } else { checkoutViewModel.ShippingAddress.DistrictID = null; } } // shipping address as billing address else { checkoutViewModel.ShippingAddress.CustomerName = checkoutViewModel.BillingAddress.CustomerName; checkoutViewModel.ShippingAddress.ProvinceID = checkoutViewModel.BillingProvinceID; checkoutViewModel.ShippingAddress.ProvinceName = provinceService.GetProvinceName(checkoutViewModel.BillingProvinceID == null ? 0 : checkoutViewModel.BillingProvinceID.Value); checkoutViewModel.ShippingAddress.CountryID = 1; if (SNumber.ToNumber(checkoutViewModel.BillingDistrictID) > 0) { checkoutViewModel.ShippingAddress.DistrictID = checkoutViewModel.BillingDistrictID; } else { checkoutViewModel.ShippingAddress.DistrictID = null; } } billingAddressID = billingAddressService.Insert(checkoutViewModel.BillingAddress); shippingAddressID = shippingAddressService.Insert(checkoutViewModel.ShippingAddress); // create order & line item TblOrder order = new TblOrder(); order.OrderStatus = Common.Active; order.CustomerID = customer.CustomerID; order.BillingStatus = Common.Pending; order.ShippingStatus = Common.Unfulfilled; order.OrderNote = checkoutViewModel.CheckoutNote; order.TotalCount = checkoutViewModel.TotalSubPrice; order.TotalShipping = checkoutViewModel.TotalShipping; order.CustomerEmail = checkoutViewModel.CustomerEmail; if (shippingAddressID == 0) { order.ShippingAddressID = null; } else { order.ShippingAddressID = shippingAddressID; } if (billingAddressID == 0) { order.BillingAddressID = null; } else { order.BillingAddressID = billingAddressID; } order.Number = orderService.GetLastNumber() + 1; order.CreatedDateTime = order.ModifiedDateTime = SDateTime.GetYYYYMMddHmmSSNow(); int orderID = orderService.Insert(order); if (SNumber.ToNumber(orderID) > 0) { if (checkoutViewModel.CartItems != null && checkoutViewModel.CartItems.Count > 0) { foreach (var item in checkoutViewModel.CartItems) { item.OrderID = orderID; LineItem lineItem = cartItemService.ToLineItem(item); lineItemService.Insert(lineItem); } } } // remove cart and cartItem if (cart != null) { cartItemService.DeleteByCartID(cart.CartID); } return(RedirectToAction("thankyou", "checkout", new { id = orderID })); } catch (Exception ex) { LogService.WriteException(ex); } return(RedirectToAction("index", "checkout", new { cookieID = checkoutViewModel.CookieID })); }
public ActionResult create(CreateOrderModel createOrderModel) { if (ModelState.IsValid) { int billingAddressID = 0, shippingAddressID = 0; decimal totalCount = 0; if (createOrderModel.LineItems != null && createOrderModel.LineItems.Count > 0) { foreach (var item in createOrderModel.LineItems) { totalCount += item.Price * item.Quantity; } } TblOrder order = new TblOrder(); order.OrderStatus = Common.Active; if (createOrderModel.Customer != null) { order.CustomerEmail = createOrderModel.Customer.CustomerEmail; if (SNumber.ToNumber(createOrderModel.Customer.CustomerID) > 0) { order.CustomerID = createOrderModel.Customer.CustomerID; BillingAddress billingAddress = new BillingAddress(); billingAddress.CountryID = createOrderModel.BillingAddress.CountryID; billingAddress.CountryName = createOrderModel.BillingAddress.CountryName; billingAddress.CustomerName = createOrderModel.BillingAddress.CustomerName; billingAddress.HomeAddress = createOrderModel.BillingAddress.HomeAddress; billingAddress.Phone = createOrderModel.BillingAddress.Phone; billingAddress.ProvinceName = createOrderModel.BillingAddress.ProvinceName; billingAddressID = billingAddressService.Insert(billingAddress); ShippingAddress shippingAddress = new ShippingAddress(); shippingAddress.CountryID = createOrderModel.ShippingAddress.CountryID; shippingAddress.CountryName = createOrderModel.ShippingAddress.CountryName; shippingAddress.CustomerName = createOrderModel.ShippingAddress.CustomerName; shippingAddress.HomeAddress = createOrderModel.ShippingAddress.HomeAddress; shippingAddress.Phone = createOrderModel.ShippingAddress.Phone; shippingAddress.ProvinceName = createOrderModel.ShippingAddress.ProvinceName; shippingAddressID = shippingAddressService.Insert(shippingAddress); } else { order.CustomerID = null; } order.BillingStatus = createOrderModel.BillingStatus; order.ShippingStatus = Common.Unfulfilled; order.OrderNote = createOrderModel.OrderNote; order.OrderStatus = Common.Active; if (shippingAddressID == 0) { order.ShippingAddressID = null; } else { order.ShippingAddressID = shippingAddressID; } if (billingAddressID == 0) { order.BillingAddressID = null; } else { order.BillingAddressID = billingAddressID; } order.TotalCount = totalCount; order.Number = orderService.GetLastNumber() + 1; order.CreatedDateTime = order.ModifiedDateTime = SDateTime.GetYYYYMMddHmmSSNow(); int orderID = orderService.Insert(order); if (SNumber.ToNumber(orderID) > 0) { if (order.BillingStatus == Common.Paid) { if (SNumber.ToNumber(order.CustomerID) > 0) { Customer customer = customerService.GetByPrimaryKey(SNumber.ToNumber(order.CustomerID)); customer.TotalCount += totalCount; customer.TotalOrder += 1; customer.ModifiedDateTime = SDateTime.GetYYYYMMddHmmSSNow(); customerService.Update(customer); } } string orderName = "#" + (Common.BaseNumberOrder + order.Number).ToString(); LogService.WriteLog2DB(accountService.GetUserId(User.Identity.GetUserName()), (int)Common.ActionID.Insert, orderID, SDateTime.GetYYYYMMddHmmSSNow(), General.GetIPAddress(), TableNameID, orderName); if (createOrderModel.LineItems != null && createOrderModel.LineItems.Count > 0) { foreach (var item in createOrderModel.LineItems) { item.OrderID = orderID; item.ShippingStatus = null; if (item.IsDefault) { item.VariantName = "Default Title"; } lineItemService.Insert(item); } } return(Json(new { id = orderID })); } } } string strErrorMessage = ""; foreach (ModelState modelState in ViewData.ModelState.Values) { foreach (ModelError error in modelState.Errors) { strErrorMessage += error.ErrorMessage + "<br/>"; } } return(Json(new { id = "0", error = strErrorMessage })); }