public PurchasedFeatures SavePurchase(PurchasedFeatures purchase)
 {
     if (purchase.ID == 0)
     {
         context.PurchasedFeatures.Add(purchase);
         context.SaveChanges();
     }
     return purchase;
 }
        public ActionResult SaveAccount(SignUpViewModel sgnv)
        {
            if (ModelState.IsValid)
            {
                // check email already exist
                var isEmailExist = userRepository.Users.Where(email => email.Email == sgnv.User.Email).ToList();
                var isAccNameExist = accountRepository.Accounts.Where(acc => acc.AccountName == sgnv.Account.AccountName).ToList();

                if (isEmailExist.Count() == 0 && isAccNameExist.Count() ==0)
                {
                    //save account details
                    sgnv.Account.AccountGUID = System.Guid.NewGuid().ToString();
                    var account = accountRepository.SaveAccount(sgnv.Account);
                    account.TablePrefix = account.ID.ToString("X");
                    account.PlanID = 2;
                    account.TimeZone = sgnv.Account.TimeZone;
                    account.Telephone = sgnv.Account.Telephone;
                    account.CreatedDate = DateTime.Now;
                    account.LastModifiedDate = DateTime.Now;
                    account.HasPurchased = false;
                    account.isOverFlow = false;
                    account.isPaymentIssue = false;
                    account.SyncPeriod = (short)featureRepository.Features.Where(pl => pl.PlanLevel == 20 && pl.Type == "Sync Period").First().Quantity;
                    account = accountRepository.SaveAccount(sgnv.Account);
                    Session["accountid"] = account.ID;

                    // save user details
                    sgnv.User.UserType = "Admin";
                    sgnv.User.PrimaryUser = true;
                    sgnv.User.AccountID = (long)Session["accountid"];
                    sgnv.User.Password = Encryption.EncryptStringAES(sgnv.User.Password, rand);
                    sgnv.User.isPasswordChange = true;
                    sgnv.User.ConfirmPassword = sgnv.User.Password;
                    sgnv.User.CreatedDate = DateTime.Now;
                    sgnv.User.LastModifiedDate = DateTime.Now;
                    var user = userRepository.SaveUser(sgnv.User);

                    // save package
                    var featuresForDefaultPlan = featureRepository.Features.Where(pl => pl.PlanLevel == 20).ToList();

                    foreach (var feat in featuresForDefaultPlan)
                    {
                        PurchasedFeatures objFeature = new PurchasedFeatures();
                        objFeature.AccountGUID = account.AccountGUID;
                        objFeature.FeatureID = feat.ID;
                        objFeature.ExpiryDate = DateTime.UtcNow;
                        objFeature.Enabled = true;
                        objFeature.Quantity = feat.Quantity;
                        ppRepository.SavePurchase(objFeature);
                    }

                    // send Email Notification
                    user.GUID = Guid.NewGuid().ToString();
                    var notifObj = new Notification()
                    {
                        Subject = "Welcome to Corporate contacts !",
                        RecipientAddress = user.Email,
                        //RecipientAddress = "*****@*****.**",
                        // Message = "Hello, \n\n" + user.FullName + " \nAccount successfully created.\nPlease click the link below to activate your account.\n\n" + Request.Url.GetLeftPart(UriPartial.Authority) + "/VerifyUserAccount/VerifyEmailAddress?GUID=" + user.GUID + "\n\nCorporate Contacts Team"
                        //Message = "Dear " + user.FullName + ",\n"
                        //+ "Thank you for registering with Corporate Contacts. You have created a new account:\n\n"
                        //+ "User Name: " + user.FullName + "\nEmail: " + user.Email +
                        //"\n\nYou still need to click the link below to verify your email address. Please follow the link below:  \n" + Request.Url.GetLeftPart(UriPartial.Authority) + "/VerifyUserAccount/VerifyEmailAddress?GUID=" + user.GUID +
                        //"\n\nIf you need help, you can find it here: http://support.corporate-contacts.com \n\nregards \nThe Corporate Contacts team \n\n(If you have received this email in error, or have any other concerns, please email [email protected] to let us know.)"

                        Message = "<div><center> <table border='0' cellpadding='0' cellspacing='0' height='100%' width='100%'> <tbody><tr> <td align='center' valign='top'><table border='0' cellpadding='0' cellspacing='0' width='600'> <tbody><tr> </tr><tr> <td align='center' valign='top'><table border='0' cellpadding='0' cellspacing='0' width='100%'> <tbody><tr> <td align='center' valign='top'><table border='0' cellpadding='0' cellspacing='0' width='600'> <tbody><tr> <td align='center' valign='top' width='600' style='padding-left:0;padding-right:0;padding-top:0;padding-bottom:0;border-top:1px solid #cccccc;border-right:1px solid #cccccc;border-left:1px solid #cccccc;background-color:#ffffff'><img src='http://corporate-contacts.com/wp-content/uploads/2014/03/CorporateContactslogo3a2.png' style='max-width: 450px;margin-top: 20px;' alt='Dyn'></td></tr></tbody></table></td></tr></tbody></table></td></tr><tr> <td align='center' valign='top' style='border-left:1px solid #cccccc;border-right:1px solid #cccccc;background-color:#ffffff'><table border='0' cellpadding='0' cellspacing='0' width='100%' style='background-color:#ffffff'> <tbody><tr> <td align='center' valign='top'><table border='0' cellpadding='0' cellspacing='0' width='100%'> <tbody><tr> <td valign='top' style='padding-top:20px;padding-right:30px;padding-left:30px'><table align='left' border='0' cellpadding='0' cellspacing='0'> <tbody><tr> <td valign='top' style='color:#444444;font-family:Helvetica;font-size:16px;line-height:125%;text-align:left;padding-bottom:20px'><p>Dear " + user.FullName + "</p><p>Thank you for registering with Corporate Contacts. You have created a new account</p><p><strong>Login Email :</strong> " + user.Email + "</p><p>You still need to <a href='" + Request.Url.GetLeftPart(UriPartial.Authority) + "/VerifyUserAccount/VerifyEmailAddress?GUID=" + user.GUID + "'>Click Here</a> to Activate your account or Please follow the link below:</p><p><a style='text-decoration:underline;color:#0066cc' href='" + Request.Url.GetLeftPart(UriPartial.Authority) + Url.RouteUrl(new { controller = "VerifyUserAccount", action = "VerifyEmailAddress" }) + "?GUID=" + user.GUID + "' target='_blank'>" + Request.Url.GetLeftPart(UriPartial.Authority) + Url.RouteUrl(new { controller = "VerifyUserAccount", action = "VerifyEmailAddress" }) + "?GUID=" + user.GUID + "</a></p><p>If you did not sign up for this account, this will be the onlycommunication you will receive. All non-confirmed accounts areautomatically deleted after 48 hours and no addresses are kept on file. Weapologize for any inconvenience this correspondence may have caused and weassure you that it was only sent at the request of someone visiting oursite requesting an account.</p><p>If you need help, you can find it here: <a href='http://[email protected]'>[email protected]</a></p><p>Regards,<br><a style='text-decoration:underline;color:#0066cc' href='http://corporate-contacts.com/' target='_blank'>The Corporate Contacts team</a></p></td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table></td></tr><tr> <td align='center' valign='top' style='border-left:1px solid #cccccc;border-right:1px solid #cccccc'><table border='0' cellpadding='0' cellspacing='0' width='100%' style='background-color: #209FD1;'> <tbody><tr> <td align='center' valign='bottom'><table border='0' cellpadding='0' cellspacing='0' width='600'> <tbody><tr> <td valign='bottom' width='600' style='padding-top:0px;padding-right:20px;padding-left:20px;padding-bottom:10px'><table align='left' border='0' cellpadding='0' cellspacing='0' width='100%'> <tbody><tr> <td valign='bottom' style='color:#ffffff;padding-top:10px;font-family:Helvetica;font-size:12px;line-height:150%;text-align:left'>Corporate Contacts Ltd.<br>Beverley, UK, HU17 0LD<br>Manchester, NH 03101</td><td valign='bottom' style='color:#ffffff;padding-top:10px;font-family:Helvetica;font-size:12px;line-height:150%;text-align:left'>Phone: +44 1482 869700<br>E-Mail: [email protected]</td></tr></tbody></table> </td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table></center></div>"
                    };
                    notifManager.SendNotification(notifObj);

                    // save state
                    user.Status = "Invite Sent";
                    user = userRepository.SaveUser(user);
                    return RedirectToAction("Success");
                }

                else
                {
                    ModelState.Clear();
                    if (isEmailExist.Count() != 0)
                        ViewBag.EmailValidation = "Please enter a different email, this email already exists";

                    if (isAccNameExist.Count() != 0)
                        ViewBag.AccountNameValidation = "Please enter a different Account Name, this is already in use";

                    //get time zone
                    System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> tz;
                    tz = TimeZoneInfo.GetSystemTimeZones();
                    List<TimeZoneViewModel> tzList = new List<TimeZoneViewModel>();

                    foreach (var t in tz)
                    {
                        TimeZoneViewModel tzvmodel = new TimeZoneViewModel();
                        tzvmodel.DisplayName = t.DisplayName;
                        tzvmodel.ID = t.Id;
                        tzList.Add(tzvmodel);
                    }
                    sgnv.TimeZoneList = tzList;

                    sgnv.User.Email = "";
                    return View(sgnv);
                }
            }
            else
            {
                return View();
            }
        }
        public Account saveFeaturesCommon(string plan, int quantities)
        {
            User userObj = (User)Session["user"];
            Account accountObj = (Account)Session["account"];
            int planLevel = Int32.Parse(plan);
            int existingPlanID = 1;

            //Delete old purchasedfeature
            var purchasedfeatures = purchRepository.Purchases.Where(p => p.AccountGUID == accountObj.AccountGUID).ToList();
            foreach (var purch in purchasedfeatures)
            {
                bool res = purchRepository.DeleteFPurchasedFeature(purch.ID);

            }

            var featuresForPlan = featureRepository.Features.Where(pl => pl.PlanLevel == planLevel).ToList();

            // save default feature
            var featuresForDefaultPlan = featureRepository.Features.Where(pl => pl.PlanLevel == planLevel && pl.Price == 0).ToList();

            foreach (var feat in featuresForDefaultPlan)
            {
                PurchasedFeatures objFeature = new PurchasedFeatures();
                objFeature.AccountGUID = accountObj.AccountGUID;
                objFeature.FeatureID = feat.ID;
                objFeature.ExpiryDate = DateTime.UtcNow;
                objFeature.Enabled = true;

                if (feat.Type == "Max Items per Folder")
                {
                    objFeature.Quantity = (feat.Quantity) * quantities;
                }
                else
                {
                    objFeature.Quantity = feat.Quantity;
                }

                purchRepository.SavePurchase(objFeature);
            }

            //Ajust Folder Fields START

            List<string> needtosave = new List<string>();
            List<CCFolderField> savedobj = new List<CCFolderField>();
            List<CCFolderField> activeFields = new List<CCFolderField>();
            List<CCFolderField> needToInActive = new List<CCFolderField>();

            List<String> objFields = new List<string>();

            if (planLevel == 10)
                objFields = FieldsConfigHelper.GetFieldForContactSimple();
            else if (planLevel == 20)
                objFields = FieldsConfigHelper.GetFieldForContactBusiness();
            else
                objFields = FieldsConfigHelper.GetFieldForContactFull();

            var folderListForAccount = CCFolderRepository.CCFolders.Where(F => F.AccountGUID == accountObj.AccountGUID).ToList();

            foreach (var fold in folderListForAccount.Where(f=>f.Type==1))
            {
                needtosave = CCFieldRepository.IsAvailableField(objFields, fold.FolderID);

                foreach (var field in needtosave)
                {
                    CCFolderField folderField = new CCFolderField();
                    folderField.FieldName = FieldsConfigHelper.GetRealName(field);
                    folderField.FieldType = FieldsConfigHelper.GetVariableType(field);
                    folderField.FolderID = fold.FolderID;
                    folderField.FieldCaption = field;
                    folderField.Restriction = "none";
                    folderField.AccountGUID = accountObj.AccountGUID;
                    folderField.isActive = true;
                    savedobj.Add(folderField);
                }

                var resp = CCFieldRepository.SaveFolderFieldsObj(savedobj);

                foreach (var field in objFields)
                {
                    CCFolderField folderField = CCFieldRepository.CCFolderFields.Where(f => f.FieldCaption == field & f.AccountGUID == accountObj.AccountGUID & f.FolderID == fold.FolderID).FirstOrDefault();
                    activeFields.Add(folderField);
                }

                var fieldList = CCFieldRepository.CCFolderFields.Where(f => f.AccountGUID == accountObj.AccountGUID & f.FolderID == fold.FolderID).ToList();

                var toInActiveList = fieldList.Except(activeFields).ToList();

                int flag = 0;

                foreach (var f1 in fieldList)
                {
                    flag = 0;
                    foreach (var f2 in activeFields)
                    {
                        try
                        {
                            if (f2.FieldID == f1.FieldID)
                            {
                                if (f1.isActive != false)
                                {
                                    flag = 1;
                                    break;
                                }
                                else
                                {
                                    flag = 2;
                                    break;
                                }

                            }
                        }
                        catch (Exception ex)
                        { }

                    }

                    if (flag == 0)
                    {
                        f1.isActive = false;
                        CCFieldRepository.SaveFolderFields(f1);
                    }

                    else if (flag == 2)
                    {
                        f1.isActive = true;
                        CCFieldRepository.SaveFolderFields(f1);
                    }
                }

                // add field into default group
                var defaultGrp = CCGroupRepository.CCGroups.Where(fid => fid.FolderID == fold.FolderID & fid.GroupName == "Default" & fid.AccountGUID == accountObj.AccountGUID).FirstOrDefault();

                if (defaultGrp != null)
                {
                    foreach (var field in resp)
                    {
                        long grpFieldOrder = 0;
                        // get group fileds last order
                        var groupFieldOrder = CCGroupFieldRepository.CCGroupsFields.Where(gid => gid.GroupID == defaultGrp.GroupID & gid.AccountGUID == accountObj.AccountGUID).OrderByDescending(grp => grp.FieldOrder).FirstOrDefault();
                        if (groupFieldOrder != null) { grpFieldOrder = groupFieldOrder.FieldOrder; }

                        CCGroupField objgrpfield = new CCGroupField();
                        objgrpfield.FieldID = field.FieldID;
                        objgrpfield.FieldOrder = grpFieldOrder + 1;
                        objgrpfield.FolderID = defaultGrp.FolderID;
                        objgrpfield.GroupID = defaultGrp.GroupID;
                        objgrpfield.AccountGUID = accountObj.AccountGUID;
                        var aa = CCGroupFieldRepository.SaveGroupField(objgrpfield);
                    }
                }
            }

            //Ajust Folder Fields END

            // change the plain id
            var planid = planRepository.Plans.FirstOrDefault(pl => pl.PlanLevel == planLevel).ID;

            var currentAccount = accountRepo.Accounts.FirstOrDefault(ac => ac.ID == userObj.AccountID);

            Account account = new Account();
            account = currentAccount;
            existingPlanID = account.PlanID;
            account.PlanID = planid;
            account.SyncPeriod = (short)featureRepository.Features.Where(pl => pl.PlanLevel == planLevel && pl.Type == "Sync Period").First().Quantity;
            account = accountRepo.SaveAccount(account);

            return account;
        }