private bool DowngradePlan(Store s, UserAccount a, HostedPlan newPlan, MerchantTribeApplication app)
        {
            int currentProductCount = app.CatalogServices.Products.FindAllCount(s.Id);

            if (currentProductCount > newPlan.MaxProducts)
            {
                return(false);
            }

            s.CurrentPlanRate    = newPlan.Rate;
            s.CurrentPlanPercent = newPlan.PercentageOfSales;
            //s.CurrentPlanDayOfMonth = DateTime.Now.Day;
            //if (s.CurrentPlanDayOfMonth > 28) s.CurrentPlanDayOfMonth = 28;

            s.PlanId = newPlan.Id;
            bool result = Stores.Update(s);

            if (result)
            {
                // Charge Card
                // Notify Admin of Change
                Utilities.MailServices.SendPlanDowngradeAlert(a, s);
            }
            return(result);
        }
        public bool ChangePlan(long storeId, long userId, int newPlanId, MerchantTribeApplication app)
        {
            Store s = Stores.FindById(storeId);

            if (s == null)
            {
                return(false);
            }
            if (s.Id < 1)
            {
                return(false);
            }
            if (s.Status == StoreStatus.Deactivated)
            {
                return(false);
            }

            UserAccount a = AdminUsers.FindById(userId);

            if (a == null)
            {
                return(false);
            }
            if (a.Id < 0)
            {
                return(false);
            }

            HostedPlan newPlan = HostedPlan.FindById(newPlanId);

            if (newPlan == null)
            {
                return(false);
            }

            if (newPlanId > s.PlanId)
            {
                return(UpgradePlan(s, a, newPlan));
            }
            if (newPlanId < s.PlanId)
            {
                return(DowngradePlan(s, a, newPlan, app));
            }

            // no chanes to plan
            return(true);
        }
        private bool UpgradePlan(Store s, UserAccount a, HostedPlan newPlan)
        {
            s.CurrentPlanRate       = newPlan.Rate;
            s.CurrentPlanPercent    = newPlan.PercentageOfSales;
            s.CurrentPlanDayOfMonth = DateTime.Now.Day;
            if (s.CurrentPlanDayOfMonth > 28)
            {
                s.CurrentPlanDayOfMonth = 28;
            }

            s.PlanId = newPlan.Id;
            bool result = Stores.Update(s);

            if (result)
            {
                // Charge Card
                // Notify Admin of Change
                Utilities.MailServices.SendPlanUpgradeAlert(a, s);
            }
            return(result);
        }
        private bool UpgradePlan(Store s, UserAccount a, HostedPlan newPlan)
        {
            s.CurrentPlanRate = newPlan.Rate;
            s.CurrentPlanPercent = newPlan.PercentageOfSales;
            s.CurrentPlanDayOfMonth = DateTime.Now.Day;
            if (s.CurrentPlanDayOfMonth > 28) s.CurrentPlanDayOfMonth = 28;

            s.PlanId = newPlan.Id;
            bool result = Stores.Update(s);
            if (result)
            {
                // Charge Card
                // Notify Admin of Change
                Utilities.MailServices.SendPlanUpgradeAlert(a, s);
            }
            return result;
        }
        private bool DowngradePlan(Store s, UserAccount a, HostedPlan newPlan, MerchantTribeApplication app)
        {
            int currentProductCount = app.CatalogServices.Products.FindAllCount(s.Id);
            if (currentProductCount > newPlan.MaxProducts) return false;

            s.CurrentPlanRate = newPlan.Rate;
            s.CurrentPlanPercent = newPlan.PercentageOfSales;
            //s.CurrentPlanDayOfMonth = DateTime.Now.Day;
            //if (s.CurrentPlanDayOfMonth > 28) s.CurrentPlanDayOfMonth = 28;

            s.PlanId = newPlan.Id;
            bool result = Stores.Update(s);
            if (result)
            {
                // Charge Card
                // Notify Admin of Change
                Utilities.MailServices.SendPlanDowngradeAlert(a, s);
            }
            return result;
        }
        public Store CreateAndSetupStore(string storeName,
                                         long userAccountId,
                                         string friendlyName,
                                         int plan,
                                         decimal rate,
                                         MerchantTribe.Billing.BillingAccount billingAccount)
        {
            Store s = null;

            if (StoreNameExists(storeName))
            {
                throw new CreateStoreException("That store name is not available. Please choose another name and try again.");
            }

            s             = new Store();
            s.StoreName   = Text.ForceAlphaNumericOnly(storeName).ToLower();
            s.Status      = StoreStatus.Active;
            s.DateCreated = DateTime.UtcNow;
            s.PlanId      = plan;
            s.CustomUrl   = string.Empty;

            if (!Stores.Create(s))
            {
                throw new CreateStoreException("Unable to create store. Unknown error. Please contact an administrator for assistance.");
            }

            s = Stores.FindByStoreName(s.StoreName);
            if (s != null)
            {
                AddUserToStore(s.Id, userAccountId, StoreAccessMode.Owner);

                s.Settings.FriendlyName         = friendlyName;
                s.Settings.MailServer.FromEmail = "*****@*****.**";
                s.Settings.LastOrderNumber      = 0;
                s.Settings.LogoImage            = "[[default]]";
                s.Settings.LogoRevision         = 0;
                s.Settings.UseLogoImage         = false;
                s.Settings.LogoText             = s.StoreName;
                s.Settings.MinumumOrderAmount   = 0;

                // Send Reminder of account information to new user
                Accounts.UserAccount u = AdminUsers.FindById(userAccountId);

                s.Settings.MailServer.EmailForGeneral     = u.Email;
                s.Settings.MailServer.EmailForNewOrder    = u.Email;
                s.Settings.MailServer.UseCustomMailServer = false;
                s.Settings.ProductReviewCount             = 3;
                s.Settings.ProductReviewModerate          = true;
                s.Settings.ProductReviewShowRating        = true;
                s.Settings.PayPal.FastSignupEmail         = u.Email;
                s.Settings.PayPal.Currency   = "USD";
                s.Settings.MaxItemsPerOrder  = 999;
                s.Settings.MaxWeightPerOrder = 9999;
                s.Settings.MaxOrderMessage   = "That's a really big order! Call us instead of ordering online.";

                s.CurrentPlanRate       = rate;
                s.CurrentPlanDayOfMonth = DateTime.Now.Day;
                if (s.CurrentPlanDayOfMonth > 28)
                {
                    s.CurrentPlanDayOfMonth = 28;
                }
                HostedPlan thePlan = HostedPlan.FindById(s.PlanId);
                if (thePlan != null)
                {
                    s.CurrentPlanPercent = thePlan.PercentageOfSales;
                }
                else
                {
                    if (plan == 0)
                    {
                        s.CurrentPlanPercent = 0;
                    }
                    else
                    {
                        s.CurrentPlanPercent = 0;
                    }
                }

                // Save data to store
                Stores.Update(s);

                // Create Billing Accout
                MerchantTribe.Billing.Service svc = new MerchantTribe.Billing.Service(WebAppSettings.ApplicationConnectionString);
                BillingAccount act = svc.Accounts.FindOrCreate(billingAccount);


                Utilities.MailServices.SendLeadAlert(u, s);
                Utilities.MailServices.SendAccountInformation(u, s);
            }

            return(s);
        }