示例#1
0
 public int addAddressBook(AddressBook addressBook)
 {
     try
     {
         if (ModelState.IsValid)
         {
             addressBook.IsDefault = false;
             int temp = addressBookService.Insert(addressBook);
             if (temp > 0)
             {
                 return(1);
             }
         }
     }
     catch (Exception ex)
     {
         LogService.WriteException(ex);
     }
     return(0);
 }
示例#2
0
        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 }));
        }