Exemplo n.º 1
0
        public virtual ActionResult Shipping(CheckoutShippingViewModel viewModel)
        {
            var access = Util.SessionAccess;

            using (var context = new TTTEntities())
            {
                if (viewModel.SalesOrderAddressID == 0)
                {
                    var salesOrderID = context.trnsalesorders
                        .Include(a => a.refstatu)
                        .Single(a => a.Email == access.Email && a.refstatu.Name == "Open")
                        .ID;

                    var oldAddress = context.lnksoaddresses
                        .Where(a => a.Active && a.IsShipping && a.SalesOrderID == salesOrderID).ToList();
                    oldAddress.ForEach(a => { a.Active = false; a.UpdateDT = DateTime.Now; });

                    context.lnksoaddresses.Add(new lnksoaddress
                    {
                        Active = true,
                        AddressLine1 = viewModel.Address.AddressLine1,
                        AddressLine2 = viewModel.Address.AddressLine2,
                        CityTown = viewModel.Address.CityTown,
                        CountryID = viewModel.Address.CountryID,
                        CreateDT = DateTime.Now,
                        FirstName = viewModel.FirstName,
                        IsShipping = true,
                        LastName = viewModel.LastName,
                        Postcode = viewModel.Address.Postcode,
                        SalesOrderID = salesOrderID,
                        State = viewModel.Address.State,
                        ChooseAddress = viewModel.AddressTypeID,
                        Phone = viewModel.Phone
                    });
                }
                else
                {
                    var salesOrderShippingAddress = context.lnksoaddresses.Single(a => a.ID == viewModel.SalesOrderAddressID);
                    salesOrderShippingAddress.AddressLine1 = viewModel.Address.AddressLine1;
                    salesOrderShippingAddress.AddressLine2 = viewModel.Address.AddressLine2;
                    salesOrderShippingAddress.CityTown = viewModel.Address.CityTown;
                    salesOrderShippingAddress.CountryID = viewModel.Address.CountryID;
                    salesOrderShippingAddress.UpdateDT = DateTime.Now;
                    salesOrderShippingAddress.FirstName = viewModel.FirstName;
                    salesOrderShippingAddress.IsShipping = true;
                    salesOrderShippingAddress.LastName = viewModel.LastName;
                    salesOrderShippingAddress.ChooseAddress = viewModel.AddressTypeID;
                    salesOrderShippingAddress.Postcode = viewModel.Address.Postcode;
                    salesOrderShippingAddress.State = viewModel.Address.State;
                    salesOrderShippingAddress.Phone = viewModel.Phone;
                }

                var addressType = (CheckoutAddressType)viewModel.AddressTypeID;
                if (addressType != CheckoutAddressType.Others)
                {
                    var isPrimary = addressType == CheckoutAddressType.Primary;
                    var accountAddress = context.tbladdresses
                        .FirstOrDefault(a => a.IsPrimary == isPrimary && a.Active && a.AccessID == access.ID);
                    var shippingExist = context.tbladdresses
                        .Any(a => a.Active && a.AccessID == access.ID && a.IsShipping && a.IsPrimary != isPrimary);
                    var isShipping = shippingExist ? false : true;

                    if (accountAddress == null)
                    {
                        accountAddress = new tbladdress
                        {
                            AccessID = access.ID,
                            Active = true,
                            AddressLine1 = viewModel.Address.AddressLine1,
                            AddressLine2 = viewModel.Address.AddressLine2,
                            CityTown = viewModel.Address.CityTown,
                            CountryID = viewModel.Address.CountryID,
                            CreateDT = DateTime.Now,
                            IsShipping = isShipping,
                            IsPrimary = isPrimary,
                            Postcode = viewModel.Address.Postcode,
                            State = viewModel.Address.State
                        };
                        context.tbladdresses.Add(accountAddress);
                    }
                    else
                    {
                        accountAddress.IsShipping = isShipping;
                    }
                }

                context.SaveChanges();
            }

            return RedirectToAction(MVC.Checkout.Summary());
        }
