Ejemplo n.º 1
0
        public virtual ActionResult Billing(int chooseAddress = 0)
        {
            var access = Util.SessionAccess;
            var viewModel = new CheckoutBillingViewModel();
            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 soBillingAddress = context.lnksoaddresses
                    .FirstOrDefault(a => a.SalesOrderID == salesOrder.ID && a.Active && a.IsBilling);
                var billingAddress = context.tbladdresses
                    .FirstOrDefault(a => a.AccessID == access.ID && a.Active && a.IsBilling);

                switch (addressType)
                {
                    case CheckoutAddressType.None:
                        if (soBillingAddress == null)
                        {
                            if (billingAddress == 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 = billingAddress.IsPrimary ? CheckoutAddressType.Primary : CheckoutAddressType.Secondary;
                                viewModel.Address = ConstructMyAccountAddress(billingAddress);
                                viewModel.IsAddressReadOnly = true;
                            }
                        }
                        else
                        {
                            viewModel.Address = ConstructMyAccountAddress(soBillingAddress);                            
                            viewModel.IsAddressReadOnly = soBillingAddress.ChooseAddress != (int)CheckoutAddressType.Others;
                            viewModel.SalesOrderBillingAddressID = soBillingAddress.ID;
                            addressType = (CheckoutAddressType)soBillingAddress.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 (soBillingAddress != null && soBillingAddress.ChooseAddress == (int)CheckoutAddressType.Others)
                        {
                            viewModel.Address = ConstructMyAccountAddress(soBillingAddress);
                            viewModel.SalesOrderBillingAddressID = soBillingAddress.ID;
                        }

                        break;

                    default:
                        break;
                }

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


                if (soBillingAddress != null && soBillingAddress.ChooseAddress == (int)addressType)
                {
                    viewModel.FirstName = soBillingAddress.FirstName;
                    viewModel.LastName = soBillingAddress.LastName;
                    viewModel.Phone = string.IsNullOrEmpty(soBillingAddress.Phone) ? access.Phone : soBillingAddress.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);
        }
Ejemplo n.º 2
0
        public virtual ActionResult Billing(CheckoutBillingViewModel viewModel)
        {
            var access = Util.SessionAccess;
            var chooseAddress = 0;

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

                if (viewModel.SalesOrderBillingAddressID == 0)
                {
                    var oldAddress = context.lnksoaddresses
                        .Where(a => a.Active && a.IsBilling && 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,
                        IsBilling = true,
                        LastName = viewModel.LastName,
                        Phone = viewModel.Phone,
                        Postcode = viewModel.Address.Postcode,
                        SalesOrderID = salesOrderID,
                        State = viewModel.Address.State,
                        ChooseAddress = viewModel.AddressTypeID
                    });
                }
                else
                {
                    var salesOrderBillingAddress = context.lnksoaddresses.Single(a => a.ID == viewModel.SalesOrderBillingAddressID);
                    salesOrderBillingAddress.AddressLine1 = viewModel.Address.AddressLine1;
                    salesOrderBillingAddress.AddressLine2 = viewModel.Address.AddressLine2;
                    salesOrderBillingAddress.CityTown = viewModel.Address.CityTown;
                    salesOrderBillingAddress.CountryID = viewModel.Address.CountryID;
                    salesOrderBillingAddress.UpdateDT = DateTime.Now;
                    salesOrderBillingAddress.FirstName = viewModel.FirstName;
                    salesOrderBillingAddress.IsBilling = true;
                    salesOrderBillingAddress.LastName = viewModel.LastName;
                    salesOrderBillingAddress.Phone = viewModel.Phone;
                    salesOrderBillingAddress.Postcode = viewModel.Address.Postcode;
                    salesOrderBillingAddress.State = viewModel.Address.State;
                    salesOrderBillingAddress.ChooseAddress = viewModel.AddressTypeID;
                }

                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 billingExist = context.tbladdresses
                        .Any(a => a.Active && a.AccessID == access.ID && a.IsBilling && a.IsPrimary != isPrimary);
                    var isBilling = billingExist ? 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,
                            IsBilling = isBilling,
                            IsPrimary = isPrimary,
                            Postcode = viewModel.Address.Postcode,
                            State = viewModel.Address.State
                        };
                        context.tbladdresses.Add(accountAddress);
                    }
                    else
                    {
                        accountAddress.IsBilling = isBilling;
                    }
                }

                context.SaveChanges();

                var soAddress = context.lnksoaddresses.FirstOrDefault(a => a.SalesOrderID == salesOrderID && a.Active && a.IsShipping);
                chooseAddress = soAddress == null ? 0 : soAddress.ChooseAddress;
            }

            return RedirectToAction(MVC.Checkout.Shipping(chooseAddress));
        }