Exemplo n.º 2
0
        public virtual ActionResult Shipping(int chooseAddress = 0)
        {
            var access = Util.SessionAccess;
            var viewModel = new CheckoutShippingViewModel();
            var addressType = (CheckoutAddressType)chooseAddress;

            using (var context = new TTTEntities())
            {
                var salesOrder = context.trnsalesorders
                    .Include(a => a.refstatu)
                    .SingleOrDefault(a => a.Email == access.Email && a.refstatu.Name == "Open");

                if (salesOrder == null)
                    throw new ApplicationException("No Order found.");

                var soShippingAddress = context.lnksoaddresses
                    .FirstOrDefault(a => a.SalesOrderID == salesOrder.ID && a.Active && a.IsShipping);
                var shippingAddress = context.tbladdresses
                    .FirstOrDefault(a => a.AccessID == access.ID && a.Active && a.IsShipping);

                switch (addressType)
                {
                    case CheckoutAddressType.None:
                        if (soShippingAddress == null)
                        {
                            if (shippingAddress == null)
                            {
                                var primayAddress = context.tbladdresses
                                    .FirstOrDefault(a => a.AccessID == access.ID && a.Active && a.IsPrimary);

                                if (primayAddress == null)
                                {
                                    var secondAddress = context.tbladdresses
                                        .FirstOrDefault(a => a.AccessID == access.ID && a.Active && !a.IsPrimary);

                                    if (secondAddress == null)
                                    {
                                        addressType = CheckoutAddressType.Primary;
                                    }
                                    else
                                    {
                                        addressType = CheckoutAddressType.Secondary;
                                        viewModel.Address = ConstructMyAccountAddress(secondAddress);
                                        viewModel.IsAddressReadOnly = true;
                                    }
                                }
                                else
                                {
                                    addressType = CheckoutAddressType.Primary;
                                    viewModel.Address = ConstructMyAccountAddress(primayAddress);
                                    viewModel.IsAddressReadOnly = true;
                                }
                            }
                            else
                            {
                                addressType = shippingAddress.IsPrimary ? CheckoutAddressType.Primary : CheckoutAddressType.Secondary;
                                viewModel.Address = ConstructMyAccountAddress(shippingAddress);
                                viewModel.IsAddressReadOnly = true;
                            }
                        }
                        else
                        {
                            viewModel.Address = ConstructMyAccountAddress(soShippingAddress);
                            viewModel.IsAddressReadOnly = soShippingAddress.ChooseAddress != (int)CheckoutAddressType.Others;
                            viewModel.SalesOrderAddressID = soShippingAddress.ID;
                            addressType = (CheckoutAddressType)soShippingAddress.ChooseAddress;
                        }

                        break;

                    case CheckoutAddressType.Primary:
                    case CheckoutAddressType.Secondary:
                        var isPrimary = addressType == CheckoutAddressType.Primary;
                        var accountAddress = context.tbladdresses
                            .FirstOrDefault(a => a.AccessID == access.ID && a.Active && a.IsPrimary == isPrimary);

                        if (accountAddress != null)
                        {
                            viewModel.Address = ConstructMyAccountAddress(accountAddress);
                            viewModel.IsAddressReadOnly = true;
                        }

                        break;

                    case CheckoutAddressType.Others:
                        if (soShippingAddress != null && soShippingAddress.ChooseAddress == (int)CheckoutAddressType.Others)
                        {
                            viewModel.Address = ConstructMyAccountAddress(soShippingAddress);
                            viewModel.SalesOrderAddressID = soShippingAddress.ID;
                        }

                        break;

                    default:
                        break;
                }

                if (addressType == CheckoutAddressType.None)
                    addressType = CheckoutAddressType.Others;
                viewModel.AddressTypeID = (int)addressType;


                if (soShippingAddress != null && soShippingAddress.ChooseAddress == (int)addressType)
                {
                    viewModel.FirstName = soShippingAddress.FirstName;
                    viewModel.LastName = soShippingAddress.LastName;
                    viewModel.Phone = string.IsNullOrEmpty(soShippingAddress.Phone) ? access.Phone : soShippingAddress.Phone;
                }
                else
                {
                    viewModel.FirstName = access.FirstName;
                    viewModel.LastName = access.LastName;
                    viewModel.Phone = access.Phone;
                }
            }

            viewModel.AddressType = addressType;

            if (viewModel.Address.CountryID == 0)
                viewModel.Address.CountryID = Util.GetDefaultCountryID(); 

            return View(viewModel);
        }